|
| 1 | +// @ts-nocheck |
| 2 | +import axios from 'axios'; |
| 3 | +import Mau from './Mau.js'; |
| 4 | + |
| 5 | +const TEST_TOKEN = 'abcdef'; |
| 6 | + |
| 7 | +const mau = new Mau(); |
| 8 | +mau.configuration = { |
| 9 | + url: 'https://dock.mau.dev', |
| 10 | + authurl: 'https://dock.mau.dev', |
| 11 | + token: TEST_TOKEN, |
| 12 | +}; |
| 13 | + |
| 14 | +vi.mock('axios'); |
| 15 | + |
| 16 | +test('validatedConfiguration should initialize when configuration is empty string', async () => { |
| 17 | + expect(mau.validateConfiguration('')).toStrictEqual({}); |
| 18 | +}); |
| 19 | + |
| 20 | +test('validatedConfiguration should initialize when configuration is valid', async () => { |
| 21 | + expect( |
| 22 | + mau.validateConfiguration({ |
| 23 | + token: TEST_TOKEN, |
| 24 | + }), |
| 25 | + ).toStrictEqual({ |
| 26 | + url: 'https://dock.mau.dev', |
| 27 | + authurl: 'https://dock.mau.dev', |
| 28 | + token: TEST_TOKEN, |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +test('maskConfiguration should mask token', async () => { |
| 33 | + expect(mau.maskConfiguration()).toEqual({ |
| 34 | + url: 'https://dock.mau.dev', |
| 35 | + authurl: 'https://dock.mau.dev', |
| 36 | + token: 'a****f', |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +test('match should return true for dock.mau.dev', async () => { |
| 41 | + expect( |
| 42 | + mau.match({ |
| 43 | + registry: { |
| 44 | + url: 'dock.mau.dev', |
| 45 | + }, |
| 46 | + }), |
| 47 | + ).toBeTruthy(); |
| 48 | +}); |
| 49 | + |
| 50 | +test('match should return true for subdomains of dock.mau.dev', async () => { |
| 51 | + expect( |
| 52 | + mau.match({ |
| 53 | + registry: { |
| 54 | + url: 'registry.dock.mau.dev', |
| 55 | + }, |
| 56 | + }), |
| 57 | + ).toBeTruthy(); |
| 58 | +}); |
| 59 | + |
| 60 | +test('match should return false for other registries', async () => { |
| 61 | + expect( |
| 62 | + mau.match({ |
| 63 | + registry: { |
| 64 | + url: 'registry.gitlab.com', |
| 65 | + }, |
| 66 | + }), |
| 67 | + ).toBeFalsy(); |
| 68 | +}); |
| 69 | + |
| 70 | +test('authenticate should perform request with token auth when token is configured', async () => { |
| 71 | + axios.mockImplementation(() => ({ |
| 72 | + data: { |
| 73 | + token: 'token', |
| 74 | + }, |
| 75 | + })); |
| 76 | + |
| 77 | + await expect( |
| 78 | + mau.authenticate( |
| 79 | + { name: 'team/image' }, |
| 80 | + { |
| 81 | + headers: {}, |
| 82 | + }, |
| 83 | + ), |
| 84 | + ).resolves.toEqual({ headers: { Authorization: 'Bearer token' } }); |
| 85 | + |
| 86 | + expect(axios).toHaveBeenCalledWith( |
| 87 | + expect.objectContaining({ |
| 88 | + headers: expect.objectContaining({ |
| 89 | + Authorization: `Basic ${Buffer.from(`:${TEST_TOKEN}`, 'utf-8').toString('base64')}`, |
| 90 | + }), |
| 91 | + }), |
| 92 | + ); |
| 93 | +}); |
| 94 | + |
| 95 | +test('authenticate should omit basic auth when token is not configured', async () => { |
| 96 | + const mauPublic = new Mau(); |
| 97 | + mauPublic.configuration = { |
| 98 | + url: 'https://dock.mau.dev', |
| 99 | + authurl: 'https://dock.mau.dev', |
| 100 | + }; |
| 101 | + |
| 102 | + axios.mockImplementation(() => ({ |
| 103 | + data: { |
| 104 | + token: 'public-token', |
| 105 | + }, |
| 106 | + })); |
| 107 | + |
| 108 | + await expect( |
| 109 | + mauPublic.authenticate( |
| 110 | + { name: 'team/image' }, |
| 111 | + { |
| 112 | + headers: {}, |
| 113 | + }, |
| 114 | + ), |
| 115 | + ).resolves.toEqual({ headers: { Authorization: 'Bearer public-token' } }); |
| 116 | + |
| 117 | + expect(axios).toHaveBeenCalledWith( |
| 118 | + expect.objectContaining({ |
| 119 | + headers: { |
| 120 | + Accept: 'application/json', |
| 121 | + }, |
| 122 | + }), |
| 123 | + ); |
| 124 | +}); |
| 125 | + |
| 126 | +test('normalizeImage should return the proper registry v2 endpoint', async () => { |
| 127 | + expect( |
| 128 | + mau.normalizeImage({ |
| 129 | + name: 'test/image', |
| 130 | + registry: { |
| 131 | + url: 'dock.mau.dev', |
| 132 | + }, |
| 133 | + }), |
| 134 | + ).toStrictEqual({ |
| 135 | + name: 'test/image', |
| 136 | + registry: { |
| 137 | + url: 'https://dock.mau.dev/v2', |
| 138 | + }, |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +test('getAuthPull should return undefined when no token is configured', async () => { |
| 143 | + const mauPublic = new Mau(); |
| 144 | + mauPublic.configuration = { |
| 145 | + url: 'https://dock.mau.dev', |
| 146 | + authurl: 'https://dock.mau.dev', |
| 147 | + }; |
| 148 | + await expect(mauPublic.getAuthPull()).resolves.toBeUndefined(); |
| 149 | +}); |
| 150 | + |
| 151 | +test('getAuthPull should return username/password when token is configured', async () => { |
| 152 | + await expect(mau.getAuthPull()).resolves.toEqual({ |
| 153 | + username: '', |
| 154 | + password: TEST_TOKEN, |
| 155 | + }); |
| 156 | +}); |
0 commit comments