-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use username instead of email for basic
use username instead of email for basic authentication
- Loading branch information
Showing
8 changed files
with
86 additions
and
196 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,12 +25,14 @@ import {BasicAuthenticationStrategyBindings, USER_REPO} from '../fixtures/keys'; | |
import {MyAuthenticationSequence} from '../fixtures/sequences/authentication.sequence'; | ||
import {BasicAuthenticationUserService} from '../fixtures/services/basic-auth-user-service'; | ||
import {BasicAuthenticationStrategy} from '../fixtures/strategies/basic-strategy'; | ||
import {User} from '../fixtures/users/user'; | ||
import {UserRepository} from '../fixtures/users/user.repository'; | ||
|
||
describe('Basic Authentication', () => { | ||
let app: Application; | ||
let server: RestServer; | ||
let users: UserRepository; | ||
let joeUser: User; | ||
beforeEach(givenAServer); | ||
beforeEach(givenControllerInApp); | ||
beforeEach(givenAuthenticatedSequence); | ||
|
@@ -40,14 +42,8 @@ describe('Basic Authentication', () => { | |
const client = whenIMakeRequestTo(server); | ||
await client | ||
.get('/whoAmI') | ||
.set( | ||
'Authorization', | ||
createBasicAuthorizationHeaderValue( | ||
users.list['[email protected]'].email, | ||
users.list['[email protected]'].password, | ||
), | ||
) | ||
.expect(users.list['[email protected]'].email); | ||
.set('Authorization', createBasicAuthorizationHeaderValue(joeUser)) | ||
.expect(joeUser.id); | ||
}); | ||
|
||
it('returns error for missing Authorization header', async () => { | ||
|
@@ -68,11 +64,7 @@ describe('Basic Authentication', () => { | |
.get('/whoAmI') | ||
.set( | ||
'Authorization', | ||
createBasicAuthorizationHeaderValue( | ||
users.list['[email protected]'].email, | ||
users.list['[email protected]'].password, | ||
{prefix: 'NotB@sic '}, | ||
), | ||
createBasicAuthorizationHeaderValue(joeUser, {prefix: 'NotB@sic '}), | ||
) | ||
.expect({ | ||
error: { | ||
|
@@ -89,10 +81,7 @@ describe('Basic Authentication', () => { | |
.get('/whoAmI') | ||
.set( | ||
'Authorization', | ||
createBasicAuthorizationHeaderValue( | ||
users.list['[email protected]'].email, | ||
users.list['[email protected]'].password, | ||
) + ' someOtherValue', | ||
createBasicAuthorizationHeaderValue(joeUser) + ' someOtherValue', | ||
) | ||
.expect({ | ||
error: { | ||
|
@@ -109,11 +98,7 @@ describe('Basic Authentication', () => { | |
.get('/whoAmI') | ||
.set( | ||
'Authorization', | ||
createBasicAuthorizationHeaderValue( | ||
users.list['[email protected]'].email, | ||
users.list['[email protected]'].password, | ||
{separator: '|'}, | ||
), | ||
createBasicAuthorizationHeaderValue(joeUser, {separator: '|'}), | ||
) | ||
.expect({ | ||
error: { | ||
|
@@ -130,11 +115,9 @@ describe('Basic Authentication', () => { | |
.get('/whoAmI') | ||
.set( | ||
'Authorization', | ||
createBasicAuthorizationHeaderValue( | ||
users.list['[email protected]'].email, | ||
users.list['[email protected]'].password, | ||
{extraSegment: 'extraPart'}, | ||
), | ||
createBasicAuthorizationHeaderValue(joeUser, { | ||
extraSegment: 'extraPart', | ||
}), | ||
) | ||
.expect({ | ||
error: { | ||
|
@@ -238,8 +221,8 @@ describe('Basic Authentication', () => { | |
@inject(AuthenticationBindings.CURRENT_USER) userProfile: UserProfile, | ||
): Promise<string> { | ||
if (!userProfile) return 'userProfile is undefined'; | ||
if (!userProfile.email) return 'userProfile email is undefined'; | ||
return userProfile.email; | ||
if (!userProfile.id) return 'userProfile id is undefined'; | ||
return userProfile.id; | ||
} | ||
} | ||
app.controller(MyController); | ||
|
@@ -258,7 +241,7 @@ describe('Basic Authentication', () => { | |
.toClass(BasicAuthenticationUserService); | ||
|
||
users = getUserRepository(); | ||
|
||
joeUser = users.list['joe888']; | ||
server.bind(USER_REPO).to(users); | ||
} | ||
|
||
|
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 |
---|---|---|
|
@@ -17,19 +17,22 @@ import { | |
} from '../..'; | ||
import { | ||
createBearerAuthorizationHeaderValue, | ||
createUserProfile, | ||
getApp, | ||
getUserRepository, | ||
} from '../fixtures/helper'; | ||
import {JWTAuthenticationStrategyBindings, USER_REPO} from '../fixtures/keys'; | ||
import {MyAuthenticationSequence} from '../fixtures/sequences/authentication.sequence'; | ||
import {JWTService} from '../fixtures/services/jwt-service'; | ||
import {JWTAuthenticationStrategy} from '../fixtures/strategies/jwt-strategy'; | ||
import {User} from '../fixtures/users/user'; | ||
import {UserRepository} from '../fixtures/users/user.repository'; | ||
|
||
describe('JWT Authentication', () => { | ||
let app: Application; | ||
let server: RestServer; | ||
let testUsers: UserRepository; | ||
let joeUser: User; | ||
let token: string; | ||
|
||
beforeEach(givenAServer); | ||
|
@@ -52,15 +55,9 @@ describe('JWT Authentication', () => { | |
// | ||
|
||
// Now with a valid userProfile, let's create a JSON web token | ||
const joeUser = this.users.list['[email protected]']; | ||
|
||
const joeUserProfile = { | ||
id: joeUser.id, | ||
email: joeUser.email, | ||
name: `${joeUser.firstname} ${joeUser.surname}`, | ||
}; | ||
|
||
return await this.tokenService.generateToken(joeUserProfile); | ||
return await this.tokenService.generateToken( | ||
createUserProfile(joeUser), | ||
); | ||
} | ||
|
||
@get('/whoAmI') | ||
|
@@ -69,8 +66,8 @@ describe('JWT Authentication', () => { | |
@inject(AuthenticationBindings.CURRENT_USER) userProfile: UserProfile, | ||
) { | ||
if (!userProfile) return 'userProfile is undefined'; | ||
if (!userProfile.email) return 'userProfile email is undefined'; | ||
return userProfile.email; | ||
if (!userProfile.id) return 'userProfile id is undefined'; | ||
return userProfile.id; | ||
} | ||
} | ||
|
||
|
@@ -83,12 +80,12 @@ describe('JWT Authentication', () => { | |
expect(token).to.be.not.null(); | ||
expect(token).to.be.String(); | ||
|
||
const email = (await whenIMakeRequestTo(server) | ||
const id = (await whenIMakeRequestTo(server) | ||
.get('/whoAmI') | ||
.set('Authorization', createBearerAuthorizationHeaderValue(token)) | ||
.expect(200)).text; | ||
|
||
expect(email).to.equal(testUsers.list['[email protected]'].email); | ||
expect(id).to.equal(joeUser.id); | ||
}); | ||
|
||
it(`returns error for missing Authorization header`, async () => { | ||
|
@@ -107,15 +104,9 @@ describe('JWT Authentication', () => { | |
// | ||
|
||
// Now with a valid userProfile, let's create a JSON web token | ||
const joeUser = this.users.list['[email protected]']; | ||
|
||
const joeUserProfile = { | ||
id: joeUser.id, | ||
email: joeUser.email, | ||
name: `${joeUser.firstname} ${joeUser.surname}`, | ||
}; | ||
|
||
return await this.tokenService.generateToken(joeUserProfile); | ||
return await this.tokenService.generateToken( | ||
createUserProfile(joeUser), | ||
); | ||
} | ||
|
||
@get('/whoAmI') | ||
|
@@ -124,8 +115,8 @@ describe('JWT Authentication', () => { | |
@inject(AuthenticationBindings.CURRENT_USER) userProfile: UserProfile, | ||
) { | ||
if (!userProfile) return 'userProfile is undefined'; | ||
if (!userProfile.email) return 'userProfile email is undefined'; | ||
return userProfile.email; | ||
if (!userProfile.id) return 'userProfile id is undefined'; | ||
return userProfile.id; | ||
} | ||
} | ||
|
||
|
@@ -165,15 +156,9 @@ describe('JWT Authentication', () => { | |
// | ||
|
||
// Now with a valid userProfile, let's create a JSON web token | ||
const joeUser = this.users.list['[email protected]']; | ||
|
||
const joeUserProfile = { | ||
id: joeUser.id, | ||
email: joeUser.email, | ||
name: `${joeUser.firstname} ${joeUser.surname}`, | ||
}; | ||
|
||
return await this.tokenService.generateToken(joeUserProfile); | ||
return await this.tokenService.generateToken( | ||
createUserProfile(joeUser), | ||
); | ||
} | ||
|
||
@get('/whoAmI') | ||
|
@@ -182,8 +167,8 @@ describe('JWT Authentication', () => { | |
@inject(AuthenticationBindings.CURRENT_USER) userProfile: UserProfile, | ||
) { | ||
if (!userProfile) return 'userProfile is undefined'; | ||
if (!userProfile.email) return 'userProfile email is undefined'; | ||
return userProfile.email; | ||
if (!userProfile.id) return 'userProfile id is undefined'; | ||
return userProfile.id; | ||
} | ||
} | ||
|
||
|
@@ -226,16 +211,9 @@ describe('JWT Authentication', () => { | |
// ...Other code for verifying a valid user (e.g. basic or local strategy)... | ||
// | ||
|
||
// Now with a valid userProfile, let's create a JSON web token | ||
const joeUser = this.users.list['[email protected]']; | ||
|
||
const joeUserProfile = { | ||
id: joeUser.id, | ||
email: joeUser.email, | ||
name: `${joeUser.firstname} ${joeUser.surname}`, | ||
}; | ||
|
||
return await this.tokenService.generateToken(joeUserProfile); | ||
return await this.tokenService.generateToken( | ||
createUserProfile(joeUser), | ||
); | ||
} | ||
|
||
@get('/whoAmI') | ||
|
@@ -244,8 +222,8 @@ describe('JWT Authentication', () => { | |
@inject(AuthenticationBindings.CURRENT_USER) userProfile: UserProfile, | ||
) { | ||
if (!userProfile) return 'userProfile is undefined'; | ||
if (!userProfile.email) return 'userProfile email is undefined'; | ||
return userProfile.email; | ||
if (!userProfile.id) return 'userProfile id is undefined'; | ||
return userProfile.id; | ||
} | ||
} | ||
|
||
|
@@ -283,8 +261,8 @@ describe('JWT Authentication', () => { | |
@inject(AuthenticationBindings.CURRENT_USER) userProfile: UserProfile, | ||
) { | ||
if (!userProfile) return 'userProfile is undefined'; | ||
if (!userProfile.email) return 'userProfile email is undefined'; | ||
return userProfile.email; | ||
if (!userProfile.id) return 'userProfile id is undefined'; | ||
return userProfile.id; | ||
} | ||
} | ||
|
||
|
@@ -315,8 +293,8 @@ describe('JWT Authentication', () => { | |
@inject(AuthenticationBindings.CURRENT_USER) userProfile: UserProfile, | ||
) { | ||
if (!userProfile) return 'userProfile is undefined'; | ||
if (!userProfile.email) return 'userProfile email is undefined'; | ||
return userProfile.email; | ||
if (!userProfile.id) return 'userProfile id is undefined'; | ||
return userProfile.id; | ||
} | ||
} | ||
|
||
|
@@ -346,8 +324,8 @@ describe('JWT Authentication', () => { | |
@inject(AuthenticationBindings.CURRENT_USER) userProfile: UserProfile, | ||
) { | ||
if (!userProfile) return 'userProfile is undefined'; | ||
if (!userProfile.email) return 'userProfile email is undefined'; | ||
return userProfile.email; | ||
if (!userProfile.id) return 'userProfile id is undefined'; | ||
return userProfile.id; | ||
} | ||
} | ||
|
||
|
@@ -367,7 +345,7 @@ describe('JWT Authentication', () => { | |
}); | ||
}); | ||
|
||
it('creates a json web token and throws error for missing email', async () => { | ||
it('creates a json web token and throws error for userProfle that is undefined', async () => { | ||
class InfoController { | ||
constructor( | ||
@inject(JWTAuthenticationStrategyBindings.TOKEN_SERVICE) | ||
|
@@ -378,15 +356,7 @@ describe('JWT Authentication', () => { | |
|
||
@get('/createtoken') | ||
async createToken() { | ||
const joeUser = this.users.list['[email protected]']; | ||
|
||
const joeUserProfile = { | ||
id: joeUser.id, | ||
email: undefined, | ||
name: `${joeUser.firstname} ${joeUser.surname}`, | ||
}; | ||
|
||
return await this.tokenService.generateToken(joeUserProfile); | ||
return await this.tokenService.generateToken(undefined); | ||
} | ||
} | ||
|
||
|
@@ -396,43 +366,7 @@ describe('JWT Authentication', () => { | |
.get('/createtoken') | ||
.expect({ | ||
error: { | ||
message: `Error generating token : userProfile 'email' is null`, | ||
name: 'UnauthorizedError', | ||
statusCode: 401, | ||
}, | ||
}); | ||
}); | ||
|
||
it('creates a json web token and throws error for missing name', async () => { | ||
class InfoController { | ||
constructor( | ||
@inject(JWTAuthenticationStrategyBindings.TOKEN_SERVICE) | ||
public tokenService: JWTService, | ||
@inject(USER_REPO) | ||
public users: UserRepository, | ||
) {} | ||
|
||
@get('/createtoken') | ||
async createToken() { | ||
const joeUser = this.users.list['[email protected]']; | ||
|
||
const joeUserProfile = { | ||
id: joeUser.id, | ||
email: joeUser.email, | ||
name: undefined, | ||
}; | ||
|
||
return await this.tokenService.generateToken(joeUserProfile); | ||
} | ||
} | ||
|
||
app.controller(InfoController); | ||
|
||
await whenIMakeRequestTo(server) | ||
.get('/createtoken') | ||
.expect({ | ||
error: { | ||
message: `Error generating token : userProfile 'name' is null`, | ||
message: `Error generating token : userProfile is null`, | ||
name: 'UnauthorizedError', | ||
statusCode: 401, | ||
}, | ||
|
@@ -527,7 +461,7 @@ describe('JWT Authentication', () => { | |
.toClass(JWTService); | ||
|
||
testUsers = getUserRepository(); | ||
|
||
joeUser = testUsers.list['joe888']; | ||
server.bind(USER_REPO).to(testUsers); | ||
} | ||
|
||
|
Oops, something went wrong.