/
specifying-the-store-locally.feature.ts
116 lines (87 loc) 路 2.44 KB
/
specifying-the-store-locally.feature.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// std
import { notStrictEqual, strictEqual } from 'assert';
// 3p
import * as request from 'supertest';
// FoalTS
import {
Config,
Context,
controller,
createApp,
createSession,
dependency,
Get,
HttpResponseOK,
IAppController,
Post,
ServiceManager,
Session,
UseSessions
} from '@foal/core';
import { RedisStore } from '@foal/redis';
describe('Feature: Specifying the store locally', () => {
let store: RedisStore|undefined;
beforeEach(() => {
Config.set('settings.redis.uri', 'redis://localhost:6380');
});
afterEach(async () => {
Config.remove('settings.redis.uri');
if (store) {
await store.close();
}
});
it('Example: Usage with Redis store', async () => {
let session: Session|undefined;
/* ======================= DOCUMENTATION BEGIN ======================= */
@UseSessions({ store: RedisStore })
class ApiController {
@dependency
store: RedisStore;
@Post('/login')
async login(ctx: Context) {
// Check the user credentials...
ctx.session = await createSession(this.store);
return new HttpResponseOK({
token: ctx.session.getToken()
});
}
@Get('/products')
readProducts(ctx: Context) {
// Not in the documentation:
session = ctx.session;
return new HttpResponseOK([]);
}
}
/* ======================= DOCUMENTATION END ========================= */
class AppController implements IAppController {
subControllers = [
controller('/api', ApiController),
];
}
const services = new ServiceManager();
const app = await createApp(AppController, { serviceManager: services });
store = services.get(RedisStore);
strictEqual(session, undefined);
await request(app)
.get('/api/products')
.expect(200)
.expect([]);
strictEqual(session, undefined);
const response = await request(app)
.post('/api/login')
.send({})
.expect(200);
strictEqual(session, undefined);
const token: undefined|string = response.body.token;
if (token === undefined) {
throw new Error('The server should have returned a session token.');
}
await request(app)
.get('/api/products')
.set('Authorization', `Bearer ${token}`)
.expect(200)
.expect([]);
notStrictEqual(session, undefined);
strictEqual((session as unknown as Session).getToken(), token);
});
});