create-soda 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js
CHANGED
|
@@ -1230,9 +1230,9 @@ import { stdin as stdin2, stdout as stdout2 } from "node:process";
|
|
|
1230
1230
|
import { fileURLToPath } from "node:url";
|
|
1231
1231
|
import { styleText as styleText3 } from "node:util";
|
|
1232
1232
|
// ../core/package.json
|
|
1233
|
-
var version = "0.
|
|
1233
|
+
var version = "0.3.0";
|
|
1234
1234
|
// package.json
|
|
1235
|
-
var version2 = "0.
|
|
1235
|
+
var version2 = "0.3.0";
|
|
1236
1236
|
|
|
1237
1237
|
// src/project.ts
|
|
1238
1238
|
import { randomBytes } from "node:crypto";
|
package/package.json
CHANGED
|
@@ -1,24 +1,18 @@
|
|
|
1
1
|
import { afterAll, describe, expect, test } from 'bun:test'
|
|
2
|
-
import {
|
|
2
|
+
import { provide } from '@getsoda/core'
|
|
3
|
+
import { createTestApp } from '@getsoda/core/testing'
|
|
3
4
|
import { AppModule } from './app.module.ts'
|
|
4
5
|
import { AppConfig } from './config.ts'
|
|
5
6
|
|
|
6
7
|
// Tests do not depend on .env: the configuration is replaced.
|
|
7
|
-
const app = await
|
|
8
|
-
logger: false,
|
|
8
|
+
const app = await createTestApp(AppModule, {
|
|
9
9
|
overrides: [provide(AppConfig, { useValue: { PORT: 0, CORS_ORIGIN: '*', JWT_SECRET: 'a-test-secret-of-at-least-32-characters' } })],
|
|
10
10
|
})
|
|
11
|
-
const server = await app.listen(0)
|
|
12
|
-
const url = (path: string) => new URL(path, server.url)
|
|
13
11
|
|
|
14
12
|
afterAll(() => app.close())
|
|
15
13
|
|
|
16
14
|
const login = async (name: string) => {
|
|
17
|
-
const response = await
|
|
18
|
-
method: 'POST',
|
|
19
|
-
headers: { 'content-type': 'application/json' },
|
|
20
|
-
body: JSON.stringify({ name }),
|
|
21
|
-
})
|
|
15
|
+
const response = await app.request('/auth/login', { method: 'POST', json: { name } })
|
|
22
16
|
|
|
23
17
|
return { response, token: ((await response.json()) as { token: string }).token }
|
|
24
18
|
}
|
|
@@ -31,13 +25,13 @@ describe('auth', () => {
|
|
|
31
25
|
expect(response.status).toBe(201)
|
|
32
26
|
expect(cookie).toStartWith(`token=${token};`)
|
|
33
27
|
expect(cookie).toContain('HttpOnly')
|
|
34
|
-
expect(await (await
|
|
35
|
-
expect(await (await
|
|
28
|
+
expect(await (await app.request('/auth/me', { headers: { authorization: `Bearer ${token}` } })).json()).toEqual({ name: 'ann' })
|
|
29
|
+
expect(await (await app.request('/auth/me', { headers: { cookie: `token=${token}` } })).json()).toEqual({ name: 'ann' })
|
|
36
30
|
})
|
|
37
31
|
|
|
38
32
|
test('refuses requests without a valid token', async () => {
|
|
39
|
-
const anonymous = await
|
|
40
|
-
const forged = await
|
|
33
|
+
const anonymous = await app.request('/auth/me')
|
|
34
|
+
const forged = await app.request('/auth/me', { headers: { authorization: 'Bearer a.b.c' } })
|
|
41
35
|
|
|
42
36
|
expect([anonymous.status, forged.status]).toEqual([401, 401])
|
|
43
37
|
})
|
|
@@ -46,28 +40,20 @@ describe('auth', () => {
|
|
|
46
40
|
describe('users', () => {
|
|
47
41
|
test('are created by signed-in users, and read by anyone', async () => {
|
|
48
42
|
const { token } = await login('ann')
|
|
49
|
-
const created = await
|
|
50
|
-
method: 'POST',
|
|
51
|
-
headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
|
|
52
|
-
body: JSON.stringify({ name: 'Bo' }),
|
|
53
|
-
})
|
|
43
|
+
const created = await app.request('/users', { method: 'POST', headers: { authorization: `Bearer ${token}` }, json: { name: 'Bo' } })
|
|
54
44
|
|
|
55
45
|
expect(created.status).toBe(201)
|
|
56
46
|
expect(await created.json()).toEqual({ id: 1, name: 'Bo', createdBy: 'ann' })
|
|
57
|
-
expect(await (await
|
|
47
|
+
expect(await (await app.request('/users/1')).json()).toEqual({ id: 1, name: 'Bo', createdBy: 'ann' })
|
|
58
48
|
})
|
|
59
49
|
|
|
60
50
|
test('refuses anonymous writes, invalid bodies and unknown users', async () => {
|
|
61
51
|
const { token } = await login('ann')
|
|
62
|
-
const anonymous = await
|
|
63
|
-
const invalid = await
|
|
64
|
-
method: 'POST',
|
|
65
|
-
headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
|
|
66
|
-
body: JSON.stringify({ name: '' }),
|
|
67
|
-
})
|
|
52
|
+
const anonymous = await app.request('/users', { method: 'POST', json: {} })
|
|
53
|
+
const invalid = await app.request('/users', { method: 'POST', headers: { authorization: `Bearer ${token}` }, json: { name: '' } })
|
|
68
54
|
|
|
69
55
|
expect(anonymous.status).toBe(401)
|
|
70
56
|
expect(invalid.status).toBe(400)
|
|
71
|
-
expect((await
|
|
57
|
+
expect((await app.request('/users/999')).status).toBe(404)
|
|
72
58
|
})
|
|
73
59
|
})
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { afterAll, expect, test } from 'bun:test'
|
|
2
|
-
import {
|
|
2
|
+
import { createTestApp } from '@getsoda/core/testing'
|
|
3
3
|
import { AppModule } from './app.module.ts'
|
|
4
4
|
|
|
5
|
-
const app = await
|
|
6
|
-
const server = await app.listen(0)
|
|
5
|
+
const app = await createTestApp(AppModule)
|
|
7
6
|
|
|
8
7
|
afterAll(() => app.close())
|
|
9
8
|
|
|
10
9
|
test('GET / says hello', async () => {
|
|
11
|
-
const response = await
|
|
10
|
+
const response = await app.request('/')
|
|
12
11
|
|
|
13
12
|
expect(response.status).toBe(200)
|
|
14
13
|
expect(await response.text()).toBe('Hello Soda!')
|