import * as request from 'supertest' ; import { Test , TestingModule } from '@nestjs/testing' ; import { INestApplication , ValidationPipe } from '@nestjs/common' ; import { AppModule } from '../../src/app.module' ; import { Connection , Repository } from 'typeorm' ; import { getRepositoryToken } from '@nestjs/typeorm' ; import { initializeTransactionalContext } from 'typeorm-transactional-cls-hooked' ; import { Role } from '../../src/SecurityModule/entity/role.entity' ; import { RoleEnum } from '../../src/SecurityModule/enum/role.enum' ; import { ClientCredentials } from '../../src/SecurityModule/entity/client-credentials.entity' ; import { ClientCredentialsEnum } from '../../src/SecurityModule/enum/client-credentials.enum' ; import { GrantTypeEnum } from '../../src/SecurityModule/enum/grant-type.enum' ; import { Constants } from '../../src/CommonsModule/constants' ; import { NewUserDTO } from '../../src/UserModule/dto/new-user.dto' ; import { CourseTaken } from '../../src/CourseModule/entity/course-taken.entity' ; import { GenderEnum } from '../../src/UserModule/enum/gender.enum' ; import { EscolarityEnum } from '../../src/UserModule/enum/escolarity.enum' ; import { UserService } from '../../src/UserModule/service/user.service' ; import { CourseTakenStatusEnum } from '../../src/CourseModule/enum/course-taken-status.enum' ; import { UserProfileEnum } from '../../src/UserModule/enum/user-profile.enum' ; import { OrderEnum } from '../../src/CommonsModule/enum/order.enum' ; import { REQUEST } from '@nestjs/core' ; import { RoleGuard } from '../../src/CommonsModule/guard/role.guard' ; const stringToBase64 = ( string ) => { return Buffer . from ( string ) . toString ( 'base64' ) ; } ; const roleGuardMock = { CanActivate : true } ; describe ( 'DashboardController (e2e)' , ( ) => { let app ; let moduleFixture ; let authorization ; const dashboardUrl = `/${ Constants . API_PREFIX }/${ Constants . API_VERSION_1 }/${ Constants . DASHBOARD_ENDPOINT }` ; const adminRoleEnum = RoleEnum . ADMIN ; let courseTakenRepository ; let dbConnection ; beforeAll ( async ( ) => { moduleFixture = await Test . createTestingModule ( { imports : [ AppModule ] , } ) . overrideProvider ( REQUEST ) . useValue ( { headers : { authorization : 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJj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owNS40NTdaIiwidmVyc2lvbiI6MSwiaWQiOiIxMDJmYmY4Ni1iNmVlLTQwMWYtODViNC0wZDM3MTNkMjkzOGYiLCJuYW1lIjoiQURNSU4ifSwiaWF0IjoxNjAxMzQ0MjYxLCJleHAiOjE2MDEzNDc4NjF9.w9EI130lFaR4aEIw6EcnvEW7nmCsiKK2FTD9e9cPRig' , } , } ) . overrideGuard ( RoleGuard ) . useValue ( roleGuardMock ) . compile ( ) ; initializeTransactionalContext ( ) ; app = moduleFixture . createNestApplication ( ) ; app . useGlobalPipes ( new ValidationPipe ( ) ) ; await app . init ( ) ; dbConnection = moduleFixture . get ( Connection ) ; await dbConnection . synchronize ( true ) ; const courseTakenRepository = moduleFixture . get < Repository < CourseTaken > > ( getRepositoryToken ( CourseTaken ) ) ; const userService = moduleFixture . get < UserService > ( UserService ) ; const roleRepository = moduleFixture . get < Repository < Role > > ( getRepositoryToken ( Role ) ) ; let roleAdmin = await roleRepository . findOne ( { name : RoleEnum . ADMIN } ) ; if ( ! roleAdmin ) { const role = new Role ( ) ; role . name = RoleEnum . ADMIN ; roleAdmin = await roleRepository . save ( role ) ; } const clientCredentialRepository = moduleFixture . get < Repository < ClientCredentials > > ( getRepositoryToken ( ClientCredentials ) ) ; let clientCredentials = await clientCredentialRepository . findOne ( { name : ClientCredentialsEnum [ 'NEWSCHOOL@FRONT' ] , } ) ; if ( ! clientCredentials ) { clientCredentials = new ClientCredentials ( ) ; clientCredentials . name = ClientCredentialsEnum [ 'NEWSCHOOL@FRONT' ] ; clientCredentials . secret = 'test2' ; clientCredentials . role = roleAdmin ; clientCredentials . authorizedGrantTypes = [ GrantTypeEnum . CLIENT_CREDENTIALS , ] ; clientCredentials . accessTokenValidity = 3600 ; clientCredentials . refreshTokenValidity = 3600 ; await clientCredentialRepository . save ( clientCredentials ) ; } authorization = stringToBase64 ( `${ clientCredentials . name }:${ clientCredentials . secret }` ) ; const newUser = { email : 'my-user1@email.com' , password : 'mypass' , profile : UserProfileEnum . STUDENT , urlInstagram : 'instagram' , urlFacebook : 'facebook' , name : 'name' , nickname : 'random nickname' , gender : GenderEnum . MALE , schooling : EscolarityEnum . ENSINO_FUNDAMENTAL_COMPLETO , profession : 'random profession' , birthday : new Date ( ) , address : 'random adress' , institutionName : 'random institution' , role : adminRoleEnum , } ; const addedUser = await userService . add ( newUser ) ; const newUser2 = { email : 'my-user2@email.com' , password : 'mypass' , profile : UserProfileEnum . STUDENT , urlInstagram : 'instagram' , urlFacebook : 'facebook' , name : 'name' , nickname : 'random nickname' , gender : GenderEnum . MALE , schooling : EscolarityEnum . ENSINO_FUNDAMENTAL_COMPLETO , profession : 'random profession' , birthday : new Date ( ) , address : 'random adress' , institutionName : 'random institution' , role : adminRoleEnum , } ; const addedUser2 = await userService . add ( newUser2 ) ; const addedCourse = { id : new Date ( ) . getTime ( ) + Math . floor ( Math . random ( ) * 100 ) , } ; const addedCourse2 = { id : new Date ( ) . getTime ( ) + Math . floor ( Math . random ( ) * 100 ) , } ; const newCourseTaken = { userId : addedUser . id , courseId : addedCourse . id , currentLessonId : 1 , currentPartId : 2 , } ; await courseTakenRepository . save ( { ... newCourseTaken , completion : 0 , status : CourseTakenStatusEnum . TAKEN , } ) ; const newCourseTaken2 = { userId : addedUser . id , courseId : addedCourse2 . id , currentLessonId : 1 , currentPartId : 2 , } ; await courseTakenRepository . save ( { ... newCourseTaken2 , completion : 100 , status : CourseTakenStatusEnum . COMPLETED , challenge : 'random challenge' , } ) ; const newCourseTaken3 = { userId : addedUser2 . id , courseId : addedCourse2 . id , currentLessonId : 1 , currentPartId : 2 , } ; await courseTakenRepository . save ( { ... newCourseTaken3 , completion : 100 , status : CourseTakenStatusEnum . COMPLETED , challenge : 'random challenge' , } ) ; } ) ; it ( 'should have 2 as quantity of certificates created' , async ( ) => { const authRes = await request ( app . getHttpServer ( ) ) . post ( '/oauth/token' ) . set ( 'Authorization' , `Basic ${ authorization }` ) . set ( 'Content-Type' , 'multipart/form-data' ) . field ( 'grant_type' , GrantTypeEnum . CLIENT_CREDENTIALS ) ; const dashboardRes = await request ( app . getHttpServer ( ) ) . get ( `${ dashboardUrl }/certificate/quantity` ) . set ( 'Authorization' , `Bearer ${ authRes . body . accessToken }` ) . expect ( 200 ) ; expect ( dashboardRes . body . totalElements ) . toEqual ( 2 ) ; } ) ; it ( 'should return 2 as quantity of users on courses' , async ( ) => { const authRes = await request ( app . getHttpServer ( ) ) . post ( '/oauth/token' ) . set ( 'Authorization' , `Basic ${ authorization }` ) . set ( 'Content-Type' , 'multipart/form-data' ) . field ( 'grant_type' , GrantTypeEnum . CLIENT_CREDENTIALS ) ; const dashboardRes = await request ( app . getHttpServer ( ) ) . get ( `${ dashboardUrl }/course-taken/user/quantity` ) . set ( 'Authorization' , `Bearer ${ authRes . body . accessToken }` ) . expect ( 200 ) ; expect ( dashboardRes . body . totalElements ) . toEqual ( 2 ) ; } ) ; it ( 'should return 1 as quantity of users that has TAKEN status' , async ( ) => { const authRes = await request ( app . getHttpServer ( ) ) . post ( '/oauth/token' ) . set ( 'Authorization' , `Basic ${ authorization }` ) . set ( 'Content-Type' , 'multipart/form-data' ) . field ( 'grant_type' , GrantTypeEnum . CLIENT_CREDENTIALS ) ; const dashboardRes = await request ( app . getHttpServer ( ) ) . get ( `${ dashboardUrl }/course-taken/user/quantity?status=${ CourseTakenStatusEnum . TAKEN }` ) . set ( 'Authorization' , `Bearer ${ authRes . body . accessToken }` ) . expect ( 200 ) ; expect ( dashboardRes . body . totalElements ) . toEqual ( 1 ) ; } ) ; it ( 'should return 2 as quantity of users that has COMPLETED status' , async ( ) => { const authRes = await request ( app . getHttpServer ( ) ) . post ( '/oauth/token' ) . set ( 'Authorization' , `Basic ${ authorization }` ) . set ( 'Content-Type' , 'multipart/form-data' ) . field ( 'grant_type' , GrantTypeEnum . CLIENT_CREDENTIALS ) ; const dashboardRes = await request ( app . getHttpServer ( ) ) . get ( `${ dashboardUrl }/course-taken/user/quantity?status=${ CourseTakenStatusEnum . COMPLETED }` ) . set ( 'Authorization' , `Bearer ${ authRes . body . accessToken }` ) . expect ( 200 ) ; expect ( dashboardRes . body . totalElements ) . toEqual ( 2 ) ; } ) ; it ( 'should return courses by views' , async ( ) => { const authRes = await request ( app . getHttpServer ( ) ) . post ( '/oauth/token' ) . set ( 'Authorization' , `Basic ${ authorization }` ) . set ( 'Content-Type' , 'multipart/form-data' ) . field ( 'grant_type' , GrantTypeEnum . CLIENT_CREDENTIALS ) ; const coursesByFrequence = await request ( app . getHttpServer ( ) ) . get ( `${ dashboardUrl }/course/views` ) . query ( { order : OrderEnum . ASC , limit : 5 } ) . set ( 'Authorization' , `Bearer ${ authRes . body . accessToken }` ) . expect ( 200 ) ; expect ( coursesByFrequence . body . length ) . toBeLessThanOrEqual ( 5 ) ; } ) ; afterAll ( async ( ) => { await app . close ( ) ; } ) ; } ) ;