-
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.
Signed-off-by: jannyHou <[email protected]>
- Loading branch information
Showing
8 changed files
with
116 additions
and
43 deletions.
There are no files selected for viewing
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
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
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,18 @@ | ||
// Copyright IBM Corp. 2018, 2019. All Rights Reserved. | ||
// Node module: @loopback4-example-shopping | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {ShoppingApplication} from '../..'; | ||
import {givenHttpServerConfig} from '@loopback/testlab'; | ||
|
||
export async function setupApplication(): Promise<ShoppingApplication> { | ||
const app = new ShoppingApplication({ | ||
rest: givenHttpServerConfig(), | ||
}); | ||
|
||
await app.boot(); | ||
await app.start(); | ||
|
||
return app; | ||
} |
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 |
---|---|---|
|
@@ -5,36 +5,44 @@ | |
|
||
import {expect, toJSON} from '@loopback/testlab'; | ||
import {MongoDataSource} from '../../src/datasources'; | ||
import { | ||
JWT_SECRET, | ||
JWTAuthenticationService, | ||
} from '../../src/services/JWT.authentication.service'; | ||
import {hashPassword} from '../../src/services/hash.password.bcryptjs'; | ||
import {JWTAuthenticationService} from '../../src/services/JWT.authentication.service'; | ||
import {ShoppingApplication} from '../..'; | ||
import {PasswordHasher} from '../../src/services/hash.password.bcryptjs'; | ||
import {UserRepository, OrderRepository} from '../../src/repositories'; | ||
import {User} from '../../src/models'; | ||
import * as _ from 'lodash'; | ||
import {JsonWebTokenError} from 'jsonwebtoken'; | ||
import {HttpErrors} from '@loopback/rest'; | ||
import { | ||
PasswordHasherBindings, | ||
JWTAuthenticationBindings, | ||
} from '../../src/keys'; | ||
import {setupApplication} from './helper'; | ||
|
||
describe('authentication services', () => { | ||
let app: ShoppingApplication; | ||
|
||
describe('authentication utilities', () => { | ||
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; | ||
let jwt_service: JWTAuthenticationService; | ||
let jwtService: JWTAuthenticationService; | ||
let bcryptHasher: PasswordHasher; | ||
|
||
before(setupApp); | ||
before(clearDatabase); | ||
before(createUser); | ||
before(createService); | ||
|
||
it('getAccessTokenForUser creates valid jwt access token', async () => { | ||
const token = await jwt_service.getAccessTokenForUser({ | ||
const token = await jwtService.getAccessTokenForUser({ | ||
email: '[email protected]', | ||
password: 'p4ssw0rd', | ||
}); | ||
|
@@ -46,7 +54,7 @@ describe('authentication utilities', () => { | |
`User with email [email protected] not found.`, | ||
); | ||
return expect( | ||
jwt_service.getAccessTokenForUser({ | ||
jwtService.getAccessTokenForUser({ | ||
email: '[email protected]', | ||
password: 'fake', | ||
}), | ||
|
@@ -58,40 +66,48 @@ describe('authentication utilities', () => { | |
'The credentials are not correct.', | ||
); | ||
return expect( | ||
jwt_service.getAccessTokenForUser({ | ||
jwtService.getAccessTokenForUser({ | ||
email: '[email protected]', | ||
password: 'fake', | ||
}), | ||
).to.be.rejectedWith(expectedError); | ||
}); | ||
|
||
it('decodeAccessToken decodes valid access token', async () => { | ||
const token = await jwt_service.getAccessTokenForUser({ | ||
const token = await jwtService.getAccessTokenForUser({ | ||
email: '[email protected]', | ||
password: 'p4ssw0rd', | ||
}); | ||
const expectedUser = getExpectedUser(newUser); | ||
const currentUser = await jwt_service.decodeAccessToken(token); | ||
const currentUser = await jwtService.decodeAccessToken(token); | ||
expect(currentUser).to.deepEqual(expectedUser); | ||
}); | ||
|
||
it('decodeAccessToken throws error for invalid accesstoken', async () => { | ||
const token = 'fake'; | ||
const error = new JsonWebTokenError('jwt malformed'); | ||
return expect(jwt_service.decodeAccessToken(token)).to.be.rejectedWith( | ||
return expect(jwtService.decodeAccessToken(token)).to.be.rejectedWith( | ||
error, | ||
); | ||
}); | ||
|
||
async function setupApp() { | ||
app = await setupApplication(); | ||
app.bind(PasswordHasherBindings.ROUNDS).to(4); | ||
} | ||
|
||
async function createUser() { | ||
user.password = await hashPassword(user.password, 4); | ||
bcryptHasher = await app.get(PasswordHasherBindings.PASSWORD_HASHER); | ||
user.password = await bcryptHasher.hashPassword(user.password); | ||
newUser = await userRepo.create(user); | ||
} | ||
|
||
async function clearDatabase() { | ||
await userRepo.deleteAll(); | ||
} | ||
|
||
async function createService() { | ||
jwt_service = new JWTAuthenticationService(userRepo, JWT_SECRET); | ||
jwtService = await app.get(JWTAuthenticationBindings.SERVICE); | ||
} | ||
}); | ||
|
||
|