|
| 1 | +const assert = require('assert'); |
| 2 | +const _axios = require('axios'); |
| 3 | +const feathers = require('@feathersjs/feathers'); |
| 4 | +const getApp = require('@feathersjs/authentication-local/test/fixture'); |
| 5 | +const { authenticate } = require('@feathersjs/authentication'); |
| 6 | + |
| 7 | +const expressify = require('../lib'); |
| 8 | +const axios = _axios.create({ |
| 9 | + baseURL: 'http://localhost:9876/' |
| 10 | +}); |
| 11 | + |
| 12 | +describe('@feathersjs/express/authentication', () => { |
| 13 | + const email = 'expresstest@authentication.com'; |
| 14 | + const password = 'superexpress'; |
| 15 | + |
| 16 | + let app, server, user, authResult; |
| 17 | + |
| 18 | + before(() => { |
| 19 | + const expressApp = expressify(feathers()) |
| 20 | + .use(expressify.json()) |
| 21 | + .use(expressify.parseAuthentication('jwt')) |
| 22 | + .configure(expressify.rest()); |
| 23 | + |
| 24 | + app = getApp(expressApp); |
| 25 | + server = app.listen(9876); |
| 26 | + |
| 27 | + app.use('/dummy', { |
| 28 | + get (id, params) { |
| 29 | + return Promise.resolve({ id, params }); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + app.use('/protected', expressify.authenticate('jwt'), (req, res) => { |
| 34 | + res.json(req.user); |
| 35 | + }); |
| 36 | + |
| 37 | + app.use(expressify.errorHandler({ |
| 38 | + logger: false |
| 39 | + })); |
| 40 | + |
| 41 | + app.service('dummy').hooks({ |
| 42 | + before: [ authenticate('jwt') ] |
| 43 | + }); |
| 44 | + |
| 45 | + return app.service('users').create({ email, password }) |
| 46 | + .then(result => { |
| 47 | + user = result; |
| 48 | + |
| 49 | + return axios.post('/authentication', { |
| 50 | + strategy: 'local', |
| 51 | + password, |
| 52 | + email |
| 53 | + }); |
| 54 | + }).then(res => { |
| 55 | + authResult = res.data; |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + after(done => server.close(done)); |
| 60 | + |
| 61 | + it('middleware needs strategies ', () => { |
| 62 | + try { |
| 63 | + expressify.parseAuthentication(); |
| 64 | + assert.fail('Should never get here'); |
| 65 | + } catch (error) { |
| 66 | + assert.strictEqual(error.message, |
| 67 | + `'parseAuthentication' middleware requires at least one strategy name` |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + expressify.authenticate(); |
| 73 | + assert.fail('Should never get here'); |
| 74 | + } catch(error) { |
| 75 | + assert.strictEqual(error.message, |
| 76 | + `'authenticate' middleware requires at least one strategy name` |
| 77 | + ); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + describe('service authentication', () => { |
| 82 | + it('successful local authentication', () => { |
| 83 | + assert.ok(authResult.accessToken); |
| 84 | + assert.deepStrictEqual(authResult.authentication, { |
| 85 | + strategy: 'local' |
| 86 | + }); |
| 87 | + assert.strictEqual(authResult.user.email, email); |
| 88 | + assert.strictEqual(authResult.user.password, undefined); |
| 89 | + }); |
| 90 | + |
| 91 | + it('local authentication with wrong password fails', () => { |
| 92 | + return axios.post('/authentication', { |
| 93 | + strategy: 'local', |
| 94 | + password: 'wrong', |
| 95 | + email |
| 96 | + }).then(() => { |
| 97 | + assert.fail('Should never get here'); |
| 98 | + }).catch(error => { |
| 99 | + const { data } = error.response; |
| 100 | + assert.strictEqual(data.name, 'NotAuthenticated'); |
| 101 | + assert.strictEqual(data.message, 'Invalid login'); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it('authenticating with JWT works but returns same accessToken', () => { |
| 106 | + const { accessToken } = authResult; |
| 107 | + |
| 108 | + return axios.post('/authentication', { |
| 109 | + strategy: 'jwt', |
| 110 | + accessToken |
| 111 | + }).then(res => { |
| 112 | + const { data } = res; |
| 113 | + |
| 114 | + assert.strictEqual(data.accessToken, accessToken); |
| 115 | + assert.strictEqual(data.authentication.strategy, 'jwt'); |
| 116 | + assert.strictEqual(data.authentication.payload.sub, user.id.toString()); |
| 117 | + assert.strictEqual(data.user.email, email); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('can make a protected request with Authorization header', () => { |
| 122 | + const { accessToken } = authResult; |
| 123 | + |
| 124 | + return axios.get('/dummy/dave', { |
| 125 | + headers: { |
| 126 | + Authorization: accessToken |
| 127 | + } |
| 128 | + }).then(res => { |
| 129 | + const { data, data: { params } } = res; |
| 130 | + |
| 131 | + assert.strictEqual(data.id, 'dave'); |
| 132 | + assert.deepStrictEqual(params.user, user); |
| 133 | + assert.strictEqual(params.authentication.accessToken, accessToken); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it('can make a protected request with Authorization header and bearer scheme', () => { |
| 138 | + const { accessToken } = authResult; |
| 139 | + |
| 140 | + return axios.get('/dummy/dave', { |
| 141 | + headers: { |
| 142 | + Authorization: ` Bearer: ${accessToken}` |
| 143 | + } |
| 144 | + }).then(res => { |
| 145 | + const { data, data: { params } } = res; |
| 146 | + |
| 147 | + assert.strictEqual(data.id, 'dave'); |
| 148 | + assert.deepStrictEqual(params.user, user); |
| 149 | + assert.strictEqual(params.authentication.accessToken, accessToken); |
| 150 | + }); |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + describe('authenticate middleware', () => { |
| 155 | + it('protected endpoint fails when JWT is not present', () => { |
| 156 | + return axios.get('/protected').then(() => { |
| 157 | + assert.fail('Should never get here'); |
| 158 | + }).catch(error => { |
| 159 | + const { data } = error.response; |
| 160 | + |
| 161 | + assert.strictEqual(data.name, 'NotAuthenticated'); |
| 162 | + assert.strictEqual(data.message, 'No valid authentication strategy available'); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it.skip('protected endpoint fails with invalid Authorization header', () => { |
| 167 | + return axios.get('/protected', { |
| 168 | + headers: { |
| 169 | + Authorization: 'Bearer: something wrong' |
| 170 | + } |
| 171 | + }).then(() => { |
| 172 | + assert.fail('Should never get here'); |
| 173 | + }).catch(error => { |
| 174 | + const { data } = error.response; |
| 175 | + |
| 176 | + assert.strictEqual(data.name, 'NotAuthenticated'); |
| 177 | + assert.strictEqual(data.message, 'Not authenticated'); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + it('can request protected endpoint with JWT present', () => { |
| 182 | + return axios.get('/protected', { |
| 183 | + headers: { |
| 184 | + Authorization: `Bearer ${authResult.accessToken}` |
| 185 | + } |
| 186 | + }).then(res => { |
| 187 | + const { data } = res; |
| 188 | + |
| 189 | + assert.strictEqual(data.email, user.email); |
| 190 | + assert.strictEqual(data.id, user.id); |
| 191 | + assert.strictEqual(data.password, undefined, 'Passed provider information'); |
| 192 | + }); |
| 193 | + }); |
| 194 | + }); |
| 195 | +}); |
0 commit comments