|
| 1 | +import { ProfileService } from "../../../src/core/profile/profile.service"; |
| 2 | + |
| 3 | +describe("ProfileService - mapCelonisEnvProfile", () => { |
| 4 | + let profileService: ProfileService; |
| 5 | + let originalCelonisUrl: string | undefined; |
| 6 | + let originalCelonisApiToken: string | undefined; |
| 7 | + let originalTeamUrl: string | undefined; |
| 8 | + let originalApiToken: string | undefined; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + profileService = new ProfileService(); |
| 12 | + originalCelonisUrl = process.env.CELONIS_URL; |
| 13 | + originalCelonisApiToken = process.env.CELONIS_API_TOKEN; |
| 14 | + originalTeamUrl = process.env.TEAM_URL; |
| 15 | + originalApiToken = process.env.API_TOKEN; |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + if (originalCelonisUrl !== undefined) { |
| 20 | + process.env.CELONIS_URL = originalCelonisUrl; |
| 21 | + } else { |
| 22 | + delete process.env.CELONIS_URL; |
| 23 | + } |
| 24 | + |
| 25 | + if (originalCelonisApiToken !== undefined) { |
| 26 | + process.env.CELONIS_API_TOKEN = originalCelonisApiToken; |
| 27 | + } else { |
| 28 | + delete process.env.CELONIS_API_TOKEN; |
| 29 | + } |
| 30 | + |
| 31 | + if (originalTeamUrl !== undefined) { |
| 32 | + process.env.TEAM_URL = originalTeamUrl; |
| 33 | + } else { |
| 34 | + delete process.env.TEAM_URL; |
| 35 | + } |
| 36 | + |
| 37 | + if (originalApiToken !== undefined) { |
| 38 | + process.env.API_TOKEN = originalApiToken; |
| 39 | + } else { |
| 40 | + delete process.env.API_TOKEN; |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + describe("when CELONIS_URL is not set", () => { |
| 45 | + it("should return early and not modify environment variables", () => { |
| 46 | + delete process.env.CELONIS_URL; |
| 47 | + delete process.env.TEAM_URL; |
| 48 | + delete process.env.API_TOKEN; |
| 49 | + |
| 50 | + (profileService as any).mapCelonisEnvProfile(); |
| 51 | + |
| 52 | + expect(process.env.TEAM_URL).toBeUndefined(); |
| 53 | + expect(process.env.API_TOKEN).toBeUndefined(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe("when CELONIS_URL is set", () => { |
| 58 | + it("should set TEAM_URL to CELONIS_URL when it already starts with https://", () => { |
| 59 | + process.env.CELONIS_URL = "https://example.celonis.cloud"; |
| 60 | + delete process.env.TEAM_URL; |
| 61 | + |
| 62 | + (profileService as any).mapCelonisEnvProfile(); |
| 63 | + |
| 64 | + expect(process.env.TEAM_URL).toBe("https://example.celonis.cloud"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should prepend https:// to CELONIS_URL when it does not start with https://", () => { |
| 68 | + process.env.CELONIS_URL = "example.celonis.cloud"; |
| 69 | + delete process.env.TEAM_URL; |
| 70 | + |
| 71 | + (profileService as any).mapCelonisEnvProfile(); |
| 72 | + |
| 73 | + expect(process.env.TEAM_URL).toBe("https://example.celonis.cloud"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should leave CELONIS_URL unchanged when it starts with http://", () => { |
| 77 | + process.env.CELONIS_URL = "http://example.celonis.cloud"; |
| 78 | + delete process.env.TEAM_URL; |
| 79 | + |
| 80 | + (profileService as any).mapCelonisEnvProfile(); |
| 81 | + |
| 82 | + expect(process.env.TEAM_URL).toBe("http://example.celonis.cloud"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should handle CELONIS_URL with path and prepend https://", () => { |
| 86 | + process.env.CELONIS_URL = "example.celonis.cloud/path/to/resource"; |
| 87 | + delete process.env.TEAM_URL; |
| 88 | + |
| 89 | + (profileService as any).mapCelonisEnvProfile(); |
| 90 | + |
| 91 | + expect(process.env.TEAM_URL).toBe("https://example.celonis.cloud/path/to/resource"); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should handle CELONIS_URL with port and prepend https://", () => { |
| 95 | + process.env.CELONIS_URL = "example.celonis.cloud:8080"; |
| 96 | + delete process.env.TEAM_URL; |
| 97 | + |
| 98 | + (profileService as any).mapCelonisEnvProfile(); |
| 99 | + |
| 100 | + expect(process.env.TEAM_URL).toBe("https://example.celonis.cloud:8080"); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe("when CELONIS_API_TOKEN is set", () => { |
| 105 | + it("should set API_TOKEN to CELONIS_API_TOKEN when CELONIS_URL starts with https://", () => { |
| 106 | + process.env.CELONIS_URL = "https://example.celonis.cloud"; |
| 107 | + process.env.CELONIS_API_TOKEN = "test-api-token"; |
| 108 | + delete process.env.API_TOKEN; |
| 109 | + |
| 110 | + (profileService as any).mapCelonisEnvProfile(); |
| 111 | + |
| 112 | + expect(process.env.API_TOKEN).toBe("test-api-token"); |
| 113 | + expect(process.env.TEAM_URL).toBe("https://example.celonis.cloud"); |
| 114 | + }); |
| 115 | + |
| 116 | + it("should set API_TOKEN to CELONIS_API_TOKEN when CELONIS_URL does not start with https://", () => { |
| 117 | + process.env.CELONIS_URL = "example.celonis.cloud"; |
| 118 | + process.env.CELONIS_API_TOKEN = "test-api-token"; |
| 119 | + delete process.env.API_TOKEN; |
| 120 | + |
| 121 | + (profileService as any).mapCelonisEnvProfile(); |
| 122 | + |
| 123 | + expect(process.env.API_TOKEN).toBe("test-api-token"); |
| 124 | + expect(process.env.TEAM_URL).toBe("https://example.celonis.cloud"); |
| 125 | + }); |
| 126 | + |
| 127 | + it("should set API_TOKEN to CELONIS_API_TOKEN when CELONIS_URL starts with http://", () => { |
| 128 | + process.env.CELONIS_URL = "http://example.celonis.cloud"; |
| 129 | + process.env.CELONIS_API_TOKEN = "test-api-token"; |
| 130 | + delete process.env.API_TOKEN; |
| 131 | + |
| 132 | + (profileService as any).mapCelonisEnvProfile(); |
| 133 | + |
| 134 | + expect(process.env.API_TOKEN).toBe("test-api-token"); |
| 135 | + expect(process.env.TEAM_URL).toBe("http://example.celonis.cloud"); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + describe("when CELONIS_API_TOKEN is not set", () => { |
| 140 | + it("should delete API_TOKEN when CELONIS_URL starts with https://", () => { |
| 141 | + process.env.CELONIS_URL = "https://example.celonis.cloud"; |
| 142 | + process.env.API_TOKEN = "existing-token"; |
| 143 | + delete process.env.CELONIS_API_TOKEN; |
| 144 | + |
| 145 | + (profileService as any).mapCelonisEnvProfile(); |
| 146 | + |
| 147 | + expect(process.env.API_TOKEN).toBeUndefined(); |
| 148 | + expect(process.env.TEAM_URL).toBe("https://example.celonis.cloud"); |
| 149 | + }); |
| 150 | + |
| 151 | + it("should delete API_TOKEN when CELONIS_URL does not start with https://", () => { |
| 152 | + process.env.CELONIS_URL = "example.celonis.cloud"; |
| 153 | + process.env.API_TOKEN = "existing-token"; |
| 154 | + delete process.env.CELONIS_API_TOKEN; |
| 155 | + |
| 156 | + (profileService as any).mapCelonisEnvProfile(); |
| 157 | + |
| 158 | + expect(process.env.API_TOKEN).toBeUndefined(); |
| 159 | + expect(process.env.TEAM_URL).toBe("https://example.celonis.cloud"); |
| 160 | + }); |
| 161 | + |
| 162 | + it("should delete API_TOKEN when CELONIS_URL starts with http://", () => { |
| 163 | + process.env.CELONIS_URL = "http://example.celonis.cloud"; |
| 164 | + process.env.API_TOKEN = "existing-token"; |
| 165 | + delete process.env.CELONIS_API_TOKEN; |
| 166 | + |
| 167 | + (profileService as any).mapCelonisEnvProfile(); |
| 168 | + |
| 169 | + expect(process.env.API_TOKEN).toBeUndefined(); |
| 170 | + expect(process.env.TEAM_URL).toBe("http://example.celonis.cloud"); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe("combined scenarios", () => { |
| 175 | + it("should handle URL without https:// and set API_TOKEN when both are provided", () => { |
| 176 | + process.env.CELONIS_URL = "example.celonis.cloud"; |
| 177 | + process.env.CELONIS_API_TOKEN = "my-token-123"; |
| 178 | + delete process.env.TEAM_URL; |
| 179 | + delete process.env.API_TOKEN; |
| 180 | + |
| 181 | + (profileService as any).mapCelonisEnvProfile(); |
| 182 | + |
| 183 | + expect(process.env.TEAM_URL).toBe("https://example.celonis.cloud"); |
| 184 | + expect(process.env.API_TOKEN).toBe("my-token-123"); |
| 185 | + }); |
| 186 | + |
| 187 | + it("should handle URL with https:// and delete API_TOKEN when token is not provided", () => { |
| 188 | + process.env.CELONIS_URL = "https://example.celonis.cloud"; |
| 189 | + process.env.API_TOKEN = "old-token"; |
| 190 | + delete process.env.CELONIS_API_TOKEN; |
| 191 | + |
| 192 | + (profileService as any).mapCelonisEnvProfile(); |
| 193 | + |
| 194 | + expect(process.env.TEAM_URL).toBe("https://example.celonis.cloud"); |
| 195 | + expect(process.env.API_TOKEN).toBeUndefined(); |
| 196 | + }); |
| 197 | + }); |
| 198 | +}); |
| 199 | + |
0 commit comments