This commit is contained in:
Ahmed Bouhuolia
2020-03-16 00:06:15 +02:00
parent 56701951b7
commit 73711384f6
7925 changed files with 18478 additions and 959 deletions

View File

@@ -4,16 +4,53 @@ import {
expect,
create,
make,
login,
} from '~/testInit';
describe('routes: `/routes`', () => {
describe('POST: `/routes`', () => {
it('Should create a new user if the user was not authorized.', () => {
let loginRes;
describe('routes: `/routes`', () => {
beforeEach(async () => {
loginRes = await login();
});
afterEach(() => {
loginRes = null;
});
describe('GET: `/users`', () => {
it('Should response unauthorized if the user was not authorized.', async () => {
const res = await request().get('/api/users');
expect(res.status).equals(401);
expect(res.body.message).equals('unauthorized');
});
it('Should retrieve the stored users with pagination meta.', async () => {
await create('user');
const res = await request()
.get('/api/users')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body.users.results.length).equals(2);
expect(res.body.users.total).equals(2);
});
});
describe('POST: `/users`', () => {
it('Should create a new user if the user was not authorized.', async () => {
const res = await request().post('/api/users');
expect(res.status).equals(401);
expect(res.body.message).equals('unauthorized');
});
it('Should `first_name` be required.', async () => {
const res = await request().post('/api/users');
const res = await request()
.post('/api/users')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
@@ -22,7 +59,10 @@ describe('routes: `/routes`', () => {
});
it('Should `last_name` be required.', async () => {
const res = await request().post('/api/users');
const res = await request()
.post('/api/users')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
@@ -31,7 +71,10 @@ describe('routes: `/routes`', () => {
});
it('Should `email` be required.', async () => {
const res = await request().post('/api/users');
const res = await request()
.post('/api/users')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
@@ -41,13 +84,16 @@ describe('routes: `/routes`', () => {
it('Should be `email` be valid format.', async () => {
const user = make('user');
const res = await request().post('/api/users').send({
first_name: user.first_name,
last_name: user.last_name,
email: 'email',
phone_number: user.phone_number,
status: 1,
});
const res = await request()
.post('/api/users')
.set('x-access-token', loginRes.body.token)
.send({
first_name: user.first_name,
last_name: user.last_name,
email: 'email',
phone_number: user.phone_number,
status: 1,
});
expect(res.status).equals(422);
@@ -57,13 +103,16 @@ describe('routes: `/routes`', () => {
it('Should `phone_number` be valid format.', async () => {
const user = make('user');
const res = await request().post('/api/users').send({
first_name: user.first_name,
last_name: user.last_name,
email: user.email,
phone_number: 'phone_number',
status: 1,
});
const res = await request()
.post('/api/users')
.set('x-access-token', loginRes.body.token)
.send({
first_name: user.first_name,
last_name: user.last_name,
email: user.email,
phone_number: 'phone_number',
status: 1,
});
expect(res.status).equals(422);
@@ -72,7 +121,10 @@ describe('routes: `/routes`', () => {
});
it('Should `password` be required.', async () => {
const res = await request().post('/api/users').send();
const res = await request()
.post('/api/users')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
@@ -81,9 +133,12 @@ describe('routes: `/routes`', () => {
});
it('Should password be equals confirm_password.', async () => {
const res = await request().post('/api/users').send({
password: '123123',
});
const res = await request()
.post('/api/users')
.set('x-access-token', loginRes.body.token)
.send({
password: '123123',
});
expect(res.status).equals(422);
@@ -92,9 +147,12 @@ describe('routes: `/routes`', () => {
});
it('Should `status` be boolean', async () => {
const res = await request().post('/api/users').send({
status: 'not_boolean',
});
const res = await request()
.post('/api/users')
.set('x-access-token', loginRes.body.token)
.send({
status: 'not_boolean',
});
expect(res.status).equals(422);
@@ -104,15 +162,18 @@ describe('routes: `/routes`', () => {
it('Should response bad request in case email was already exist.', async () => {
const user = await create('user');
const res = await request().post('/api/users').send({
first_name: user.first_name,
last_name: user.last_name,
email: user.email,
phone_number: user.phone_number,
password: '123123123',
confirm_password: '123123123',
status: 1,
});
const res = await request()
.post('/api/users')
.set('x-access-token', loginRes.body.token)
.send({
first_name: user.firstName,
last_name: user.lastName,
email: user.email,
phone_number: user.phoneNumber,
password: '123123123',
confirm_password: '123123123',
status: 1,
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
@@ -121,16 +182,20 @@ describe('routes: `/routes`', () => {
});
it('Should response bad request in case phone number was already exist.', async () => {
const user = await create('user');
const res = await request().post('/api/users').send({
first_name: user.first_name,
last_name: user.last_name,
email: user.email,
phone_number: user.phone_number,
password: user.password,
confirm_password: user.password,
status: 1,
});
const user = await create('user', { phone_number: '0927918381' });
const res = await request()
.post('/api/users')
.set('x-access-token', loginRes.body.token)
.send({
first_name: user.firstName,
last_name: user.lastName,
email: user.email,
phone_number: '0927918381',
password: user.password,
confirm_password: user.password,
status: 1,
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
@@ -139,29 +204,40 @@ describe('routes: `/routes`', () => {
});
it('Should response success with correct data type.', async () => {
const user = await make('user');
const res = await request().post('/api/users').send({
first_name: user.first_name,
last_name: user.last_name,
email: user.email,
phone_number: user.phone_number,
password: user.password,
confirm_password: user.password,
status: 1,
});
const user = await make('user', { phone_number: '0920000000' });
const res = await request()
.post('/api/users')
.set('x-access-token', loginRes.body.token)
.send({
first_name: user.firstName,
last_name: user.lastName,
email: user.email,
phone_number: '0920000000',
password: user.password,
confirm_password: user.password,
status: 1,
});
expect(res.status).equals(200);
expect(res.body.user.id).equals(2);
});
});
describe('POST: `/users/:id`', () => {
it('Should create a new user if the user was not authorized.', () => {
it('Should create a new user if the user was not authorized.', async () => {
const user = await create('user');
const res = await request().post(`/api/users/${user.id}`);
expect(res.status).equals(401);
expect(res.body.message).equals('unauthorized');
});
it('Should `first_name` be required.', async () => {
const user = await create('user');
const res = await request().post(`/api/users/${user.id}`);
const res = await request()
.post(`/api/users/${user.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
@@ -171,7 +247,10 @@ describe('routes: `/routes`', () => {
it('Should `last_name` be required.', async () => {
const user = await create('user');
const res = await request().post(`/api/users/${user.id}`);
const res = await request()
.post(`/api/users/${user.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
@@ -181,7 +260,10 @@ describe('routes: `/routes`', () => {
it('Should `email` be required.', async () => {
const user = await create('user');
const res = await request().post(`/api/users/${user.id}`);
const res = await request()
.post(`/api/users/${user.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
@@ -191,13 +273,16 @@ describe('routes: `/routes`', () => {
it('Should be `email` be valid format.', async () => {
const user = await create('user');
const res = await request().post(`/api/users/${user.id}`).send({
first_name: user.first_name,
last_name: user.last_name,
email: 'email',
phone_number: user.phone_number,
status: 1,
});
const res = await request()
.post(`/api/users/${user.id}`)
.set('x-access-token', loginRes.body.token)
.send({
first_name: user.first_name,
last_name: user.last_name,
email: 'email',
phone_number: user.phone_number,
status: 1,
});
expect(res.status).equals(422);
@@ -207,13 +292,16 @@ describe('routes: `/routes`', () => {
it('Should `phone_number` be valid format.', async () => {
const user = create('user');
const res = await request().post(`/api/users/${user.id}`).send({
first_name: user.first_name,
last_name: user.last_name,
email: user.email,
phone_number: 'phone_number',
status: 1,
});
const res = await request()
.post(`/api/users/${user.id}`)
.set('x-access-token', loginRes.body.token)
.send({
first_name: user.first_name,
last_name: user.last_name,
email: user.email,
phone_number: 'phone_number',
status: 1,
});
expect(res.status).equals(422);
@@ -223,7 +311,10 @@ describe('routes: `/routes`', () => {
it('Should `password` be required.', async () => {
const user = create('user');
const res = await request().post(`/api/users/${user.id}`).send();
const res = await request()
.post(`/api/users/${user.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
@@ -233,9 +324,12 @@ describe('routes: `/routes`', () => {
it('Should password be equals confirm_password.', async () => {
const user = create('user');
const res = await request().post(`/api/users/${user.id}`).send({
password: '123123',
});
const res = await request()
.post(`/api/users/${user.id}`)
.set('x-access-token', loginRes.body.token)
.send({
password: '123123',
});
expect(res.status).equals(422);
@@ -245,9 +339,12 @@ describe('routes: `/routes`', () => {
it('Should `status` be boolean', async () => {
const user = create('user');
const res = await request().post(`/api/users/${user.id}`).send({
status: 'not_boolean',
});
const res = await request()
.post(`/api/users/${user.id}`)
.set('x-access-token', loginRes.body.token)
.send({
status: 'not_boolean',
});
expect(res.status).equals(422);
@@ -262,14 +359,20 @@ describe('routes: `/routes`', () => {
});
it('Should response not found if the user was not exist.', async () => {
const res = await request().get('/api/users/10').send();
const res = await request()
.get('/api/users/10')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(404);
});
it('Should response success if the user was exist.', async () => {
const user = await create('user');
const res = await request().get(`/api/users/${user.id}`).send();
const res = await request()
.get(`/api/users/${user.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(200);
});
@@ -281,24 +384,35 @@ describe('routes: `/routes`', () => {
});
it('Should response not found if the user was not exist.', async () => {
const res = await request().delete('/api/users/10').send();
const res = await request()
.delete('/api/users/10')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(404);
expect(res.body.errors).include.something.that.deep.equals({
type: 'USER_NOT_FOUND', code: 100,
});
});
it('Should response success if the user was exist.', async () => {
const user = await create('user');
const res = await request().delete(`/api/users/${user.id}`).send();
const res = await request()
.delete(`/api/users/${user.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(200);
expect(res.status).equals(200);
});
it('Should delete the give user from the storage.', async () => {
const user = await create('user');
await request().delete(`/api/users/${user.id}`).send();
await request()
.delete(`/api/users/${user.id}`)
.set('x-access-token', loginRes.body.token)
.send();
const storedUsers = await knex('users').where('id', user.id);
expect(storedUsers).to.have.lengthOf(0);
});
});