11import request from 'supertest' ;
22import { app , server } from '../index' ;
3- import { createConnection , getRepository } from 'typeorm' ;
3+ import { getRepository } from 'typeorm' ;
44import { User , UserInterface } from '../entities/User' ;
55
66import { cleanDatabase } from './test-assets/DatabaseCleanup' ;
@@ -9,11 +9,17 @@ import { dbConnection } from '../startups/dbConnection';
99
1010import bcrypt from 'bcrypt' ;
1111import jwt from 'jsonwebtoken' ;
12+ import googleAuth from '../services/userServices/googleAuthservice' ;
13+ import { Request , Response } from 'express' ;
14+
15+ let req : Partial < Request > ;
16+ let res : Partial < Response > ;
1217
1318const userId = uuid ( ) ;
1419const user1Id = uuid ( ) ;
1520const user2Id = uuid ( ) ;
1621const user3Id = uuid ( ) ;
22+ const user4Id = uuid ( ) ;
1723
1824const getAccessToken = ( id : string , email : string ) => {
1925 return jwt . sign (
@@ -86,17 +92,41 @@ const sampleUser3: UserInterface = {
8692 role : 'VENDOR' ,
8793} ;
8894
95+ const sampleUser4 : UserInterface = {
96+ id : user4Id ,
97+ firstName : 'user4' ,
98+ lastName : 'user' ,
99+ 100+ password : '' ,
101+ userType : 'Admin' ,
102+ verified : true ,
103+ twoFactorEnabled : true ,
104+ twoFactorCode : '123456' ,
105+ twoFactorCodeExpiresAt : new Date ( Date . now ( ) + 10 * 60 * 1000 ) ,
106+ gender : 'Male' ,
107+ phoneNumber : '126380996347' ,
108+ photoUrl : 'https://example.com/photo.jpg' ,
109+ role : 'ADMIN' ,
110+ } ;
111+
112+
89113beforeAll ( async ( ) => {
90114 const connection = await dbConnection ( ) ;
91115 sampleUser . password = await bcrypt . hash ( 'password' , 10 ) ;
92116 sampleUser2 . password = await bcrypt . hash ( 'password' , 10 ) ;
93117 sampleUser3 . password = await bcrypt . hash ( 'password' , 10 ) ;
118+ sampleUser4 . password = await bcrypt . hash ( 'password' , 10 ) ;
94119
95120 const userRepository = connection ?. getRepository ( User ) ;
96121 await userRepository ?. save ( { ...sampleUser } ) ;
97122 await userRepository ?. save ( { ...sampleUser1 } ) ;
98123 await userRepository ?. save ( { ...sampleUser2 } ) ;
99124 await userRepository ?. save ( { ...sampleUser3 } ) ;
125+ await userRepository ?. save ( { ...sampleUser4 } ) ;
126+
127+ res = {
128+ redirect : jest . fn ( ) ,
129+ } ;
100130} ) ;
101131
102132afterAll ( async ( ) => {
@@ -131,6 +161,34 @@ describe('User service Test', () => {
131161 } ) ;
132162 } ) ;
133163
164+ it ( 'admin should get all registered user' , async ( ) => {
165+ // Arrange
166+
167+ // Act
168+ const res = await request ( app ) . get ( '/user/allUsers' ) . set (
169+ {
170+ 'authorization' : `Bearer ${ getAccessToken ( sampleUser4 . id ! , sampleUser4 . email ) } `
171+ }
172+ )
173+ // Assert
174+ expect ( res . status ) . toBe ( 200 ) ;
175+ expect ( res . body . users ) . toBeDefined ( ) ;
176+ } ) ;
177+
178+ it ( 'admin should be able to get data for a single user' , async ( ) => {
179+ // Arrange
180+
181+ // Act
182+ const res = await request ( app ) . get ( `/user/single/${ sampleUser . id } ` ) . set (
183+ {
184+ 'authorization' : `Bearer ${ getAccessToken ( sampleUser4 . id ! , sampleUser4 . email ) } `
185+ }
186+ )
187+ // Assert
188+ expect ( res . status ) . toBe ( 200 ) ;
189+ expect ( res . body . user ) . toBeDefined ( ) ;
190+ } ) ;
191+
134192 it ( 'should Login a user, with valid credentials' , async ( ) => {
135193 const res = await request ( app ) . post ( '/user/login' ) . send ( {
136194@@ -533,4 +591,71 @@ describe('User service Test', () => {
533591 expect ( res . body ) . toEqual ( { status : 'error' , message : 'Incorrect email or password' } ) ;
534592 } , 10000 ) ;
535593 } ) ;
594+
595+ describe ( 'google OAuth controller' , ( ) => {
596+ it ( 'should redirect with error status, when something went wrong on server' , async ( ) => {
597+ await googleAuth ( req as Request , res as Response ) ;
598+ expect ( res . redirect ) . toHaveBeenCalledWith ( `${ process . env . CLIENT_URL } /login/google-auth?status=error` ) ;
599+ } ) ;
600+ it ( 'should redirect with success status' , async ( ) => {
601+ req = {
602+ user : {
603+ id : '123' ,
604+ firstName : 'sample' ,
605+ lastName : 'User' ,
606+ 607+ role : 'user' ,
608+ status : 'active' ,
609+ twoFactorEnabled : false ,
610+ phoneNumber : '1234567890' ,
611+ } ,
612+ } ;
613+
614+ await googleAuth ( req as Request , res as Response ) ;
615+ expect ( res . redirect ) . toHaveBeenCalled ( ) ;
616+ } ) ;
617+
618+ it ( 'should redirect with userSuspended status' , async ( ) => {
619+ req = {
620+ user : {
621+ id : '123' ,
622+ firstName : 'sample' ,
623+ lastName : 'User' ,
624+ 625+ role : 'user' ,
626+ status : 'suspended' ,
627+ twoFactorEnabled : false ,
628+ phoneNumber : '1234567890' ,
629+ } ,
630+ } ;
631+
632+ await googleAuth ( req as Request , res as Response ) ;
633+ expect ( res . redirect ) . toHaveBeenCalledWith ( `${ process . env . CLIENT_URL } /login/google-auth?status=userSuspended` ) ;
634+ } ) ;
635+
636+ it ( 'should redirect with otp status' , async ( ) => {
637+ req = {
638+ user : {
639+ id : '123' ,
640+ firstName : 'sample' ,
641+ lastName : 'User' ,
642+ 643+ role : 'user' ,
644+ status : 'active' ,
645+ twoFactorEnabled : true ,
646+ phoneNumber : '1234567890' ,
647+ } ,
648+ } ;
649+
650+ await googleAuth ( req as Request , res as Response ) ;
651+ expect ( res . redirect ) . toHaveBeenCalledWith ( `${ process . env . CLIENT_URL } /login/google-auth?status=otp&[email protected] ` ) ; 652+ } ) ;
653+
654+ it ( 'should redirect with userNotFound status' , async ( ) => {
655+ req . user = undefined ;
656+ await googleAuth ( req as Request , res as Response ) ;
657+ expect ( res . redirect ) . toHaveBeenCalledWith ( `${ process . env . CLIENT_URL } /login/google-auth?status=userNotFound` ) ;
658+ } ) ;
659+
660+ } ) ;
536661} ) ;
0 commit comments