So I am working on a test for password comparison and the salted and hashed password for signing up should match the password for signing in.
The error in terminal is:
FAIL src/users/auth.service.spec.ts AuthService
✓ can create an instance of auth service (17 ms)
✓ creates a new user with a salted and hashed password (58 ms)
✓ throws an error if user signs up with email that is in use (2 ms)
✓ throws if sign in is called with an unused email (2 ms)
✓ throws if sign in is called with an unused email (49 ms)
✕ returns a user if correct password is provided (48 ms)● AuthService › returns a user if correct password is provided
NotFoundException: user not found 41 | const [user] = await this.usersService.find(email); 42 | if (!user) { > 43 | throw new NotFoundException('user not found'); | ^ 44 | } 45 | 46 | const [salt, storedHash] = user.password.split('.'); at AuthService.signin (users/auth.service.ts:43:13) at Object.<anonymous> (users/auth.service.spec.ts:83:18)
I console logged that user that is signing up in memory so the user is there.
describe('AuthService', () => {
let service: AuthService;
let fakeUsersService: Partial<UsersService>;
beforeEach(async () => {
// Create a fake copy of the UsersService
const users: User[] = [];
fakeUsersService = {
find: (email: string) => {
const filteredUsers = users.filter((user) => user.email === email);
return Promise.resolve(filteredUsers);
},
create: (email: string, password: string) => {
const user = {
id: Math.floor(Math.random() * 999999),
email,
password,
} as User;
users.push(user);
return Promise.resolve(user);
},
};
const module = await Test.createTestingModule({
providers: [
AuthService,
{ provide: UsersService, useValue: fakeUsersService },
],
}).compile();
service = module.get(AuthService);
});
it('can create an instance of auth service', async () => {
expect(service).toBeDefined();
});
it('creates a new user with a salted and hashed password', async () => {
const user = await service.signup('[email protected]', 'asdf');
expect(user.password).not.toEqual('asdf');
const [salt, hash] = user.password.split('.');
expect(salt).toBeDefined();
expect(hash).toBeDefined();
});
it('throws an error if user signs up with email that is in use', async () => {
fakeUsersService.find = () =>
Promise.resolve([{ id: 1, email: 'a', password: '1' } as User]);
try {
await service.signup('[email protected]', 'asdf');
} catch (error) {
return error;
}
});
it('throws if sign in is called with an unused email', async () => {
try {
await service.signin('[email protected]', 'sdbadfbd');
} catch (error) {
return error;
}
});
it('throws if sign in is called with an unused email', (done) => {
fakeUsersService.find = () =>
Promise.resolve([
{ email: '[email protected]', password: 'asdfgh' } as User,
]);
service.signin('emailaddress', 'adfgbd').catch(() => done());
});
it('returns a user if correct password is provided', async () => {
const inMemoryUser = await service.signup('[email protected]', 'mypassword');
console.log(inMemoryUser);
const user = await service.signin('[email protected]', 'mypassword');
expect(user).toBeDefined();
});
});