Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

translator.js 9.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. const expect = require("expect")
  2. const translator = require("../../docker/configurator/translator")
  3. const dedent = require("dedent")
  4. describe("docker: env translator", function() {
  5. describe("fundamentals", function() {
  6. it("should generate an empty baseline config", function () {
  7. const input = {}
  8. expect(translator(input)).toEqual(``)
  9. })
  10. it("should call an onFound callback", function () {
  11. const input = {
  12. MY_THING: "hey"
  13. }
  14. const onFoundSpy = expect.createSpy()
  15. const schema = {
  16. MY_THING: {
  17. type: "string",
  18. name: "myThing",
  19. onFound: onFoundSpy
  20. }
  21. }
  22. const res = translator(input, {
  23. schema
  24. })
  25. expect(res).toEqual(`myThing: "hey",`)
  26. expect(onFoundSpy.calls.length).toEqual(1)
  27. })
  28. it("should use a regular value over a legacy one, regardless of order", function () {
  29. const schema = {
  30. MY_THING: {
  31. type: "string",
  32. name: "myThing"
  33. },
  34. MY_OTHER_THING: {
  35. type: "string",
  36. name: "myThing",
  37. legacy: true
  38. }
  39. }
  40. // Regular value provided first
  41. expect(translator({
  42. MY_THING: "hey",
  43. MY_OTHER_THING: "hello"
  44. }, {
  45. schema
  46. })).toEqual(`myThing: "hey",`)
  47. // Legacy value provided first
  48. expect(translator({
  49. MY_OTHER_THING: "hello",
  50. MY_THING: "hey"
  51. }, {
  52. schema
  53. })).toEqual(`myThing: "hey",`)
  54. })
  55. it("should use a legacy value over a base one, regardless of order", function () {
  56. const schema = {
  57. MY_THING: {
  58. type: "string",
  59. name: "myThing",
  60. legacy: true
  61. }
  62. }
  63. const baseConfig = {
  64. myThing: {
  65. value: "base",
  66. schema: {
  67. type: "string",
  68. base: true
  69. }
  70. }
  71. }
  72. // Regular value provided first
  73. expect(translator({
  74. MY_THING: "legacy"
  75. }, {
  76. injectBaseConfig: true,
  77. schema,
  78. baseConfig
  79. })).toEqual(`myThing: "legacy",`)
  80. })
  81. })
  82. describe("Swagger UI configuration", function() {
  83. it("should generate a base config including the base content", function () {
  84. const input = {}
  85. expect(translator(input, {
  86. injectBaseConfig: true
  87. })).toEqual(dedent(`
  88. url: "https://petstore.swagger.io/v2/swagger.json",
  89. "dom_id": "#swagger-ui",
  90. deepLinking: true,
  91. presets: [
  92. SwaggerUIBundle.presets.apis,
  93. SwaggerUIStandalonePreset
  94. ],
  95. plugins: [
  96. SwaggerUIBundle.plugins.DownloadUrl
  97. ],
  98. layout: "StandaloneLayout",
  99. `))
  100. })
  101. it("should ignore an unknown config", function () {
  102. const input = {
  103. ASDF1234: "wow hello"
  104. }
  105. expect(translator(input)).toEqual(dedent(``))
  106. })
  107. it("should generate a string config", function () {
  108. const input = {
  109. URL: "http://petstore.swagger.io/v2/swagger.json",
  110. FILTER: ""
  111. }
  112. expect(translator(input)).toEqual(dedent(`
  113. url: "http://petstore.swagger.io/v2/swagger.json",
  114. filter: "",`
  115. ).trim())
  116. })
  117. it("should generate a boolean config", function () {
  118. const input = {
  119. DEEP_LINKING: "true",
  120. SHOW_EXTENSIONS: "false",
  121. SHOW_COMMON_EXTENSIONS: ""
  122. }
  123. expect(translator(input)).toEqual(dedent(`
  124. deepLinking: true,
  125. showExtensions: false,
  126. showCommonExtensions: undefined,`
  127. ))
  128. })
  129. it("should generate an object config", function () {
  130. const input = {
  131. SPEC: `{ swagger: "2.0" }`
  132. }
  133. expect(translator(input)).toEqual(dedent(`
  134. spec: { swagger: "2.0" },`
  135. ).trim())
  136. })
  137. it("should generate an array config", function () {
  138. const input = {
  139. URLS: `["/one", "/two"]`,
  140. SUPPORTED_SUBMIT_METHODS: ""
  141. }
  142. expect(translator(input)).toEqual(dedent(`
  143. urls: ["/one", "/two"],
  144. supportedSubmitMethods: undefined,`
  145. ).trim())
  146. })
  147. it("should properly escape key names when necessary", function () {
  148. const input = {
  149. URLS: `["/one", "/two"]`,
  150. URLS_PRIMARY_NAME: "one",
  151. }
  152. expect(translator(input)).toEqual(dedent(`
  153. urls: ["/one", "/two"],
  154. "urls.primaryName": "one",`
  155. ).trim())
  156. })
  157. it("should disregard a legacy variable in favor of a regular one", function () {
  158. const input = {
  159. // Order is important to this test... legacy vars should be
  160. // superseded regardless of what is fed in first.
  161. API_URL: "/old.json",
  162. URL: "/swagger.json",
  163. URLS: `["/one", "/two"]`,
  164. API_URLS: `["/three", "/four"]`,
  165. }
  166. expect(translator(input)).toEqual(dedent(`
  167. url: "/swagger.json",
  168. urls: ["/one", "/two"],`
  169. ).trim())
  170. })
  171. it("should pick up legacy variables when using base config", function () {
  172. const input = {
  173. API_URL: "/swagger.json",
  174. API_URLS: `["/one", "/two"]`,
  175. }
  176. expect(translator(input, { injectBaseConfig: true })).toEqual(dedent(`
  177. "dom_id": "#swagger-ui",
  178. deepLinking: true,
  179. presets: [
  180. SwaggerUIBundle.presets.apis,
  181. SwaggerUIStandalonePreset
  182. ],
  183. plugins: [
  184. SwaggerUIBundle.plugins.DownloadUrl
  185. ],
  186. layout: "StandaloneLayout",
  187. url: "/swagger.json",
  188. urls: ["/one", "/two"],`
  189. ).trim())
  190. })
  191. it("should generate a full config k:v string", function () {
  192. const input = {
  193. API_URL: "/old.yaml",
  194. API_URLS: `["/old", "/older"]`,
  195. CONFIG_URL: "/wow",
  196. DOM_ID: "#swagger_ui",
  197. SPEC: `{ swagger: "2.0" }`,
  198. URL: "/swagger.json",
  199. URLS: `["/one", "/two"]`,
  200. URLS_PRIMARY_NAME: "one",
  201. LAYOUT: "BaseLayout",
  202. DEEP_LINKING: "false",
  203. DISPLAY_OPERATION_ID: "true",
  204. DEFAULT_MODELS_EXPAND_DEPTH: "0",
  205. DEFAULT_MODEL_EXPAND_DEPTH: "1",
  206. DEFAULT_MODEL_RENDERING: "example",
  207. DISPLAY_REQUEST_DURATION: "true",
  208. DOC_EXPANSION: "full",
  209. FILTER: "wowee",
  210. MAX_DISPLAYED_TAGS: "4",
  211. SHOW_EXTENSIONS: "true",
  212. SHOW_COMMON_EXTENSIONS: "false",
  213. OAUTH2_REDIRECT_URL: "http://google.com/",
  214. SHOW_MUTATED_REQUEST: "true",
  215. SUPPORTED_SUBMIT_METHODS: `["get", "post"]`,
  216. VALIDATOR_URL: "http://smartbear.com/"
  217. }
  218. expect(translator(input)).toEqual(dedent(`
  219. configUrl: "/wow",
  220. "dom_id": "#swagger_ui",
  221. spec: { swagger: "2.0" },
  222. url: "/swagger.json",
  223. urls: ["/one", "/two"],
  224. "urls.primaryName": "one",
  225. layout: "BaseLayout",
  226. deepLinking: false,
  227. displayOperationId: true,
  228. defaultModelsExpandDepth: 0,
  229. defaultModelExpandDepth: 1,
  230. defaultModelRendering: "example",
  231. displayRequestDuration: true,
  232. docExpansion: "full",
  233. filter: "wowee",
  234. maxDisplayedTags: 4,
  235. showExtensions: true,
  236. showCommonExtensions: false,
  237. oauth2RedirectUrl: "http://google.com/",
  238. showMutatedRequest: true,
  239. supportedSubmitMethods: ["get", "post"],
  240. validatorUrl: "http://smartbear.com/",`
  241. ).trim())
  242. })
  243. it("should generate a full config k:v string including base config", function () {
  244. const input = {
  245. API_URL: "/old.yaml",
  246. API_URLS: `["/old", "/older"]`,
  247. CONFIG_URL: "/wow",
  248. DOM_ID: "#swagger_ui",
  249. SPEC: `{ swagger: "2.0" }`,
  250. URL: "/swagger.json",
  251. URLS: `["/one", "/two"]`,
  252. URLS_PRIMARY_NAME: "one",
  253. LAYOUT: "BaseLayout",
  254. DEEP_LINKING: "false",
  255. DISPLAY_OPERATION_ID: "true",
  256. DEFAULT_MODELS_EXPAND_DEPTH: "0",
  257. DEFAULT_MODEL_EXPAND_DEPTH: "1",
  258. DEFAULT_MODEL_RENDERING: "example",
  259. DISPLAY_REQUEST_DURATION: "true",
  260. DOC_EXPANSION: "full",
  261. FILTER: "wowee",
  262. MAX_DISPLAYED_TAGS: "4",
  263. SHOW_EXTENSIONS: "true",
  264. SHOW_COMMON_EXTENSIONS: "false",
  265. OAUTH2_REDIRECT_URL: "http://google.com/",
  266. SHOW_MUTATED_REQUEST: "true",
  267. SUPPORTED_SUBMIT_METHODS: `["get", "post"]`,
  268. VALIDATOR_URL: "http://smartbear.com/"
  269. }
  270. expect(translator(input, { injectBaseConfig: true })).toEqual(dedent(`
  271. presets: [
  272. SwaggerUIBundle.presets.apis,
  273. SwaggerUIStandalonePreset
  274. ],
  275. plugins: [
  276. SwaggerUIBundle.plugins.DownloadUrl
  277. ],
  278. configUrl: "/wow",
  279. "dom_id": "#swagger_ui",
  280. spec: { swagger: "2.0" },
  281. url: "/swagger.json",
  282. urls: ["/one", "/two"],
  283. "urls.primaryName": "one",
  284. layout: "BaseLayout",
  285. deepLinking: false,
  286. displayOperationId: true,
  287. defaultModelsExpandDepth: 0,
  288. defaultModelExpandDepth: 1,
  289. defaultModelRendering: "example",
  290. displayRequestDuration: true,
  291. docExpansion: "full",
  292. filter: "wowee",
  293. maxDisplayedTags: 4,
  294. showExtensions: true,
  295. showCommonExtensions: false,
  296. oauth2RedirectUrl: "http://google.com/",
  297. showMutatedRequest: true,
  298. supportedSubmitMethods: ["get", "post"],
  299. validatorUrl: "http://smartbear.com/",`
  300. ).trim())
  301. })
  302. })
  303. })