-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(domain): test users service remaining cases
- Loading branch information
1 parent
6814a21
commit 07297d8
Showing
2 changed files
with
81 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { User } from '@sharingan/database'; | ||
import SharinganError, { errors } from '@sharingan/utils'; | ||
import SharinganError, { errors, generateRandomId } from '@sharingan/utils'; | ||
|
||
import { roleService, userService } from '../../../index'; | ||
import { | ||
|
@@ -55,6 +55,49 @@ describe('Test User service', () => { | |
await deleteTestUsersById([createdUser.id]); | ||
}); | ||
|
||
it('should create a user with no username', async () => { | ||
// GIVEN | ||
const role = await findTestRole('user'); | ||
const createUserDto = await createTestUserDto({ roleId: role.id, username: null }); | ||
|
||
// WHEN | ||
const createdUser = await userService.create(createUserDto); | ||
|
||
// THEN | ||
expect(createdUser).toMatchObject<User>({ | ||
createdAt: expect.any(Date), | ||
email: createUserDto.email, | ||
id: createUserDto.toUser().id, | ||
isEnabled: createUserDto.toUser().isEnabled, | ||
name: createUserDto.toUser().name, | ||
oauthProvider: createUserDto.toUser().oauthProvider, | ||
password: null, | ||
pictureUrl: createUserDto.toUser().pictureUrl, | ||
roleId: createUserDto.toUser().roleId, | ||
timezone: createUserDto.toUser().timezone, | ||
updatedAt: expect.any(Date), | ||
username: expect.any(String), | ||
}); | ||
|
||
await deleteTestUsersById([createdUser.id]); | ||
}); | ||
|
||
it('should create a user with a username that already exists', async () => { | ||
// GIVEN | ||
const role = await findTestRole('user'); | ||
const user = await createTestUser({ username: 'roloto' }); | ||
|
||
const createUserDto = await createTestUserDto({ roleId: role.id, username: 'roloto' }); | ||
|
||
// WHEN | ||
const createdUser = await userService.create(createUserDto); | ||
|
||
// THEN | ||
expect(createdUser.username).not.toEqual('roloto'); | ||
|
||
await deleteTestUsersById([user.id, createdUser.id]); | ||
}); | ||
|
||
it('should create a user - validation check', async () => { | ||
// GIVEN | ||
const role = await findTestRole('user'); | ||
|
@@ -84,6 +127,21 @@ describe('Test User service', () => { | |
await deleteTestUsersById([createdUser.id]); | ||
}); | ||
|
||
it('should fail create a user because the email address already exists', async () => { | ||
// GIVEN | ||
const user = await createTestUser({ email: '[email protected]' }); | ||
const role = await findTestRole('user'); | ||
const createUserDto = await createTestUserDto({ email: '[email protected]', roleId: role.id }); | ||
|
||
// WHEN | ||
// THEN | ||
await expect(async () => { | ||
await userService.create(createUserDto); | ||
}).rejects.toThrow(new SharinganError(errors.EMAIL_ALREADY_TAKEN, 'EMAIL_ALREADY_TAKEN')); | ||
|
||
await deleteTestUsersById([user.id]); | ||
}); | ||
|
||
it('should update user information', async () => { | ||
// GIVEN | ||
const currentUser = await createTestUser({}); | ||
|
@@ -178,4 +236,15 @@ describe('Test User service', () => { | |
|
||
await deleteTestUsersById([user.id]); | ||
}); | ||
|
||
it('should found no user given the ID provided', async () => { | ||
// GIVEN | ||
const snippetId = generateRandomId(); | ||
|
||
// WHEN | ||
const user = await userService.findById(snippetId); | ||
|
||
// THEN | ||
expect(user).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters