-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract decode user to util
Signed-off-by: jannyHou <[email protected]>
- Loading branch information
Showing
4 changed files
with
81 additions
and
96 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright IBM Corp. 2019. All Rights Reserved. | ||
// Node module: @loopback/example-shopping | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {expect, toJSON} from '@loopback/testlab'; | ||
import {MongoDataSource} from '../../src/datasources'; | ||
import { | ||
decodeAccessToken, | ||
getAccessTokenForUser, | ||
} from '../../src/utils/user.authentication'; | ||
import {UserRepository, OrderRepository} from '../../src/repositories'; | ||
import {User} from '../../src/models'; | ||
import * as _ from 'lodash'; | ||
const SECRET = 'secretforjwt'; | ||
|
||
describe('authentication', () => { | ||
const mongodbDS = new MongoDataSource(); | ||
const orderRepo = new OrderRepository(mongodbDS); | ||
const userRepo = new UserRepository(mongodbDS, orderRepo); | ||
const user = { | ||
email: '[email protected]', | ||
password: 'p4ssw0rd', | ||
firstname: 'unit', | ||
surname: 'test', | ||
}; | ||
let newUser: User; | ||
|
||
before('create user', async () => { | ||
newUser = await userRepo.create(user); | ||
}); | ||
|
||
it('decodes valid access token', async () => { | ||
const token = await getAccessTokenForUser(userRepo, { | ||
email: '[email protected]', | ||
password: 'p4ssw0rd', | ||
}); | ||
const expectedUser = getExpectedUser(newUser); | ||
const currentUser = await decodeAccessToken(token, SECRET); | ||
expect(currentUser).to.deepEqual(expectedUser); | ||
}); | ||
|
||
it('throws error for invalid accesstoken', async () => { | ||
const token = 'fake'; | ||
try { | ||
await decodeAccessToken(token, SECRET); | ||
expect('throws error').to.be.true(); | ||
} catch (err) { | ||
expect(err.message).to.equal('jwt malformed'); | ||
} | ||
}); | ||
}); | ||
|
||
function getExpectedUser(originalUser: User) { | ||
const userProfile: Partial<User> = _.pick(toJSON(originalUser), [ | ||
'id', | ||
'email', | ||
'firstName', | ||
]); | ||
return { | ||
id: userProfile.id, | ||
email: userProfile.email, | ||
name: userProfile.firstname, | ||
}; | ||
} |