Skip to content

Commit

Permalink
fix: increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakrkris committed Apr 15, 2020
1 parent 02cf3b4 commit d501f45
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import {Client, supertest, expect} from '@loopback/testlab';
import {MockTestOauth2SocialApp} from '@loopback/authentication-passport';
import {ExpressServer} from '../../server';
import {User} from '../../models';
import {User, UserIdentity} from '../../models';
import {startApplication} from '../../';
import * as url from 'url';
import qs from 'qs';
Expand Down Expand Up @@ -65,7 +65,6 @@ describe('example-passport-login acceptance test', () => {

describe('User login scenarios', () => {
let Cookie: string;
let createdUser: User;

/**
***************************************
Expand Down Expand Up @@ -148,7 +147,20 @@ describe('example-passport-login acceptance test', () => {
const users = response.body as User[];
expect(users.length).to.eql(1);
expect(users[0].email).to.eql('[email protected]');
createdUser = users[0];
});

it('able to invoke api endpoints with basic auth', async () => {
await supertest('')
.get('http://localhost:3000/api/profiles')
.auth('[email protected]', 'password', {type: 'basic'})
.expect(204);
});

it('basic auth fails for incorrect password', async () => {
await supertest('')
.get('http://localhost:3000/api/profiles')
.auth('[email protected]', 'incorrect-password', {type: 'basic'})
.expect(401);
});
});

Expand Down Expand Up @@ -256,13 +268,11 @@ describe('example-passport-login acceptance test', () => {
});

it('check if profile is linked to existing user', async () => {
const filter = 'filter={"include":[{"relation": "profiles"}]}';
const response = await supertest('')
.get('http://localhost:3000/api/users/' + createdUser.id)
.query(filter);
const user = response.body as User;
expect(user.profiles?.length).to.eql(1);
const profiles = user.profiles ?? [];
.get('http://localhost:3000/api/profiles')
.auth('[email protected]', 'password', {type: 'basic'});
const profiles = response.body as UserIdentity[];
expect(profiles?.length).to.eql(1);
expect(profiles[0].profile).to.eql({
emails: [{value: '[email protected]'}],
});
Expand Down
26 changes: 0 additions & 26 deletions examples/passport-login/src/authentication-strategies/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,6 @@ export class GoogleOauth2Authorization implements AuthenticationStrategy {
);
}

/**
* verify function for the oauth2 strategy
*
* @param accessToken
* @param refreshToken
* @param profile
* @param done
*/
verify(
accessToken: string,
refreshToken: string,
profile: Profile,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
done: (error: any, user?: any, info?: any) => void,
) {
// look up a linked user for the profile
this.userService
.findOrCreateUser(profile)
.then((user: User) => {
done(null, user);
})
.catch((err: Error) => {
done(err);
});
}

/**
* authenticate a request
* @param request
Expand Down
26 changes: 0 additions & 26 deletions examples/passport-login/src/authentication-strategies/oauth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,6 @@ export class Oauth2AuthStrategy implements AuthenticationStrategy {
);
}

/**
* verify function for the oauth2 strategy
*
* @param accessToken
* @param refreshToken
* @param profile
* @param done
*/
verify(
accessToken: string,
refreshToken: string,
profile: Profile,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
done: (error: any, user?: any, info?: any) => void,
) {
// look up a linked user for the profile
this.userService
.findOrCreateUser(profile)
.then((user: User) => {
done(null, user);
})
.catch((err: Error) => {
done(err);
});
}

/**
* authenticate a request
* @param request
Expand Down
21 changes: 20 additions & 1 deletion examples/passport-login/src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import {
Response,
RestBindings,
RequestWithSession,
get,
} from '@loopback/rest';
import {UserRepository} from '../repositories';
import {repository} from '@loopback/repository';
import {SecurityBindings, UserProfile} from '@loopback/security';
import {SecurityBindings, UserProfile, securityId} from '@loopback/security';
import {authenticate} from '@loopback/authentication';
import {UserCredentialsRepository} from '../repositories/user-credentials.repository';
import {UserIdentityRepository} from '../repositories/user-identity.repository';
Expand Down Expand Up @@ -130,4 +131,22 @@ export class UserLoginController {
await this.userIdentityRepository.deleteAll();
await this.userRepository.deleteAll();
}

@authenticate('basic')
@get('/profiles')
async getExternalProfiles(
@inject(SecurityBindings.USER) profile: UserProfile,
) {
const user = await this.userRepository.findById(
parseInt(profile[securityId]),
{
include: [
{
relation: 'profiles',
},
],
},
);
return user.profiles;
}
}

0 comments on commit d501f45

Please sign in to comment.