Skip to content

Commit d019f84

Browse files
authored
Merge pull request #29 from Testlio/bugfix/authentication-test
Bugfix/authentication test
2 parents a5c26d1 + 5b7f262 commit d019f84

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

test/authentication-test.js

+24-12
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ tape.test('If invalid token then return 401', function(t) {
3434
tape.test('If valid token then return decoded token', function(t) {
3535

3636
try {
37-
const decoded = auth.isValidToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwic2NvcGVzIjpbInRlc3RlciJdfQ.MjK579tyUaxtY9FXpTktC-vssI-rOS1RNsGl8KWX9mM');
37+
const decoded = auth.isValidToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwic2NvcGUiOlsidGVzdGVyIl19.ZzBZRdxQHFemCW2TwwFRn8Jk-uWt-OLtsi6O5pWpM34');
3838
t.equal(decoded.sub, '[email protected]');
3939
t.end();
4040
} catch (err) {
@@ -71,7 +71,7 @@ tape.test('If valid token with altered secret then return decoded token', functi
7171
tape.test('If valid token with Bearer keyword then return decoded token', function(t) {
7272

7373
try {
74-
const decoded = auth.isValidToken('Bearer ' + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwic2NvcGVzIjpbInRlc3RlciJdfQ.MjK579tyUaxtY9FXpTktC-vssI-rOS1RNsGl8KWX9mM');
74+
const decoded = auth.isValidToken('Bearer ' + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwic2NvcGUiOlsidGVzdGVyIl19.ZzBZRdxQHFemCW2TwwFRn8Jk-uWt-OLtsi6O5pWpM34');
7575
t.equal(decoded.sub, '[email protected]');
7676
t.end();
7777
} catch (err) {
@@ -154,21 +154,33 @@ tape.test('if no unallowed roles present with None rule then return true', funct
154154
t.end();
155155
});
156156

157-
tape.test('if valid token and valid scope then return true', function(t) {
157+
tape.test('if valid token and valid scope then resolve decoded token', function(t) {
158158

159-
const authPromise = auth.authenticate('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwic2NvcGVzIjpbInRlc3RlciJdfQ.MjK579tyUaxtY9FXpTktC-vssI-rOS1RNsGl8KWX9mM', {
159+
const expectedDecodedToken = {
160+
161+
'scope': [
162+
'tester'
163+
]
164+
};
165+
166+
const authPromise = auth.authenticate('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwic2NvcGUiOlsidGVzdGVyIl19.ZzBZRdxQHFemCW2TwwFRn8Jk-uWt-OLtsi6O5pWpM34', {
160167
scope: ['admin'],
161168
rule: auth.RULE.NONE
162169
});
163170

164-
t.ok(authPromise, 'true expected');
165-
t.end();
171+
authPromise.then(function(decodedToken) {
172+
t.deepEqual(expectedDecodedToken, decodedToken, 'decoded token doesn\'t match');
173+
t.end();
174+
}).catch(function(err) {
175+
t.fail(err.message);
176+
t.end();
177+
});
166178
});
167179

168-
tape.test('if invalid token and valid scope then false', function(t) {
180+
tape.test('if invalid token and valid scope then reject', function(t) {
169181

170-
const authPromise = auth.authenticate('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwic2NvcGVzIjpbInRlc3RlciJdfQ.MjK579tyUaxtY9FXpTktC-vssI-rOS1RNsGl8KWX9mM' + 'foo', {
171-
scope: ['admin'],
182+
const authPromise = auth.authenticate('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwic2NvcGUiOlsidGVzdGVyIl19.ZzBZRdxQHFemCW2TwwFRn8Jk-uWt-OLtsi6O5pWpM34' + 'foo', {
183+
scope: ['tester'],
172184
rule: auth.RULE.NONE
173185
});
174186

@@ -182,10 +194,10 @@ tape.test('if invalid token and valid scope then false', function(t) {
182194
});
183195
});
184196

185-
tape.test('if valid token and invalid scope then false', function(t) {
197+
tape.test('if valid token and invalid scope then reject', function(t) {
186198

187-
const authPromise = auth.authenticate('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwic2NvcGVzIjpbInRlc3RlciJdfQ.MjK579tyUaxtY9FXpTktC-vssI-rOS1RNsGl8KWX9mM', {
188-
scope: ['tester']
199+
const authPromise = auth.authenticate('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwic2NvcGUiOlsidGVzdGVyIl19.ZzBZRdxQHFemCW2TwwFRn8Jk-uWt-OLtsi6O5pWpM34', {
200+
scope: ['admin']
189201
});
190202

191203
authPromise.then(function() {

0 commit comments

Comments
 (0)