|
| 1 | +import {describe, expect, it} from 'vitest'; |
| 2 | +import {actionButtons} from './extras'; |
| 3 | + |
| 4 | +const button = (http: object) => ({ |
| 5 | + label: 'test', |
| 6 | + http: {method: 'POST', url: 'https://x.y/', ...http}, |
| 7 | +}); |
| 8 | +const parse = (...buttons: unknown[]) => actionButtons({'client::action': {buttons}}); |
| 9 | + |
| 10 | +describe('actionButtons', () => { |
| 11 | + it('returns nothing without client::action', () => { |
| 12 | + expect(actionButtons(undefined)).toEqual({buttons: []}); |
| 13 | + expect(actionButtons({})).toEqual({buttons: []}); |
| 14 | + expect(actionButtons({'client::action': null})).toEqual({buttons: []}); |
| 15 | + }); |
| 16 | + |
| 17 | + it.each([{}, {buttons: 'nope'}])('reports a malformed client::action %j', (action) => { |
| 18 | + const result = actionButtons({'client::action': action}); |
| 19 | + expect(result.buttons).toEqual([]); |
| 20 | + expect(result.error).toContain('→ at ["client::action"].buttons'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('parses the format from gotify/server#494', () => { |
| 24 | + const result = parse( |
| 25 | + { |
| 26 | + label: 'Power off', |
| 27 | + http: { |
| 28 | + method: 'post', |
| 29 | + url: 'https://example.com/power_off?id=5', |
| 30 | + headers: {Authorization: 'bearer token'}, |
| 31 | + }, |
| 32 | + }, |
| 33 | + {label: 'Dismiss', http: {method: 'get', url: 'http://example.com/dismiss'}} |
| 34 | + ); |
| 35 | + expect(result).toEqual({ |
| 36 | + buttons: [ |
| 37 | + { |
| 38 | + label: 'Power off', |
| 39 | + http: { |
| 40 | + method: 'POST', |
| 41 | + url: 'https://example.com/power_off?id=5', |
| 42 | + headers: {Authorization: 'bearer token'}, |
| 43 | + }, |
| 44 | + }, |
| 45 | + { |
| 46 | + label: 'Dismiss', |
| 47 | + http: {method: 'GET', url: 'http://example.com/dismiss', headers: {}}, |
| 48 | + }, |
| 49 | + ], |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('reports every invalid button and keeps the valid ones', () => { |
| 54 | + const result = parse( |
| 55 | + button({url: 'ftp://x.y/'}), |
| 56 | + {label: 'ok', http: {method: 'POST', url: 'https://x.y/'}}, |
| 57 | + button({method: 1}) |
| 58 | + ); |
| 59 | + expect(result.buttons.map((b) => b.label)).toEqual(['ok']); |
| 60 | + expect(result.error).toContain('→ at ["client::action"].buttons[0].http.url'); |
| 61 | + expect(result.error).toContain('→ at ["client::action"].buttons[2].http.method'); |
| 62 | + }); |
| 63 | + |
| 64 | + it.each([ |
| 65 | + ['null', null], |
| 66 | + ['a string', 'string'], |
| 67 | + ['an empty label', {label: '', http: {method: 'POST', url: 'https://x.y/'}}], |
| 68 | + ['a blank label', {label: ' ', http: {method: 'POST', url: 'https://x.y/'}}], |
| 69 | + ['a missing http', {label: 'no http'}], |
| 70 | + ['a missing url', button({url: undefined})], |
| 71 | + ['a missing method', button({method: undefined})], |
| 72 | + ['an invalid url', button({url: 'not a url'})], |
| 73 | + ['a javascript url', button({url: 'javascript:alert(1)'})], |
| 74 | + ['a data url', button({url: 'data:text/plain,hi'})], |
| 75 | + ['a non-string header', button({headers: {A: 'b', C: 1}})], |
| 76 | + ['a non-string body', button({body: {a: 1}})], |
| 77 | + ])('rejects a button with %s', (_, invalid) => { |
| 78 | + const result = parse(invalid); |
| 79 | + expect(result.buttons).toEqual([]); |
| 80 | + expect(result.error).toContain('→ at ["client::action"].buttons[0]'); |
| 81 | + }); |
| 82 | +}); |
0 commit comments