@@ -2,6 +2,7 @@ import session from 'supertest-session';
2
2
import mockingoose from 'mockingoose' ;
3
3
import { server } from '../server' ;
4
4
import {
5
+ VALID_USERID ,
5
6
CREDIT_CARD ,
6
7
INVALID_CREDIT_CARD ,
7
8
LOGIN_SUCCESS_MOCK_USER ,
@@ -23,6 +24,54 @@ describe('Test /creditcards API routes', () => {
23
24
. expect ( 200 ) ;
24
25
} ) ;
25
26
27
+ describe ( 'Test getCreditCards' , ( ) => {
28
+ it ( 'getCreditCards success, return 200 status, and error = false' , async ( ) => {
29
+ mockingoose . User . toReturn ( VALID_USERID , 'findOne' ) ;
30
+ mockingoose . CreditCard . toReturn ( CREDIT_CARD , 'find' ) ;
31
+ const response = await serverSession
32
+ . get ( '/api/creditcards' )
33
+ . set ( 'Accept' , 'text/html, application/json' ) ;
34
+ expect ( response . statusCode ) . toBe ( 200 ) ;
35
+ expect ( response . error ) . toEqual ( false ) ;
36
+ } ) ;
37
+ it ( 'getCreditCards failure, find User error, return 400 status, and error = true' , async ( ) => {
38
+ mockingoose . User . toReturn ( new Error ( 'Error' ) , 'findOne' ) ;
39
+ const response = await serverSession
40
+ . get ( '/api/creditcards' )
41
+ . set ( 'Accept' , 'text/html application/json' ) ;
42
+ expect ( response . statusCode ) . toBe ( 400 ) ;
43
+ expect ( response . body . error ) . toEqual ( true ) ;
44
+ } ) ;
45
+ it ( 'getCreditCards failure, find CreditCard, return 400 status, and error = true' , async ( ) => {
46
+ mockingoose . User . toReturn ( VALID_USERID , 'findOne' ) ;
47
+ mockingoose . CreditCard . toReturn ( new Error ( 'Error' ) , 'find' ) ;
48
+ const response = await serverSession
49
+ . get ( '/api/creditcards' )
50
+ . set ( 'Accept' , 'text/html application/json' ) ;
51
+ expect ( response . statusCode ) . toBe ( 400 ) ;
52
+ expect ( response . body . error ) . toEqual ( true ) ;
53
+ } ) ;
54
+ } ) ;
55
+
56
+ describe ( 'Test getCreditCardById' , ( ) => {
57
+ it ( 'getCreditCardById success, return 200 status, and error = false' , async ( ) => {
58
+ mockingoose . CreditCard . toReturn ( CREDIT_CARD , 'findOne' ) ;
59
+ const response = await serverSession
60
+ . get ( '/api/creditcards/8675309' )
61
+ . set ( 'Accept' , 'text/html application/json' ) ;
62
+ expect ( response . statusCode ) . toBe ( 200 ) ;
63
+ expect ( response . body . error ) . toEqual ( false ) ;
64
+ } ) ;
65
+ it ( 'getCreditCardById failure, return 400 status, and error = true' , async ( ) => {
66
+ mockingoose . CreditCard . toReturn ( new Error ( 'Error' ) , 'findOne' ) ;
67
+ const response = await serverSession
68
+ . get ( '/api/creditcards/8675309' )
69
+ . set ( 'Accept' , 'text/html application/json' ) ;
70
+ expect ( response . statusCode ) . toBe ( 400 ) ;
71
+ expect ( response . body . error ) . toEqual ( true ) ;
72
+ } ) ;
73
+ } ) ;
74
+
26
75
describe ( 'Test addCreditCard' , ( ) => {
27
76
it ( 'addCreditCard success, return 200 status, and error = false' , async ( ) => {
28
77
mockingoose . CreditCard . toReturn ( CREDIT_CARD , 'save' ) ;
@@ -32,16 +81,90 @@ describe('Test /creditcards API routes', () => {
32
81
. send ( CREDIT_CARD ) ;
33
82
expect ( response . body . data . limit ) . toEqual ( 10000 ) ;
34
83
} ) ;
35
- it ( 'addCreditCard failure, error = true' , async ( ) => {
84
+ it ( 'addCreditCard failure, return 400 status, error = true' , async ( ) => {
36
85
mockingoose . CreditCard . toReturn ( INVALID_CREDIT_CARD , 'save' ) ;
37
86
const response = await serverSession
38
87
. post ( '/api/creditcards' )
39
88
. set ( 'Accept' , 'text/html, application/json' )
40
89
. send ( INVALID_CREDIT_CARD ) ;
90
+ expect ( response . statusCode ) . toBe ( 400 ) ;
41
91
expect ( response . body . error ) . toBe ( true ) ;
42
- expect ( response . body . message ) . toEqual (
43
- 'Error adding data, all fields required' ,
92
+ expect ( response . body . message ) . toEqual ( 'Error adding data' ) ;
93
+ } ) ;
94
+ } ) ;
95
+
96
+ describe ( 'Test deleteCreditCard' , ( ) => {
97
+ it ( 'deleteCreditCard success, return 200 status, and error = false' , async ( ) => {
98
+ mockingoose . CreditCard . toReturn ( VALID_USERID , 'findOne' ) . toReturn (
99
+ VALID_USERID ,
100
+ 'remove' ,
101
+ ) ;
102
+ const response = await serverSession
103
+ . delete ( '/api/creditcards/8675309' )
104
+ . set ( 'Accept' , 'text/html application/json' ) ;
105
+ expect ( response . statusCode ) . toBe ( 200 ) ;
106
+ expect ( response . body . error ) . toEqual ( false ) ;
107
+ } ) ;
108
+ it ( 'deleteCreditCard failure, invalid userId, return 400 status, and error = true' , async ( ) => {
109
+ mockingoose . CreditCard . toReturn ( new Error ( 'Error' ) , 'findOne' ) . toReturn (
110
+ VALID_USERID ,
111
+ 'remove' ,
112
+ ) ;
113
+ const response = await serverSession
114
+ . delete ( '/api/creditcards/8675309' )
115
+ . set ( 'Accept' , 'text/html application/json' ) ;
116
+ expect ( response . statusCode ) . toBe ( 400 ) ;
117
+ expect ( response . body . error ) . toEqual ( true ) ;
118
+ } ) ;
119
+ it ( 'deleteCreditCard failure, invalid credit card, return 400 status, and error = true' , async ( ) => {
120
+ mockingoose . CreditCard . toReturn ( VALID_USERID , 'findOne' ) . toReturn (
121
+ new Error ( 'Error' ) ,
122
+ 'remove' ,
123
+ ) ;
124
+ const response = await serverSession
125
+ . delete ( '/api/creditcards/8675309' )
126
+ . set ( 'Accept' , 'text/html application/json' ) ;
127
+ expect ( response . statusCode ) . toBe ( 400 ) ;
128
+ expect ( response . body . error ) . toEqual ( true ) ;
129
+ } ) ;
130
+ } ) ;
131
+
132
+ describe ( 'Test putOrUpdate' , ( ) => {
133
+ it ( 'putOrUpdate success, return 200 status, and error = false' , async ( ) => {
134
+ mockingoose . CreditCard . toReturn ( CREDIT_CARD , 'findOne' ) . toReturn (
135
+ CREDIT_CARD ,
136
+ 'save' ,
44
137
) ;
138
+ const response = await serverSession
139
+ . put ( '/api/creditcards/8675309' )
140
+ . set ( 'Accept' , 'text/html application/json' )
141
+ . send ( CREDIT_CARD ) ;
142
+ expect ( response . statusCode ) . toBe ( 200 ) ;
143
+ expect ( response . body . error ) . toEqual ( false ) ;
144
+ } ) ;
145
+ it ( 'putOrUpdate failure, find error, return 400 status, and error = true' , async ( ) => {
146
+ mockingoose . CreditCard . toReturn ( new Error ( 'Error' ) , 'findOne' ) . toReturn (
147
+ CREDIT_CARD ,
148
+ 'save' ,
149
+ ) ;
150
+ const response = await serverSession
151
+ . put ( '/api/creditcards/8675309' )
152
+ . set ( 'Accept' , 'text/html application/json' )
153
+ . send ( CREDIT_CARD ) ;
154
+ expect ( response . statusCode ) . toBe ( 400 ) ;
155
+ expect ( response . body . error ) . toEqual ( true ) ;
156
+ } ) ;
157
+ it ( 'putOrUpdate failure, update error, return 400 status, and error = true' , async ( ) => {
158
+ mockingoose . CreditCard . toReturn ( CREDIT_CARD , 'findOne' ) . toReturn (
159
+ new Error ( 'Error' ) ,
160
+ 'save' ,
161
+ ) ;
162
+ const response = await serverSession
163
+ . put ( '/api/creditcards/8675309' )
164
+ . set ( 'Accept' , 'text/html application/json' )
165
+ . send ( CREDIT_CARD ) ;
166
+ expect ( response . statusCode ) . toBe ( 400 ) ;
167
+ expect ( response . body . error ) . toEqual ( true ) ;
45
168
} ) ;
46
169
} ) ;
47
170
} ) ;
0 commit comments