-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapter_test.js
118 lines (105 loc) · 3.45 KB
/
adapter_test.js
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
const assert = require('assert');
const { isMatch, pick } = require('lodash');
const uuid = require('uuid/v4');
const epochTime = require('./helpers/epoch_time');
const instance = require('./helpers/weak_cache');
module.exports = class AdapterTest {
constructor(provider, accountId = uuid, clientId = uuid) {
this.provider = provider;
this.data = {
accountId: accountId(),
authTime: epochTime(),
claims: {
id_token: {
email: null,
family_name: { essential: true },
gender: { essential: false },
given_name: { value: 'John' },
locale: { values: ['en-US', 'en-GB'] },
middle_name: {},
},
},
clientId: clientId(),
grantId: uuid(),
nonce: String(Math.random()),
redirectUri: 'http://client.example.com/cb',
scope: 'openid profile',
};
}
async execute() {
await this.authorizationCodeInsert()
.then(this.authorizationCodeFind.bind(this))
.then(this.authorizationCodeConsume.bind(this))
.then(this.accessTokenSave.bind(this))
.then(this.accessTokenFind.bind(this))
.then(this.accessTokenDestroy.bind(this));
if (instance(this.provider).configuration('features.deviceFlow')) {
const dc = new (this.provider.DeviceCode)({
clientId: this.data.clientId,
grantId: this.data.grantId,
userCode: '123-456-789',
params: {
client_id: 'client',
scope: 'openid',
},
});
await dc.save();
const found = await this.provider.DeviceCode.findByUserCode('123-456-789', { ignoreExpiration: true });
assert(found, 'expected device code to be found by user code');
}
}
authorizationCodeInsert() {
const ac = new (this.provider.AuthorizationCode)(this.data);
return ac.save().then((saved) => {
assert(saved, 'expected code to be saved');
return saved;
});
}
authorizationCodeFind(code) {
this.ac = code;
return this.provider.AuthorizationCode.find(code, {
ignoreExpiration: true,
}).then((found) => {
this.code = found;
assert(found, 'expected code to be found');
assert(isMatch(found, this.data), 'expected stored values to match the original ones');
return found;
});
}
authorizationCodeConsume(code) {
return code.consume().then(() => this.provider.AuthorizationCode.find(this.ac, {
ignoreExpiration: true,
})).then((found) => {
assert(found.consumed, 'expected code to be consumed');
});
}
accessTokenSave() {
const at = new (this.provider.AccessToken)(pick(this.code, 'accountId', 'claims', 'clientId', 'grantId', 'scope'));
return at.save().then((saved) => {
assert(saved, 'expected access token to be saved');
return saved;
});
}
accessTokenFind(token) {
this.token = token;
return this.provider.AccessToken.find(token, {
ignoreExpiration: true,
}).then((found) => {
assert(found, 'expected token to be found');
return found;
});
}
accessTokenDestroy(token) {
return token.destroy().then(() => this.provider.AccessToken.find(this.token, {
ignoreExpiration: true,
})).then((found) => {
assert(!found, 'expected token not to be found');
})
.then(() => this.provider.AuthorizationCode.find(this.ac, {
ignoreExpiration: true,
}))
.then((found) => {
assert(!found, 'expected authorization code not to be found');
});
}
};