|
| 1 | +import { ErrorStatusCode } from '../src/types' |
| 2 | +import { |
| 3 | + clearAllIndexes, |
| 4 | + config, |
| 5 | + MeiliSearch, |
| 6 | + BAD_HOST, |
| 7 | + getClient, |
| 8 | +} from './utils/meilisearch-test-utils' |
| 9 | + |
| 10 | +beforeEach(async () => { |
| 11 | + await clearAllIndexes(config) |
| 12 | +}) |
| 13 | + |
| 14 | +describe.each([{ permission: 'Master' }, { permission: 'Admin' }])( |
| 15 | + 'Test on snapshot', |
| 16 | + ({ permission }) => { |
| 17 | + test(`${permission} key: create a new snapshot`, async () => { |
| 18 | + const client = await getClient(permission) |
| 19 | + const { taskUid } = await client.createSnapshot() |
| 20 | + |
| 21 | + await client.waitForTask(taskUid) |
| 22 | + }) |
| 23 | + } |
| 24 | +) |
| 25 | + |
| 26 | +describe.each([{ permission: 'Search' }])( |
| 27 | + 'Test on snapshot with search api key should not have access', |
| 28 | + ({ permission }) => { |
| 29 | + test(`${permission} key: try to create snapshot with search key and be denied`, async () => { |
| 30 | + const client = await getClient(permission) |
| 31 | + await expect(client.createSnapshot()).rejects.toHaveProperty( |
| 32 | + 'code', |
| 33 | + ErrorStatusCode.INVALID_API_KEY |
| 34 | + ) |
| 35 | + }) |
| 36 | + } |
| 37 | +) |
| 38 | + |
| 39 | +describe.each([{ permission: 'No' }])( |
| 40 | + 'Test on snapshot without api key should not have access', |
| 41 | + ({ permission }) => { |
| 42 | + test(`${permission} key: try to create snapshot with no key and be denied`, async () => { |
| 43 | + const client = await getClient(permission) |
| 44 | + await expect(client.createSnapshot()).rejects.toHaveProperty( |
| 45 | + 'code', |
| 46 | + ErrorStatusCode.MISSING_AUTHORIZATION_HEADER |
| 47 | + ) |
| 48 | + }) |
| 49 | + } |
| 50 | +) |
| 51 | + |
| 52 | +describe.each([ |
| 53 | + { host: BAD_HOST, trailing: false }, |
| 54 | + { host: `${BAD_HOST}/api`, trailing: false }, |
| 55 | + { host: `${BAD_HOST}/trailing/`, trailing: true }, |
| 56 | +])('Tests on url construction', ({ host, trailing }) => { |
| 57 | + test(`Test createSnapshot route`, async () => { |
| 58 | + const route = `snapshots` |
| 59 | + const client = new MeiliSearch({ host }) |
| 60 | + const strippedHost = trailing ? host.slice(0, -1) : host |
| 61 | + |
| 62 | + await expect(client.createSnapshot()).rejects.toHaveProperty( |
| 63 | + 'message', |
| 64 | + `request to ${strippedHost}/${route} failed, reason: connect ECONNREFUSED ${BAD_HOST.replace( |
| 65 | + 'http://', |
| 66 | + '' |
| 67 | + )}` |
| 68 | + ) |
| 69 | + }) |
| 70 | +}) |
0 commit comments