Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/__tests__/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('AppController', () => {
getProfile: jest.fn().mockResolvedValue({}),
updateSettings: jest.fn().mockResolvedValue({}),
updateProfile: jest.fn().mockResolvedValue({}),
getUserFromToken: jest.fn().mockReturnValue(undefined),
},
},
{
Expand Down Expand Up @@ -213,6 +214,25 @@ describe('AppController', () => {
expect(result).toEqual({ success: true });
});

it('should mark an anonymous contact form submission as not logged in', async () => {
await appController.submitContact({ name: 'A', email: 'a@b.com', message: 'hi' }, undefined, { cookies: {} } as any);

expect(mailSendService.sendMail).toHaveBeenCalledWith(
expect.objectContaining({ html: expect.stringContaining('לא מחובר') }),
);
});

it('should add the logged in user to the contact form mail', async () => {
(authService.getUserFromToken as jest.Mock).mockReturnValue({ id: 7, email: 'user@school.com', name: 'School' });

await appController.submitContact({ name: 'A', email: 'a@b.com', message: 'hi' }, undefined, { cookies: { Authentication: 'tok' } } as any);

expect(authService.getUserFromToken).toHaveBeenCalledWith('tok');
expect(mailSendService.sendMail).toHaveBeenCalledWith(
expect.objectContaining({ html: expect.stringContaining('user@school.com, מזהה 7') }),
);
});

it('should reject a contact form submission missing required fields', async () => {
await expect(
appController.submitContact({ name: 'Test User' }, undefined),
Expand Down
6 changes: 4 additions & 2 deletions app/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { AppService } from './app.service';
import { AuthService, UpdateProfileData } from '@shared/auth/auth.service';
import { JwtAuthGuard } from '@shared/auth/jwt-auth.guard';
import { LocalAuthGuard } from '@shared/auth/local-auth.guard';
import { Response } from 'express';
import { Request as ExpressRequest, Response } from 'express';
import { LocalRegisterAuthGuard } from '@shared/auth/local-register-auth.guard';
import { getUserIdFromUser } from '@shared/auth/auth.util';
import { isAdmin } from '@shared/utils/permissionsUtil';
Expand Down Expand Up @@ -123,17 +123,19 @@ export class AppController {

@Post('contact')
@HttpCode(200)
async submitContact(@Body() body: ContactFormBody, @Headers('origin') origin?: string) {
async submitContact(@Body() body: ContactFormBody, @Headers('origin') origin?: string, @Request() req?: ExpressRequest) {
if (!body?.name || !body?.email || !body?.message) {
throw new BadRequestException('נא למלא שם, אימייל והודעה');
}
const loggedInUser = this.authService.getUserFromToken(req?.cookies?.Authentication);
const html = `
<p><b>שם:</b> ${body.name}</p>
<p><b>אימייל:</b> ${body.email}</p>
${body.phone ? `<p><b>טלפון:</b> ${body.phone}</p>` : ''}
<p><b>הודעה:</b></p>
<p>${body.message}</p>
${origin ? `<p><b>מקור:</b> ${origin}</p>` : ''}
<p><b>משתמש מחובר:</b> ${loggedInUser ? `${loggedInUser.name || ''} (${loggedInUser.email}, מזהה ${loggedInUser.id})` : 'לא מחובר'}</p>
`;
await this.mailSendService.sendMail({
to: CONTACT_MAIL_TO,
Expand Down
17 changes: 17 additions & 0 deletions auth/__tests__/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('AuthService', () => {
provide: JwtService,
useValue: {
sign: jest.fn(),
verify: jest.fn(),
},
},
{
Expand Down Expand Up @@ -583,4 +584,20 @@ describe('AuthService', () => {
});
});
});

describe('getUserFromToken', () => {
it('returns undefined without a token', () => {
expect(service.getUserFromToken(undefined)).toBeUndefined();
});

it('returns the verified payload', () => {
(jwtService.verify as jest.Mock).mockReturnValue({ id: 3 });
expect(service.getUserFromToken('tok')).toEqual({ id: 3 });
});

it('returns undefined for an invalid token', () => {
(jwtService.verify as jest.Mock).mockImplementation(() => { throw new Error('bad'); });
expect(service.getUserFromToken('tok')).toBeUndefined();
});
});
});
11 changes: 11 additions & 0 deletions auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ export class AuthService {
});
}

getUserFromToken(token?: string): IAuthenticatedUser | undefined {
if (!token) {
return undefined;
}
try {
return this.jwtService.verify(token);
} catch {
return undefined;
}
}

async getCookieForLogOut(user?: IAuthenticatedUser) {
if (!user?.impersonated) {
return cookie.serialize('Authentication', '', {
Expand Down
Loading