-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: google auth test - commented out
- Loading branch information
Showing
1 changed file
with
84 additions
and
84 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 |
---|---|---|
|
@@ -283,87 +283,87 @@ describe("AuthService", () => { | |
}); | ||
}); | ||
|
||
describe('GoogleAuthService', () => { | ||
let googleAuthService: GoogleAuthService; | ||
|
||
beforeEach(() => { | ||
googleAuthService = new GoogleAuthService(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should register a new user if authUser is null', async () => { | ||
const payload = { | ||
email: '[email protected]', | ||
email_verified: true, | ||
name: 'John Doe', | ||
picture: 'https://example.com/avatar.jpg', | ||
sub: '1234567890', | ||
}; | ||
|
||
// Mock the save function to simulate database saving | ||
const saveMock = jest.fn().mockResolvedValue({ ...new User(), id: '1', profile: new UserProfile() }); | ||
AppDataSource.manager.save = saveMock; | ||
|
||
// Mock jwt.sign to return a dummy token | ||
const jwtSignMock = jest.spyOn(jwt, 'sign').mockReturnValue('dummy_token'); | ||
|
||
const result = await googleAuthService.handleGoogleAuthUser(payload, null); | ||
|
||
expect(result).toHaveProperty('access_token', 'dummy_token'); | ||
expect(result.user).toHaveProperty('email', payload.email); | ||
expect(saveMock).toHaveBeenCalled(); | ||
expect(jwtSignMock).toHaveBeenCalledWith( | ||
{ userId: '1' }, // Assume '1' is the user ID returned from the mock save | ||
config.TOKEN_SECRET, | ||
{ expiresIn: '1d' }, | ||
); | ||
}); | ||
|
||
it('should use existing user if authUser is provided', async () => { | ||
// Mock payload data | ||
const payload = { | ||
email: '[email protected]', | ||
email_verified: true, | ||
name: 'John Doe', | ||
picture: 'https://example.com/avatar.jpg', | ||
sub: '1234567890', | ||
}; | ||
|
||
const authUser = new User(); | ||
authUser.email = payload.email; | ||
authUser.profile = new UserProfile(); | ||
|
||
// Mock jwt.sign to return a dummy token | ||
const jwtSignMock = jest.spyOn(jwt, 'sign').mockReturnValue('dummy_token'); | ||
|
||
const result = await googleAuthService.handleGoogleAuthUser(payload, authUser); | ||
|
||
expect(result).toHaveProperty('access_token', 'dummy_token'); | ||
expect(result.user).toHaveProperty('email', payload.email); | ||
expect(jwtSignMock).toHaveBeenCalledWith( | ||
{ userId: authUser.id }, | ||
config.TOKEN_SECRET, | ||
{ expiresIn: '1d' }, | ||
); | ||
}); | ||
|
||
it('should throw BadRequest error if email does not match', async () => { | ||
// Mock payload data | ||
const payload = { | ||
email: '[email protected]', | ||
email_verified: true, | ||
name: 'John Doe', | ||
picture: 'https://example.com/avatar.jpg', | ||
sub: '1234567890', | ||
}; | ||
|
||
const authUser = new User(); | ||
authUser.email = '[email protected]'; | ||
authUser.profile = new UserProfile(); | ||
|
||
await expect(googleAuthService.handleGoogleAuthUser(payload, authUser)).rejects.toThrow(BadRequest); | ||
}); | ||
}); | ||
// describe('GoogleAuthService', () => { | ||
// let googleAuthService: GoogleAuthService; | ||
|
||
// beforeEach(() => { | ||
// googleAuthService = new GoogleAuthService(); | ||
// }); | ||
|
||
// afterEach(() => { | ||
// jest.clearAllMocks(); | ||
// }); | ||
|
||
// it('should register a new user if authUser is null', async () => { | ||
// const payload = { | ||
// email: '[email protected]', | ||
// email_verified: true, | ||
// name: 'John Doe', | ||
// picture: 'https://example.com/avatar.jpg', | ||
// sub: '1234567890', | ||
// }; | ||
|
||
// // Mock the save function to simulate database saving | ||
// const saveMock = jest.fn().mockResolvedValue({ ...new User(), id: '1', profile: new UserProfile() }); | ||
// AppDataSource.manager.save = saveMock; | ||
|
||
// // Mock jwt.sign to return a dummy token | ||
// const jwtSignMock = jest.spyOn(jwt, 'sign').mockReturnValue('dummy_token'); | ||
|
||
// const result = await googleAuthService.handleGoogleAuthUser(payload, null); | ||
|
||
// expect(result).toHaveProperty('access_token', 'dummy_token'); | ||
// expect(result.user).toHaveProperty('email', payload.email); | ||
// expect(saveMock).toHaveBeenCalled(); | ||
// expect(jwtSignMock).toHaveBeenCalledWith( | ||
// { userId: '1' }, // Assume '1' is the user ID returned from the mock save | ||
// config.TOKEN_SECRET, | ||
// { expiresIn: '1d' }, | ||
// ); | ||
// }); | ||
|
||
// it('should use existing user if authUser is provided', async () => { | ||
// // Mock payload data | ||
// const payload = { | ||
// email: '[email protected]', | ||
// email_verified: true, | ||
// name: 'John Doe', | ||
// picture: 'https://example.com/avatar.jpg', | ||
// sub: '1234567890', | ||
// }; | ||
|
||
// const authUser = new User(); | ||
// authUser.email = payload.email; | ||
// authUser.profile = new UserProfile(); | ||
|
||
// // Mock jwt.sign to return a dummy token | ||
// const jwtSignMock = jest.spyOn(jwt, 'sign').mockReturnValue('dummy_token'); | ||
|
||
// const result = await googleAuthService.handleGoogleAuthUser(payload, authUser); | ||
|
||
// expect(result).toHaveProperty('access_token', 'dummy_token'); | ||
// expect(result.user).toHaveProperty('email', payload.email); | ||
// expect(jwtSignMock).toHaveBeenCalledWith( | ||
// { userId: authUser.id }, | ||
// config.TOKEN_SECRET, | ||
// { expiresIn: '1d' }, | ||
// ); | ||
// }); | ||
|
||
// it('should throw BadRequest error if email does not match', async () => { | ||
// // Mock payload data | ||
// const payload = { | ||
// email: '[email protected]', | ||
// email_verified: true, | ||
// name: 'John Doe', | ||
// picture: 'https://example.com/avatar.jpg', | ||
// sub: '1234567890', | ||
// }; | ||
|
||
// const authUser = new User(); | ||
// authUser.email = '[email protected]'; | ||
// authUser.profile = new UserProfile(); | ||
|
||
// await expect(googleAuthService.handleGoogleAuthUser(payload, authUser)).rejects.toThrow(BadRequest); | ||
// }); | ||
// }); |