|
| 1 | +import parseContentpassToken, * as ParseContentpassTokenModule from './parseContentpassToken'; |
| 2 | +import validateSubscription from './validateSubscription'; |
| 3 | + |
| 4 | +const EXAMPLE_CONTENTPASS_TOKEN: ReturnType<typeof parseContentpassToken> = { |
| 5 | + body: { |
| 6 | + aud: 'cc3fc4ad', |
| 7 | + auth: true, |
| 8 | + exp: 1733312081, |
| 9 | + iat: 1733135681, |
| 10 | + type: 'cp', |
| 11 | + plans: ['planId1'], |
| 12 | + }, |
| 13 | + header: { |
| 14 | + alg: 'RS256', |
| 15 | + kid: 'kid', |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +describe('validateSubscription', () => { |
| 20 | + let parseContentpassTokenSpy: jest.SpyInstance; |
| 21 | + beforeEach(() => { |
| 22 | + parseContentpassTokenSpy = jest |
| 23 | + .spyOn(ParseContentpassTokenModule, 'default') |
| 24 | + .mockReturnValue(EXAMPLE_CONTENTPASS_TOKEN); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + jest.restoreAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should return true if the token is valid', () => { |
| 32 | + parseContentpassTokenSpy.mockReturnValue(EXAMPLE_CONTENTPASS_TOKEN); |
| 33 | + const result = validateSubscription('example_contentpass_token'); |
| 34 | + |
| 35 | + expect(result).toBe(true); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should return false if the token is invalid', () => { |
| 39 | + parseContentpassTokenSpy.mockImplementation(() => { |
| 40 | + throw new Error('Invalid token'); |
| 41 | + }); |
| 42 | + const result = validateSubscription('example_contentpass_token'); |
| 43 | + |
| 44 | + expect(result).toBe(false); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should return false if the user is not authenticated', () => { |
| 48 | + parseContentpassTokenSpy.mockReturnValue({ |
| 49 | + ...EXAMPLE_CONTENTPASS_TOKEN, |
| 50 | + body: { |
| 51 | + ...EXAMPLE_CONTENTPASS_TOKEN.body, |
| 52 | + auth: false, |
| 53 | + }, |
| 54 | + }); |
| 55 | + const result = validateSubscription('example_contentpass_token'); |
| 56 | + |
| 57 | + expect(result).toBe(false); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should return false if the user has no plans', () => { |
| 61 | + parseContentpassTokenSpy.mockReturnValue({ |
| 62 | + ...EXAMPLE_CONTENTPASS_TOKEN, |
| 63 | + body: { |
| 64 | + ...EXAMPLE_CONTENTPASS_TOKEN.body, |
| 65 | + plans: [], |
| 66 | + }, |
| 67 | + }); |
| 68 | + const result = validateSubscription('example_contentpass_token'); |
| 69 | + |
| 70 | + expect(result).toBe(false); |
| 71 | + }); |
| 72 | +}); |
0 commit comments