Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/review test courses #154

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:e2e": "jest --config ./test/jest-e2e.config.js --runInBand"
},
"dependencies": {
"@nest-modules/mailer": "^1.1.3",
Expand Down
5 changes: 3 additions & 2 deletions src/CourseModule/controllers/course.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ export class CourseController {
summary: 'Update course',
description: 'Update course by id',
})
@ApiBody({ type: CourseUpdateDTO })
@NeedRole(RoleEnum.ADMIN)
@UseGuards(RoleGuard)
public async update(
@Param('id') id: CourseDTO['id'],
@Body() courseUpdatedInfo: CourseUpdateDTO,
) {
return await this.service.update(id, courseUpdatedInfo);
): Promise<CourseDTO> {
return this.mapper.toDto(await this.service.update(id, courseUpdatedInfo));
}

@Delete(':id')
Expand Down
10 changes: 10 additions & 0 deletions src/CourseModule/dto/course-update.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ export class CourseUpdateDTO {
@Min(1)
@Expose()
workload: Course['workload'];

@IsNotEmpty()
@IsString()
@Expose()
authorName: Course['authorName'];

@IsNotEmpty()
@IsString()
@Expose()
authorDescription: Course['authorDescription'];
}
181 changes: 130 additions & 51 deletions test/CourseModule/course.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import * as request from 'supertest';
import * as fs from 'fs';
import * as util from 'util';
import * as path from 'path';
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { AppModule } from '../../src/app.module';
import { Connection, EntityManager, QueryRunner, Repository } from 'typeorm';
import { ClientCredentials, Role } from '../../src/SecurityModule/entity';
import { Course } from '../../src/CourseModule/entity';
import { getRepositoryToken } from '@nestjs/typeorm';
import {
ClientCredentialsEnum,
GrantTypeEnum,
RoleEnum,
} from '../../src/SecurityModule/enum';
import { Constants } from '../../src/CommonsModule';
import { NewCourseDTO, CourseUpdateDTO } from 'src/CourseModule/dto';
import { NewCourseDTO } from 'src/CourseModule/dto';
import { initializeTransactionalContext } from 'typeorm-transactional-cls-hooked';

const stringToBase64 = (string: string) => {
return Buffer.from(string).toString('base64');
};

const fileToUpload = path.resolve(
path.join(__dirname, '..', '..', 'README.md'),
);

describe('CourseController (e2e)', () => {
let app: INestApplication;
let moduleFixture: TestingModule;
Expand Down Expand Up @@ -71,19 +79,28 @@ describe('CourseController (e2e)', () => {
.set('Content-Type', 'multipart/form-data')
.field('grant_type', GrantTypeEnum.CLIENT_CREDENTIALS)
.then(res => {
const newCourse = {
title: 'Teste E3E to add',
thumbUrl: 'http://teste.com/thumb.png',
authorName: 'Test',
authorDescription: 'Test description',
description: 'Este é um registro de teste',
workload: 1,
} as NewCourseDTO;
return request(app.getHttpServer())
.post(courseUrl)
.set('Authorization', `Bearer ${res.body.accessToken}`)
.send({
title: 'Teste E3E',
thumbUrl: 'http://teste.com/thumb.png',
authorId: '1',
description: 'Este é um registro de teste',
} as NewCourseDTO)
.field('title', newCourse.title)
.field('thumbUrl', newCourse.thumbUrl)
.field('authorName', newCourse.authorName)
.field('authorDescription', newCourse.authorDescription)
.field('description', newCourse.description)
.field('workload', newCourse.workload)
.attach('photo', fileToUpload)
.expect(201)
.expect(res => {
expect(res.body.id).not.toBeUndefined();
expect(res.body.slug).toBe('teste-e3e');
expect(res.body.slug).toBe('teste-e3e-to-add');
})
.then(() => done());
});
Expand Down Expand Up @@ -113,15 +130,24 @@ describe('CourseController (e2e)', () => {
.set('Content-Type', 'multipart/form-data')
.field('grant_type', GrantTypeEnum.CLIENT_CREDENTIALS)
.then(res => {
const newCourse = {
title: 'Teste E3E2',
thumbUrl: 'http://teste.com/thumb.png',
authorName: 'Teste',
authorDescription: 'Test description',
description: 'teste 2',
workload: 1,
} as NewCourseDTO;
return request(app.getHttpServer())
.post(courseUrl)
.set('Authorization', `Bearer ${res.body.accessToken}`)
.send({
title: 'Teste E3E2',
thumbUrl: 'http://teste.com/thumb.png',
authorId: '1',
description: 'teste 2',
} as NewCourseDTO)
.field('title', newCourse.title)
.field('thumbUrl', newCourse.thumbUrl)
.field('authorName', newCourse.authorName)
.field('authorDescription', newCourse.authorDescription)
.field('description', newCourse.description)
.field('workload', newCourse.workload)
.attach('photo', fileToUpload)
.then(_res => {
return request(app.getHttpServer())
.get(`${courseUrl}/${_res.body.id}`)
Expand All @@ -141,15 +167,24 @@ describe('CourseController (e2e)', () => {
.set('Content-Type', 'multipart/form-data')
.field('grant_type', GrantTypeEnum.CLIENT_CREDENTIALS)
.then(res => {
const newCourse = {
title: 'Teste E3E3',
thumbUrl: 'http://teste.com/thumb.png',
authorName: 'Teste',
authorDescription: 'Test description',
description: 'teste 2',
workload: 1,
} as NewCourseDTO;
return request(app.getHttpServer())
.post(courseUrl)
.set('Authorization', `Bearer ${res.body.accessToken}`)
.send({
title: 'Teste E3E3',
thumbUrl: 'http://teste.com/thumb.png',
authorId: '1',
description: 'teste 2',
} as NewCourseDTO)
.field('title', newCourse.title)
.field('thumbUrl', newCourse.thumbUrl)
.field('authorName', newCourse.authorName)
.field('authorDescription', newCourse.authorDescription)
.field('description', newCourse.description)
.field('workload', newCourse.workload)
.attach('photo', fileToUpload)
.then(_res => {
return request(app.getHttpServer())
.get(`${courseUrl}/slug/${_res.body.slug}`)
Expand All @@ -170,24 +205,13 @@ describe('CourseController (e2e)', () => {
.field('grant_type', GrantTypeEnum.CLIENT_CREDENTIALS)
.then(res => {
return request(app.getHttpServer())
.post(courseUrl)
.get(`${courseUrl}/slug/randomSlug`)
.set('Authorization', `Bearer ${res.body.accessToken}`)
.send({
title: 'Teste E3E4',
thumbUrl: 'http://teste.com/thumb.png',
authorId: '1',
description: 'teste 2',
} as NewCourseDTO)
.then(() => {
return request(app.getHttpServer())
.get(`${courseUrl}/slug/randomSlug`)
.set('Authorization', `Bearer ${res.body.accessToken}`)
.expect(404);
});
.expect(404);
});
});

it('should return 404 if ID doesnt exist', async done => {
it('should return 404 if ID doesnt exist', async () => {
return request(app.getHttpServer())
.post('/oauth/token')
.set('Authorization', `Basic ${authorization}`)
Expand All @@ -199,8 +223,7 @@ describe('CourseController (e2e)', () => {
.set('Accept', 'application/json')
.set('Authorization', `Bearer ${res.body.accessToken}`)
.expect('Content-Type', /json/)
.expect(404)
.then(() => done());
.expect(404);
});
});

Expand All @@ -211,15 +234,24 @@ describe('CourseController (e2e)', () => {
.set('Content-Type', 'multipart/form-data')
.field('grant_type', GrantTypeEnum.CLIENT_CREDENTIALS)
.then(res => {
const newCourse = {
title: 'Teste E3E to delete',
thumbUrl: 'http://teste.com/thumb.png',
authorName: 'Teste',
authorDescription: 'Teste',
description: 'Este é um registro de teste',
workload: 1,
} as NewCourseDTO;
return request(app.getHttpServer())
.post(courseUrl)
.set('Authorization', `Bearer ${res.body.accessToken}`)
.send({
title: 'Teste E3E',
thumbUrl: 'http://teste.com/thumb.png',
authorId: '1',
description: 'Este é um registro de teste',
} as NewCourseDTO)
.field('title', newCourse.title)
.field('thumbUrl', newCourse.thumbUrl)
.field('authorName', newCourse.authorName)
.field('authorDescription', newCourse.authorDescription)
.field('description', newCourse.description)
.field('workload', newCourse.workload)
.attach('photo', fileToUpload)
.then(_res => {
return request(app.getHttpServer())
.delete(`${courseUrl}/${_res.body.id}`)
Expand All @@ -236,31 +268,43 @@ describe('CourseController (e2e)', () => {
.set('Content-Type', 'multipart/form-data')
.field('grant_type', GrantTypeEnum.CLIENT_CREDENTIALS)
.then(res => {
const newCourse = {
title: 'Teste E3E to update',
thumbUrl: 'http://teste.com/thumb.png',
authorName: 'Teste',
authorDescription: 'Teste',
description: 'Este é um registro de teste',
workload: 1,
} as NewCourseDTO;
return request(app.getHttpServer())
.post(courseUrl)
.set('Authorization', `Bearer ${res.body.accessToken}`)
.send({
title: 'Teste E3E',
thumbUrl: 'http://teste.com/thumb.png',
authorId: '1',
description: 'Este é um registro de teste',
} as NewCourseDTO)
.field('title', newCourse.title)
.field('thumbUrl', newCourse.thumbUrl)
.field('authorName', newCourse.authorName)
.field('authorDescription', newCourse.authorDescription)
.field('description', newCourse.description)
.field('workload', newCourse.workload)
.attach('photo', fileToUpload)
.then(_res => {
return request(app.getHttpServer())
.put(`${courseUrl}/${_res.body.id}`)
.set('Authorization', `Bearer ${res.body.accessToken}`)
.send({
title: 'Test Update',
thumbUrl: _res.body.thumbUrl,
authorId: _res.body.authorId,
authorName: _res.body.authorName,
authorDescription: _res.body.authorDescription,
description: _res.body.description,
} as CourseUpdateDTO)
.set('Authorization', `Bearer ${res.body.accessToken}`)
workload: _res.body.workload,
} as NewCourseDTO)
.then(__res => {
return request(app.getHttpServer())
.get(`${courseUrl}/${__res.body.id}`)
.set('Authorization', `Bearer ${res.body.accessToken}`)
.expect(response => {
expect(response.body.title).toBe('Test Update');
expect(response.body.slug).toBe('test-update');
})
.then(() => done());
});
Expand All @@ -269,6 +313,41 @@ describe('CourseController (e2e)', () => {
});

afterAll(async () => {
const courseRepository: Repository<Course> = moduleFixture.get<
Repository<Course>
>(getRepositoryToken(Course));
const courses = await courseRepository.find();
const unlink = util.promisify(fs.unlink);
const fileExists = util.promisify(fs.exists);
await Promise.all(
courses.map(async course => {
const pathFile = path.resolve(
path.join(__dirname, '..', '..', 'upload', course.photoName),
);
if (await fileExists(pathFile)) {
await unlink(pathFile);
}
await courseRepository.remove(course);
}),
);
const clientCredentialRepository: Repository<ClientCredentials> = moduleFixture.get<
Repository<ClientCredentials>
>(getRepositoryToken(ClientCredentials));
const clients = await clientCredentialRepository.find({
name: ClientCredentialsEnum['NEWSCHOOL@FRONT'],
});
if (clients.length) {
await clientCredentialRepository.remove(clients[0]);
}
const roleRepository: Repository<Role> = moduleFixture.get<
Repository<Role>
>(getRepositoryToken(Role));
const roles = await roleRepository.find({
name: RoleEnum.ADMIN,
});
if (roles.length) {
await roleRepository.remove(roles[0]);
}
await app.close();
});
});
18 changes: 18 additions & 0 deletions test/MessageModule/message.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ describe('MessageController (e2e)', () => {
});

afterAll(async () => {
const clientCredentialRepository: Repository<ClientCredentials> = moduleFixture.get<
pethersonmoreno marked this conversation as resolved.
Show resolved Hide resolved
Repository<ClientCredentials>
>(getRepositoryToken(ClientCredentials));
const clients = await clientCredentialRepository.find({
name: ClientCredentialsEnum['NEWSCHOOL@EXTERNAL'],
});
if (clients.length) {
await clientCredentialRepository.remove(clients[0]);
}
const roleRepository: Repository<Role> = moduleFixture.get<
Repository<Role>
>(getRepositoryToken(Role));
const roles = await roleRepository.find({
name: RoleEnum.ADMIN,
});
if (roles.length) {
await roleRepository.remove(roles[0]);
}
await app.close();
});
});
Loading