Skip to content

Commit 59cae01

Browse files
authored
Merge pull request #302 from bounswe/hasancan-code-patch-1
FIXED
2 parents 4f95fd2 + bd1d66b commit 59cae01

File tree

1 file changed

+125
-151
lines changed

1 file changed

+125
-151
lines changed
+125-151
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { expect } from 'expect';
22
import mongoose from 'mongoose';
33
import request from 'supertest';
4-
import { Category, Lesson, User } from '../src/models/index.js';
4+
import { Category, User, Lesson } from '../src/models/index.js';
55
import app from './../src/app.js';
66

77
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()];
99
const user_ids = [mongoose.Types.ObjectId(), mongoose.Types.ObjectId()];
10+
const lecturer_ids = [mongoose.Types.ObjectId(), mongoose.Types.ObjectId()];
1011
const invalid_ids = [mongoose.Types.ObjectId(), mongoose.Types.ObjectId()];
1112

1213
const category = {
@@ -19,22 +20,20 @@ const lessons = [
1920
{
2021
_id: lesson_ids[0],
2122
name: "Lesson1",
22-
category_id: category._id
23+
category_id: category._id,
24+
lecturer: lecturer_ids[0]
2325
},
2426
{
2527
_id: lesson_ids[1],
2628
name: "Lesson2",
27-
category_id: category._id
29+
category_id: category._id,
30+
lecturer: lecturer_ids[0]
2831
},
2932
{
3033
_id: lesson_ids[2],
3134
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]
3837
}
3938
];
4039

@@ -44,14 +43,16 @@ const users = [
4443
4544
password: "password1",
4645
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: []
4848
},
4949
{
5050
_id: user_ids[1],
5151
5252
password: "password2",
5353
name: "username2",
54-
enrolledLessons: [lesson_ids[1],lesson_ids[2]]
54+
enrolledLessons: [lesson_ids[1],lesson_ids[2]],
55+
attendedEvents: []
5556
}
5657
]
5758

@@ -63,7 +64,6 @@ const setLessons = async () => {
6364
await new Lesson(lessons[0]).save();
6465
await new Lesson(lessons[1]).save();
6566
await new Lesson(lessons[2]).save();
66-
await new Lesson(lessons[3]).save();
6767
}
6868

6969
const setUsers = async () => {
@@ -72,144 +72,118 @@ const setUsers = async () => {
7272
}
7373

7474
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);
18384

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);
20991
});
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);
21599
});
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

Comments
 (0)