...

Source file src/github.com/go-openapi/spec/auth_test.go

Documentation: github.com/go-openapi/spec

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package spec
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestSerialization_AuthSerialization(t *testing.T) {
    22  	assertSerializeJSON(t, BasicAuth(), `{"type":"basic"}`)
    23  
    24  	assertSerializeJSON(t, APIKeyAuth("api-key", "header"), `{"type":"apiKey","name":"api-key","in":"header"}`)
    25  
    26  	assertSerializeJSON(
    27  		t,
    28  		OAuth2Implicit("http://foo.com/authorization"),
    29  		`{"type":"oauth2","flow":"implicit","authorizationUrl":"http://foo.com/authorization"}`)
    30  
    31  	assertSerializeJSON(
    32  		t,
    33  		OAuth2Password("http://foo.com/token"),
    34  		`{"type":"oauth2","flow":"password","tokenUrl":"http://foo.com/token"}`)
    35  
    36  	assertSerializeJSON(t,
    37  		OAuth2Application("http://foo.com/token"),
    38  		`{"type":"oauth2","flow":"application","tokenUrl":"http://foo.com/token"}`)
    39  
    40  	assertSerializeJSON(
    41  		t,
    42  		OAuth2AccessToken("http://foo.com/authorization", "http://foo.com/token"),
    43  		`{"type":"oauth2","flow":"accessCode","authorizationUrl":"http://foo.com/authorization",`+
    44  			`"tokenUrl":"http://foo.com/token"}`)
    45  
    46  	auth1 := OAuth2Implicit("http://foo.com/authorization")
    47  	auth1.AddScope("email", "read your email")
    48  	assertSerializeJSON(
    49  		t,
    50  		auth1,
    51  		`{"type":"oauth2","flow":"implicit","authorizationUrl":"http://foo.com/authorization",`+
    52  			`"scopes":{"email":"read your email"}}`)
    53  
    54  	auth2 := OAuth2Password("http://foo.com/authorization")
    55  	auth2.AddScope("email", "read your email")
    56  	assertSerializeJSON(
    57  		t,
    58  		auth2,
    59  		`{"type":"oauth2","flow":"password","tokenUrl":"http://foo.com/authorization",`+
    60  			`"scopes":{"email":"read your email"}}`)
    61  
    62  	auth3 := OAuth2Application("http://foo.com/token")
    63  	auth3.AddScope("email", "read your email")
    64  	assertSerializeJSON(
    65  		t,
    66  		auth3,
    67  		`{"type":"oauth2","flow":"application","tokenUrl":"http://foo.com/token","scopes":{"email":"read your email"}}`)
    68  
    69  	auth4 := OAuth2AccessToken("http://foo.com/authorization", "http://foo.com/token")
    70  	auth4.AddScope("email", "read your email")
    71  	assertSerializeJSON(
    72  		t,
    73  		auth4,
    74  		`{"type":"oauth2","flow":"accessCode","authorizationUrl":"http://foo.com/authorization",`+
    75  			`"tokenUrl":"http://foo.com/token","scopes":{"email":"read your email"}}`)
    76  }
    77  
    78  func TestSerialization_AuthDeserialization(t *testing.T) {
    79  
    80  	assertParsesJSON(t, `{"type":"basic"}`, BasicAuth())
    81  
    82  	assertParsesJSON(
    83  		t,
    84  		`{"in":"header","name":"api-key","type":"apiKey"}`,
    85  		APIKeyAuth("api-key", "header"))
    86  
    87  	assertParsesJSON(
    88  		t,
    89  		`{"authorizationUrl":"http://foo.com/authorization","flow":"implicit","type":"oauth2"}`,
    90  		OAuth2Implicit("http://foo.com/authorization"))
    91  
    92  	assertParsesJSON(
    93  		t,
    94  		`{"flow":"password","tokenUrl":"http://foo.com/token","type":"oauth2"}`,
    95  		OAuth2Password("http://foo.com/token"))
    96  
    97  	assertParsesJSON(
    98  		t,
    99  		`{"flow":"application","tokenUrl":"http://foo.com/token","type":"oauth2"}`,
   100  		OAuth2Application("http://foo.com/token"))
   101  
   102  	assertParsesJSON(
   103  		t,
   104  		`{"authorizationUrl":"http://foo.com/authorization","flow":"accessCode","tokenUrl":"http://foo.com/token",`+
   105  			`"type":"oauth2"}`,
   106  		OAuth2AccessToken("http://foo.com/authorization", "http://foo.com/token"))
   107  
   108  	auth1 := OAuth2Implicit("http://foo.com/authorization")
   109  	auth1.AddScope("email", "read your email")
   110  	assertParsesJSON(t,
   111  		`{"authorizationUrl":"http://foo.com/authorization","flow":"implicit","scopes":{"email":"read your email"},`+
   112  			`"type":"oauth2"}`,
   113  		auth1)
   114  
   115  	auth2 := OAuth2Password("http://foo.com/token")
   116  	auth2.AddScope("email", "read your email")
   117  	assertParsesJSON(t,
   118  		`{"flow":"password","scopes":{"email":"read your email"},"tokenUrl":"http://foo.com/token","type":"oauth2"}`,
   119  		auth2)
   120  
   121  	auth3 := OAuth2Application("http://foo.com/token")
   122  	auth3.AddScope("email", "read your email")
   123  	assertParsesJSON(t,
   124  		`{"flow":"application","scopes":{"email":"read your email"},"tokenUrl":"http://foo.com/token","type":"oauth2"}`,
   125  		auth3)
   126  
   127  	auth4 := OAuth2AccessToken("http://foo.com/authorization", "http://foo.com/token")
   128  	auth4.AddScope("email", "read your email")
   129  	assertParsesJSON(
   130  		t,
   131  		`{"authorizationUrl":"http://foo.com/authorization","flow":"accessCode","scopes":{"email":"read your email"},`+
   132  			`"tokenUrl":"http://foo.com/token","type":"oauth2"}`,
   133  		auth4)
   134  
   135  }
   136  

View as plain text