Skip to content

Commit

Permalink
test: increase coverage for auth service
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Apr 27, 2024
1 parent 1990e42 commit a72d72d
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 10 deletions.
1 change: 1 addition & 0 deletions services/liquid-auth-api-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"node": ">=18.0.0 <21.0.0"
},
"jest": {
"testTimeout": 10000,
"extensionsToTreatAsEsm": [
".ts"
],
Expand Down

This file was deleted.

27 changes: 27 additions & 0 deletions services/liquid-auth-api-js/src/auth/auth.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { AuthGuard } from './auth.guard.js';
import { ExecutionContext } from '@nestjs/common';
import sessionFixtures from '../__fixtures__/session.fixtures.json';
describe('AuthGuard', () => {
const mockExecutionContext: Partial<
Record<
jest.FunctionPropertyNames<ExecutionContext>,
jest.MockedFunction<any>
>
> = {
switchToHttp: jest.fn().mockReturnValue({
getRequest: jest
.fn()
.mockReturnValue({ session: sessionFixtures.authorized }),
getResponse: jest.fn().mockReturnThis(),
}),
};
it('should be defined', () => {
expect(new AuthGuard()).toBeDefined();
});
it('should guard a route', () => {
const guard = new AuthGuard();
expect(guard.canActivate(mockExecutionContext as ExecutionContext)).toBe(
true,
);
});
});
130 changes: 130 additions & 0 deletions services/liquid-auth-api-js/src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getModelToken } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { AuthService } from './auth.service.js';
import { User } from './auth.schema.js';
import { Session } from './session.schema.js';

describe('AuthService', () => {
const mockUser = {
wallet: '2SPDE6XLJNXFTOO7OIGNRNKSEDOHJWVD3HBSEAPHONZQ4IQEYOGYTP6LXA',
credentials: [],
};
const mockCredential = {
device: 'Pixel 8 Pro',
publicKey:
'pQECAyYgASFYIB2dcp3wanhReRhgRIpJCUfRSwkCvyE9OdvEL_NlncSJIlggkSIz7h7O5nrAXGJrkCOmeolChSc09eHzniCFLFxaKH0',
credId:
'AYMPi2Rbhcnu2gSHOO1TNvzDJ38iU00rrlCqyH874XCIEoIotRc7eVRFpx0TvsQurg7BAnXy5KnWdKC8LeWs0k0',
prevCounter: 0,
};
const mockSession = {
_id: '60b3b3b3b3b3b3b3b3b3b3b3',
expires: new Date(),
session: JSON.stringify({ wallet: mockUser.wallet }),
};
let service: AuthService;
let userModel;
let mockUserModel;
let mockSessionModel;
beforeEach(async () => {
mockUserModel = jest.fn(() => ({
save: jest.fn().mockResolvedValue(mockUser),
toObject: jest.fn().mockReturnValue({}),
})) as unknown as Model<User>;
mockUserModel.findOne = jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue(mockUser),
});
mockUserModel.findOneAndUpdate = jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue(mockUser),
});
mockSessionModel = jest.fn(() => ({
save: jest.fn().mockResolvedValue(mockSession),
toObject: jest.fn().mockReturnValue({}),
})) as unknown as Model<Session>;
mockSessionModel.findOne = jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue(mockSession),
});
mockSessionModel.findOneAndUpdate = jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue(mockSession),
});
const module: TestingModule = await Test.createTestingModule({
providers: [
{
provide: getModelToken(User.name),
useValue: mockUserModel,
},
{
provide: getModelToken(Session.name),
useValue: mockSessionModel,
},
AuthService,
],
}).compile();
userModel = module.get<Model<User>>(getModelToken(User.name));
service = module.get<AuthService>(AuthService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
it('should initialize a new user', async () => {
userModel.findOne = jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue(null),
});
const user = await service.init(mockUser.wallet);
expect(user).toEqual(mockUser);
});
it('should fail to create invalid user', async () => {
await expect(service.init('&$@#%')).rejects.toThrow(TypeError);
});
it('should search for a user', async () => {
const user = await service.search({ wallet: mockUser.wallet });
expect(user).toEqual(mockUser);
});
it('should update a user', async () => {
const user = await service.update(mockUser as User);
expect(user).toEqual(mockUser);
});
it('should find a credential', async () => {
userModel.findOne = jest.fn().mockReturnValue({
exec: jest
.fn()
.mockResolvedValue({ ...mockUser, credentials: [mockCredential] }),
});
const credential = await service.findCredential(mockCredential.credId);
expect(credential).toEqual(mockCredential);
});
it('should add a credential to a user', async () => {
const user = await service.addCredential(mockUser.wallet, mockCredential);
expect(user).toEqual({ ...mockUser, credentials: [mockCredential] });
});
it('should not add duplicate credentials', async () => {
userModel.findOne = jest.fn().mockReturnValue({
exec: jest
.fn()
.mockResolvedValue({ ...mockUser, credentials: [mockCredential] }),
});
const user = await service.addCredential(mockUser.wallet, mockCredential);
expect(user).toEqual({ ...mockUser, credentials: [mockCredential] });
});
it('should remove a credential from a user', async () => {
const user = await service.removeCredential(
{ ...mockUser, credentials: [mockCredential] } as User,
mockCredential.credId,
);
expect(user).toEqual(mockUser);
});

it('should find a session', async () => {
const session = await service.findSession(mockSession._id);
expect(session).toEqual(mockSession);
});
it('should update a session', async () => {
const session = await service.updateSessionWallet(
mockSession,
mockUser.wallet,
);
expect(session).toEqual(mockSession);
});
});
4 changes: 0 additions & 4 deletions services/liquid-auth-api-js/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ export class AuthService {
return this.update(user);
}

async all() {
return this.userModel.find({}).exec();
}

/**
* Find a Session by ID
*
Expand Down

0 comments on commit a72d72d

Please sign in to comment.