Skip to content

Commit

Permalink
test: google auth test - commented out
Browse files Browse the repository at this point in the history
  • Loading branch information
Khingz committed Jul 28, 2024
1 parent e2f20cf commit 6b732d2
Showing 1 changed file with 84 additions and 84 deletions.
168 changes: 84 additions & 84 deletions src/test/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// });
// });

0 comments on commit 6b732d2

Please sign in to comment.