|
| 1 | +import { AdminCreateLogInput, AdminFindManyLogInput, AdminUpdateLogInput, Log } from '@pubkey-link/sdk' |
| 2 | +import { getAliceCookie, getBobCookie, sdk, uniqueId } from '../support' |
| 3 | + |
| 4 | +describe('api-log-feature', () => { |
| 5 | + describe('api-log-admin-resolver', () => { |
| 6 | + const logName = uniqueId('acme-log') |
| 7 | + let logId: string |
| 8 | + let cookie: string |
| 9 | + |
| 10 | + beforeAll(async () => { |
| 11 | + cookie = await getAliceCookie() |
| 12 | + const created = await sdk.adminCreateLog({ input: { name: logName } }, { cookie }) |
| 13 | + logId = created.data.created.id |
| 14 | + }) |
| 15 | + |
| 16 | + describe('authorized', () => { |
| 17 | + beforeAll(async () => { |
| 18 | + cookie = await getAliceCookie() |
| 19 | + }) |
| 20 | + |
| 21 | + it('should create a log', async () => { |
| 22 | + const input: AdminCreateLogInput = { |
| 23 | + name: uniqueId('log'), |
| 24 | + } |
| 25 | + |
| 26 | + const res = await sdk.adminCreateLog({ input }, { cookie }) |
| 27 | + |
| 28 | + const item: Log = res.data.created |
| 29 | + expect(item.name).toBe(input.name) |
| 30 | + expect(item.id).toBeDefined() |
| 31 | + expect(item.createdAt).toBeDefined() |
| 32 | + expect(item.updatedAt).toBeDefined() |
| 33 | + }) |
| 34 | + |
| 35 | + it('should update a log', async () => { |
| 36 | + const createInput: AdminCreateLogInput = { |
| 37 | + name: uniqueId('log'), |
| 38 | + } |
| 39 | + const createdRes = await sdk.adminCreateLog({ input: createInput }, { cookie }) |
| 40 | + const logId = createdRes.data.created.id |
| 41 | + const input: AdminUpdateLogInput = { |
| 42 | + name: uniqueId('log'), |
| 43 | + } |
| 44 | + |
| 45 | + const res = await sdk.adminUpdateLog({ logId, input }, { cookie }) |
| 46 | + |
| 47 | + const item: Log = res.data.updated |
| 48 | + expect(item.name).toBe(input.name) |
| 49 | + }) |
| 50 | + |
| 51 | + it('should find a list of logs (find all)', async () => { |
| 52 | + const createInput: AdminCreateLogInput = { |
| 53 | + name: uniqueId('log'), |
| 54 | + } |
| 55 | + const createdRes = await sdk.adminCreateLog({ input: createInput }, { cookie }) |
| 56 | + const logId = createdRes.data.created.id |
| 57 | + |
| 58 | + const input: AdminFindManyLogInput = {} |
| 59 | + |
| 60 | + const res = await sdk.adminFindManyLog({ input }, { cookie }) |
| 61 | + |
| 62 | + expect(res.data.paging.meta.totalCount).toBeGreaterThan(1) |
| 63 | + expect(res.data.paging.data.length).toBeGreaterThan(1) |
| 64 | + // First item should be the one we created above |
| 65 | + expect(res.data.paging.data[0].id).toBe(logId) |
| 66 | + }) |
| 67 | + |
| 68 | + it('should find a list of logs (find new one)', async () => { |
| 69 | + const createInput: AdminCreateLogInput = { |
| 70 | + name: uniqueId('log'), |
| 71 | + } |
| 72 | + const createdRes = await sdk.adminCreateLog({ input: createInput }, { cookie }) |
| 73 | + const logId = createdRes.data.created.id |
| 74 | + |
| 75 | + const input: AdminFindManyLogInput = { |
| 76 | + search: logId, |
| 77 | + } |
| 78 | + |
| 79 | + const res = await sdk.adminFindManyLog({ input }, { cookie }) |
| 80 | + |
| 81 | + expect(res.data.paging.meta.totalCount).toBe(1) |
| 82 | + expect(res.data.paging.data.length).toBe(1) |
| 83 | + expect(res.data.paging.data[0].id).toBe(logId) |
| 84 | + }) |
| 85 | + |
| 86 | + it('should find a log by id', async () => { |
| 87 | + const createInput: AdminCreateLogInput = { |
| 88 | + name: uniqueId('log'), |
| 89 | + } |
| 90 | + const createdRes = await sdk.adminCreateLog({ input: createInput }, { cookie }) |
| 91 | + const logId = createdRes.data.created.id |
| 92 | + |
| 93 | + const res = await sdk.adminFindOneLog({ logId }, { cookie }) |
| 94 | + |
| 95 | + expect(res.data.item.id).toBe(logId) |
| 96 | + }) |
| 97 | + |
| 98 | + it('should delete a log', async () => { |
| 99 | + const createInput: AdminCreateLogInput = { |
| 100 | + name: uniqueId('log'), |
| 101 | + } |
| 102 | + const createdRes = await sdk.adminCreateLog({ input: createInput }, { cookie }) |
| 103 | + const logId = createdRes.data.created.id |
| 104 | + |
| 105 | + const res = await sdk.adminDeleteLog({ logId }, { cookie }) |
| 106 | + |
| 107 | + expect(res.data.deleted).toBe(true) |
| 108 | + |
| 109 | + const findRes = await sdk.adminFindManyLog({ input: { search: logId } }, { cookie }) |
| 110 | + expect(findRes.data.paging.meta.totalCount).toBe(0) |
| 111 | + expect(findRes.data.paging.data.length).toBe(0) |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + describe('unauthorized', () => { |
| 116 | + let cookie: string |
| 117 | + beforeAll(async () => { |
| 118 | + cookie = await getBobCookie() |
| 119 | + }) |
| 120 | + |
| 121 | + it('should not create a log', async () => { |
| 122 | + expect.assertions(1) |
| 123 | + const input: AdminCreateLogInput = { |
| 124 | + name: uniqueId('log'), |
| 125 | + } |
| 126 | + |
| 127 | + try { |
| 128 | + await sdk.adminCreateLog({ input }, { cookie }) |
| 129 | + } catch (e) { |
| 130 | + expect(e.message).toBe('Unauthorized: User is not Admin') |
| 131 | + } |
| 132 | + }) |
| 133 | + |
| 134 | + it('should not update a log', async () => { |
| 135 | + expect.assertions(1) |
| 136 | + try { |
| 137 | + await sdk.adminUpdateLog({ logId, input: {} }, { cookie }) |
| 138 | + } catch (e) { |
| 139 | + expect(e.message).toBe('Unauthorized: User is not Admin') |
| 140 | + } |
| 141 | + }) |
| 142 | + |
| 143 | + it('should not find a list of logs (find all)', async () => { |
| 144 | + expect.assertions(1) |
| 145 | + try { |
| 146 | + await sdk.adminFindManyLog({ input: {} }, { cookie }) |
| 147 | + } catch (e) { |
| 148 | + expect(e.message).toBe('Unauthorized: User is not Admin') |
| 149 | + } |
| 150 | + }) |
| 151 | + |
| 152 | + it('should not find a log by id', async () => { |
| 153 | + expect.assertions(1) |
| 154 | + try { |
| 155 | + await sdk.adminFindOneLog({ logId }, { cookie }) |
| 156 | + } catch (e) { |
| 157 | + expect(e.message).toBe('Unauthorized: User is not Admin') |
| 158 | + } |
| 159 | + }) |
| 160 | + |
| 161 | + it('should not delete a log', async () => { |
| 162 | + expect.assertions(1) |
| 163 | + try { |
| 164 | + await sdk.adminDeleteLog({ logId }, { cookie }) |
| 165 | + } catch (e) { |
| 166 | + expect(e.message).toBe('Unauthorized: User is not Admin') |
| 167 | + } |
| 168 | + }) |
| 169 | + }) |
| 170 | + }) |
| 171 | +}) |
0 commit comments