From 2367e1e31c971b21c0e3b55362ae98c8150de3f2 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Thu, 7 Mar 2019 09:39:16 +0100 Subject: [PATCH] KB Prep: Added App.Utils.baseUrl() and .joinUrlComponents() helper functions. --- .../javascripts/app/lib/app_post/utils.coffee | 17 ++++++ public/assets/tests/html_utils.js | 56 +++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/app/assets/javascripts/app/lib/app_post/utils.coffee b/app/assets/javascripts/app/lib/app_post/utils.coffee index bc1206760..f4efa7891 100644 --- a/app/assets/javascripts/app/lib/app_post/utils.coffee +++ b/app/assets/javascripts/app/lib/app_post/utils.coffee @@ -1184,3 +1184,20 @@ class App.Utils ctx = canvas.getContext('2d') ctx.drawImage(img, 0, 0) canvas.toDataURL('image/png') + + @baseUrl: -> + fqdn = App.Config.get('fqdn') + http_type = App.Config.get('http_type') + if !fqdn || fqdn is 'zammad.example.com' + url = window.location.origin + else + url = "#{http_type}://#{fqdn}" + + @joinUrlComponents: (array...) -> + if Array.isArray(array[0]) + array = array[0] + + array + .filter (elem) -> + elem? + .join '/' diff --git a/public/assets/tests/html_utils.js b/public/assets/tests/html_utils.js index 12afc4f18..3d132e2a5 100644 --- a/public/assets/tests/html_utils.js +++ b/public/assets/tests/html_utils.js @@ -3167,3 +3167,59 @@ var htmlImage2DataUrlTest = function() { $('#image2text img').one('load', htmlImage2DataUrlTest) } + +test('App.Utils.baseUrl()', function() { + // When FQDN is undefined or null, + // expect @baseUrl() to return window.location.origin + App.Config.get = function(key) { return undefined } + equal(App.Utils.baseUrl(), window.location.origin, 'with undefined FQDN') + App.Config.get = function(key) { return null } + equal(App.Utils.baseUrl(), window.location.origin, 'with null FQDN') + + // When FQDN is zammad.example.com, + // expect @baseUrl() to return window.location.origin + App.Config.get = function(key) { + if (key === 'fqdn') { + return 'zammad.example.com' + } + } + equal(App.Utils.baseUrl(), window.location.origin, 'with FQDN zammad.example.com') + + // Otherwise, + // expect @baseUrl() to return FQDN with current HTTP(S) scheme + App.Config.get = function(key) { + if (key === 'fqdn') { + return 'foo.zammad.com' + } else if (key === 'http_type') { + return 'https' + } + } + equal(App.Utils.baseUrl(), 'https://foo.zammad.com', 'with any other FQDN (and https scheme)') + + App.Config.get = function(key) { + if (key === 'fqdn') { + return 'bar.zammad.com' + } else if (key === 'http_type') { + return 'http' + } + } + equal(App.Utils.baseUrl(), 'http://bar.zammad.com', 'with any other FQDN (and http scheme)') +}); + +test('App.Utils.joinUrlComponents()', function() { + // When given a list of strings, + // expect @joinUrlComponents() to join them with slashes + equal(App.Utils.joinUrlComponents('foo', 'bar', 'baz'), 'foo/bar/baz', 'with a destructured list of strings') + + // When given an array of strings, + // expect @joinUrlComponents() to join them with slashes + equal(App.Utils.joinUrlComponents(['foo', 'bar', 'baz']), 'foo/bar/baz', 'with an array of strings') + + // When given a list of many types, + // expect @joinUrlComponents() to join their string representations with slashes + equal(App.Utils.joinUrlComponents(0, 1, 'two', true, false, { foo: 'bar' }), '0/1/two/true/false/[object Object]', 'with a list of many types') + + // When given a list including null or undefined, + // expect @joinUrlComponents() to filter them out of the results before joining the rest with slashes + equal(App.Utils.joinUrlComponents('foo', undefined, 'bar', null, 'baz'), 'foo/bar/baz', 'with a list including null or undefined') +});