ui-soxo-bootstrap-core 2.6.63-dev.0 → 2.6.63-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,335 +1,78 @@
1
- /**
2
- * @jest-environment node
3
- */
4
- import { createDecipheriv, createHmac, randomBytes, webcrypto } from 'crypto';
1
+ import ApiUtils from '../../utils/api/api.utils';
2
+ import { GENERATE_TOKEN_URL, MODES, getSupportUrl, openInNewTab, resolveSupportConfig } from './support-desk.utils';
5
3
 
6
- // The module reads Web Crypto from `window`. Provide it in the node environment.
7
- global.window = { crypto: webcrypto };
4
+ jest.mock('../../utils/api/api.utils', () => ({ __esModule: true, default: { post: jest.fn() } }));
8
5
 
9
- function setOnline(value) {
10
- Object.defineProperty(global, 'navigator', { value: { onLine: value }, configurable: true, writable: true });
11
- }
12
-
13
- const {
14
- API_ERROR_MESSAGES,
15
- TicketApiError,
16
- buildFormUrl,
17
- buildTicketsApiUrl,
18
- buildTicketsPageUrl,
19
- createToken,
20
- fetchTickets,
21
- formatDate,
22
- normalize,
23
- resolveSupportConfig,
24
- } = require('./support-desk.utils');
25
-
26
- const SECRET = randomBytes(64);
27
-
28
- const SETTING = {
29
- helpDeskLink: 'https://help.example.com/raise-ticket/new',
30
- projectToken: 'PROJECT_TOKEN',
31
- encryptedToken: SECRET.toString('base64'),
32
- };
33
-
34
- const USER = { email: 'jane@example.com' };
35
-
36
- const b64 = (value) => Buffer.from(value, 'base64url');
37
-
38
- /** Decrypts a compact JWE the same way the Helpdesk does, and checks its integrity tag. */
39
- function decryptJwe(jwe, secret = SECRET) {
40
- const [header, encryptedKey, iv, ciphertext, tag] = jwe.split('.');
41
- expect(encryptedKey).toBe('');
42
- expect(JSON.parse(b64(header).toString())).toEqual({ alg: 'dir', enc: 'A256CBC-HS512' });
43
-
44
- const al = Buffer.alloc(8);
45
- al.writeBigUInt64BE(BigInt(header.length * 8));
46
- const expectedTag = createHmac('sha512', secret.subarray(0, 32))
47
- .update(Buffer.concat([Buffer.from(header), b64(iv), b64(ciphertext), al]))
48
- .digest()
49
- .subarray(0, 32);
50
- expect(expectedTag.equals(b64(tag))).toBe(true);
51
-
52
- const decipher = createDecipheriv('aes-256-cbc', secret.subarray(32), b64(iv));
53
- return JSON.parse(Buffer.concat([decipher.update(b64(ciphertext)), decipher.final()]).toString());
54
- }
6
+ const config = { enabled: true };
55
7
 
56
8
  describe('resolveSupportConfig', () => {
57
- it('is enabled when the link, project token and secret are set', () => {
58
- const config = resolveSupportConfig(SETTING, USER);
59
-
60
- expect(config.enabled).toBe(true);
61
- expect(config.formUrl).toBe(SETTING.helpDeskLink);
62
- expect(config.user).toBe('jane@example.com');
63
- });
64
-
65
- it.each(['helpDeskLink', 'projectToken', 'encryptedToken'])('is disabled when %s is missing', (key) => {
66
- expect(resolveSupportConfig({ ...SETTING, [key]: undefined }, USER).enabled).toBe(false);
67
- });
68
-
69
- it('is disabled for an empty or missing setting', () => {
70
- expect(resolveSupportConfig(undefined, USER).enabled).toBe(false);
71
- expect(resolveSupportConfig(null, USER).enabled).toBe(false);
72
- });
73
-
74
- it('derives the ticket list and API host from the form link', () => {
75
- const config = resolveSupportConfig(SETTING, USER);
76
-
77
- expect(config.ticketsUrl).toBe('https://help.example.com/my-tickets');
78
- expect(config.apiBase).toBe('https://help.example.com');
79
- });
80
-
81
- it('prefers myTicketsLink and apiUrl, and trims trailing slashes', () => {
82
- const config = resolveSupportConfig(
83
- { ...SETTING, myTicketsLink: 'https://other.example.com/tickets', apiUrl: 'https://api.example.com//' },
84
- USER
85
- );
86
-
87
- expect(config.ticketsUrl).toBe('https://other.example.com/tickets');
88
- expect(config.apiBase).toBe('https://api.example.com');
89
- });
90
-
91
- it('uses the username when the user has no email', () => {
92
- expect(resolveSupportConfig(SETTING, { username: 'jane' }).user).toBe('jane');
93
- expect(resolveSupportConfig(SETTING, {}).user).toBe('');
9
+ it('is enabled only when showSupportBtn is true', () => {
10
+ expect(resolveSupportConfig({ showSupportBtn: true }).enabled).toBe(true);
11
+ expect(resolveSupportConfig({ showSupportBtn: false }).enabled).toBe(false);
12
+ expect(resolveSupportConfig(undefined).enabled).toBe(false);
94
13
  });
95
14
 
96
- it('defaults the token lifetime and enforces the minimum', () => {
97
- expect(resolveSupportConfig(SETTING, USER).ttlSeconds).toBe(300);
98
- expect(resolveSupportConfig({ ...SETTING, tokenTtlSeconds: 'abc' }, USER).ttlSeconds).toBe(300);
99
- expect(resolveSupportConfig({ ...SETTING, tokenTtlSeconds: 5 }, USER).ttlSeconds).toBe(30);
100
- expect(resolveSupportConfig({ ...SETTING, tokenTtlSeconds: 60 }, USER).ttlSeconds).toBe(60);
101
- });
102
- });
103
-
104
- describe('createToken', () => {
105
- beforeEach(() => {
106
- jest.spyOn(Date, 'now').mockReturnValue(1_700_000_000_000);
107
- });
108
-
109
- afterEach(() => {
110
- jest.restoreAllMocks();
111
- });
112
-
113
- it('encrypts the project token and user into a JWE the Helpdesk can read', async () => {
114
- const payload = decryptJwe(await createToken(resolveSupportConfig(SETTING, USER)));
115
-
116
- expect(payload.token).toBe('PROJECT_TOKEN');
117
- expect(payload.user).toBe('jane@example.com');
118
- });
119
-
120
- it('adds clock skew leeway on both sides of the lifetime', async () => {
121
- const payload = decryptJwe(await createToken(resolveSupportConfig({ ...SETTING, tokenTtlSeconds: 60 }, USER)));
122
-
123
- expect(payload.iat).toBe(1_700_000_000 - 30);
124
- expect(payload.exp).toBe(1_700_000_000 + 60 + 30);
125
- });
126
-
127
- it('makes a different token every time', async () => {
128
- const config = resolveSupportConfig(SETTING, USER);
129
-
130
- expect(await createToken(config)).not.toBe(await createToken(config));
131
- });
132
-
133
- it('rejects a secret that is not 64 bytes', async () => {
134
- const config = resolveSupportConfig({ ...SETTING, encryptedToken: randomBytes(32).toString('base64') }, USER);
135
-
136
- await expect(createToken(config)).rejects.toThrow('must decode to 64 bytes (got 32)');
137
- });
138
-
139
- it('rejects a secret that is not base64', async () => {
140
- const config = resolveSupportConfig({ ...SETTING, encryptedToken: '***not base64***' }, USER);
141
-
142
- await expect(createToken(config)).rejects.toThrow('not valid base64');
143
- });
144
-
145
- it('rejects when the user has no email or username', async () => {
146
- await expect(createToken(resolveSupportConfig(SETTING, {}))).rejects.toThrow('no email');
147
- });
148
-
149
- it('rejects when the project token is missing', async () => {
150
- const config = resolveSupportConfig({ ...SETTING, projectToken: '' }, USER);
151
-
152
- await expect(createToken(config)).rejects.toThrow('not configured');
153
- });
154
15
  });
155
16
 
156
- describe('link builders', () => {
157
- const config = resolveSupportConfig(SETTING, USER);
17
+ describe('getSupportUrl', () => {
18
+ beforeEach(() => ApiUtils.post.mockReset());
158
19
 
159
- it('adds the token to the form link', async () => {
160
- const url = new URL(await buildFormUrl(config));
161
-
162
- expect(`${url.origin}${url.pathname}`).toBe('https://help.example.com/raise-ticket/new');
163
- expect(decryptJwe(url.searchParams.get('d')).user).toBe('jane@example.com');
164
- });
165
-
166
- it('uses & when the link already has a query', async () => {
167
- const withQuery = resolveSupportConfig({ ...SETTING, helpDeskLink: 'https://help.example.com/new?x=1' }, USER);
168
- const url = new URL(await buildFormUrl(withQuery));
169
-
170
- expect(url.searchParams.get('x')).toBe('1');
171
- expect(url.searchParams.get('d')).toBeTruthy();
172
- });
173
-
174
- it('adds the token to the ticket list page link', async () => {
175
- const url = new URL(await buildTicketsPageUrl(config));
176
-
177
- expect(url.pathname).toBe('/my-tickets');
178
- expect(url.searchParams.get('d')).toBeTruthy();
179
- });
180
-
181
- it('builds the API link and skips empty params', async () => {
182
- const url = new URL(await buildTicketsApiUrl(config, { page: 2, search: '', status: undefined, priority: 'High' }));
183
-
184
- expect(url.pathname).toBe('/api/method/get_tickets_based_on_token');
185
- expect(url.searchParams.get('page')).toBe('2');
186
- expect(url.searchParams.get('priority')).toBe('High');
187
- expect(url.searchParams.has('search')).toBe(false);
188
- expect(url.searchParams.has('status')).toBe(false);
189
- });
190
-
191
- it('fails clearly when a link is not configured', async () => {
192
- const noLinks = { ...config, formUrl: '', ticketsUrl: '', apiBase: '' };
193
-
194
- await expect(buildFormUrl(noLinks)).rejects.toThrow('form link is not configured');
195
- await expect(buildTicketsPageUrl(noLinks)).rejects.toThrow('list link is not configured');
196
- await expect(buildTicketsApiUrl(noLinks)).rejects.toThrow('API is not configured');
197
- });
198
- });
199
-
200
- describe('fetchTickets', () => {
201
- const config = resolveSupportConfig(SETTING, USER);
202
- const reply = (body, ok = true, status = 200) => ({ ok, status, json: async () => body });
203
-
204
- beforeEach(() => setOnline(true));
205
-
206
- afterEach(() => {
207
- delete global.fetch;
208
- jest.restoreAllMocks();
209
- });
210
-
211
- it('returns tickets from a Frappe style response', async () => {
212
- global.fetch = jest.fn().mockResolvedValue(
213
- reply({ message: { success: true, tickets: [{ name: 'HD-1' }], total_count: 7, statuses: ['Open'], priorities: ['High'] } })
214
- );
215
-
216
- expect(await fetchTickets(config, { page: 1 })).toEqual({
217
- tickets: [{ name: 'HD-1' }],
218
- total: 7,
219
- statuses: ['Open'],
220
- priorities: ['High'],
20
+ it('posts only the mode and returns the url', async () => {
21
+ ApiUtils.post.mockResolvedValue({ success: true, data: { secureUrl: 'https://h.app/raise-ticket/new?d=x' } });
22
+ await expect(getSupportUrl(config, MODES.RAISE)).resolves.toBe('https://h.app/raise-ticket/new?d=x');
23
+ expect(ApiUtils.post).toHaveBeenCalledWith({
24
+ url: GENERATE_TOKEN_URL,
25
+ formBody: { mode: 'helpdesk' },
26
+ hideError: true,
221
27
  });
222
- expect(global.fetch.mock.calls[0][1]).toMatchObject({ credentials: 'omit' });
223
- });
224
-
225
- it('accepts a response that is not wrapped in message', async () => {
226
- global.fetch = jest.fn().mockResolvedValue(reply({ success: true }));
227
-
228
- expect(await fetchTickets(config, {})).toEqual({ tickets: [], total: 0, statuses: [], priorities: [] });
229
- });
230
-
231
- it('retries once with a new token when the token has expired', async () => {
232
- global.fetch = jest
233
- .fn()
234
- .mockResolvedValueOnce(reply({ message: { success: false, error: 'expired_token' } }))
235
- .mockResolvedValueOnce(reply({ message: { success: true, tickets: [] } }));
236
-
237
- await expect(fetchTickets(config, {})).resolves.toMatchObject({ tickets: [] });
238
-
239
- expect(global.fetch).toHaveBeenCalledTimes(2);
240
- const tokens = global.fetch.mock.calls.map(([url]) => new URL(url).searchParams.get('d'));
241
- expect(tokens[0]).not.toBe(tokens[1]);
242
- });
243
-
244
- it('gives up after one retry', async () => {
245
- global.fetch = jest.fn().mockResolvedValue(reply({ message: { success: false, error: 'expired_token' } }));
246
-
247
- await expect(fetchTickets(config, {})).rejects.toThrow(API_ERROR_MESSAGES.expired_token);
248
- expect(global.fetch).toHaveBeenCalledTimes(2);
249
28
  });
250
29
 
251
- it('turns a known API error into a readable message', async () => {
252
- global.fetch = jest.fn().mockResolvedValue(reply({ message: { success: false, error: 'project_not_found' } }));
253
-
254
- await expect(fetchTickets(config, {})).rejects.toThrow(API_ERROR_MESSAGES.project_not_found);
30
+ it('uses the tickets mode for My Tickets', async () => {
31
+ ApiUtils.post.mockResolvedValue({ data: { secureUrl: 'https://h.app/my-tickets?d=x' } });
32
+ await getSupportUrl(config, MODES.MINE);
33
+ expect(ApiUtils.post.mock.calls[0][0].formBody.mode).toBe('tickets');
255
34
  });
256
35
 
257
- it('falls back to a generic message for an unknown API error', async () => {
258
- global.fetch = jest.fn().mockResolvedValue(reply({ message: { success: false, error: 'something_new' } }));
259
-
260
- await expect(fetchTickets(config, {})).rejects.toThrow(API_ERROR_MESSAGES.invalid_token);
36
+ it('asks for a new url on every call', async () => {
37
+ ApiUtils.post.mockResolvedValue({ data: { secureUrl: 'https://h.app/x?d=1' } });
38
+ await getSupportUrl(config, MODES.RAISE);
39
+ await getSupportUrl(config, MODES.RAISE);
40
+ expect(ApiUtils.post).toHaveBeenCalledTimes(2);
261
41
  });
262
42
 
263
- it('reports an HTTP error status', async () => {
264
- global.fetch = jest.fn().mockResolvedValue(reply({}, false, 500));
265
-
266
- await expect(fetchTickets(config, {})).rejects.toMatchObject({ code: 'http', message: expect.stringContaining('500') });
43
+ it('rejects a missing or non-https url', async () => {
44
+ ApiUtils.post.mockResolvedValue({ data: {} });
45
+ await expect(getSupportUrl(config, MODES.RAISE)).rejects.toThrow();
46
+ ApiUtils.post.mockResolvedValue({ data: { secureUrl: 'http://h.app/x' } });
47
+ await expect(getSupportUrl(config, MODES.RAISE)).rejects.toThrow();
267
48
  });
268
49
 
269
- it('reports a blocked request (CORS) so the page can fall back', async () => {
270
- global.fetch = jest.fn().mockRejectedValue(new TypeError('Failed to fetch'));
271
-
272
- await expect(fetchTickets(config, {})).rejects.toMatchObject({ code: 'blocked' });
273
- });
274
-
275
- it('reports offline before blocked', async () => {
276
- setOnline(false);
277
- global.fetch = jest.fn().mockRejectedValue(new TypeError('Failed to fetch'));
278
-
279
- await expect(fetchTickets(config, {})).rejects.toMatchObject({ code: 'offline' });
280
- });
281
-
282
- it('reports a cancelled request as aborted', async () => {
283
- const abort = new Error('aborted');
284
- abort.name = 'AbortError';
285
- global.fetch = jest.fn().mockRejectedValue(abort);
286
-
287
- await expect(fetchTickets(config, {})).rejects.toMatchObject({ code: 'aborted' });
288
- });
289
-
290
- it('reports a slow Helpdesk as a timeout', async () => {
291
- // Fire the 15 second request timer immediately.
292
- const realSetTimeout = global.setTimeout;
293
- jest.spyOn(global, 'setTimeout').mockImplementation((fn, ms, ...args) => (ms === 15000 ? (fn(), 0) : realSetTimeout(fn, ms, ...args)));
294
- global.fetch = jest.fn((url, { signal }) => new Promise((resolve, reject) => {
295
- const abort = () => reject(Object.assign(new Error('aborted'), { name: 'AbortError' }));
296
- if (signal.aborted) abort();
297
- signal.addEventListener('abort', abort);
298
- }));
299
-
300
- await expect(fetchTickets(config, {})).rejects.toMatchObject({ code: 'timeout' });
50
+ it('shows the server message when the request fails', async () => {
51
+ ApiUtils.post.mockRejectedValue({ message: 'Branch not found' });
52
+ await expect(getSupportUrl(config, MODES.RAISE)).rejects.toThrow('Branch not found');
301
53
  });
54
+ });
302
55
 
303
- it('stops when the caller cancels', async () => {
304
- const controller = new AbortController();
305
- global.fetch = jest.fn(
306
- (url, { signal }) =>
307
- new Promise((resolve, reject) => {
308
- signal.addEventListener('abort', () => {
309
- const err = new Error('aborted');
310
- err.name = 'AbortError';
311
- reject(err);
312
- });
313
- })
314
- );
56
+ describe('openInNewTab', () => {
57
+ afterEach(() => jest.restoreAllMocks());
315
58
 
316
- const pending = fetchTickets(config, {}, controller.signal);
317
- const assertion = expect(pending).rejects.toBeInstanceOf(TicketApiError);
318
- await new Promise((resolve) => setTimeout(resolve, 20));
319
- controller.abort();
320
- await assertion;
59
+ it('opens the tab first, then points it at the link', async () => {
60
+ const popup = { close: jest.fn(), location: {} };
61
+ jest.spyOn(window, 'open').mockReturnValue(popup);
62
+ await openInNewTab(async () => 'https://h.app/x');
63
+ expect(popup.location.href).toBe('https://h.app/x');
64
+ expect(popup.opener).toBeNull();
321
65
  });
322
- });
323
66
 
324
- describe('helpers', () => {
325
- it('normalize makes a lowercase, dashed key', () => {
326
- expect(normalize('Work In Progress')).toBe('work-in-progress');
327
- expect(normalize(undefined)).toBe('');
67
+ it('closes the tab when the link fails', async () => {
68
+ const popup = { close: jest.fn(), location: {} };
69
+ jest.spyOn(window, 'open').mockReturnValue(popup);
70
+ await expect(openInNewTab(async () => { throw new Error('x'); })).rejects.toThrow('x');
71
+ expect(popup.close).toHaveBeenCalled();
328
72
  });
329
73
 
330
- it('formatDate formats the Helpdesk timestamp and tolerates bad input', () => {
331
- expect(formatDate('2026-10-21 12:07:02')).toMatch(/21/);
332
- expect(formatDate('')).toBe('-');
333
- expect(formatDate('not a date')).toBe('not a date');
74
+ it('reports a blocked popup', async () => {
75
+ jest.spyOn(window, 'open').mockReturnValue(null);
76
+ await expect(openInNewTab(async () => 'https://h.app/x')).rejects.toThrow('blocked');
334
77
  });
335
78
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ui-soxo-bootstrap-core",
3
- "version": "2.6.63-dev.0",
3
+ "version": "2.6.63-dev.1",
4
4
  "description": "All the Core Components for you to start",
5
5
  "keywords": [
6
6
  "all in one"