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

fix(avatar): get avatar returns 401 unauthorized before #297

Merged
merged 1 commit into from
Sep 24, 2024
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
10 changes: 3 additions & 7 deletions src/avatars/avatars.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,21 @@ import {
Res,
StreamableFile,
UploadedFile,
UseFilters,
UseInterceptors,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { AvatarType } from '@prisma/client';
import { Response } from 'express';
import * as fs from 'fs';
import { BaseErrorExceptionFilter } from '../common/error/error-filter';
import { AuthToken, Guard, ResourceId } from '../auth/guard.decorator';
import { getFileHash, getFileMimeType } from '../common/helper/file.helper';
import { TokenValidateInterceptor } from '../common/interceptor/token-validate.interceptor';
import { UploadAvatarResponseDto } from './DTO/upload-avatar.dto';
import {
CorrespondentFileNotExistError,
InvalidAvatarTypeError,
} from './avatars.error';
import { AvatarsService } from './avatars.service';
import { AuthToken, Guard, ResourceId } from '../auth/guard.decorator';
import { NoAuth } from '../common/interceptor/token-validate.interceptor';

@Controller('/avatars')
export class AvatarsController {
Expand Down Expand Up @@ -84,7 +80,7 @@ export class AvatarsController {
}

@Get('/:id')
@Guard('query', 'avatar')
@NoAuth()
async getAvatar(
@Headers('If-None-Match') ifNoneMatch: string,
@Param('id', ParseIntPipe) @ResourceId() id: number,
Expand Down
2 changes: 1 addition & 1 deletion src/users/role-permission.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class RolePermissionService {
},
},
{
authorizedActions: ['create', 'query', 'query-default', 'enumerate'],
authorizedActions: ['create', 'query-default', 'enumerate'],
authorizedResource: {
ownedByUser: undefined,
types: ['avatar'],
Expand Down
14 changes: 7 additions & 7 deletions test/avatars.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ describe('Avatar Module', () => {
expect(respond.body.message).toMatch(/^AvatarNotFoundError: /);
expect(respond.status).toBe(404);
});
it('should return AuthenticationRequiredError', async () => {
const respond = await request(app.getHttpServer())
.get(`/avatars/${AvatarId}`)
.send();
expect(respond.body.message).toMatch(/^AuthenticationRequiredError: /);
expect(respond.status).toBe(401);
});
// it('should return AuthenticationRequiredError', async () => {
// const respond = await request(app.getHttpServer())
// .get(`/avatars/${AvatarId}`)
// .send();
// expect(respond.body.message).toMatch(/^AuthenticationRequiredError: /);
// expect(respond.status).toBe(401);
// });
Comment on lines +114 to +120
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

Update the test to reflect the new unauthorized status code

The recent changes modify the unauthorized response from 401 to 501. Instead of removing the test case, it should be updated to verify the new status code.

  • Action Items:
    1. Update the expected status code in the test from 401 to 501.
    2. Modify the error message match to align with the new AuthenticationRequiredError structure if it has changed.
    3. Add a comment explaining the reason for updating the test to maintain clarity.
Analysis chain

Clarify the reason for commenting out this test case

The removal of this test case raises some concerns:

  1. It reduces test coverage for unauthenticated requests to retrieve avatars.
  2. There's no explanation for why this test was commented out.
  3. The PR objective mentions fixing a 501 unauthorized error, but this test was checking for a 401 error.

Could you please provide more context on:

  1. Why was this test case commented out?
  2. How does this change relate to the PR objective of fixing a 501 unauthorized error?
  3. Has there been a change in the API's behavior that this test no longer reflects?

To ensure we're not missing any recent changes related to this test case, let's check for any modifications in the avatar retrieval logic:

If this test case is no longer valid, consider adding a comment explaining why it was removed, or update the test to reflect the current expected behavior. If it's a temporary change, use a TODO comment to indicate when and how it should be re-enabled.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for recent changes in avatar-related files

# Test: Search for files related to avatar retrieval
echo "Files related to avatar retrieval:"
fd -e ts avatar
echo ""

# Test: Check for recent changes in these files
echo "Recent changes in avatar-related files:"
git log -n 5 --oneline -- $(fd -e ts avatar)

Length of output: 916

});
describe('get default avatar', () => {
it('should get default avatar', async () => {
Expand Down