diff --git a/src/common/adapters/artists.adapter.spec.ts b/src/common/adapters/artists.adapter.spec.ts index 7804dab2..f19bdbac 100644 --- a/src/common/adapters/artists.adapter.spec.ts +++ b/src/common/adapters/artists.adapter.spec.ts @@ -1,5 +1,3 @@ -import { describe, test, expect } from 'vitest' - import { spotifyArtistMock, artistMock, diff --git a/src/common/adapters/audio-features.adapter.spec.ts b/src/common/adapters/audio-features.adapter.spec.ts index 00cf0d2b..54583a3d 100644 --- a/src/common/adapters/audio-features.adapter.spec.ts +++ b/src/common/adapters/audio-features.adapter.spec.ts @@ -1,5 +1,3 @@ -import { expect, test, describe } from 'vitest' - import { spotifyAudioFeaturesMock, audioFeaturesMock } from '../mocks' import { adaptAudioFeatures } from './audio-features.adapter' diff --git a/src/common/adapters/devices.adapter.spec.ts b/src/common/adapters/devices.adapter.spec.ts index 606e381d..d9c6188b 100644 --- a/src/common/adapters/devices.adapter.spec.ts +++ b/src/common/adapters/devices.adapter.spec.ts @@ -1,5 +1,3 @@ -import { expect, test, describe } from 'vitest' - import { spotifyDevicesMock, devicesMock } from '../mocks' import { adaptDevices } from './devices.adapter' diff --git a/src/common/adapters/genres.adapter.spec.ts b/src/common/adapters/genres.adapter.spec.ts index b5914638..b7982f9c 100644 --- a/src/common/adapters/genres.adapter.spec.ts +++ b/src/common/adapters/genres.adapter.spec.ts @@ -1,5 +1,3 @@ -import { test, describe, expect } from 'vitest' - import { spotifyArtistsMock, topGenresMock } from '../mocks' import { adaptGenres } from './genres.adapter' diff --git a/src/common/adapters/paginated.adapter.spec.ts b/src/common/adapters/paginated.adapter.spec.ts index 770db085..9db1b4fc 100644 --- a/src/common/adapters/paginated.adapter.spec.ts +++ b/src/common/adapters/paginated.adapter.spec.ts @@ -1,5 +1,3 @@ -import { describe, test, expect } from 'vitest' - import { artistsMock, spotifyArtistsMock, diff --git a/src/common/adapters/playback-state.adapter.spec.ts b/src/common/adapters/playback-state.adapter.spec.ts index cabbd8fe..ab82e076 100644 --- a/src/common/adapters/playback-state.adapter.spec.ts +++ b/src/common/adapters/playback-state.adapter.spec.ts @@ -1,5 +1,3 @@ -import { test, describe, expect } from 'vitest' - import { spotifyPlaybackStateMock, playbackStateMock } from '../mocks' import { adaptPlaybackState } from './playback-state.adapter' diff --git a/src/common/adapters/profile.adapter.spec.ts b/src/common/adapters/profile.adapter.spec.ts index 2e3ff96a..d1ffea69 100644 --- a/src/common/adapters/profile.adapter.spec.ts +++ b/src/common/adapters/profile.adapter.spec.ts @@ -1,5 +1,3 @@ -import { test, describe, expect } from 'vitest' - import { spotifyProfileMock, profileMock } from '../mocks' import { adaptProfile } from './profile.adapter' diff --git a/src/common/adapters/secret-data.adapter.spec.ts b/src/common/adapters/secret-data.adapter.spec.ts index 4804f3d4..2f594c12 100644 --- a/src/common/adapters/secret-data.adapter.spec.ts +++ b/src/common/adapters/secret-data.adapter.spec.ts @@ -1,5 +1,3 @@ -import { test, describe, expect } from 'vitest' - import { SpotifyToken } from '../types/spotify' import { adaptSecretData } from './secret-data.adapter' diff --git a/src/common/adapters/tracks.adapter.spec.ts b/src/common/adapters/tracks.adapter.spec.ts index 1f2785ff..d07df167 100644 --- a/src/common/adapters/tracks.adapter.spec.ts +++ b/src/common/adapters/tracks.adapter.spec.ts @@ -1,5 +1,3 @@ -import { test, describe, expect } from 'vitest' - import { spotifyTracksMock, tracksMock, diff --git a/src/common/mocks/axios-response.factory.mock.ts b/src/common/mocks/axios-response.factory.mock.ts index e3083ab4..e7c5e090 100644 --- a/src/common/mocks/axios-response.factory.mock.ts +++ b/src/common/mocks/axios-response.factory.mock.ts @@ -1,10 +1,11 @@ import { AxiosResponse, InternalAxiosRequestConfig } from 'axios' export const axiosResponseMockFactory = ( - data: TData + data: TData, + status = 200 ): AxiosResponse => ({ data, - status: 200, + status, statusText: 'OK', headers: {}, config: {} as InternalAxiosRequestConfig, diff --git a/src/common/utils/apply-authorization-header.util.spec.ts b/src/common/utils/apply-authorization-header.util.spec.ts index e5da09c8..72ab415e 100644 --- a/src/common/utils/apply-authorization-header.util.spec.ts +++ b/src/common/utils/apply-authorization-header.util.spec.ts @@ -1,5 +1,3 @@ -import { test, describe, expect } from 'vitest' - import { applyAuthorizationHeader } from './apply-authorization-header.util' describe('applyAuthorizationHeader', () => { diff --git a/src/common/utils/catch-spotify-error.spec.ts b/src/common/utils/catch-spotify-error.spec.ts index 8f25b0b7..8dec4b78 100644 --- a/src/common/utils/catch-spotify-error.spec.ts +++ b/src/common/utils/catch-spotify-error.spec.ts @@ -1,52 +1,77 @@ -import { test, describe, expect } from 'vitest' +import { BadGatewayException, UnauthorizedException } from '@nestjs/common' + import { - InternalServerErrorException, - UnauthorizedException, -} from '@nestjs/common' + SPOTIFY_DEFAULT_ERROR_MESSAGE, + SpotifyAuthError, + catchSpotifyError, +} from './catch-spotify-error' -import { catchSpotifyError } from './catch-spotify-error' +import { axiosResponseMockFactory } from '@common/mocks' describe('catchSpotifyError', () => { test('should throw UnauthorizedException', () => { + const message = 'Unauthorized' + expect(() => - catchSpotifyError({ - response: { - data: { + catchSpotifyError( + axiosResponseMockFactory( + { error: { - message: 'Unauthorized', + message, + status: 401, }, }, - status: 401, - }, - }) - ).toThrowError(UnauthorizedException) + 401 + ) + ) + ).toThrowError(new UnauthorizedException(message)) }) test('should throw UnauthorizedException as invalid grant', () => { + const message = 'Invalid token' + expect(() => - catchSpotifyError({ - response: { - data: { + catchSpotifyError( + axiosResponseMockFactory( + { error: 'invalid_grant', + error_description: message, }, - status: 401, - }, - }) - ).toThrowError(UnauthorizedException) + 401 + ) + ) + ).toThrowError(new UnauthorizedException(message)) }) - test('should throw InternalServerErrorException', () => { + test('should throw BadGatewayException', () => { + const message = 'Bad Gateway' + expect(() => - catchSpotifyError({ - response: { - data: { - error: { - message: 'Internal Server Error', - }, + catchSpotifyError( + axiosResponseMockFactory({ + error: { + message, + status: 502, }, - status: 500, - }, - }) - ).toThrowError(InternalServerErrorException) + }) + ) + ).toThrowError( + new BadGatewayException(SPOTIFY_DEFAULT_ERROR_MESSAGE + message) + ) + }) + + test('should throw BadGatewayException', () => { + const message = 'Bad Gateway' + + expect(() => + catchSpotifyError( + axiosResponseMockFactory({ + error: message, + error_description: message, + }) + ) + ).toThrowError( + new BadGatewayException(SPOTIFY_DEFAULT_ERROR_MESSAGE + message) + ) }) }) diff --git a/src/common/utils/catch-spotify-error.ts b/src/common/utils/catch-spotify-error.ts index 0d11e8dd..61667e21 100644 --- a/src/common/utils/catch-spotify-error.ts +++ b/src/common/utils/catch-spotify-error.ts @@ -1,29 +1,45 @@ -import { - ForbiddenException, - InternalServerErrorException, - UnauthorizedException, -} from '@nestjs/common' - -export const catchSpotifyError = error => { - console.log(error) - - const { - response: { data, status }, - } = error - - if (data?.error === 'invalid_grant') - throw new UnauthorizedException('Invalid token') - if (status === 401) throw new UnauthorizedException(data?.error?.message) - if ( - status === 403 && - data === 'User not registered in the Developer Dashboard' - ) - throw new ForbiddenException( - 'User not registered in the Developer Dashboard' +import { BadGatewayException, UnauthorizedException } from '@nestjs/common' +import { AxiosResponse } from 'axios' + +export interface SpotifyAuthError { + error: string + error_description: string +} + +export interface SpotifyError { + error: { + status: number + message: string + } +} + +export type SpotifyResponseError = AxiosResponse< + SpotifyError | SpotifyAuthError +> + +export const SPOTIFY_DEFAULT_ERROR_MESSAGE = + 'Something went wrong with fetching data from spotify API:' + +export const catchSpotifyError = (response: SpotifyResponseError) => { + console.log(response.data.error) + + const { data, status } = response + + if ('error_description' in data) { + console.log('ee') + if (data.error === 'invalid_grant') + throw new UnauthorizedException('Invalid token') + + throw new BadGatewayException( + SPOTIFY_DEFAULT_ERROR_MESSAGE + data.error_description ) + } + + console.log(status) + + if (status === 401) throw new UnauthorizedException(data.error.message) - throw new InternalServerErrorException( - 'Something went wrong with fetching data from spotify API', - data?.error + throw new BadGatewayException( + SPOTIFY_DEFAULT_ERROR_MESSAGE + data.error.message ) } diff --git a/src/common/utils/get-most-frequent-items.util.spec.ts b/src/common/utils/get-most-frequent-items.util.spec.ts index 2076cfb2..7112accd 100644 --- a/src/common/utils/get-most-frequent-items.util.spec.ts +++ b/src/common/utils/get-most-frequent-items.util.spec.ts @@ -1,5 +1,3 @@ -import { test, describe, expect } from 'vitest' - import { getMostFrequentItems } from '.' describe('GetMostFrequentItems', () => { diff --git a/src/modules/auth/auth.controller.spec.ts b/src/modules/auth/auth.controller.spec.ts index 27eeddb3..86383d59 100644 --- a/src/modules/auth/auth.controller.spec.ts +++ b/src/modules/auth/auth.controller.spec.ts @@ -1,4 +1,3 @@ -import { test, describe, expect, beforeEach, vi } from 'vitest' import { HttpStatus } from '@nestjs/common' import { ConfigService } from '@nestjs/config' import { Test, TestingModule } from '@nestjs/testing' diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index 65199a86..711813ed 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -9,7 +9,12 @@ import { } from '@nestjs/common' import { ConfigService } from '@nestjs/config' import { firstValueFrom } from 'rxjs' -import { ApiExcludeEndpoint, ApiOkResponse, ApiTags } from '@nestjs/swagger' +import { + ApiExcludeEndpoint, + ApiOkResponse, + ApiOperation, + ApiTags, +} from '@nestjs/swagger' import { AuthService } from './auth.service' import { spotifyAuthorizationScopes } from './config' @@ -94,6 +99,9 @@ export class AuthController { } @Get('refresh') + @ApiOperation({ + summary: 'Refreshing access token.', + }) @ApiAuth(AuthenticationType.REFRESH_TOKEN) @ApiOkResponse({ description: 'Access token has been succesfully refreshed', @@ -104,6 +112,9 @@ export class AuthController { } @Get('profile') + @ApiOperation({ + summary: "Getting current user's profile.", + }) @ApiAuth(AuthenticationType.ACCESS_TOKEN) @ApiOkResponse({ description: "User's profile has been succesfully found", diff --git a/src/modules/auth/auth.service.spec.ts b/src/modules/auth/auth.service.spec.ts index deb2ef5f..4e844554 100644 --- a/src/modules/auth/auth.service.spec.ts +++ b/src/modules/auth/auth.service.spec.ts @@ -1,6 +1,5 @@ import { URLSearchParams } from 'node:url' -import { test, describe, expect, beforeEach, vi } from 'vitest' import { HttpService } from '@nestjs/axios' import { ConfigService } from '@nestjs/config' import { JwtService } from '@nestjs/jwt' diff --git a/src/modules/auth/decorators/token.decorator.spec.ts b/src/modules/auth/decorators/token.decorator.spec.ts index 6c53d8b8..d77b94f7 100644 --- a/src/modules/auth/decorators/token.decorator.spec.ts +++ b/src/modules/auth/decorators/token.decorator.spec.ts @@ -1,4 +1,3 @@ -import { test, describe, expect, vi } from 'vitest' import { ExecutionContext, UnauthorizedException } from '@nestjs/common' import { mock } from 'vitest-mock-extended' diff --git a/src/modules/images/images.repository.spec.ts b/src/modules/images/images.repository.spec.ts index d3d0bfac..0207d125 100644 --- a/src/modules/images/images.repository.spec.ts +++ b/src/modules/images/images.repository.spec.ts @@ -1,4 +1,3 @@ -import { beforeEach, describe, expect, test, vi } from 'vitest' import { Test } from '@nestjs/testing' import { DataSource } from 'typeorm' diff --git a/src/modules/player/player.controller.spec.ts b/src/modules/player/player.controller.spec.ts index 758cd05f..4cad2848 100644 --- a/src/modules/player/player.controller.spec.ts +++ b/src/modules/player/player.controller.spec.ts @@ -1,4 +1,3 @@ -import { test, describe, expect, beforeEach, vi } from 'vitest' import { Test, TestingModule } from '@nestjs/testing' import { firstValueFrom, of } from 'rxjs' diff --git a/src/modules/player/player.controller.ts b/src/modules/player/player.controller.ts index 8d09b1f3..e42f7359 100644 --- a/src/modules/player/player.controller.ts +++ b/src/modules/player/player.controller.ts @@ -2,6 +2,7 @@ import { Controller, Get, Put, Query } from '@nestjs/common' import { ApiForbiddenResponse, ApiOkResponse, + ApiOperation, ApiQuery, ApiTags, } from '@nestjs/swagger' @@ -19,6 +20,9 @@ export class PlayerController { constructor(private readonly playerService: PlayerService) {} @Get('/devices') + @ApiOperation({ + summary: "Getting current user's available devices.", + }) @ApiOkResponse({ description: 'Available devices has been succesfully found', }) @@ -27,6 +31,9 @@ export class PlayerController { } @Get('/state') + @ApiOperation({ + summary: "Getting current user's playback state.", + }) @ApiOkResponse({ description: 'Current playback state has been succesfully found', }) @@ -35,13 +42,16 @@ export class PlayerController { } @Put('/pause') + @ApiOperation({ + summary: "Pausing current user's player.", + }) @ApiQuery({ name: 'afterTime', type: Number, required: false }) @ApiQuery({ name: 'deviceId', type: String, required: false }) @ApiForbiddenResponse({ description: 'No device is currently playing', }) @ApiOkResponse({ - description: 'Player state has been succesfully paused', + description: 'Player has been succesfully paused', type: Success, }) pausePlayer( @@ -53,12 +63,15 @@ export class PlayerController { } @Put('/resume') + @ApiOperation({ + summary: "Resuming current user's player.", + }) @ApiQuery({ name: 'deviceId', type: String, required: false }) @ApiForbiddenResponse({ description: 'Device is already playing', }) @ApiOkResponse({ - description: 'Player state has been succesfully resumed', + description: 'Player has been succesfully resumed', type: Success, }) resumePlayer( diff --git a/src/modules/player/player.service.spec.ts b/src/modules/player/player.service.spec.ts index 97e44e26..b7e2beb0 100644 --- a/src/modules/player/player.service.spec.ts +++ b/src/modules/player/player.service.spec.ts @@ -1,4 +1,3 @@ -import { test, describe, expect, beforeEach, vi } from 'vitest' import { HttpService } from '@nestjs/axios' import { TestingModule, Test } from '@nestjs/testing' import { of, firstValueFrom, throwError, catchError } from 'rxjs' diff --git a/src/modules/profiles/profiles.repository.spec.ts b/src/modules/profiles/profiles.repository.spec.ts index ccf3323f..e2d701ee 100644 --- a/src/modules/profiles/profiles.repository.spec.ts +++ b/src/modules/profiles/profiles.repository.spec.ts @@ -1,4 +1,3 @@ -import { beforeEach, describe, expect, test, vi } from 'vitest' import { DataSource } from 'typeorm' import { Test, TestingModule } from '@nestjs/testing' diff --git a/src/modules/profiles/profiles.service.spec.ts b/src/modules/profiles/profiles.service.spec.ts index d3c46732..5ae3748a 100644 --- a/src/modules/profiles/profiles.service.spec.ts +++ b/src/modules/profiles/profiles.service.spec.ts @@ -1,4 +1,3 @@ -import { beforeEach, describe, expect, test, vi } from 'vitest' import { Test } from '@nestjs/testing' import { mock } from 'vitest-mock-extended' diff --git a/src/modules/statistics/statistics.controller.spec.ts b/src/modules/statistics/statistics.controller.spec.ts index 32733acd..14ddffc2 100644 --- a/src/modules/statistics/statistics.controller.spec.ts +++ b/src/modules/statistics/statistics.controller.spec.ts @@ -1,4 +1,3 @@ -import { test, describe, expect, beforeEach, vi } from 'vitest' import { Test, TestingModule } from '@nestjs/testing' import { firstValueFrom, of } from 'rxjs' diff --git a/src/modules/statistics/statistics.controller.ts b/src/modules/statistics/statistics.controller.ts index 343d7710..514dd553 100644 --- a/src/modules/statistics/statistics.controller.ts +++ b/src/modules/statistics/statistics.controller.ts @@ -1,5 +1,5 @@ import { Controller, Get, Query } from '@nestjs/common' -import { ApiOkResponse, ApiQuery, ApiTags } from '@nestjs/swagger' +import { ApiOkResponse, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger' import { StatisticsService } from './statistics.service' import { LastItemQuery, TopItemQuery } from './dtos' @@ -15,6 +15,9 @@ export class StatisticsController { constructor(private readonly statisticsService: StatisticsService) {} @Get('last-tracks') + @ApiOperation({ + summary: "Getting current user's last tracks.", + }) @ApiItemQuery({ withCursors: true }) @ApiOkResponse({ description: 'Last tracks has been succesfully found', @@ -27,6 +30,9 @@ export class StatisticsController { } @Get('top/tracks') + @ApiOperation({ + summary: "Getting current user's top tracks.", + }) @ApiItemQuery({ withOffset: true }) @ApiOkResponse({ description: 'Top tracks has been succesfully found', diff --git a/src/modules/statistics/statistics.service.spec.ts b/src/modules/statistics/statistics.service.spec.ts index 8ab11897..c9800fc7 100644 --- a/src/modules/statistics/statistics.service.spec.ts +++ b/src/modules/statistics/statistics.service.spec.ts @@ -1,4 +1,3 @@ -import { test, describe, expect, beforeEach, vi } from 'vitest' import { HttpService } from '@nestjs/axios' import { TestingModule, Test } from '@nestjs/testing' import { firstValueFrom, of } from 'rxjs' diff --git a/src/modules/users/user.entity.ts b/src/modules/users/user.entity.ts index 962ed070..43110e38 100644 --- a/src/modules/users/user.entity.ts +++ b/src/modules/users/user.entity.ts @@ -14,7 +14,7 @@ import { Profile } from '@modules/profiles' @Entity() export class User { @PrimaryGeneratedColumn('uuid') - @ApiProperty() + @ApiProperty({ type: String }) id: string @OneToOne('Profile', 'user', { cascade: true, eager: true }) diff --git a/src/modules/users/users-profile.controller.spec.ts b/src/modules/users/users-profile.controller.spec.ts index ff3d1960..73865301 100644 --- a/src/modules/users/users-profile.controller.spec.ts +++ b/src/modules/users/users-profile.controller.spec.ts @@ -14,6 +14,7 @@ import { topGenresMock, spotifyResponseWithOffsetMockFactory, artistsMock, + analysisMock, } from '@common/mocks' import { SecretData } from '@modules/auth/dtos' @@ -60,6 +61,7 @@ describe('UsersProfileController', () => { topTracks: vi.fn(), topGenres: vi.fn(), topArtists: vi.fn(), + analysis: vi.fn(), }, }, ], @@ -322,4 +324,33 @@ describe('UsersProfileController', () => { ) }) }) + + describe('getAnalysis', () => { + test('should get user analysis', async () => { + const findOneBySpy = vi + .spyOn(usersRepository, 'findOneBy') + .mockResolvedValue(userMock) + const tokenSpy = vi + .spyOn(authService, 'token') + .mockReturnValue(of(secretDataMock)) + const analysisSpy = vi + .spyOn(statisticsService, 'analysis') + .mockReturnValue(of(analysisMock)) + + expect(await usersProfileController.getAnalysis(id)).toEqual(analysisMock) + expect(tokenSpy).toHaveBeenCalledWith({ + refreshToken: userMock.refreshToken, + }) + expect(analysisSpy).toHaveBeenCalledWith(accessToken) + expect(findOneBySpy).toHaveBeenCalledWith({ id }) + }) + + test('should throw an error if no user is found', () => { + const findOneBySpy = vi.spyOn(usersRepository, 'findOneBy') + + expect(usersProfileController.getAnalysis(id)).rejects.toThrowError() + + expect(findOneBySpy).toHaveBeenCalledWith({ id }) + }) + }) }) diff --git a/src/modules/users/users-profile.controller.ts b/src/modules/users/users-profile.controller.ts index af1bece7..313b7f34 100644 --- a/src/modules/users/users-profile.controller.ts +++ b/src/modules/users/users-profile.controller.ts @@ -14,6 +14,7 @@ import { ApiOkResponse, ApiNotFoundResponse, ApiBadRequestResponse, + ApiTags, } from '@nestjs/swagger' import { firstValueFrom } from 'rxjs' @@ -34,6 +35,7 @@ import { } from '@common/constants' @Controller('users/:id/profile') +@ApiTags('users/{id}/profile') export class UsersProfileController { constructor( private readonly usersRepository: UsersRepository, @@ -177,4 +179,32 @@ export class UsersProfileController { this.statisticsService.topGenres(accessToken, limit, timeRange, offset) ) } + + @Get('analysis') + @ApiOperation({ + summary: "Getting user's analysis.", + }) + @ApiParam({ name: 'id' }) + @ApiOkResponse({ + description: ONE_SUCCESFULLY_FOUND(USER), + }) + @ApiNotFoundResponse({ + description: NOT_BEEN_FOUND(USER), + }) + @ApiBadRequestResponse({ + description: ONE_IS_INVALID('uuid'), + }) + async getAnalysis(@Param('id', ParseUUIDPipe) id: string) { + const foundUser = await this.usersRepository.findOneBy({ id }) + + if (!foundUser) throw new NotFoundException(NOT_BEEN_FOUND(USER)) + + const { accessToken } = await firstValueFrom( + this.authService.token({ + refreshToken: foundUser.refreshToken, + }) + ) + + return firstValueFrom(this.statisticsService.analysis(accessToken)) + } } diff --git a/src/modules/users/users.controller.spec.ts b/src/modules/users/users.controller.spec.ts index b21206ce..41ecefad 100644 --- a/src/modules/users/users.controller.spec.ts +++ b/src/modules/users/users.controller.spec.ts @@ -1,4 +1,3 @@ -import { beforeEach, describe, expect, test, vi } from 'vitest' import { Test } from '@nestjs/testing' import { UsersController } from './users.controller' @@ -8,7 +7,7 @@ import { userMock, usersMock } from '@common/mocks' describe('UsersController', () => { const id = '1' - const username = 'username' + const displayName = 'displayName' let usersController: UsersController let usersRepository: UsersRepository @@ -52,9 +51,9 @@ describe('UsersController', () => { .spyOn(usersRepository, 'findOneByDisplayName') .mockResolvedValue(userMock) - expect(await usersController.getAll(username)).toEqual(userMock) + expect(await usersController.getAll(displayName)).toEqual(userMock) - expect(findOneByDisplayNameSpy).toHaveBeenCalledWith(username) + expect(findOneByDisplayNameSpy).toHaveBeenCalledWith(displayName) }) test('should throw an error if no user is found', () => { @@ -63,8 +62,8 @@ describe('UsersController', () => { 'findOneByDisplayName' ) - expect(usersController.getAll(username)).rejects.toThrowError() - expect(findOneByDisplayNameSpy).toHaveBeenCalledWith(username) + expect(usersController.getAll(displayName)).rejects.toThrowError() + expect(findOneByDisplayNameSpy).toHaveBeenCalledWith(displayName) }) }) diff --git a/src/modules/users/users.controller.ts b/src/modules/users/users.controller.ts index 997d3ec1..d3db6cc3 100644 --- a/src/modules/users/users.controller.ts +++ b/src/modules/users/users.controller.ts @@ -20,6 +20,7 @@ import { } from '@nestjs/swagger' import { UsersRepository } from './users.repository' +import { User } from './user.entity' import { MANY_SUCCESFULLY_FOUND, @@ -40,17 +41,18 @@ export class UsersController { @ApiOperation({ summary: 'Getting all users.', }) - @ApiQuery({ name: 'username', required: false }) + @ApiQuery({ name: 'displayName', required: false }) @ApiOkResponse({ - description: MANY_SUCCESFULLY_FOUND(USERS), + description: MANY_SUCCESFULLY_FOUND(USER), + type: [User], }) @ApiNoContentResponse({ description: NOT_BEEN_FOUND(USER), }) - async getAll(@Query('username') username?: string) { - if (username) { + async getAll(@Query('displayName') displayName?: string) { + if (displayName) { const foundUser = - await this.usersRepository.findOneByDisplayName(username) + await this.usersRepository.findOneByDisplayName(displayName) if (!foundUser) throw new HttpException(NOT_BEEN_FOUND(USER), HttpStatus.NO_CONTENT) @@ -68,6 +70,7 @@ export class UsersController { @ApiParam({ name: 'id' }) @ApiOkResponse({ description: ONE_SUCCESFULLY_FOUND(USER), + type: User, }) @ApiNotFoundResponse({ description: NOT_BEEN_FOUND(USER), diff --git a/src/modules/users/users.repository.spec.ts b/src/modules/users/users.repository.spec.ts index a3152fa5..7a496a97 100644 --- a/src/modules/users/users.repository.spec.ts +++ b/src/modules/users/users.repository.spec.ts @@ -1,4 +1,3 @@ -import { beforeEach, describe, expect, test, vi } from 'vitest' import { Test } from '@nestjs/testing' import { DataSource } from 'typeorm'