1
1
import { expect } from 'expect' ;
2
2
import mongoose from 'mongoose' ;
3
3
import request from 'supertest' ;
4
- import { Category , Lesson , User } from '../src/models/index.js' ;
4
+ import { Category , User , Lesson } from '../src/models/index.js' ;
5
5
import app from './../src/app.js' ;
6
6
7
7
const categoryId = mongoose . Types . ObjectId ( ) ;
8
- const lesson_ids = [ mongoose . Types . ObjectId ( ) , mongoose . Types . ObjectId ( ) , mongoose . Types . ObjectId ( ) , mongoose . Types . ObjectId ( ) ] ;
8
+ const lesson_ids = [ mongoose . Types . ObjectId ( ) , mongoose . Types . ObjectId ( ) , mongoose . Types . ObjectId ( ) ] ;
9
9
const user_ids = [ mongoose . Types . ObjectId ( ) , mongoose . Types . ObjectId ( ) ] ;
10
+ const lecturer_ids = [ mongoose . Types . ObjectId ( ) , mongoose . Types . ObjectId ( ) ] ;
10
11
const invalid_ids = [ mongoose . Types . ObjectId ( ) , mongoose . Types . ObjectId ( ) ] ;
11
12
12
13
const category = {
@@ -19,22 +20,20 @@ const lessons = [
19
20
{
20
21
_id : lesson_ids [ 0 ] ,
21
22
name : "Lesson1" ,
22
- category_id : category . _id
23
+ category_id : category . _id ,
24
+ lecturer : lecturer_ids [ 0 ]
23
25
} ,
24
26
{
25
27
_id : lesson_ids [ 1 ] ,
26
28
name : "Lesson2" ,
27
- category_id : category . _id
29
+ category_id : category . _id ,
30
+ lecturer : lecturer_ids [ 0 ]
28
31
} ,
29
32
{
30
33
_id : lesson_ids [ 2 ] ,
31
34
name : "Lesson3" ,
32
- category_id : category . _id
33
- } ,
34
- {
35
- _id : lesson_ids [ 3 ] ,
36
- name : "Lesson3" ,
37
- category_id : category . _id
35
+ category_id : category . _id ,
36
+ lecturer : lecturer_ids [ 1 ]
38
37
}
39
38
] ;
40
39
@@ -44,14 +43,16 @@ const users = [
44
43
45
44
password : "password1" ,
46
45
name : "username1" ,
47
- enrolledLessons : [ lesson_ids [ 0 ] , lesson_ids [ 1 ] , lesson_ids [ 2 ] ]
46
+ enrolledLessons : [ lesson_ids [ 0 ] , lesson_ids [ 1 ] , lesson_ids [ 2 ] ] ,
47
+ attendedEvents : [ ]
48
48
} ,
49
49
{
50
50
_id : user_ids [ 1 ] ,
51
51
52
52
password : "password2" ,
53
53
name : "username2" ,
54
- enrolledLessons : [ lesson_ids [ 1 ] , lesson_ids [ 2 ] ]
54
+ enrolledLessons : [ lesson_ids [ 1 ] , lesson_ids [ 2 ] ] ,
55
+ attendedEvents : [ ]
55
56
}
56
57
]
57
58
@@ -63,7 +64,6 @@ const setLessons = async () => {
63
64
await new Lesson ( lessons [ 0 ] ) . save ( ) ;
64
65
await new Lesson ( lessons [ 1 ] ) . save ( ) ;
65
66
await new Lesson ( lessons [ 2 ] ) . save ( ) ;
66
- await new Lesson ( lessons [ 3 ] ) . save ( ) ;
67
67
}
68
68
69
69
const setUsers = async ( ) => {
@@ -72,144 +72,118 @@ const setUsers = async () => {
72
72
}
73
73
74
74
const addDummyData = async ( ) => {
75
- await setCategory ( ) ;
76
- await setLessons ( ) ;
77
- await setUsers ( ) ;
78
- } ;
79
-
80
- describe ( 'POST /user/enroll' , ( ) => {
81
- const enrollUrl = '/user/enroll' ;
82
- beforeEach ( addDummyData ) ;
83
-
84
- it ( 'should return validation error if the user_id is missing in body' , ( done ) => {
85
- request ( app )
86
- . post ( enrollUrl )
87
- . send ( { lesson_id : lesson_ids [ 0 ] } )
88
- . expect ( 400 )
89
- . end ( done ) ;
90
- } ) ;
91
-
92
- it ( 'should return validation error if the lesson_id is missing in body' , ( done ) => {
93
- request ( app )
94
- . post ( enrollUrl )
95
- . send ( { user_id : user_ids [ 0 ] } )
96
- . expect ( 400 )
97
- . end ( done ) ;
98
- } ) ;
99
-
100
- it ( 'should return validation error if the user_id is invalid in body' , ( done ) => {
101
- request ( app )
102
- . post ( enrollUrl )
103
- . send ( {
104
- user_id : "id" ,
105
- lesson_id : lesson_ids [ 0 ]
106
- } )
107
- . expect ( 400 )
108
- . end ( done ) ;
109
- } ) ;
110
-
111
- it ( 'should return validation error if the lesson_id is invalid in body' , ( done ) => {
112
- request ( app )
113
- . post ( enrollUrl )
114
- . send ( {
115
- user_id : user_ids [ 0 ] ,
116
- lesson_id : "55444645161"
117
- } )
118
- . expect ( 400 )
119
- . end ( done ) ;
120
- } ) ;
121
-
122
- it ( 'should return not found error if the user with the given user_id does not exist' , ( done ) => {
123
- request ( app )
124
- . post ( enrollUrl )
125
- . send ( {
126
- user_id : invalid_ids [ 0 ] ,
127
- lesson_id : lesson_ids [ 0 ]
128
- } )
129
- . expect ( 404 )
130
- . end ( done ) ;
131
- } ) ;
132
-
133
- it ( 'should return not found error if the lesson with the given lesson_id does not exist' , ( done ) => {
134
- request ( app )
135
- . post ( enrollUrl )
136
- . send ( {
137
- user_id : user_ids [ 0 ] ,
138
- lesson_id : invalid_ids [ 1 ]
139
- } )
140
- . expect ( 404 )
141
- . end ( done ) ;
142
- } ) ;
143
-
144
- it ( 'should return not found error if the user with the given user_id does take the lesson with the given lesson_id' , ( done ) => {
145
- request ( app )
146
- . post ( enrollUrl )
147
- . send ( {
148
- user_id : user_ids [ 0 ] ,
149
- lesson_id : lesson_ids [ 0 ]
150
- } )
151
- . expect ( 404 )
152
- . end ( done ) ;
153
- } ) ;
154
-
155
- it ( 'should return the user with the given user_id with updated enrolledLessons list and added lesson with the given lesson_id' , ( done ) => {
156
-
157
- request ( app )
158
- . post ( enrollUrl )
159
- . send ( { user_id : user_ids [ 1 ] , lesson_id : lesson_ids [ 0 ] } )
160
- . expect ( ( res ) => {
161
- expect ( res . body . user ) . not . toBeNull ( ) ;
162
- expect ( res . body . user ) . not . toBeUndefined ( ) ;
163
- expect ( res . body . user . email ) . not . toBeNull ( ) ;
164
- expect ( res . body . user . email ) . not . toBeUndefined ( ) ;
165
- expect ( res . body . user . name ) . not . toBeNull ( ) ;
166
- expect ( res . body . user . name ) . not . toBeUndefined ( ) ;
167
- expect ( res . body . user . enrolledLessons ) . not . toBeNull ( ) ;
168
- expect ( res . body . user . enrolledLessons ) . not . toBeUndefined ( ) ;
169
- expect ( res . body . user . enrolledLessons ) . toHaveLength ( 2 ) ;
170
- expect ( res . body . lesson ) . not . toBeNull ( ) ;
171
- expect ( res . body . lesson ) . not . toBeUndefined ( ) ;
172
- expect ( res . body . lesson . id ) . not . toBeNull ( ) ;
173
- expect ( res . body . lesson . id ) . not . toBeUndefined ( ) ;
174
- const firstLesson = res . body . user . enrolledLessons [ 0 ] ;
175
- expect ( firstLesson ) . not . toBeNull ( ) ;
176
- expect ( firstLesson ) . not . toBeUndefined ( ) ;
177
- expect ( res . statusCode ) . toBe ( 200 ) ;
178
- } )
179
- . end ( done ) ;
180
- } ) ;
181
-
182
- it ( 'should return the user with the given user_id with updated enrolledLessons list and added lesson with the given lesson_id' , ( done ) => {
75
+ await setCategory ( ) ;
76
+ await setLessons ( ) ;
77
+ await setUsers ( ) ;
78
+ } ;
79
+
80
+
81
+ describe ( 'POST /user/enroll' , ( ) => {
82
+ const takeUrl = '/user/enroll' ;
83
+ beforeEach ( addDummyData ) ;
183
84
184
- request ( app )
185
- . post ( enrollUrl )
186
- . send ( { user_id : user_ids [ 1 ] , lesson_id : lesson_ids [ 3 ] } )
187
- . expect ( ( res ) => {
188
- expect ( res . body . user ) . not . toBeNull ( ) ;
189
- expect ( res . body . user ) . not . toBeUndefined ( ) ;
190
- expect ( res . body . user . email ) . not . toBeNull ( ) ;
191
- expect ( res . body . user . email ) . not . toBeUndefined ( ) ;
192
- expect ( res . body . user . name ) . not . toBeNull ( ) ;
193
- expect ( res . body . user . name ) . not . toBeUndefined ( ) ;
194
- expect ( res . body . user . enrolledLessons ) . not . toBeNull ( ) ;
195
- expect ( res . body . user . enrolledLessons ) . not . toBeUndefined ( ) ;
196
- expect ( res . body . user . enrolledLessons ) . toHaveLength ( 1 ) ;
197
- expect ( res . body . lesson ) . not . toBeNull ( ) ;
198
- expect ( res . body . lesson ) . not . toBeUndefined ( ) ;
199
- expect ( res . body . lesson . id ) . not . toBeNull ( ) ;
200
- expect ( res . body . lesson . id ) . not . toBeUndefined ( ) ;
201
- const firstLesson = res . body . user . enrolledLessons [ 0 ] ;
202
- expect ( firstLesson ) . not . toBeNull ( ) ;
203
- expect ( firstLesson ) . not . toBeUndefined ( ) ;
204
- expect ( res . statusCode ) . toBe ( 200 ) ;
205
- } )
206
- . end ( done ) ;
207
- } ) ;
208
-
85
+ it ( 'should return validation error if the user_id is missing in body' , ( done ) => {
86
+ request ( app )
87
+ . post ( takeUrl )
88
+ . send ( { lesson_id : lesson_ids [ 0 ] } )
89
+ . expect ( 400 )
90
+ . end ( done ) ;
209
91
} ) ;
210
-
211
- afterEach ( async ( ) => {
212
- await Category . findByIdAndDelete ( category . _id ) ;
213
- await Lesson . deleteMany ( { _id : { $in : lesson_ids } } ) ;
214
- await User . deleteMany ( { _id : { $in : user_ids } } ) ;
92
+
93
+ it ( 'should return validation error if the lesson_id is missing in body' , ( done ) => {
94
+ request ( app )
95
+ . post ( takeUrl )
96
+ . send ( { user_id : user_ids [ 0 ] } )
97
+ . expect ( 400 )
98
+ . end ( done ) ;
215
99
} ) ;
100
+
101
+ it ( 'should return validation error if the user_id is invalid in body' , ( done ) => {
102
+ request ( app )
103
+ . post ( takeUrl )
104
+ . send ( {
105
+ user_id : "id" ,
106
+ lesson_id : lesson_ids [ 0 ]
107
+ } )
108
+ . expect ( 400 )
109
+ . end ( done ) ;
110
+ } ) ;
111
+
112
+ it ( 'should return validation error if the lesson_id is invalid in body' , ( done ) => {
113
+ request ( app )
114
+ . post ( takeUrl )
115
+ . send ( {
116
+ user_id : user_ids [ 0 ] ,
117
+ lesson_id : "55444645161"
118
+ } )
119
+ . expect ( 400 )
120
+ . end ( done ) ;
121
+ } ) ;
122
+
123
+ it ( 'should return not found error if the user with the given user_id does not exist' , ( done ) => {
124
+ request ( app )
125
+ . post ( takeUrl )
126
+ . send ( {
127
+ user_id : invalid_ids [ 0 ] ,
128
+ lesson_id : lesson_ids [ 0 ]
129
+ } )
130
+ . expect ( 404 )
131
+ . end ( done ) ;
132
+ } ) ;
133
+
134
+ it ( 'should return not found error if the lesson with the given lesson_id does not exist' , ( done ) => {
135
+ request ( app )
136
+ . post ( takeUrl )
137
+ . send ( {
138
+ user_id : user_ids [ 0 ] ,
139
+ lesson_id : invalid_ids [ 1 ]
140
+ } )
141
+ . expect ( 404 )
142
+ . end ( done ) ;
143
+ } ) ;
144
+
145
+ it ( 'should return not found error if the user with the given user_id does take the lesson with the given lesson_id' , ( done ) => {
146
+ request ( app )
147
+ . post ( takeUrl )
148
+ . send ( {
149
+ user_id : user_ids [ 1 ] ,
150
+ lesson_id : lesson_ids [ 1 ]
151
+ } )
152
+ . expect ( 404 )
153
+ . end ( done ) ;
154
+ } ) ;
155
+
156
+ it ( 'should return the user with the given user_id with updated enrolledLessons list and added lesson with the given lesson_id' , ( done ) => {
157
+
158
+ request ( app )
159
+ . post ( takeUrl )
160
+ . send ( { user_id : user_ids [ 1 ] , lesson_id : lesson_ids [ 0 ] } )
161
+ . expect ( ( res ) => {
162
+ expect ( res . body . user ) . not . toBeNull ( ) ;
163
+ expect ( res . body . user ) . not . toBeUndefined ( ) ;
164
+ expect ( res . body . user . email ) . not . toBeNull ( ) ;
165
+ expect ( res . body . user . email ) . not . toBeUndefined ( ) ;
166
+ expect ( res . body . user . name ) . not . toBeNull ( ) ;
167
+ expect ( res . body . user . name ) . not . toBeUndefined ( ) ;
168
+ expect ( res . body . user . enrolledLessons ) . not . toBeNull ( ) ;
169
+ expect ( res . body . user . enrolledLessons ) . not . toBeUndefined ( ) ;
170
+ expect ( res . body . user . enrolledLessons ) . toHaveLength ( 3 ) ;
171
+ expect ( res . body . lesson ) . not . toBeNull ( ) ;
172
+ expect ( res . body . lesson ) . not . toBeUndefined ( ) ;
173
+ expect ( res . body . lesson . id ) . not . toBeNull ( ) ;
174
+ expect ( res . body . lesson . id ) . not . toBeUndefined ( ) ;
175
+ const firstLesson = res . body . user . enrolledLessons [ 0 ] ;
176
+ expect ( firstLesson ) . not . toBeNull ( ) ;
177
+ expect ( firstLesson ) . not . toBeUndefined ( ) ;
178
+ expect ( res . statusCode ) . toBe ( 200 ) ;
179
+ } )
180
+ . end ( done ) ;
181
+ } ) ;
182
+
183
+ } ) ;
184
+
185
+ afterEach ( async ( ) => {
186
+ await Category . findByIdAndDelete ( category . _id ) ;
187
+ await Lesson . deleteMany ( { _id : { $in : lesson_ids } } ) ;
188
+ await User . deleteMany ( { _id : { $in : user_ids } } ) ;
189
+ } ) ;
0 commit comments