Add robot tests

This commit is contained in:
2023-10-08 18:45:53 +02:00
parent a65ce6595a
commit 38bbe56e9b
5 changed files with 79 additions and 23 deletions
+16 -9
View File
@@ -20,10 +20,15 @@ import { CreateArtistDto } from './dto/create-artist.dto';
import { Request } from 'express'; import { Request } from 'express';
import { ArtistService } from './artist.service'; import { ArtistService } from './artist.service';
import { Prisma, Artist } from '@prisma/client'; import { Prisma, Artist } from '@prisma/client';
import { ApiNotFoundResponse, ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger'; import {
ApiNotFoundResponse,
ApiOkResponse,
ApiOperation,
ApiTags,
} from '@nestjs/swagger';
import { createReadStream, existsSync } from 'fs'; import { createReadStream, existsSync } from 'fs';
import { FilterQuery } from 'src/utils/filter.pipe'; import { FilterQuery } from 'src/utils/filter.pipe';
import { Artist as _Artist} from 'src/_gen/prisma-class/artist'; import { Artist as _Artist } from 'src/_gen/prisma-class/artist';
@Controller('artist') @Controller('artist')
@ApiTags('artist') @ApiTags('artist')
@@ -33,7 +38,9 @@ export class ArtistController {
constructor(private readonly service: ArtistService) {} constructor(private readonly service: ArtistService) {}
@Post() @Post()
@ApiOperation({ description: "Register a new artist, should not be used by frontend"}) @ApiOperation({
description: 'Register a new artist, should not be used by frontend',
})
async create(@Body() dto: CreateArtistDto) { async create(@Body() dto: CreateArtistDto) {
try { try {
return await this.service.create(dto); return await this.service.create(dto);
@@ -43,7 +50,7 @@ export class ArtistController {
} }
@Delete(':id') @Delete(':id')
@ApiOperation({ description: "Delete an artist by id"}) @ApiOperation({ description: 'Delete an artist by id' })
async remove(@Param('id', ParseIntPipe) id: number) { async remove(@Param('id', ParseIntPipe) id: number) {
try { try {
return await this.service.delete({ id }); return await this.service.delete({ id });
@@ -53,8 +60,8 @@ export class ArtistController {
} }
@Get(':id/illustration') @Get(':id/illustration')
@ApiOperation({ description: "Get an artist's illustration"}) @ApiOperation({ description: "Get an artist's illustration" })
@ApiNotFoundResponse({ description: "Artist or illustration not found"}) @ApiNotFoundResponse({ description: 'Artist or illustration not found' })
async getIllustration(@Param('id', ParseIntPipe) id: number) { async getIllustration(@Param('id', ParseIntPipe) id: number) {
const artist = await this.service.get({ id }); const artist = await this.service.get({ id });
if (!artist) throw new NotFoundException('Artist not found'); if (!artist) throw new NotFoundException('Artist not found');
@@ -71,7 +78,7 @@ export class ArtistController {
} }
@Get() @Get()
@ApiOperation({ description: "Get all artists paginated"}) @ApiOperation({ description: 'Get all artists paginated' })
@ApiOkResponsePlaginated(_Artist) @ApiOkResponsePlaginated(_Artist)
async findAll( async findAll(
@Req() req: Request, @Req() req: Request,
@@ -89,8 +96,8 @@ export class ArtistController {
} }
@Get(':id') @Get(':id')
@ApiOperation({ description: "Get an artist by id"}) @ApiOperation({ description: 'Get an artist by id' })
@ApiOkResponse({ type: _Artist}) @ApiOkResponse({ type: _Artist })
async findOne(@Param('id', ParseIntPipe) id: number) { async findOne(@Param('id', ParseIntPipe) id: number) {
const res = await this.service.get({ id }); const res = await this.service.get({ id });
+15 -9
View File
@@ -10,14 +10,20 @@ import {
Request, Request,
UseGuards, UseGuards,
} from '@nestjs/common'; } from '@nestjs/common';
import { ApiCreatedResponse, ApiOkResponse, ApiOperation, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger'; import {
ApiCreatedResponse,
ApiOkResponse,
ApiOperation,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { SearchHistory, SongHistory } from '@prisma/client'; import { SearchHistory, SongHistory } from '@prisma/client';
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard'; import { JwtAuthGuard } from 'src/auth/jwt-auth.guard';
import { SongHistoryDto } from './dto/SongHistoryDto'; import { SongHistoryDto } from './dto/SongHistoryDto';
import { HistoryService } from './history.service'; import { HistoryService } from './history.service';
import { SearchHistoryDto } from './dto/SearchHistoryDto'; import { SearchHistoryDto } from './dto/SearchHistoryDto';
import { SongHistory as _SongHistory } from 'src/_gen/prisma-class/song_history'; import { SongHistory as _SongHistory } from 'src/_gen/prisma-class/song_history';
import { SearchHistory as _SearchHistory} from 'src/_gen/prisma-class/search_history'; import { SearchHistory as _SearchHistory } from 'src/_gen/prisma-class/search_history';
@Controller('history') @Controller('history')
@ApiTags('history') @ApiTags('history')
@@ -26,9 +32,9 @@ export class HistoryController {
@Get() @Get()
@HttpCode(200) @HttpCode(200)
@ApiOperation({ description: "Get song history of connected user"}) @ApiOperation({ description: 'Get song history of connected user' })
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: _SongHistory, isArray: true}) @ApiOkResponse({ type: _SongHistory, isArray: true })
@ApiUnauthorizedResponse({ description: 'Invalid token' }) @ApiUnauthorizedResponse({ description: 'Invalid token' })
async getHistory( async getHistory(
@Request() req: any, @Request() req: any,
@@ -40,9 +46,9 @@ export class HistoryController {
@Get('search') @Get('search')
@HttpCode(200) @HttpCode(200)
@ApiOperation({ description: "Get search history of connected user"}) @ApiOperation({ description: 'Get search history of connected user' })
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: _SearchHistory, isArray: true}) @ApiOkResponse({ type: _SearchHistory, isArray: true })
@ApiUnauthorizedResponse({ description: 'Invalid token' }) @ApiUnauthorizedResponse({ description: 'Invalid token' })
async getSearchHistory( async getSearchHistory(
@Request() req: any, @Request() req: any,
@@ -54,15 +60,15 @@ export class HistoryController {
@Post() @Post()
@HttpCode(201) @HttpCode(201)
@ApiOperation({ description: "Create a record of a song played by a user"}) @ApiOperation({ description: 'Create a record of a song played by a user' })
@ApiCreatedResponse({ description: "Succesfully created a record"}) @ApiCreatedResponse({ description: 'Succesfully created a record' })
async create(@Body() record: SongHistoryDto): Promise<SongHistory> { async create(@Body() record: SongHistoryDto): Promise<SongHistory> {
return this.historyService.createSongHistoryRecord(record); return this.historyService.createSongHistoryRecord(record);
} }
@Post('search') @Post('search')
@HttpCode(201) @HttpCode(201)
@ApiOperation({ description: "Creates a search record in the users history"}) @ApiOperation({ description: 'Creates a search record in the users history' })
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ApiUnauthorizedResponse({ description: 'Invalid token' }) @ApiUnauthorizedResponse({ description: 'Invalid token' })
async createSearchHistory( async createSearchHistory(
+1
View File
@@ -174,6 +174,7 @@ export class SongController {
} }
@Get(':id') @Get(':id')
@UseGuards(JwtAuthGuard)
@ApiOperation({ description: 'Get a specific song data' }) @ApiOperation({ description: 'Get a specific song data' })
@ApiNotFoundResponse({ description: 'Song not found' }) @ApiNotFoundResponse({ description: 'Song not found' })
@ApiOkResponse({ type: _Song, description: 'Requested song' }) @ApiOkResponse({ type: _Song, description: 'Requested song' })
+3 -4
View File
@@ -18,11 +18,10 @@ export function mapInclude<IncludeType>(
for (const key of include.split(',')) { for (const key of include.split(',')) {
const value = const value =
typeof fields[key] === 'function' typeof fields[key] === 'function'
? // @ts-expect-error typescript is dumb, once again ? fields[key]({ user: req.user })
fields[key]({ user: req.user })
: fields[key]; : fields[key];
if (value === true) include[key] = true; if (value !== false && value !== undefined) ret[key] = value;
else if (value !== false) { else {
throw new BadRequestException( throw new BadRequestException(
`Invalid include, ${key} is not valid. Valid includes are: ${Object.keys( `Invalid include, ${key} is not valid. Valid includes are: ${Object.keys(
fields, fields,
+44 -1
View File
@@ -3,6 +3,7 @@ Documentation Tests of the /song route.
... Ensures that the songs CRUD works corectly. ... Ensures that the songs CRUD works corectly.
Resource ../rest.resource Resource ../rest.resource
Resource ../auth/auth.resource
*** Test Cases *** *** Test Cases ***
@@ -133,5 +134,47 @@ Get midi file
Integer response status 201 Integer response status 201
GET /song/${res.body.id}/midi GET /song/${res.body.id}/midi
Integer response status 200 Integer response status 200
#Output # Output
[Teardown] DELETE /song/${res.body.id} [Teardown] DELETE /song/${res.body.id}
Find a song with artist
[Documentation] Create a song and find it with it's artist
&{res2}= POST /artist { "name": "Tghjmk"}
Output
Integer response status 201
&{res}= POST
... /song
... {"name": "Mama miaeyi", "artistId": ${res2.body.id}, "difficulties": {}, "midiPath": "/musics/Beethoven-125-4.midi", "musicXmlPath": "/musics/Beethoven-125-4.mxl"}
Output
Integer response status 201
&{get}= GET /song/${res.body.id}?include=artist
Output
Integer response status 200
Should Be Equal ${res2.body} ${get.body.artist}
[Teardown] Run Keywords DELETE /song/${res.body.id}
... AND DELETE /artist/${res2.body.id}
Find a song with artist and history
[Documentation] Create a song and find it with it's artist
${userID}= RegisterLogin wowusersfkj
&{res2}= POST /artist { "name": "Tghjmk"}
Output
Integer response status 201
&{res}= POST
... /song
... {"name": "Mama miaeyi", "artistId": ${res2.body.id}, "difficulties": {}, "midiPath": "/musics/Beethoven-125-4.midi", "musicXmlPath": "/musics/Beethoven-125-4.mxl"}
Output
Integer response status 201
&{res3}= POST
... /history
... { "songID": ${res.body.id}, "userID": ${userID}, "score": 12, "difficulties": {}, "info": {} }
Output
Integer response status 201
&{get}= GET /song/${res.body.id}?include=artist,SongHistory
Output
Integer response status 200
Should Be Equal ${res2.body} ${get.body.artist}
Should Be Equal ${res3.body} ${get.body.SongHistory[0]}
[Teardown] Run Keywords DELETE /auth/me
... AND DELETE /song/${res.body.id}
... AND DELETE /artist/${res2.body.id}