github.com/goldeneggg/goa@v1.3.1/design/security_test.go (about)

     1  package design_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	. "github.com/goadesign/goa/design"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("SecuritySchemeDefinition", func() {
    12  	var scheme, host, tokenURL, authorizationURL string
    13  
    14  	var def *SecuritySchemeDefinition
    15  
    16  	BeforeEach(func() {
    17  		def = nil
    18  		tokenURL = ""
    19  		authorizationURL = ""
    20  		scheme = ""
    21  		host = ""
    22  	})
    23  
    24  	JustBeforeEach(func() {
    25  		Design.Schemes = []string{scheme}
    26  		Design.Host = host
    27  		def = &SecuritySchemeDefinition{
    28  			TokenURL:         tokenURL,
    29  			AuthorizationURL: authorizationURL,
    30  		}
    31  	})
    32  
    33  	Context("with valid token and authorization URLs", func() {
    34  		BeforeEach(func() {
    35  			tokenURL = "http://valid.com/token"
    36  			authorizationURL = "http://valid.com/auth"
    37  		})
    38  
    39  		It("validates", func() {
    40  			Ω(def.Validate()).ShouldNot(HaveOccurred())
    41  		})
    42  	})
    43  
    44  	Context("with an invalid token URL", func() {
    45  		BeforeEach(func() {
    46  			tokenURL = ":"
    47  			authorizationURL = "http://valid.com/auth"
    48  		})
    49  
    50  		It("does not validate", func() {
    51  			err := def.Validate()
    52  			Ω(err).Should(HaveOccurred())
    53  			Ω(err.Error()).Should(ContainSubstring(tokenURL))
    54  		})
    55  	})
    56  
    57  	Context("with an absolute token URL", func() {
    58  		BeforeEach(func() {
    59  			tokenURL = "http://valid.com/auth"
    60  		})
    61  
    62  		It("Finalize does not modify it", func() {
    63  			priorURL := def.TokenURL
    64  			def.Finalize()
    65  			Ω(def.TokenURL).Should(Equal(priorURL))
    66  		})
    67  	})
    68  
    69  	Context("with a relative token URL", func() {
    70  		BeforeEach(func() {
    71  			scheme = "http"
    72  			host = "foo.com"
    73  			tokenURL = "/auth"
    74  		})
    75  
    76  		It("Finalize makes it absolute", func() {
    77  			priorURL := def.TokenURL
    78  			def.Finalize()
    79  			Ω(def.TokenURL).Should(Equal(fmt.Sprintf("%s://%s%s", scheme, host, priorURL)))
    80  		})
    81  	})
    82  
    83  	Context("with an invalid authorization URL", func() {
    84  		BeforeEach(func() {
    85  			tokenURL = "http://valid.com/auth"
    86  			authorizationURL = ":"
    87  		})
    88  
    89  		It("does not validate", func() {
    90  			err := def.Validate()
    91  			Ω(err).Should(HaveOccurred())
    92  			Ω(err.Error()).Should(ContainSubstring(authorizationURL))
    93  		})
    94  	})
    95  
    96  	Context("with an absolute authorization URL", func() {
    97  		BeforeEach(func() {
    98  			authorizationURL = "http://valid.com/auth"
    99  		})
   100  
   101  		It("Finalize does not modify it", func() {
   102  			priorURL := def.AuthorizationURL
   103  			def.Finalize()
   104  			Ω(def.AuthorizationURL).Should(Equal(priorURL))
   105  		})
   106  	})
   107  
   108  	Context("with a relative authorization URL", func() {
   109  		BeforeEach(func() {
   110  			scheme = "http"
   111  			host = "foo.com"
   112  			authorizationURL = "/auth"
   113  		})
   114  
   115  		It("Finalize makes it absolute", func() {
   116  			priorURL := def.AuthorizationURL
   117  			def.Finalize()
   118  			Ω(def.AuthorizationURL).Should(Equal(fmt.Sprintf("%s://%s%s", scheme, host, priorURL)))
   119  		})
   120  	})
   121  
   122  })