Skip to content

Commit

Permalink
test: add controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwwinter committed Aug 16, 2024
1 parent 5cbd030 commit d73dced
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
57 changes: 42 additions & 15 deletions src/account/account.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ import { Test, TestingModule } from '@nestjs/testing';
import { AccountController } from './account.controller';
import { HttpService } from '@nestjs/axios';
import { User } from '../auth/user.dto';
import { of } from 'rxjs';
import { of, throwError } from "rxjs";
import { NotFoundException } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { KeycloakService } from './keycloak.service';
import { KeycloakUser } from './keycloak-user.dto';

describe('AccountController', () => {
let controller: AccountController;
const mockHttp = {
put: jest.fn().mockReturnValue(of({ data: undefined })),
get: jest.fn().mockReturnValue(of({ data: undefined })),
post: jest.fn().mockReturnValue(of({ date: undefined })),
delete: jest.fn().mockReturnValue(of({ date: undefined })),
};

let mockHttp;

const user: User = {
sub: 'user-id',
realm: 'ndb-dev',
Expand All @@ -24,6 +21,15 @@ describe('AccountController', () => {
};

beforeEach(async () => {
mockHttp = {
put: jest.fn().mockReturnValue(of({ data: undefined })),
get: jest.fn().mockReturnValue(of({ data: undefined })),
post: jest.fn().mockReturnValue(of({ date: undefined })),
delete: jest.fn().mockReturnValue(of({ date: undefined })),
}

jest.clearAllMocks();

const module: TestingModule = await Test.createTestingModule({
imports: [ConfigModule],
controllers: [AccountController],
Expand All @@ -34,7 +40,6 @@ describe('AccountController', () => {
}).compile();

controller = module.get<AccountController>(AccountController);
jest.clearAllMocks();
});

it('should be defined', () => {
Expand Down Expand Up @@ -70,21 +75,43 @@ describe('AccountController', () => {
});
});

it('should delete a user account', (done) => {
it('should delete return 200 with bodyDeleted true', (done) => {
mockHttp.delete.mockReturnValue(of(""));
const id = 'my-id';

controller
.deleteAccount({ user }, id )
.subscribe(() => {
// delete user
expect(mockHttp.delete).toHaveBeenCalledWith(
expect.stringContaining("/ndb-dev/users/my-id"),
);
done();
.subscribe({
next: (value) => {
expect(value.userDeleted).toBeTruthy()
// delete user
expect(mockHttp.delete).toHaveBeenCalledWith(
expect.stringContaining("/ndb-dev/users/my-id"),
);
done()
}
});
});

it('should delete return 200 with bodyDeleted false', (done) => {
mockHttp.delete.mockReturnValue(throwError(() => new Error()));
const id = 'my-id';

controller
.deleteAccount({ user }, id )
.subscribe({
next: (value) => {
expect(value.userDeleted).toBeFalsy()
// delete user
expect(mockHttp.delete).toHaveBeenCalledWith(
expect.stringContaining("/ndb-dev/users/my-id"),
);
done()
}
})
;
});

it('should return a user with the assigned roles', async () => {
const requestedUser: KeycloakUser = { username: 'my-user', id: 'user-id' };
const roles = ['user-role-1', 'user-role-2'];
Expand Down
20 changes: 17 additions & 3 deletions src/account/account.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ import {
import { BearerGuard } from '../auth/bearer/bearer.guard';
import { ApiBearerAuth, ApiHeader, ApiOperation } from '@nestjs/swagger';
import {
catchError,
concatMap,
concatWith,
firstValueFrom,
last,
Observable,
tap,
} from 'rxjs';
of,
switchMap,
tap
} from "rxjs";
import { ForgotEmailReq } from './forgot-email-req.dto';
import { SetEmailReq } from './set-email-req.dto';
import { User } from '../auth/user.dto';
Expand Down Expand Up @@ -168,7 +171,18 @@ export class AccountController {

return this.keycloak.deleteUser(
user.realm,
userId,
userId
).pipe(
switchMap(() => {
return of({
userDeleted: true
});
}),
catchError(() => {
return of({
userDeleted: false
});
}),
)
}

Expand Down
6 changes: 1 addition & 5 deletions src/account/keycloak.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ export class KeycloakService {
return this.perform(
this.http.delete,
`${realm}/users/${id}`,
).pipe(
catchError(() => {
return of("")
}),
);
)
}

/**
Expand Down

0 comments on commit d73dced

Please sign in to comment.