preauthorize.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { fromJS } from "immutable"
  2. import { preauthorizeBasic, preauthorizeApiKey } from "corePlugins/auth"
  3. import { authorize } from "corePlugins/auth/actions"
  4. const S2_SYSTEM = {
  5. authActions: {
  6. authorize
  7. },
  8. specSelectors: {
  9. isOAS3: () => false,
  10. specJson: () => {
  11. return fromJS({
  12. swagger: "2.0",
  13. securityDefinitions: {
  14. "APIKeyHeader": {
  15. "type": "apiKey",
  16. "in": "header",
  17. "name": "X-API-Key"
  18. },
  19. "basicAuth": {
  20. "type": "basic"
  21. }
  22. }
  23. })
  24. }
  25. }
  26. }
  27. const OAI3_SYSTEM = {
  28. authActions: {
  29. authorize
  30. },
  31. specSelectors: {
  32. isOAS3: () => true,
  33. specJson: () => {
  34. return fromJS({
  35. openapi: "3.0.0",
  36. components: {
  37. securitySchemes: {
  38. basicAuth: {
  39. type: "http",
  40. scheme: "basic"
  41. },
  42. APIKeyHeader: {
  43. type: "apiKey",
  44. in: "header",
  45. name: "X-API-Key"
  46. }
  47. }
  48. }
  49. })
  50. }
  51. }
  52. }
  53. describe("auth plugin - preauthorizers", () => {
  54. describe("preauthorizeBasic", () => {
  55. it("should return a valid authorize action in Swagger 2", () => {
  56. const res = preauthorizeBasic(S2_SYSTEM, "basicAuth", "user", "pass")
  57. expect(res).toEqual({
  58. type: "authorize",
  59. payload: {
  60. basicAuth: {
  61. schema: {
  62. type: "basic"
  63. },
  64. value: {
  65. username: "user",
  66. password: "pass"
  67. }
  68. }
  69. }
  70. })
  71. })
  72. it("should return a valid authorize action in OpenAPI 3", () => {
  73. const res = preauthorizeBasic(OAI3_SYSTEM, "basicAuth", "user", "pass")
  74. expect(res).toEqual({
  75. type: "authorize",
  76. payload: {
  77. basicAuth: {
  78. schema: {
  79. type: "http",
  80. scheme: "basic"
  81. },
  82. value: {
  83. username: "user",
  84. password: "pass"
  85. }
  86. }
  87. }
  88. })
  89. })
  90. it("should return null when the authorization name is invalid in Swagger 2", () => {
  91. const res = preauthorizeBasic(S2_SYSTEM, "fakeBasicAuth", "user", "pass")
  92. expect(res).toEqual(null)
  93. })
  94. it("should return null when the authorization name is invalid in OpenAPI 3", () => {
  95. const res = preauthorizeBasic(OAI3_SYSTEM, "fakeBasicAuth", "user", "pass")
  96. expect(res).toEqual(null)
  97. })
  98. })
  99. describe("preauthorizeApiKey", () => {
  100. it("should return a valid authorize action in Swagger 2", () => {
  101. const res = preauthorizeApiKey(S2_SYSTEM, "APIKeyHeader", "Asdf1234")
  102. expect(res).toEqual({
  103. type: "authorize",
  104. payload: {
  105. APIKeyHeader: {
  106. schema: {
  107. type: "apiKey",
  108. name: "X-API-Key",
  109. "in": "header"
  110. },
  111. value: "Asdf1234"
  112. }
  113. }
  114. })
  115. })
  116. it("should return a valid authorize action in OpenAPI 3", () => {
  117. const res = preauthorizeApiKey(OAI3_SYSTEM, "APIKeyHeader", "Asdf1234")
  118. expect(res).toEqual({
  119. type: "authorize",
  120. payload: {
  121. APIKeyHeader: {
  122. schema: {
  123. type: "apiKey",
  124. "in": "header",
  125. name: "X-API-Key"
  126. },
  127. value: "Asdf1234"
  128. }
  129. }
  130. })
  131. })
  132. it("should return null when the authorization name is invalid in Swagger 2", () => {
  133. const res = preauthorizeApiKey(S2_SYSTEM, "FakeAPIKeyHeader", "Asdf1234")
  134. expect(res).toEqual(null)
  135. })
  136. it("should return null when the authorization name is invalid in OpenAPI 3", () => {
  137. const res = preauthorizeApiKey(OAI3_SYSTEM, "FakeAPIKeyHeader", "Asdf1234")
  138. expect(res).toEqual(null)
  139. })
  140. })
  141. })