generated from xgeekshq/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: issue-1593- validate access token on login register (#1595)
- Loading branch information
1 parent
6371b9c
commit 681f14c
Showing
16 changed files
with
494 additions
and
71 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
22 changes: 22 additions & 0 deletions
22
backend/src/libs/test-utils/mocks/factories/azure-user-factory.ts
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import faker from '@faker-js/faker'; | ||
import { buildTestFactory } from './generic-factory.mock'; | ||
import { AzureUserDTO } from 'src/modules/azure/dto/azure-user.dto'; | ||
|
||
const mockUserData = (): AzureUserDTO => { | ||
const mail = faker.internet.email(); | ||
|
||
return { | ||
id: faker.datatype.uuid(), | ||
displayName: faker.name.firstName() + faker.name.lastName(), | ||
mail: mail, | ||
userPrincipalName: mail, | ||
createdDateTime: faker.date.past(5), | ||
accountEnabled: faker.datatype.boolean(), | ||
deletedDateTime: faker.datatype.boolean() ? faker.date.recent(1) : null, | ||
employeeLeaveDateTime: faker.datatype.boolean() ? faker.date.recent(1) : null | ||
}; | ||
}; | ||
|
||
export const AzureUserFactory = buildTestFactory<AzureUserDTO>(() => { | ||
return mockUserData(); | ||
}); |
133 changes: 133 additions & 0 deletions
133
backend/src/modules/azure/applications/register-or-login.azure.use-case.spec.ts
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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { DeepMocked, createMock } from '@golevelup/ts-jest'; | ||
import { UseCase } from 'src/libs/interfaces/use-case.interface'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import LoggedUserDto from 'src/modules/users/dto/logged.user.dto'; | ||
import { registerOrLoginUseCase } from '../azure.providers'; | ||
import { AUTH_AZURE_SERVICE } from '../constants'; | ||
import { AuthAzureServiceInterface } from '../interfaces/services/auth.azure.service.interface'; | ||
import { CREATE_USER_SERVICE, GET_USER_SERVICE } from 'src/modules/users/constants'; | ||
import { GetUserServiceInterface } from 'src/modules/users/interfaces/services/get.user.service.interface'; | ||
import { CreateUserServiceInterface } from 'src/modules/users/interfaces/services/create.user.service.interface'; | ||
import { GET_TOKEN_AUTH_SERVICE, UPDATE_USER_SERVICE } from 'src/modules/auth/constants'; | ||
import { UpdateUserServiceInterface } from 'src/modules/users/interfaces/services/update.user.service.interface'; | ||
import { GetTokenAuthServiceInterface } from 'src/modules/auth/interfaces/services/get-token.auth.service.interface'; | ||
import { STORAGE_SERVICE } from 'src/modules/storage/constants'; | ||
import { StorageServiceInterface } from 'src/modules/storage/interfaces/services/storage.service'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import configService from 'src/libs/test-utils/mocks/configService.mock'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
|
||
describe('RegisterOrLoginUserUseCase', () => { | ||
let registerOrLogin: UseCase<string, LoggedUserDto | null>; | ||
let authAzureServiceMock: DeepMocked<AuthAzureServiceInterface>; | ||
let getUserService: DeepMocked<GetUserServiceInterface>; | ||
let updateUserServiceMock: DeepMocked<UpdateUserServiceInterface>; | ||
let tokenServiceMock: DeepMocked<GetTokenAuthServiceInterface>; | ||
|
||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
registerOrLoginUseCase, | ||
{ | ||
provide: AUTH_AZURE_SERVICE, | ||
useValue: createMock<AuthAzureServiceInterface>() | ||
}, | ||
{ | ||
provide: GET_USER_SERVICE, | ||
useValue: createMock<GetUserServiceInterface>() | ||
}, | ||
{ | ||
provide: CREATE_USER_SERVICE, | ||
useValue: createMock<CreateUserServiceInterface>() | ||
}, | ||
{ | ||
provide: UPDATE_USER_SERVICE, | ||
useValue: createMock<UpdateUserServiceInterface>() | ||
}, | ||
{ | ||
provide: GET_TOKEN_AUTH_SERVICE, | ||
useValue: createMock<GetTokenAuthServiceInterface>() | ||
}, | ||
{ | ||
provide: STORAGE_SERVICE, | ||
useValue: createMock<StorageServiceInterface>() | ||
}, | ||
{ | ||
provide: ConfigService, | ||
useValue: configService | ||
}, | ||
{ | ||
provide: JwtService, | ||
useValue: createMock<JwtService>() | ||
} | ||
] | ||
}).compile(); | ||
|
||
registerOrLogin = module.get(registerOrLoginUseCase.provide); | ||
authAzureServiceMock = module.get(AUTH_AZURE_SERVICE); | ||
getUserService = module.get(GET_USER_SERVICE); | ||
updateUserServiceMock = module.get(UPDATE_USER_SERVICE); | ||
tokenServiceMock = module.get(GET_TOKEN_AUTH_SERVICE); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(registerOrLogin).toBeDefined(); | ||
}); | ||
describe('execute', () => { | ||
it('should return null when validateAccessToken returns false', async () => { | ||
const spy = jest | ||
.spyOn(registerOrLogin, 'validateAccessToken' as any) | ||
.mockResolvedValueOnce(false); | ||
expect(await registerOrLogin.execute('')).toBe(null); | ||
spy.mockRestore(); | ||
}); | ||
it('should restore user when is deleted and signin normally', async () => { | ||
const spy = jest.spyOn(registerOrLogin, 'validateAccessToken' as any).mockResolvedValueOnce({ | ||
unique_name: 'test', | ||
email: '[email protected]', | ||
name: 'test', | ||
given_name: 'test', | ||
family_name: 'test' | ||
}); | ||
authAzureServiceMock.getUserFromAzure.mockResolvedValueOnce({ | ||
accountEnabled: true, | ||
deletedDateTime: null | ||
} as never); | ||
getUserService.getByEmail.mockResolvedValueOnce({ | ||
_id: 'id', | ||
email: '[email protected]', | ||
isDeleted: true | ||
} as never); | ||
tokenServiceMock.getTokens.mockResolvedValueOnce({} as never); | ||
expect(await registerOrLogin.execute('')).toHaveProperty('email', '[email protected]'); | ||
expect(updateUserServiceMock.restoreUser).toHaveBeenCalled(); | ||
spy.mockRestore(); | ||
}); | ||
it('should singIn the user', async () => { | ||
const spy = jest.spyOn(registerOrLogin, 'validateAccessToken' as any).mockResolvedValueOnce({ | ||
unique_name: 'test', | ||
email: '[email protected]', | ||
name: 'test', | ||
given_name: 'test', | ||
family_name: 'test' | ||
}); | ||
authAzureServiceMock.getUserFromAzure.mockResolvedValueOnce({ | ||
accountEnabled: true, | ||
deletedDateTime: null | ||
} as never); | ||
getUserService.getByEmail.mockResolvedValueOnce({ | ||
_id: 'id', | ||
email: '[email protected]', | ||
isDeleted: false | ||
} as never); | ||
tokenServiceMock.getTokens.mockResolvedValueOnce({} as never); | ||
expect(await registerOrLogin.execute('')).toHaveProperty('email', '[email protected]'); | ||
spy.mockRestore(); | ||
}); | ||
}); | ||
}); |
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
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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export type AzureUserDTO = { | ||
id: string; | ||
mail: string; | ||
displayName: string; | ||
userPrincipalName: string; | ||
createdDateTime: Date; | ||
accountEnabled: boolean; | ||
deletedDateTime: Date | null; | ||
employeeLeaveDateTime: Date | null; | ||
}; |
4 changes: 2 additions & 2 deletions
4
backend/src/modules/azure/interfaces/services/auth.azure.service.interface.ts
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,6 +1,6 @@ | ||
import { AzureUserFound } from '../../services/auth.azure.service'; | ||
import { AzureUserDTO } from '../../dto/azure-user.dto'; | ||
|
||
export interface AuthAzureServiceInterface { | ||
getUserFromAzure(email: string): Promise<AzureUserFound | undefined>; | ||
getUserFromAzure(email: string): Promise<AzureUserDTO | undefined>; | ||
fetchUserPhoto(userId: string): Promise<any>; | ||
} |
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
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
Oops, something went wrong.