-
-
Notifications
You must be signed in to change notification settings - Fork 795
Expand file tree
/
Copy pathstrategy.test.ts
More file actions
158 lines (132 loc) · 4.36 KB
/
strategy.test.ts
File metadata and controls
158 lines (132 loc) · 4.36 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import assert from 'assert';
import omit from 'lodash/omit';
import { Application } from '@feathersjs/feathers';
import { LocalStrategy } from '../src';
import { createApplication, ServiceTypes } from './fixture';
describe('@feathersjs/authentication-local/strategy', () => {
const password = 'localsecret';
const email = 'localtester@feathersjs.com';
let app: Application<ServiceTypes>;
let user: any;
beforeEach(async () => {
app = createApplication();
user = await app.service('users').create({ email, password });
});
it('throw error when configuration is not set', () => {
const auth = app.service('authentication');
try {
auth.register('something', new LocalStrategy());
assert.fail('Should never get here');
} catch (error) {
assert.strictEqual(error.message,
'\'something\' authentication strategy requires a \'usernameField\' setting'
);
}
});
it('fails when entity not found', async () => {
const authService = app.service('authentication');
try {
await authService.create({
strategy: 'local',
email: 'not in database',
password
});
assert.fail('Should never get here');
} catch (error) {
assert.strictEqual(error.name, 'NotAuthenticated');
assert.strictEqual(error.message, 'Invalid login');
}
});
it('getEntity', async () => {
const [ strategy ] = app.service('authentication').getStrategies('local') as [ LocalStrategy ];
let entity = await strategy.getEntity(user, {});
assert.deepStrictEqual(entity, user);
entity = await strategy.getEntity(user, {
provider: 'testing'
});
assert.deepStrictEqual(entity, {
...omit(user, 'password'),
fromGet: true
});
try {
await strategy.getEntity({}, {});
assert.fail('Should never get here');
} catch (error) {
assert.strictEqual(error.message, 'Could not get local entity');
}
});
it('strategy fails when strategy is different', async () => {
const [ local ] = app.service('authentication').getStrategies('local');
await assert.rejects(() => local.authenticate({
strategy: 'not-me',
password: 'dummy',
email
}, {}), {
name: 'NotAuthenticated',
message: 'Invalid login'
});
});
it('fails when password is wrong', async () => {
const authService = app.service('authentication');
try {
await authService.create({
strategy: 'local',
email,
password: 'dummy'
});
assert.fail('Should never get here');
} catch (error) {
assert.strictEqual(error.name, 'NotAuthenticated');
assert.strictEqual(error.message, 'Invalid login');
}
});
it('fails when password field is not available', async () => {
const userEmail = 'someuser@localtest.com';
const authService = app.service('authentication');
try {
await app.service('users').create({
email: userEmail
});
await authService.create({
strategy: 'local',
password: 'dummy',
email: userEmail
});
assert.fail('Should never get here');
} catch (error) {
assert.strictEqual(error.name, 'NotAuthenticated');
assert.strictEqual(error.message, 'Invalid login');
}
});
it('authenticates an existing user', async () => {
const authService = app.service('authentication');
const authResult = await authService.create({
strategy: 'local',
email,
password
});
const { accessToken } = authResult;
assert.ok(accessToken);
assert.strictEqual(authResult.user.email, email);
const decoded = await authService.verifyAccessToken(accessToken);
assert.strictEqual(decoded.sub, `${user.id}`);
});
it('returns safe result when params.provider is set, works without pagination', async () => {
const authService = app.service('authentication');
const authResult = await authService.create({
strategy: 'local',
email,
password
}, {
provider: 'rest',
paginate: false
});
const { accessToken } = authResult;
assert.ok(accessToken);
assert.strictEqual(authResult.user.email, email);
assert.strictEqual(authResult.user.password, undefined);
assert.ok(authResult.user.fromGet);
const decoded = await authService.verifyAccessToken(accessToken);
assert.strictEqual(decoded.sub, `${user.id}`);
});
});