Skip to content

Commit

Permalink
chore: use username instead of email for basic
Browse files Browse the repository at this point in the history
use username instead of email for basic authentication
  • Loading branch information
emonddr committed May 3, 2019
1 parent 72b046e commit 7edb7ee
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 () => {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand Down Expand Up @@ -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);
Expand All @@ -258,7 +241,7 @@ describe('Basic Authentication', () => {
.toClass(BasicAuthenticationUserService);

users = getUserRepository();

joeUser = users.list['joe888'];
server.bind(USER_REPO).to(users);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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')
Expand All @@ -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;
}
}

Expand All @@ -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 () => {
Expand All @@ -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')
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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')
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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')
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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)
Expand All @@ -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);
}
}

Expand All @@ -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,
},
Expand Down Expand Up @@ -527,7 +461,7 @@ describe('JWT Authentication', () => {
.toClass(JWTService);

testUsers = getUserRepository();

joeUser = testUsers.list['joe888'];
server.bind(USER_REPO).to(testUsers);
}

Expand Down
Loading

0 comments on commit 7edb7ee

Please sign in to comment.