@@ -3,6 +3,7 @@ import {NextFunction, Request, Response} from 'express';
33import { ObjectID } from 'mongodb' ;
44import { testSapi , testUrl } from '../../spec/helpers/sakuraapi' ;
55import { Db , Json , Model , SakuraApiModel } from '../@model' ;
6+ import { DUPLICATE_RESOURCE } from '../helpers/http-status' ;
67import { Routable , routableSymbols , Route , SakuraApiRoutable } from './' ;
78import { IRoutableLocals } from './routable' ;
89import request = require( 'supertest' ) ;
@@ -336,6 +337,15 @@ describe('core/@Routable', () => {
336337 @Db ( { model : Contact } )
337338 @Json ( )
338339 contact = new Contact ( ) ;
340+
341+ @Db ( 'email' )
342+ email : string ;
343+
344+ // constructor() {
345+ // super();
346+ //
347+ // this.email = `test+${new Date().getTime().toString()}@test .olivetech.net`;
348+ // }
339349 }
340350
341351 @Model ( {
@@ -386,15 +396,14 @@ describe('core/@Routable', () => {
386396 } ) ;
387397
388398 beforeEach ( ( done ) => {
389- sapi
390- . listen ( { bootMessage : '' } )
399+ sapi . listen ( { bootMessage : '' } )
400+ . then ( ( ) => User . removeAll ( { } ) )
391401 . then ( done )
392402 . catch ( done . fail ) ;
393403 } ) ;
394404
395405 afterEach ( ( done ) => {
396- sapi
397- . close ( )
406+ sapi . close ( )
398407 . then ( done )
399408 . catch ( done . fail ) ;
400409 } ) ;
@@ -996,6 +1005,61 @@ describe('core/@Routable', () => {
9961005 . catch ( done . fail ) ;
9971006 } ) ;
9981007
1008+ it ( 'sends http status 409 on MongoError: E11000' , ( done ) => {
1009+ let indexName ;
1010+
1011+ const userDb = sapi . dbConnections . getDb ( 'userDb' ) ;
1012+ userDb
1013+ . collection ( 'usersRoutableTests' )
1014+ . createIndex ( { email : 1 } , { unique : true } )
1015+ . then ( ( idxName ) => {
1016+ indexName = idxName ;
1017+
1018+ const user1 = User . fromJson ( {
1019+ email : 'test'
1020+ } ) ;
1021+
1022+ const user2 = User . fromJson ( {
1023+ email : 'test'
1024+ } ) ;
1025+
1026+ const wait = [ ] ;
1027+
1028+ wait . push ( user1 . create ( ) ) ;
1029+ wait . push ( user2 . create ( ) ) ;
1030+
1031+ return Promise . all ( wait ) ;
1032+ } )
1033+ . then ( ( ) => {
1034+ done . fail ( 'A MongoDB duplicate error should have been thrown, this test is invalid' ) ;
1035+ } )
1036+ . catch ( ( err ) => {
1037+ expect ( err . name ) . toBe ( 'MongoError' , 'Test setup should have returned a MongoError. ' +
1038+ `It returned ${ ( err || { } as any ) . name } instead. This test is not in a valid state` ) ;
1039+ expect ( err . code ) . toBe ( 11000 , 'The wrong kind of mongo error was thrown, the test is in an invalid state' ) ;
1040+ } )
1041+ . then ( ( ) => {
1042+ return request ( sapi . app )
1043+ . post ( testUrl ( `/user` ) )
1044+ . type ( 'application/json' )
1045+ . send ( {
1046+ email : 'test' ,
1047+ firstName : 'george' ,
1048+ lastName : 'washington' ,
1049+ password : '123'
1050+ } )
1051+ . expect ( DUPLICATE_RESOURCE ) ;
1052+ } )
1053+ . then ( ( ) => {
1054+ return sapi
1055+ . dbConnections
1056+ . getDb ( 'userDb' )
1057+ . collection ( 'usersRoutableTests' )
1058+ . dropIndex ( indexName ) ;
1059+ } )
1060+ . then ( done )
1061+ . catch ( done . fail ) ;
1062+ } ) ;
9991063 } ) ;
10001064
10011065 describe ( 'PUT ./model' , ( ) => {
0 commit comments