1
+ const { chai, server, should } = require ( './testConfig' ) ;
2
+ const BookModel = require ( "../models/BookModel" ) ;
3
+
4
+ /**
5
+ * Test cases to test all the book APIs
6
+ * Covered Routes:
7
+ * (1) Login
8
+ * (2) Store book
9
+ * (3) Get all books
10
+ * (4) Get single book
11
+ * (5) Update book
12
+ * (6) Delete book
13
+ */
14
+
15
+ describe ( 'Book' , ( ) => {
16
+ //Before each test we empty the database
17
+ before ( ( done ) => {
18
+ BookModel . deleteMany ( { } , ( err ) => {
19
+ done ( ) ;
20
+ } ) ;
21
+ } ) ;
22
+
23
+ // Prepare data for testing
24
+ const userTestData = {
25
+ "password" :"Test@123" ,
26
+
27
+ }
28
+
29
+ // Prepare data for testing
30
+ const testData = {
31
+ "title" :"testing book" ,
32
+ "description" :"testing book desc" ,
33
+ "isbn" :"3214htrff4"
34
+ } ;
35
+
36
+ /*
37
+ * Test the /POST route
38
+ */
39
+ describe ( '/POST Login' , ( ) => {
40
+ it ( 'it should do user Login for book' , ( done ) => {
41
+ chai . request ( server )
42
+ . post ( '/api/auth/login' )
43
+ . send ( { "email" : userTestData . email , "password" : userTestData . password } )
44
+ . end ( ( err , res ) => {
45
+ res . should . have . status ( 200 ) ;
46
+ res . body . should . have . property ( 'message' ) . eql ( "Login Success." ) ;
47
+ userTestData . token = res . body . data . token ;
48
+ done ( ) ;
49
+ } ) ;
50
+ } ) ;
51
+ } ) ;
52
+
53
+ /*
54
+ * Test the /POST route
55
+ */
56
+ describe ( '/POST Book Store' , ( ) => {
57
+ it ( 'It should send validation error for store book' , ( done ) => {
58
+ chai . request ( server )
59
+ . post ( '/api/book' )
60
+ . send ( )
61
+ . set ( 'Authorization' , 'Bearer ' + userTestData . token )
62
+ . end ( ( err , res ) => {
63
+ res . should . have . status ( 400 ) ;
64
+ done ( ) ;
65
+ } ) ;
66
+ } ) ;
67
+ } ) ;
68
+
69
+ /*
70
+ * Test the /POST route
71
+ */
72
+ describe ( '/POST Book Store' , ( ) => {
73
+ it ( 'It should store book' , ( done ) => {
74
+ chai . request ( server )
75
+ . post ( '/api/book' )
76
+ . send ( testData )
77
+ . set ( 'Authorization' , 'Bearer ' + userTestData . token )
78
+ . end ( ( err , res ) => {
79
+ res . should . have . status ( 200 ) ;
80
+ res . body . should . have . property ( 'message' ) . eql ( "Book add Success." ) ;
81
+ done ( ) ;
82
+ } ) ;
83
+ } ) ;
84
+ } ) ;
85
+
86
+ /*
87
+ * Test the /GET route
88
+ */
89
+ describe ( '/GET All book' , ( ) => {
90
+ it ( 'it should GET all the books' , ( done ) => {
91
+ chai . request ( server )
92
+ . get ( '/api/book' )
93
+ . set ( 'Authorization' , 'Bearer ' + userTestData . token )
94
+ . end ( ( err , res ) => {
95
+ res . should . have . status ( 200 ) ;
96
+ res . body . should . have . property ( 'message' ) . eql ( "Operation success" ) ;
97
+ testData . _id = res . body . data [ 0 ] . _id ;
98
+ done ( ) ;
99
+ } ) ;
100
+ } ) ;
101
+ } ) ;
102
+
103
+ /*
104
+ * Test the /GET/:id route
105
+ */
106
+ describe ( '/GET/:id book' , ( ) => {
107
+ it ( 'it should GET the books' , ( done ) => {
108
+ chai . request ( server )
109
+ . get ( '/api/book/' + testData . _id )
110
+ . set ( 'Authorization' , 'Bearer ' + userTestData . token )
111
+ . end ( ( err , res ) => {
112
+ res . should . have . status ( 200 ) ;
113
+ res . body . should . have . property ( 'message' ) . eql ( "Operation success" ) ;
114
+ done ( ) ;
115
+ } ) ;
116
+ } ) ;
117
+ } ) ;
118
+
119
+ /*
120
+ * Test the /PUT/:id route
121
+ */
122
+ describe ( '/PUT/:id book' , ( ) => {
123
+ it ( 'it should PUT the books' , ( done ) => {
124
+ chai . request ( server )
125
+ . put ( '/api/book/' + testData . _id )
126
+ . send ( testData )
127
+ . set ( 'Authorization' , 'Bearer ' + userTestData . token )
128
+ . end ( ( err , res ) => {
129
+ res . should . have . status ( 200 ) ;
130
+ res . body . should . have . property ( 'message' ) . eql ( "Book update Success." ) ;
131
+ done ( ) ;
132
+ } ) ;
133
+ } ) ;
134
+ } ) ;
135
+
136
+ /*
137
+ * Test the /DELETE/:id route
138
+ */
139
+ describe ( '/DELETE/:id book' , ( ) => {
140
+ it ( 'it should DELETE the books' , ( done ) => {
141
+ chai . request ( server )
142
+ . delete ( '/api/book/' + testData . _id )
143
+ . set ( 'Authorization' , 'Bearer ' + userTestData . token )
144
+ . end ( ( err , res ) => {
145
+ res . should . have . status ( 200 ) ;
146
+ res . body . should . have . property ( 'message' ) . eql ( "Book delete Success." ) ;
147
+ done ( ) ;
148
+ } ) ;
149
+ } ) ;
150
+ } ) ;
151
+ } ) ;
0 commit comments