Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jannisvisser committed Jan 28, 2025
1 parent d0b528f commit 68ebdb0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
30 changes: 24 additions & 6 deletions services/API-service/src/api/user/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const user: UserEntity = {
lastName: 'Example',
userRole: UserRole.DisasterManager,
countries: [],
disasterTypes: disasters,
disasterTypes: disasters, // NOTE: if this is passed as empty array, a mock for disasterRepository.find() is needed
password: '',
created: new Date(),
hashPassword: function (): void {
Expand All @@ -51,12 +51,30 @@ describe('UserService', () => {
userService = new UserService(new LookupService());
});

describe('generateJWT', () => {
it('should generate a JWT token of type string and starting with the characters "eyJ"', async () => {
const generated = await userService.generateJWT(user);
describe('buildUserRO', () => {
it('should generate an object including a JWT token starting with the characters "eyJ"', async () => {
// Arrange
const includeToken = true;

// Act
const userRO = await userService.buildUserRO(user, includeToken);

// Assert
const expectedFirstCharacters = 'eyJ';
expect(typeof generated).toBe('string');
expect(generated.indexOf(expectedFirstCharacters)).toBe(0);
expect(userRO.user.token).toBeDefined();
expect(userRO.user.token?.indexOf(expectedFirstCharacters)).toBe(0);
});

it('should generate an object without a JWT token when instructed as such', async () => {
// Arrange
const includeToken = false;

// Act
const userRO = await userService.buildUserRO(user, includeToken);

// Assert
expect(userRO.user).toBeDefined();
expect(userRO.user.token).not.toBeDefined();
});
});
});
2 changes: 0 additions & 2 deletions services/API-service/src/api/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ export class UserService {
return this.buildUserRO(updateUser);
}

// UNIT TEST?
public async updateUser(
email: string,
updateUserData: UpdateUserDto,
Expand Down Expand Up @@ -212,7 +211,6 @@ export class UserService {
return result;
}

// UNIT TEST?
public async buildUserRO(
user: UserEntity,
includeToken = true,
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/helpers/API-service/json/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"email": "dunant@redcross.nl",
"firstName": "Henry",
"lastName": "Dunant",
"userRole": "admin"
"userRole": "admin",
"password": "password"
},
{
"email": "uganda@redcross.nl",
Expand Down

0 comments on commit 68ebdb0

Please sign in to comment.