@@ -275,7 +275,6 @@ describe('AppController (e2e)', () => {
275275 . post ( '/graphql' )
276276 . send ( { query : registerMutation , variables } ) ;
277277
278- console . log ( response . body ) ;
279278 // Assert the response
280279 expect ( response . status ) . toBe ( 200 ) ;
281280 expect ( response . body . errors ) . toBeFalsy ( ) ;
@@ -307,7 +306,7 @@ describe('AppController (e2e)', () => {
307306 input : {
308307 email : usersData [ 0 ] . email ,
309308 name : 'New User' ,
310- password :'Test123!' ,
309+ password : 'Test123!' ,
311310 } ,
312311 } ;
313312
@@ -327,5 +326,102 @@ describe('AppController (e2e)', () => {
327326 expect ( newUser ) . toBeFalsy ( ) ;
328327 } ) ;
329328 } ) ;
329+
330+ describe ( 'Registration and Login' , ( ) => {
331+ it ( 'should register a new user and then log in with the credentials' , async ( ) => {
332+ // Define the mutation payload for registration
333+ const registerMutation = `
334+ mutation Register($input: RegisterUserInput!) {
335+ register(input: $input) {
336+ user {
337+ id
338+ name
339+ }
340+ token
341+ }
342+ }
343+ ` ;
344+ const variables = {
345+ input : {
346+ email : 'newuser@example.com' ,
347+ name : 'New User' ,
348+ password : 'StrongPassword1!' ,
349+ } ,
350+ } ;
351+
352+ // Send the registration mutation request and check the response
353+ const registerResponse = await request ( app . getHttpServer ( ) )
354+ . post ( '/graphql' )
355+ . send ( { query : registerMutation , variables } ) ;
356+
357+ // Assert the response
358+ expect ( registerResponse . status ) . toBe ( 200 ) ;
359+ expect ( registerResponse . body . errors ) . toBeFalsy ( ) ;
360+ expect ( registerResponse . body . data . register . user . name ) . toBe ( 'New User' ) ;
361+ expect ( registerResponse . body . data . register . token ) . toBeTruthy ( ) ;
362+
363+ // Define the mutation payload for login
364+ const loginMutation = `
365+ mutation Login($input: LoginUserInput!) {
366+ login(input: $input) {
367+ user {
368+ id
369+ name
370+ }
371+ token
372+ }
373+ }
374+ ` ;
375+ const loginVariables = {
376+ input : {
377+ email : 'newuser@example.com' ,
378+ password : 'StrongPassword1!' ,
379+ } ,
380+ } ;
381+
382+ // Send the login mutation request and check the response
383+ const loginResponse = await request ( app . getHttpServer ( ) )
384+ . post ( '/graphql' )
385+ . send ( { query : loginMutation , variables : loginVariables } ) ;
386+
387+ // Assert the response
388+ expect ( loginResponse . status ) . toBe ( 200 ) ;
389+ expect ( loginResponse . body . errors ) . toBeFalsy ( ) ;
390+ expect ( loginResponse . body . data . login . user . name ) . toBe ( 'New User' ) ;
391+ expect ( loginResponse . body . data . login . token ) . toBeTruthy ( ) ;
392+ } ) ;
393+
394+ it ( 'should return an error if the password is not strong enough' , async ( ) => {
395+ // Define the mutation payload for registration with a weak password
396+ const registerMutation = `
397+ mutation Register($input: RegisterUserInput!) {
398+ register(input: $input) {
399+ user {
400+ id
401+ name
402+ }
403+ token
404+ }
405+ }
406+ ` ;
407+ const variables = {
408+ input : {
409+ email : 'newuser@example.com' ,
410+ name : 'New User' ,
411+ password : 'weakpassword' ,
412+ } ,
413+ } ;
414+
415+ // Send the registration mutation request and check the response
416+ const response = await request ( app . getHttpServer ( ) )
417+ . post ( '/graphql' )
418+ . send ( { query : registerMutation , variables } ) ;
419+
420+ // Assert the response
421+ expect ( response . status ) . toBe ( 200 ) ;
422+ expect ( response . body . errors [ 0 ] . extensions . code ) . toBe ( 'BAD_REQUEST' ) ;
423+ } ) ;
424+ } ) ;
425+
330426} ) ;
331427
0 commit comments