selectors.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { fromJS } from "immutable"
  2. import { definitionsToAuthorize, definitionsForRequirements } from "corePlugins/auth/selectors"
  3. describe("auth plugin - selectors", () => {
  4. describe("definitionsToAuthorize", () => {
  5. it("should return securityDefinitions as a List", () => {
  6. const securityDefinitions = {
  7. "petstore_auth": {
  8. "type": "oauth2",
  9. "authorizationUrl": "http://petstore.swagger.io/oauth/dialog",
  10. "flow": "implicit",
  11. "scopes": {
  12. "write:pets": "modify pets in your account",
  13. "read:pets": "read your pets"
  14. }
  15. },
  16. "api_key": {
  17. "type": "apiKey",
  18. "name": "api_key",
  19. "in": "header"
  20. }
  21. }
  22. const system = {
  23. specSelectors: {
  24. securityDefinitions() {
  25. return fromJS(securityDefinitions)
  26. }
  27. }
  28. }
  29. const res = definitionsToAuthorize({})(system)
  30. expect(res.toJS()).toEqual([
  31. {
  32. "petstore_auth": securityDefinitions["petstore_auth"]
  33. },
  34. {
  35. "api_key": securityDefinitions["api_key"]
  36. },
  37. ])
  38. })
  39. it("should fail gracefully with bad data", () => {
  40. const securityDefinitions = null
  41. const system = {
  42. specSelectors: {
  43. securityDefinitions() {
  44. return fromJS(securityDefinitions)
  45. }
  46. }
  47. }
  48. const res = definitionsToAuthorize({})(system)
  49. expect(res.toJS()).toEqual([])
  50. })
  51. })
  52. describe("definitionsForRequirements", () => {
  53. it("should return applicable securityDefinitions as a List", () => {
  54. const securityDefinitions = {
  55. "petstore_auth": {
  56. "type": "oauth2",
  57. "authorizationUrl": "http://petstore.swagger.io/oauth/dialog",
  58. "flow": "implicit",
  59. "scopes": {
  60. "write:pets": "modify pets in your account",
  61. "read:pets": "read your pets"
  62. }
  63. },
  64. "api_key": {
  65. "type": "apiKey",
  66. "name": "api_key",
  67. "in": "header"
  68. }
  69. }
  70. const system = {
  71. authSelectors: {
  72. definitionsToAuthorize() {
  73. return fromJS([
  74. {
  75. "petstore_auth": securityDefinitions["petstore_auth"]
  76. },
  77. {
  78. "api_key": securityDefinitions["api_key"]
  79. },
  80. ])
  81. }
  82. }
  83. }
  84. const securities = fromJS([
  85. {
  86. "petstore_auth": [
  87. "write:pets",
  88. "read:pets"
  89. ]
  90. }
  91. ])
  92. const res = definitionsForRequirements({}, securities)(system)
  93. expect(res.toJS()).toEqual([
  94. {
  95. "petstore_auth": securityDefinitions["petstore_auth"]
  96. }
  97. ])
  98. })
  99. it("should fail gracefully with bad data", () => {
  100. const securityDefinitions = null
  101. const system = {
  102. authSelectors: {
  103. definitionsToAuthorize() {
  104. return null
  105. }
  106. }
  107. }
  108. const securities = null
  109. const res = definitionsForRequirements({}, securities)(system)
  110. expect(res.toJS()).toEqual([])
  111. })
  112. })
  113. })