/implementations/server/swagger-java-react-ts-webpack/swagger-editor/test/plugins/validation/semantic/security.js

https://gitlab.com/abh1/web-applications-study-group · JavaScript · 135 lines · 128 code · 7 blank · 0 comment · 0 complexity · c56ed3bde0f5e4755c97f6e3d361b51b MD5 · raw file

  1. import expect from "expect"
  2. import { validate } from "plugins/validation/semantic-validators/validators/security"
  3. describe("validation plugin - semantic - security", () => {
  4. it("should return an error when an operation references a non-existing security scope", () => {
  5. const spec = {
  6. "securityDefinitions": {
  7. "api_key": {
  8. "type": "apiKey",
  9. "name": "apikey",
  10. "in": "query",
  11. "scopes": {
  12. "asdf": "blah blah"
  13. }
  14. }
  15. },
  16. "paths": {
  17. "/": {
  18. "get": {
  19. "description": "asdf",
  20. "security": [
  21. {
  22. "api_key": [
  23. "write:pets"
  24. ]
  25. }
  26. ]
  27. }
  28. }
  29. }
  30. }
  31. let res = validate({ resolvedSpec: spec })
  32. expect(res.errors.length).toEqual(1)
  33. expect(res.errors[0].path).toEqual(["paths", "/", "get", "security", "0", "0"])
  34. expect(res.errors[0].message).toEqual("Security scope definition write:pets could not be resolved")
  35. expect(res.warnings.length).toEqual(0)
  36. })
  37. it("should return an error when an operation references a security definition with no scopes", () => {
  38. const spec = {
  39. "securityDefinitions": {
  40. "api_key": {
  41. "type": "apiKey",
  42. "name": "apikey",
  43. "in": "query"
  44. }
  45. },
  46. "paths": {
  47. "/": {
  48. "get": {
  49. "description": "asdf",
  50. "security": [
  51. {
  52. "api_key": [
  53. "write:pets"
  54. ]
  55. }
  56. ]
  57. }
  58. }
  59. }
  60. }
  61. let res = validate({ resolvedSpec: spec })
  62. expect(res.errors.length).toEqual(1)
  63. expect(res.errors[0].path).toEqual(["paths", "/", "get", "security", "0", "0"])
  64. expect(res.errors[0].message).toEqual("Security scope definition write:pets could not be resolved")
  65. expect(res.warnings.length).toEqual(0)
  66. })
  67. it("should return an error when an operation references a non-existing security definition", () => {
  68. const spec = {
  69. "securityDefinitions": {
  70. "api_key": {
  71. "type": "apiKey",
  72. "name": "apikey",
  73. "in": "query"
  74. }
  75. },
  76. "paths": {
  77. "/": {
  78. "get": {
  79. "description": "asdf",
  80. "security": [
  81. {
  82. "fictional_security_definition": [
  83. "write:pets"
  84. ]
  85. }
  86. ]
  87. }
  88. }
  89. }
  90. }
  91. let res = validate({ resolvedSpec: spec })
  92. expect(res.errors.length).toEqual(1)
  93. expect(res.errors[0].path).toEqual(["paths", "/", "get", "security", "0"])
  94. expect(res.errors[0].message).toEqual("security requirements must match a security definition")
  95. expect(res.warnings.length).toEqual(0)
  96. })
  97. it("should not return an error when an operation references an existing security scope", () => {
  98. const spec = {
  99. "securityDefinitions": {
  100. "api_key": {
  101. "type": "apiKey",
  102. "name": "apikey",
  103. "in": "query",
  104. "scopes": {
  105. "write:pets": "write to pets"
  106. }
  107. }
  108. },
  109. "paths": {
  110. "/": {
  111. "get": {
  112. "description": "asdf",
  113. "security": [
  114. {
  115. "api_key": [
  116. "write:pets"
  117. ]
  118. }
  119. ]
  120. }
  121. }
  122. }
  123. }
  124. let res = validate({ resolvedSpec: spec })
  125. expect(res.errors.length).toEqual(0)
  126. expect(res.warnings.length).toEqual(0)
  127. })
  128. })