auth_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. package spec
  15. import (
  16. "testing"
  17. )
  18. func TestSerialization_AuthSerialization(t *testing.T) {
  19. assertSerializeJSON(t, BasicAuth(), `{"type":"basic"}`)
  20. assertSerializeJSON(t, APIKeyAuth("api-key", "header"), `{"type":"apiKey","name":"api-key","in":"header"}`)
  21. assertSerializeJSON(
  22. t,
  23. OAuth2Implicit("http://foo.com/authorization"),
  24. `{"type":"oauth2","flow":"implicit","authorizationUrl":"http://foo.com/authorization"}`)
  25. assertSerializeJSON(
  26. t,
  27. OAuth2Password("http://foo.com/token"),
  28. `{"type":"oauth2","flow":"password","tokenUrl":"http://foo.com/token"}`)
  29. assertSerializeJSON(t,
  30. OAuth2Application("http://foo.com/token"),
  31. `{"type":"oauth2","flow":"application","tokenUrl":"http://foo.com/token"}`)
  32. assertSerializeJSON(
  33. t,
  34. OAuth2AccessToken("http://foo.com/authorization", "http://foo.com/token"),
  35. `{"type":"oauth2","flow":"accessCode","authorizationUrl":"http://foo.com/authorization",`+
  36. `"tokenUrl":"http://foo.com/token"}`)
  37. auth1 := OAuth2Implicit("http://foo.com/authorization")
  38. auth1.AddScope("email", "read your email")
  39. assertSerializeJSON(
  40. t,
  41. auth1,
  42. `{"type":"oauth2","flow":"implicit","authorizationUrl":"http://foo.com/authorization",`+
  43. `"scopes":{"email":"read your email"}}`)
  44. auth2 := OAuth2Password("http://foo.com/authorization")
  45. auth2.AddScope("email", "read your email")
  46. assertSerializeJSON(
  47. t,
  48. auth2,
  49. `{"type":"oauth2","flow":"password","tokenUrl":"http://foo.com/authorization",`+
  50. `"scopes":{"email":"read your email"}}`)
  51. auth3 := OAuth2Application("http://foo.com/token")
  52. auth3.AddScope("email", "read your email")
  53. assertSerializeJSON(
  54. t,
  55. auth3,
  56. `{"type":"oauth2","flow":"application","tokenUrl":"http://foo.com/token","scopes":{"email":"read your email"}}`)
  57. auth4 := OAuth2AccessToken("http://foo.com/authorization", "http://foo.com/token")
  58. auth4.AddScope("email", "read your email")
  59. assertSerializeJSON(
  60. t,
  61. auth4,
  62. `{"type":"oauth2","flow":"accessCode","authorizationUrl":"http://foo.com/authorization",`+
  63. `"tokenUrl":"http://foo.com/token","scopes":{"email":"read your email"}}`)
  64. }
  65. func TestSerialization_AuthDeserialization(t *testing.T) {
  66. assertParsesJSON(t, `{"type":"basic"}`, BasicAuth())
  67. assertParsesJSON(
  68. t,
  69. `{"in":"header","name":"api-key","type":"apiKey"}`,
  70. APIKeyAuth("api-key", "header"))
  71. assertParsesJSON(
  72. t,
  73. `{"authorizationUrl":"http://foo.com/authorization","flow":"implicit","type":"oauth2"}`,
  74. OAuth2Implicit("http://foo.com/authorization"))
  75. assertParsesJSON(
  76. t,
  77. `{"flow":"password","tokenUrl":"http://foo.com/token","type":"oauth2"}`,
  78. OAuth2Password("http://foo.com/token"))
  79. assertParsesJSON(
  80. t,
  81. `{"flow":"application","tokenUrl":"http://foo.com/token","type":"oauth2"}`,
  82. OAuth2Application("http://foo.com/token"))
  83. assertParsesJSON(
  84. t,
  85. `{"authorizationUrl":"http://foo.com/authorization","flow":"accessCode","tokenUrl":"http://foo.com/token",`+
  86. `"type":"oauth2"}`,
  87. OAuth2AccessToken("http://foo.com/authorization", "http://foo.com/token"))
  88. auth1 := OAuth2Implicit("http://foo.com/authorization")
  89. auth1.AddScope("email", "read your email")
  90. assertParsesJSON(t,
  91. `{"authorizationUrl":"http://foo.com/authorization","flow":"implicit","scopes":{"email":"read your email"},`+
  92. `"type":"oauth2"}`,
  93. auth1)
  94. auth2 := OAuth2Password("http://foo.com/token")
  95. auth2.AddScope("email", "read your email")
  96. assertParsesJSON(t,
  97. `{"flow":"password","scopes":{"email":"read your email"},"tokenUrl":"http://foo.com/token","type":"oauth2"}`,
  98. auth2)
  99. auth3 := OAuth2Application("http://foo.com/token")
  100. auth3.AddScope("email", "read your email")
  101. assertParsesJSON(t,
  102. `{"flow":"application","scopes":{"email":"read your email"},"tokenUrl":"http://foo.com/token","type":"oauth2"}`,
  103. auth3)
  104. auth4 := OAuth2AccessToken("http://foo.com/authorization", "http://foo.com/token")
  105. auth4.AddScope("email", "read your email")
  106. assertParsesJSON(
  107. t,
  108. `{"authorizationUrl":"http://foo.com/authorization","flow":"accessCode","scopes":{"email":"read your email"},`+
  109. `"tokenUrl":"http://foo.com/token","type":"oauth2"}`,
  110. auth4)
  111. }