Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(http): rename statusCode to status_code and prevent email exposure in response #1358

Merged
merged 1 commit into from
Mar 2, 2025
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
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { initializeDataSource } from '@database/data-source';
import { SeedingService } from '@database/seeding/seeding.service';
import { ResponseInterceptor } from '@shared/inteceptors/response.interceptor';
import { Request, Response } from 'express';
import { HttpExceptionFilter } from '@shared/helpers/http-exception-filter';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule, { bufferLogs: true });

Expand All @@ -34,6 +35,7 @@ async function bootstrap() {
app.enableCors();
app.setGlobalPrefix('api/v1', { exclude: ['/', 'health', 'api', 'api/v1', 'api/docs', 'probe'] });
app.useGlobalInterceptors(new ResponseInterceptor());
app.useGlobalFilters(new HttpExceptionFilter());

const options = new DocumentBuilder()
.setTitle('HNG Boilerplate')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ export class NewsletterSubscriptionService {
const subscription = await this.newsletterSubscriptionRepository.findOne({ where: { email } });

if (!subscription) {
throw new NotFoundException(`Email ${email} not found in the subscription list`);
throw new NotFoundException('Email not found in the subscription list');
}

subscription.isUnsubscribed = true;
await this.newsletterSubscriptionRepository.save(subscription);

return { message: `Email ${email} has been unsubscribed successfully` };
return { message: 'Email has been unsubscribed successfully' };
}
async resubscribe(dto: ResubscribeNewsletterDto): Promise<{ message: string }> {
const { id, email } = dto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ describe('NewsletterService', () => {
const email = '[email protected]';
const mockSubscription = { id: '1', email, isUnsubscribed: false } as NewsletterSubscription;

// Mock `findOne` to return a subscribed user
jest.spyOn(repository, 'findOne').mockResolvedValue(mockSubscription);
// Mock `save` to return updated object
jest.spyOn(repository, 'save').mockImplementation(
async sub =>
({
Expand All @@ -169,21 +167,18 @@ describe('NewsletterService', () => {
);

const result = await service.unsubscribe(email);
expect(result).toEqual({ message: `Email ${email} has been unsubscribed successfully` });
expect(result).toEqual({ message: 'Email has been unsubscribed successfully' });

// Ensure `isUnsubscribed` was updated
expect(repository.save).toHaveBeenCalledWith(expect.objectContaining({ isUnsubscribed: true }));
});

it('should throw NotFoundException if email is not found', async () => {
const email = '[email protected]';

// Mock `findOne` to return null
jest.spyOn(repository, 'findOne').mockResolvedValue(null);

// Expecting an error
await expect(service.unsubscribe(email)).rejects.toThrow(
new NotFoundException(`Email ${email} not found in the subscription list`)
new NotFoundException('Email not found in the subscription list')
);

expect(repository.findOne).toHaveBeenCalledWith({ where: { email } });
Expand Down
2 changes: 1 addition & 1 deletion src/shared/helpers/http-exception-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class HttpExceptionFilter implements ExceptionFilter {
const error = typeof exceptionResponse === 'string' ? '' : (exceptionResponse as ExceptionResponse).error;

response.status(status).json({
status,
status_code: status,
error: error,
message: errorMessage,
});
Expand Down