@@ -73,7 +73,230 @@ describe('Movie Management System (e2e)', () => {
73
73
} ) ;
74
74
} ) ;
75
75
76
-
76
+ describe ( 'Authentication' , ( ) => {
77
+ it ( 'should authenticate manager and provide token' , async ( ) => {
78
+ const response = await request ( app . getHttpServer ( ) )
79
+ . post ( '/auth/login' )
80
+ . send ( {
81
+ username : 'testmanager' ,
82
+ password : 'Manager123!'
83
+ } )
84
+ . expect ( 200 ) ;
85
+
86
+ expect ( response . body ) . toHaveProperty ( 'accessToken' ) ;
87
+ managerToken = response . body . accessToken ;
88
+ } ) ;
89
+
90
+ it ( 'should authenticate customer and provide token' , async ( ) => {
91
+ const response = await request ( app . getHttpServer ( ) )
92
+ . post ( '/auth/login' )
93
+ . send ( {
94
+ username : 'testcustomer' ,
95
+ password : 'Customer123!'
96
+ } )
97
+ . expect ( 200 ) ;
98
+
99
+ expect ( response . body ) . toHaveProperty ( 'accessToken' ) ;
100
+ customerToken = response . body . accessToken ;
101
+ } ) ;
102
+
103
+ it ( 'should reject invalid credentials' , async ( ) => {
104
+ await request ( app . getHttpServer ( ) )
105
+ . post ( '/auth/login' )
106
+ . send ( {
107
+ username : 'testmanager' ,
108
+ password : 'wrongpassword'
109
+ } )
110
+ . expect ( 401 ) ;
111
+ } ) ;
112
+ } ) ;
113
+ } ) ;
114
+
115
+ describe ( 'Movie Management' , ( ) => {
116
+ describe ( 'Create and Manage Movies' , ( ) => {
117
+ it ( 'should allow manager to create a movie' , async ( ) => {
118
+ const response = await request ( app . getHttpServer ( ) )
119
+ . post ( '/movies' )
120
+ . set ( 'Authorization' , `Bearer ${ managerToken } ` )
121
+ . send ( {
122
+ name : 'Test Movie' ,
123
+ ageRestriction : 12
124
+ } )
125
+ . expect ( 201 ) ;
126
+
127
+ expect ( response . body ) . toHaveProperty ( 'id' ) ;
128
+ expect ( response . body . name ) . toBe ( 'Test Movie' ) ;
129
+ movieId = response . body . id ;
130
+ } ) ;
131
+
132
+ it ( 'should prevent customer from creating movies' , async ( ) => {
133
+ await request ( app . getHttpServer ( ) )
134
+ . post ( '/movies' )
135
+ . set ( 'Authorization' , `Bearer ${ customerToken } ` )
136
+ . send ( {
137
+ name : 'Customer Movie' ,
138
+ ageRestriction : 12
139
+ } )
140
+ . expect ( 403 ) ;
141
+ } ) ;
142
+
143
+ it ( 'should allow manager to add a session' , async ( ) => {
144
+ const response = await request ( app . getHttpServer ( ) )
145
+ . post ( `/movies/${ movieId } /sessions` )
146
+ . set ( 'Authorization' , `Bearer ${ managerToken } ` )
147
+ . send ( {
148
+ date : '2025-03-01' ,
149
+ timeSlot : '14:00-16:00' ,
150
+ roomNumber : 1 ,
151
+ availableSeats : 100
152
+ } )
153
+ . expect ( 201 ) ;
154
+
155
+ expect ( response . body . sessions ) . toHaveLength ( 1 ) ;
156
+ sessionId = response . body . sessions [ 0 ] . id ;
157
+ } ) ;
158
+
159
+ it ( 'should prevent double-booking of rooms' , async ( ) => {
160
+ await request ( app . getHttpServer ( ) )
161
+ . post ( `/movies/${ movieId } /sessions` )
162
+ . set ( 'Authorization' , `Bearer ${ managerToken } ` )
163
+ . send ( {
164
+ date : '2025-03-01' ,
165
+ timeSlot : '14:00-16:00' ,
166
+ roomNumber : 1 ,
167
+ availableSeats : 100
168
+ } )
169
+ . expect ( 409 ) ;
170
+ } ) ;
171
+ } ) ;
172
+
173
+ describe ( 'View Movies' , ( ) => {
174
+ it ( 'should list all available movies' , async ( ) => {
175
+ const response = await request ( app . getHttpServer ( ) )
176
+ . get ( '/movies' )
177
+ . set ( 'Authorization' , `Bearer ${ customerToken } ` )
178
+ . expect ( 200 ) ;
77
179
78
- } )
180
+ expect ( Array . isArray ( response . body ) ) . toBe ( true ) ;
181
+ expect ( response . body . length ) . toBeGreaterThan ( 0 ) ;
182
+ expect ( response . body [ 0 ] ) . toHaveProperty ( 'sessions' ) ;
183
+ } ) ;
184
+
185
+ it ( 'should allow sorting movies' , async ( ) => {
186
+ await request ( app . getHttpServer ( ) )
187
+ . get ( '/movies?sortBy=name&sortOrder=ASC' )
188
+ . set ( 'Authorization' , `Bearer ${ customerToken } ` )
189
+ . expect ( 200 ) ;
190
+ } ) ;
191
+ } ) ;
192
+ } ) ;
193
+
194
+ describe ( 'Ticket System' , ( ) => {
195
+ describe ( 'Purchase and Use Tickets' , ( ) => {
196
+ it ( 'should allow customer to buy a ticket' , async ( ) => {
197
+ const response = await request ( app . getHttpServer ( ) )
198
+ . post ( '/tickets/buy' )
199
+ . set ( 'Authorization' , `Bearer ${ customerToken } ` )
200
+ . send ( {
201
+ movieId,
202
+ sessionId
203
+ } )
204
+ . expect ( 201 ) ;
205
+
206
+ expect ( response . body ) . toHaveProperty ( 'id' ) ;
207
+ ticketId = response . body . id ;
208
+ } ) ;
209
+
210
+ it ( 'should prevent duplicate ticket purchase' , async ( ) => {
211
+ await request ( app . getHttpServer ( ) )
212
+ . post ( '/tickets/buy' )
213
+ . set ( 'Authorization' , `Bearer ${ customerToken } ` )
214
+ . send ( {
215
+ movieId,
216
+ sessionId
217
+ } )
218
+ . expect ( 409 ) ;
219
+ } ) ;
220
+
221
+ it ( 'should allow customer to use a ticket' , async ( ) => {
222
+ const response = await request ( app . getHttpServer ( ) )
223
+ . post ( `/tickets/${ ticketId } /use` )
224
+ . set ( 'Authorization' , `Bearer ${ customerToken } ` )
225
+ . expect ( 200 ) ;
226
+
227
+ expect ( response . body ) . toHaveProperty ( 'usedDate' ) ;
228
+ } ) ;
229
+
230
+ it ( 'should prevent using the same ticket twice' , async ( ) => {
231
+ await request ( app . getHttpServer ( ) )
232
+ . post ( `/tickets/${ ticketId } /use` )
233
+ . set ( 'Authorization' , `Bearer ${ customerToken } ` )
234
+ . expect ( 409 ) ;
235
+ } ) ;
236
+ } ) ;
237
+
238
+ describe ( 'View Ticket History' , ( ) => {
239
+ it ( 'should show customer watch history' , async ( ) => {
240
+ const response = await request ( app . getHttpServer ( ) )
241
+ . get ( '/tickets/history' )
242
+ . set ( 'Authorization' , `Bearer ${ customerToken } ` )
243
+ . expect ( 200 ) ;
244
+
245
+ expect ( Array . isArray ( response . body ) ) . toBe ( true ) ;
246
+ expect ( response . body . length ) . toBeGreaterThan ( 0 ) ;
247
+ expect ( response . body [ 0 ] ) . toHaveProperty ( 'usedDate' ) ;
248
+ } ) ;
249
+
250
+ it ( 'should show unused tickets' , async ( ) => {
251
+ const response = await request ( app . getHttpServer ( ) )
252
+ . get ( '/tickets/unused' )
253
+ . set ( 'Authorization' , `Bearer ${ customerToken } ` )
254
+ . expect ( 200 ) ;
255
+
256
+ expect ( Array . isArray ( response . body ) ) . toBe ( true ) ;
257
+ } ) ;
258
+ } ) ;
259
+ } ) ;
260
+
261
+ describe ( 'Age Restrictions' , ( ) => {
262
+ it ( 'should prevent underage customers from viewing restricted movies' , async ( ) => {
263
+ // Create a movie with high age restriction
264
+ const restrictedMovie = await request ( app . getHttpServer ( ) )
265
+ . post ( '/movies' )
266
+ . set ( 'Authorization' , `Bearer ${ managerToken } ` )
267
+ . send ( {
268
+ name : 'Adult Movie' ,
269
+ ageRestriction : 21
270
+ } )
271
+ . expect ( 201 ) ;
272
+
273
+ // Try to get details as underage customer
274
+ await request ( app . getHttpServer ( ) )
275
+ . get ( `/movies/${ restrictedMovie . body . id } ` )
276
+ . set ( 'Authorization' , `Bearer ${ customerToken } ` )
277
+ . expect ( 403 ) ;
278
+ } ) ;
279
+ } ) ;
280
+
281
+ describe ( 'Error Handling' , ( ) => {
282
+ it ( 'should handle invalid movie IDs' , async ( ) => {
283
+ await request ( app . getHttpServer ( ) )
284
+ . get ( '/movies/invalid-id' )
285
+ . set ( 'Authorization' , `Bearer ${ customerToken } ` )
286
+ . expect ( 404 ) ;
287
+ } ) ;
288
+
289
+ it ( 'should handle invalid ticket IDs' , async ( ) => {
290
+ await request ( app . getHttpServer ( ) )
291
+ . post ( '/tickets/invalid-id/use' )
292
+ . set ( 'Authorization' , `Bearer ${ customerToken } ` )
293
+ . expect ( 404 ) ;
294
+ } ) ;
295
+
296
+ it ( 'should handle unauthorized access' , async ( ) => {
297
+ await request ( app . getHttpServer ( ) )
298
+ . get ( '/movies' )
299
+ . expect ( 401 ) ;
300
+ } ) ;
301
+ } ) ;
79
302
} ) ;
0 commit comments