|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import { describe, it, before, after } from 'node:test' |
| 3 | + |
| 4 | +import { init } from './index.js' |
| 5 | +import Group from '../lib/group.js' |
| 6 | +import User from '../lib/user.js' |
| 7 | +import Zone from '../lib/zone.js' |
| 8 | + |
| 9 | +import groupCase from './test/group.json' with { type: 'json' } |
| 10 | +import userCase from './test/user.json' with { type: 'json' } |
| 11 | +import nsCase from './test/zone.json' with { type: 'json' } |
| 12 | + |
| 13 | +let server |
| 14 | +let case2Id = 4094 |
| 15 | + |
| 16 | +before(async () => { |
| 17 | + await Zone.destroy({ id: case2Id }) |
| 18 | + await Group.create(groupCase) |
| 19 | + await User.create(userCase) |
| 20 | + await Zone.create(nsCase) |
| 21 | + server = await init() |
| 22 | +}) |
| 23 | + |
| 24 | +after(async () => { |
| 25 | + // await Zone.destroy({ id: case2Id }) |
| 26 | + server.stop() |
| 27 | +}) |
| 28 | + |
| 29 | +describe('zone routes', () => { |
| 30 | + let sessionCookie |
| 31 | + |
| 32 | + it('POST /session establishes a session', async () => { |
| 33 | + const res = await server.inject({ |
| 34 | + method: 'POST', |
| 35 | + url: '/session', |
| 36 | + payload: { |
| 37 | + username: `${userCase.username}@${groupCase.name}`, |
| 38 | + password: userCase.password, |
| 39 | + }, |
| 40 | + }) |
| 41 | + assert.ok(res.headers['set-cookie'][0]) |
| 42 | + sessionCookie = res.headers['set-cookie'][0].split(';')[0] |
| 43 | + }) |
| 44 | + |
| 45 | + it(`GET /zone/${nsCase.id}`, async () => { |
| 46 | + const res = await server.inject({ |
| 47 | + method: 'GET', |
| 48 | + url: `/zone/${nsCase.id}`, |
| 49 | + headers: { |
| 50 | + Cookie: sessionCookie, |
| 51 | + }, |
| 52 | + }) |
| 53 | + // console.log(res.result) |
| 54 | + assert.equal(res.statusCode, 200) |
| 55 | + assert.equal(res.result.zone.name, nsCase.name) |
| 56 | + }) |
| 57 | + |
| 58 | + it(`POST /zone (${case2Id})`, async () => { |
| 59 | + const testCase = JSON.parse(JSON.stringify(nsCase)) |
| 60 | + testCase.id = case2Id // make it unique |
| 61 | + testCase.gid = case2Id |
| 62 | + testCase.zone = 'route2.example.com.' |
| 63 | + |
| 64 | + const res = await server.inject({ |
| 65 | + method: 'POST', |
| 66 | + url: '/zone', |
| 67 | + headers: { |
| 68 | + Cookie: sessionCookie, |
| 69 | + }, |
| 70 | + payload: testCase, |
| 71 | + }) |
| 72 | + // console.log(res.result) |
| 73 | + assert.equal(res.statusCode, 201) |
| 74 | + assert.ok(res.result.zone.gid) |
| 75 | + }) |
| 76 | + |
| 77 | + it(`GET /zone/${case2Id}`, async () => { |
| 78 | + const res = await server.inject({ |
| 79 | + method: 'GET', |
| 80 | + url: `/zone/${case2Id}`, |
| 81 | + headers: { |
| 82 | + Cookie: sessionCookie, |
| 83 | + }, |
| 84 | + }) |
| 85 | + // console.log(res.result) |
| 86 | + assert.equal(res.statusCode, 200) |
| 87 | + assert.ok(res.result.zone.gid) |
| 88 | + }) |
| 89 | + |
| 90 | + it(`DELETE /zone/${case2Id}`, async () => { |
| 91 | + const res = await server.inject({ |
| 92 | + method: 'DELETE', |
| 93 | + url: `/zone/${case2Id}`, |
| 94 | + headers: { |
| 95 | + Cookie: sessionCookie, |
| 96 | + }, |
| 97 | + }) |
| 98 | + // console.log(res.result) |
| 99 | + assert.equal(res.statusCode, 200) |
| 100 | + }) |
| 101 | + |
| 102 | + it(`DELETE /zone/${case2Id}`, async () => { |
| 103 | + const res = await server.inject({ |
| 104 | + method: 'DELETE', |
| 105 | + url: `/zone/${case2Id}`, |
| 106 | + headers: { |
| 107 | + Cookie: sessionCookie, |
| 108 | + }, |
| 109 | + }) |
| 110 | + // console.log(res.result) |
| 111 | + assert.equal(res.statusCode, 404) |
| 112 | + }) |
| 113 | + |
| 114 | + it(`GET /zone/${case2Id}`, async () => { |
| 115 | + const res = await server.inject({ |
| 116 | + method: 'GET', |
| 117 | + url: `/zone/${case2Id}`, |
| 118 | + headers: { |
| 119 | + Cookie: sessionCookie, |
| 120 | + }, |
| 121 | + }) |
| 122 | + // console.log(res.result) |
| 123 | + // assert.equal(res.statusCode, 200) |
| 124 | + assert.equal(res.result.zone, undefined) |
| 125 | + }) |
| 126 | + |
| 127 | + it(`GET /zone/${case2Id} (deleted)`, async () => { |
| 128 | + const res = await server.inject({ |
| 129 | + method: 'GET', |
| 130 | + url: `/zone/${case2Id}?deleted=true`, |
| 131 | + headers: { |
| 132 | + Cookie: sessionCookie, |
| 133 | + }, |
| 134 | + }) |
| 135 | + // console.log(res.result) |
| 136 | + assert.equal(res.statusCode, 200) |
| 137 | + assert.equal(res?.result?.zone, undefined) |
| 138 | + }) |
| 139 | + |
| 140 | + it('DELETE /session', async () => { |
| 141 | + const res = await server.inject({ |
| 142 | + method: 'DELETE', |
| 143 | + url: '/session', |
| 144 | + headers: { |
| 145 | + Cookie: sessionCookie, |
| 146 | + }, |
| 147 | + }) |
| 148 | + assert.equal(res.statusCode, 200) |
| 149 | + }) |
| 150 | +}) |
0 commit comments