@@ -15,6 +15,8 @@ const path = require('path');
1515const chai = require ( 'chai' ) ;
1616const request = require ( 'supertest' ) ;
1717const chaiResponseValidator = require ( 'chai-openapi-response-validator' ) ;
18+ const { tag : { CreateTagUseCase } } = require ( '../../lib/usecases' ) ;
19+ const { dtos : { CreateTagDto } } = require ( '../../lib/domain' ) ;
1820
1921const { expect } = chai ;
2022
@@ -300,6 +302,123 @@ module.exports = () => {
300302 } ) ;
301303 } ) ;
302304
305+ describe ( 'DELETE /api/tags/:tagId' , ( ) => {
306+ let createdTag ;
307+
308+ beforeEach ( async ( ) => {
309+ const createTagDto = await CreateTagDto . validateAsync ( {
310+ body : {
311+ text : `TAG#${ new Date ( ) . getTime ( ) } ` ,
312+ } ,
313+ } ) ;
314+
315+ createdTag = await new CreateTagUseCase ( )
316+ . execute ( createTagDto ) ;
317+ } ) ;
318+
319+ it ( 'should return 400 if the tag id is not a number' , ( done ) => {
320+ request ( server )
321+ . delete ( '/api/tags/abc' )
322+ . expect ( 400 )
323+ . end ( ( err , res ) => {
324+ if ( err ) {
325+ done ( err ) ;
326+ return ;
327+ }
328+
329+ // Response must satisfy the OpenAPI specification
330+ expect ( res ) . to . satisfyApiSpec ;
331+
332+ const { errors } = res . body ;
333+ const titleError = errors . find ( ( err ) => err . source . pointer === '/data/attributes/params/tagId' ) ;
334+ expect ( titleError . detail ) . to . equal ( '"params.tagId" must be a number' ) ;
335+
336+ done ( ) ;
337+ } ) ;
338+ } ) ;
339+
340+ it ( 'should return 400 if the tag id is not positive' , ( done ) => {
341+ request ( server )
342+ . delete ( '/api/tags/-1' )
343+ . expect ( 400 )
344+ . end ( ( err , res ) => {
345+ if ( err ) {
346+ done ( err ) ;
347+ return ;
348+ }
349+
350+ // Response must satisfy the OpenAPI specification
351+ expect ( res ) . to . satisfyApiSpec ;
352+
353+ const { errors } = res . body ;
354+ const titleError = errors . find ( ( err ) => err . source . pointer === '/data/attributes/params/tagId' ) ;
355+ expect ( titleError . detail ) . to . equal ( '"params.tagId" must be a positive number' ) ;
356+
357+ done ( ) ;
358+ } ) ;
359+ } ) ;
360+
361+ it ( 'should return 400 if the tag id is not a whole number' , ( done ) => {
362+ request ( server )
363+ . delete ( '/api/tags/0.5' )
364+ . expect ( 400 )
365+ . end ( ( err , res ) => {
366+ if ( err ) {
367+ done ( err ) ;
368+ return ;
369+ }
370+
371+ // Response must satisfy the OpenAPI specification
372+ expect ( res ) . to . satisfyApiSpec ;
373+
374+ const { errors } = res . body ;
375+ const titleError = errors . find ( ( err ) => err . source . pointer === '/data/attributes/params/tagId' ) ;
376+ expect ( titleError . detail ) . to . equal ( '"params.tagId" must be an integer' ) ;
377+
378+ done ( ) ;
379+ } ) ;
380+ } ) ;
381+
382+ it ( 'should return 404 if the tag could not be found' , ( done ) => {
383+ request ( server )
384+ . delete ( '/api/tags/999999999' )
385+ . expect ( 404 )
386+ . end ( ( err , res ) => {
387+ if ( err ) {
388+ done ( err ) ;
389+ return ;
390+ }
391+
392+ // Response must satisfy the OpenAPI specification
393+ expect ( res ) . to . satisfyApiSpec ;
394+
395+ expect ( res . body . errors [ 0 ] . title ) . to . equal ( 'Tag with this id (999999999) could not be found' ) ;
396+
397+ done ( ) ;
398+ } ) ;
399+ } ) ;
400+
401+ it ( 'should return 200 in all other cases' , ( done ) => {
402+ request ( server )
403+ . delete ( `/api/tags/${ createdTag . id } ` )
404+ . expect ( 200 )
405+ . end ( ( err , res ) => {
406+ if ( err ) {
407+ done ( err ) ;
408+ return ;
409+ }
410+
411+ // Response must satisfy the OpenAPI specification
412+ expect ( res ) . to . satisfyApiSpec ;
413+
414+ expect ( res . body . data . id ) . to . equal ( createdTag . id ) ;
415+ expect ( res . body . data . text ) . to . equal ( createdTag . text ) ;
416+
417+ done ( ) ;
418+ } ) ;
419+ } ) ;
420+ } ) ;
421+
303422 describe ( 'GET /api/tags/:tagId/logs' , ( ) => {
304423 it ( 'should return 400 if the tag id is not a number' , ( done ) => {
305424 request ( server )
0 commit comments