translator.js 9.0 KB

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