Skip to content

Commit

Permalink
create: test e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
adiCacti committed May 16, 2022
1 parent 3de42a6 commit ec23d23
Showing 1 changed file with 234 additions and 13 deletions.
247 changes: 234 additions & 13 deletions test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,245 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as pactum from 'pactum';
import { AppModule } from '../src/app.module';
import { AuthDto } from '../src/auth/dto';
import { CreateBookmarkDto, EditBookmarkDto } from '../src/bookmark/dto';
import { PrismaService } from '../src/prisma/prisma.service';
import { EditUserDto } from '../src/user/dto';

describe('AppController (e2e)', () => {
describe('App e2e', () => {
let app: INestApplication;
let prisma: PrismaService;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
app = moduleRef.createNestApplication();
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
}),
);
await app.init();
await app.listen(3333);

prisma = app.get(PrismaService);
await prisma.cleanDb();
pactum.request.setBaseUrl('http://localhost:3333');
});

afterAll(() => {
app.close();
});

describe('Auth', () => {
const dto: AuthDto = {
email: 'vlad@gmail.com',
password: '123',
};
describe('Signup', () => {
it('should throw if email empty', () => {
return pactum
.spec()
.post('/auth/signup')
.withBody({
password: dto.password,
})
.expectStatus(400);
});
it('should throw if password empty', () => {
return pactum
.spec()
.post('/auth/signup')
.withBody({
email: dto.email,
})
.expectStatus(400);
});
it('should throw if no body provided', () => {
return pactum.spec().post('/auth/signup').expectStatus(400);
});
it('should signup', () => {
return pactum
.spec()
.post('/auth/signup')
.withBody(dto)
.expectStatus(201);
});
});

describe('Signin', () => {
it('should throw if email empty', () => {
return pactum
.spec()
.post('/auth/signin')
.withBody({
password: dto.password,
})
.expectStatus(400);
});
it('should throw if password empty', () => {
return pactum
.spec()
.post('/auth/signin')
.withBody({
email: dto.email,
})
.expectStatus(400);
});
it('should throw if no body provided', () => {
return pactum.spec().post('/auth/signin').expectStatus(400);
});
it('should signin', () => {
return pactum
.spec()
.post('/auth/signin')
.withBody(dto)
.expectStatus(200)
.stores('userAt', 'access_token');
});
});
});

it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
describe('User', () => {
describe('Get me', () => {
it('should get current user', () => {
return pactum
.spec()
.get('/users/me')
.withHeaders({
Authorization: 'Bearer $S{userAt}',
})
.expectStatus(200);
});
});

describe('Edit user', () => {
it('should edit user', () => {
const dto: EditUserDto = {
firstName: 'Vladimir',
email: 'vlad@codewithvlad.com',
};
return pactum
.spec()
.patch('/users')
.withHeaders({
Authorization: 'Bearer $S{userAt}',
})
.withBody(dto)
.expectStatus(200)
.expectBodyContains(dto.firstName)
.expectBodyContains(dto.email);
});
});
});

describe('Bookmarks', () => {
describe('Get empty bookmarks', () => {
it('should get bookmarks', () => {
return pactum
.spec()
.get('/bookmarks')
.withHeaders({
Authorization: 'Bearer $S{userAt}',
})
.expectStatus(200)
.expectBody([]);
});
});

describe('Create bookmark', () => {
const dto: CreateBookmarkDto = {
title: 'First Bookmark',
link: 'https://www.youtube.com/watch?v=d6WC5n9G_sM',
};
it('should create bookmark', () => {
return pactum
.spec()
.post('/bookmarks')
.withHeaders({
Authorization: 'Bearer $S{userAt}',
})
.withBody(dto)
.expectStatus(201)
.stores('bookmarkId', 'id');
});
});

describe('Get bookmarks', () => {
it('should get bookmarks', () => {
return pactum
.spec()
.get('/bookmarks')
.withHeaders({
Authorization: 'Bearer $S{userAt}',
})
.expectStatus(200)
.expectJsonLength(1);
});
});

describe('Get bookmark by id', () => {
it('should get bookmark by id', () => {
return pactum
.spec()
.get('/bookmarks/{id}')
.withPathParams('id', '$S{bookmarkId}')
.withHeaders({
Authorization: 'Bearer $S{userAt}',
})
.expectStatus(200)
.expectBodyContains('$S{bookmarkId}');
});
});

describe('Edit bookmark by id', () => {
const dto: EditBookmarkDto = {
title:
'Kubernetes Course - Full Beginners Tutorial (Containerize Your Apps!)',
description:
'Learn how to use Kubernetes in this complete course. Kubernetes makes it possible to containerize applications and simplifies app deployment to production.',
};
it('should edit bookmark', () => {
return pactum
.spec()
.patch('/bookmarks/{id}')
.withPathParams('id', '$S{bookmarkId}')
.withHeaders({
Authorization: 'Bearer $S{userAt}',
})
.withBody(dto)
.expectStatus(200)
.expectBodyContains(dto.title)
.expectBodyContains(dto.description);
});
});

describe('Delete bookmark by id', () => {
it('should delete bookmark', () => {
return pactum
.spec()
.delete('/bookmarks/{id}')
.withPathParams('id', '$S{bookmarkId}')
.withHeaders({
Authorization: 'Bearer $S{userAt}',
})
.expectStatus(204);
});

it('should get empty bookmarks', () => {
return pactum
.spec()
.get('/bookmarks')
.withHeaders({
Authorization: 'Bearer $S{userAt}',
})
.expectStatus(200)
.expectJsonLength(0)
.inspect();
});
});
});
});

0 comments on commit ec23d23

Please sign in to comment.