@@ -4,6 +4,8 @@ import supertest from "supertest";
4
4
import app from "../app" ;
5
5
import Question from "../src/models/Question" ;
6
6
import {
7
+ PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE ,
8
+ PAGE_LIMIT_REQUIRED_MESSAGE ,
7
9
QN_NOT_FOUND_MESSAGE ,
8
10
SERVER_ERROR_MESSAGE ,
9
11
} from "../src/utils/constants" ;
@@ -21,31 +23,147 @@ jest.mock("../src/middlewares/basicAccessControl", () => ({
21
23
} ) ) ;
22
24
23
25
describe ( "Question routes" , ( ) => {
24
- it ( "Delete existing question" , async ( ) => {
25
- const title = faker . lorem . lines ( 1 ) ;
26
- const complexity = "Easy" ;
27
- const categories = [ "Algorithms" ] ;
28
- const description = faker . lorem . lines ( ) ;
29
- const newQuestion = new Question ( {
30
- title,
31
- complexity,
32
- category : categories ,
33
- description,
34
- } ) ;
35
- await newQuestion . save ( ) ;
36
- const res = await request . delete ( `${ BASE_URL } /${ newQuestion . id } ` ) ;
37
- expect ( res . status ) . toBe ( 200 ) ;
26
+ describe ( "GET /" , ( ) => {
27
+ it ( "Reads existing questions" , async ( ) => {
28
+ const qnLimit = 10 ;
29
+ const res = await request . get ( `${ BASE_URL } ?page=1&qnLimit=${ qnLimit } ` ) ;
30
+ expect ( res . status ) . toBe ( 200 ) ;
31
+ expect ( res . body . questions . length ) . toBeLessThanOrEqual ( qnLimit ) ;
32
+ } ) ;
33
+
34
+ it ( "Reads existing questions with title filter" , async ( ) => {
35
+ const qnLimit = 10 ;
36
+ const title = "tree" ;
37
+ const res = await request . get (
38
+ `${ BASE_URL } ?page=1&qnLimit=${ qnLimit } &title=${ title } ` ,
39
+ ) ;
40
+ expect ( res . status ) . toBe ( 200 ) ;
41
+ expect ( res . body . questions . length ) . toBeLessThanOrEqual ( qnLimit ) ;
42
+ for ( const qn of res . body . questions ) {
43
+ expect ( qn . title . toLowerCase ( ) ) . toContain ( title ) ;
44
+ }
45
+ } ) ;
46
+
47
+ it ( "Reads existing questions with complexity filter" , async ( ) => {
48
+ const qnLimit = 10 ;
49
+ const complexity = "Easy" ;
50
+ const res = await request . get (
51
+ `${ BASE_URL } ?page=1&qnLimit=${ qnLimit } &complexities=${ complexity } ` ,
52
+ ) ;
53
+ expect ( res . status ) . toBe ( 200 ) ;
54
+ expect ( res . body . questions . length ) . toBeLessThanOrEqual ( qnLimit ) ;
55
+ for ( const qn of res . body . questions ) {
56
+ expect ( qn . complexity ) . toBe ( complexity ) ;
57
+ }
58
+ } ) ;
59
+
60
+ it ( "Reads existing questions with category filters" , async ( ) => {
61
+ const qnLimit = 10 ;
62
+ const category = "Algorithms" ;
63
+ const res = await request . get (
64
+ `${ BASE_URL } ?page=1&qnLimit=${ qnLimit } &categories=${ category } ` ,
65
+ ) ;
66
+ expect ( res . status ) . toBe ( 200 ) ;
67
+ expect ( res . body . questions . length ) . toBeLessThanOrEqual ( qnLimit ) ;
68
+ for ( const qn of res . body . questions ) {
69
+ expect ( qn . categories ) . toContain ( category ) ;
70
+ }
71
+ } ) ;
72
+
73
+ it ( "Does not read without page" , async ( ) => {
74
+ const res = await request . get ( `${ BASE_URL } ?qnLimit=10` ) ;
75
+ expect ( res . status ) . toBe ( 400 ) ;
76
+ expect ( res . body . message ) . toBe ( PAGE_LIMIT_REQUIRED_MESSAGE ) ;
77
+ } ) ;
78
+
79
+ it ( "Does not read without qnLimit" , async ( ) => {
80
+ const res = await request . get ( `${ BASE_URL } ?page=1` ) ;
81
+ expect ( res . status ) . toBe ( 400 ) ;
82
+ expect ( res . body . message ) . toBe ( PAGE_LIMIT_REQUIRED_MESSAGE ) ;
83
+ } ) ;
84
+
85
+ it ( "Does not read with negative page" , async ( ) => {
86
+ const res = await request . get ( `${ BASE_URL } ?page=-1&qnLimit=10` ) ;
87
+ expect ( res . status ) . toBe ( 400 ) ;
88
+ expect ( res . body . message ) . toBe ( PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE ) ;
89
+ } ) ;
90
+
91
+ it ( "Does not read with negative qnLimit" , async ( ) => {
92
+ const res = await request . get ( `${ BASE_URL } ?page=1&qnLimit=-10` ) ;
93
+ expect ( res . status ) . toBe ( 400 ) ;
94
+ expect ( res . body . message ) . toBe ( PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE ) ;
95
+ } ) ;
96
+ } ) ;
97
+
98
+ describe ( "GET /:id" , ( ) => {
99
+ it ( "Reads existing question" , async ( ) => {
100
+ const title = faker . lorem . lines ( 1 ) ;
101
+ const complexity = "Easy" ;
102
+ const categories = [ "Algorithms" ] ;
103
+ const description = faker . lorem . lines ( ) ;
104
+ const newQuestion = new Question ( {
105
+ title,
106
+ complexity,
107
+ category : categories ,
108
+ description,
109
+ } ) ;
110
+ await newQuestion . save ( ) ;
111
+ const res = await request . get ( `${ BASE_URL } /${ newQuestion . id } ` ) ;
112
+ expect ( res . status ) . toBe ( 200 ) ;
113
+ expect ( res . body . question . title ) . toBe ( title ) ;
114
+ expect ( res . body . question . complexity ) . toBe ( complexity ) ;
115
+ expect ( res . body . question . categories ) . toEqual ( categories ) ;
116
+ expect ( res . body . question . description ) . toBe ( description ) ;
117
+ } ) ;
118
+
119
+ it ( "Does not read non-existing question with invalid object id" , async ( ) => {
120
+ const res = await request . get ( `${ BASE_URL } /blah` ) ;
121
+ expect ( res . status ) . toBe ( 500 ) ;
122
+ expect ( res . body . message ) . toBe ( SERVER_ERROR_MESSAGE ) ;
123
+ } ) ;
124
+
125
+ it ( "Does not read non-existing question with valid object id" , async ( ) => {
126
+ const res = await request . get ( `${ BASE_URL } /66f77e9f27ab3f794bdae664` ) ;
127
+ expect ( res . status ) . toBe ( 404 ) ;
128
+ expect ( res . body . message ) . toBe ( QN_NOT_FOUND_MESSAGE ) ;
129
+ } ) ;
38
130
} ) ;
39
131
40
- it ( "Delete non-existing question with invalid object id" , async ( ) => {
41
- const res = await request . delete ( `${ BASE_URL } /blah` ) ;
42
- expect ( res . status ) . toBe ( 500 ) ;
43
- expect ( res . body . message ) . toBe ( SERVER_ERROR_MESSAGE ) ;
132
+ describe ( "GET /categories" , ( ) => {
133
+ it ( "Reads existing question categories" , async ( ) => {
134
+ const res = await request . get ( `${ BASE_URL } /categories` ) ;
135
+ expect ( res . status ) . toBe ( 200 ) ;
136
+ expect ( res . body ) . toHaveProperty ( "categories" ) ;
137
+ } ) ;
44
138
} ) ;
45
139
46
- it ( "Delete non-existing question with valid object id" , async ( ) => {
47
- const res = await request . delete ( `${ BASE_URL } /66f77e9f27ab3f794bdae664` ) ;
48
- expect ( res . status ) . toBe ( 404 ) ;
49
- expect ( res . body . message ) . toBe ( QN_NOT_FOUND_MESSAGE ) ;
140
+ describe ( "DELETE /:id" , ( ) => {
141
+ it ( "Deletes existing question" , async ( ) => {
142
+ const title = faker . lorem . lines ( 1 ) ;
143
+ const complexity = "Easy" ;
144
+ const categories = [ "Algorithms" ] ;
145
+ const description = faker . lorem . lines ( ) ;
146
+ const newQuestion = new Question ( {
147
+ title,
148
+ complexity,
149
+ category : categories ,
150
+ description,
151
+ } ) ;
152
+ await newQuestion . save ( ) ;
153
+ const res = await request . delete ( `${ BASE_URL } /${ newQuestion . id } ` ) ;
154
+ expect ( res . status ) . toBe ( 200 ) ;
155
+ } ) ;
156
+
157
+ it ( "Does not delete non-existing question with invalid object id" , async ( ) => {
158
+ const res = await request . delete ( `${ BASE_URL } /blah` ) ;
159
+ expect ( res . status ) . toBe ( 500 ) ;
160
+ expect ( res . body . message ) . toBe ( SERVER_ERROR_MESSAGE ) ;
161
+ } ) ;
162
+
163
+ it ( "Does not delete non-existing question with valid object id" , async ( ) => {
164
+ const res = await request . delete ( `${ BASE_URL } /66f77e9f27ab3f794bdae664` ) ;
165
+ expect ( res . status ) . toBe ( 404 ) ;
166
+ expect ( res . body . message ) . toBe ( QN_NOT_FOUND_MESSAGE ) ;
167
+ } ) ;
50
168
} ) ;
51
169
} ) ;
0 commit comments