Skip to content

Commit 70c3b73

Browse files
committed
refactor: update AuthController to implement magic link login and redirect strategies
1 parent ad6e988 commit 70c3b73

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

server/src/auth/auth.controller.spec.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ import { Request, Response } from 'express';
33

44
import { AuthController } from './auth.controller';
55
import { AuthService } from './auth.service';
6+
import { MagicLinkEmailStrategy } from './strategies/magicLinkEmail.strategy';
67

78
const mockAuthService = {
89
githubLogin: jest.fn(),
910
googleLogin: jest.fn(),
1011
discordLogin: jest.fn(),
1112
verifyToken: jest.fn(),
13+
loginWithEmail: jest.fn(),
14+
};
15+
16+
const mockMagicLinkEmailStrategy = {
17+
send: jest.fn(),
1218
};
1319

1420
describe('AuthController', () => {
@@ -23,6 +29,10 @@ describe('AuthController', () => {
2329
provide: AuthService,
2430
useValue: mockAuthService,
2531
},
32+
{
33+
provide: MagicLinkEmailStrategy,
34+
useValue: mockMagicLinkEmailStrategy,
35+
},
2636
],
2737
}).compile();
2838

@@ -56,6 +66,13 @@ describe('AuthController', () => {
5666
});
5767
});
5868

69+
describe('githubLogin', () => {
70+
it('should call AuthService.githubLogin', async () => {
71+
await controller.githubLogin();
72+
expect(authService.githubLogin).toHaveBeenCalled();
73+
});
74+
});
75+
5976
describe('googleRedirect', () => {
6077
it('should call AuthService.googleLogin', async () => {
6178
const req = {} as Request;
@@ -78,6 +95,13 @@ describe('AuthController', () => {
7895
});
7996
});
8097

98+
describe('googleLogin', () => {
99+
it('should call AuthService.googleLogin', async () => {
100+
await controller.googleLogin();
101+
expect(authService.googleLogin).toHaveBeenCalled();
102+
});
103+
});
104+
81105
describe('discordRedirect', () => {
82106
it('should call AuthService.discordLogin', async () => {
83107
const req = {} as Request;
@@ -100,6 +124,46 @@ describe('AuthController', () => {
100124
});
101125
});
102126

127+
describe('discordLogin', () => {
128+
it('should call AuthService.discordLogin', async () => {
129+
await controller.discordLogin();
130+
expect(authService.discordLogin).toHaveBeenCalled();
131+
});
132+
});
133+
134+
describe('magicLinkRedirect', () => {
135+
it('should call AuthService.loginWithEmail', async () => {
136+
const req = {} as Request;
137+
const res = {} as Response;
138+
139+
await controller.magicLinkRedirect(req, res);
140+
141+
expect(authService.loginWithEmail).toHaveBeenCalledWith(req, res);
142+
});
143+
144+
it('should handle exceptions', async () => {
145+
const req = {} as Request;
146+
const res = {} as Response;
147+
const error = new Error('Test error');
148+
(authService.loginWithEmail as jest.Mock).mockRejectedValueOnce(error);
149+
150+
await expect(controller.magicLinkRedirect(req, res)).rejects.toThrow(
151+
'Test error',
152+
);
153+
});
154+
});
155+
156+
describe('magicLinkLogin', () => {
157+
it('should call AuthService.discordLogin', async () => {
158+
const req = {} as Request;
159+
const res = {} as Response;
160+
161+
await controller.magicLinkLogin(req, res);
162+
163+
expect(mockMagicLinkEmailStrategy.send).toHaveBeenCalledWith(req, res);
164+
});
165+
});
166+
103167
describe('verify', () => {
104168
it('should call AuthService.verifyToken', async () => {
105169
const req = {} as Request;

server/src/auth/auth.controller.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
Body,
32
Controller,
43
Get,
54
Inject,
@@ -11,12 +10,10 @@ import {
1110
} from '@nestjs/common';
1211
import { AuthGuard } from '@nestjs/passport';
1312
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
14-
import { NewEmailUserDto } from '@shared/validation/user/dto/NewEmailUser.dto';
1513
import type { Request, Response } from 'express';
1614

1715
import { AuthService } from './auth.service';
1816
import { MagicLinkEmailStrategy } from './strategies/magicLinkEmail.strategy';
19-
import { Throttle } from '@nestjs/throttler';
2017

2118
@Controller('auth')
2219
@ApiTags('auth')
@@ -51,7 +48,7 @@ export class AuthController {
5148
},
5249
},
5350
})
54-
public async signInWithEmail(@Req() req: Request, @Res() res: Response) {
51+
public async magicLinkLogin(@Req() req: Request, @Res() res: Response) {
5552
return this.magicLinkEmailStrategy.send(req, res);
5653
}
5754

@@ -60,7 +57,7 @@ export class AuthController {
6057
summary: 'Will send the user a email with a single use login link',
6158
})
6259
@UseGuards(AuthGuard('magic-link'))
63-
public async signIn(@Req() req: Request, @Res() res: Response) {
60+
public async magicLinkRedirect(@Req() req: Request, @Res() res: Response) {
6461
return this.authService.loginWithEmail(req, res);
6562
}
6663

0 commit comments

Comments
 (0)