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

Backend development #21

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion backend/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
import pactum from 'pactum';
import AuthTestSuite from './auth.test';
import TenantTestSuite from './tenant.test';
import OwnerTestSuite from './owner.test';
import CoworkingTestSuite from './coworking-space.test';
import BookingTestSuite from './booking.test';

describe('AppController', () => {
beforeAll(async () => {
Expand All @@ -29,4 +32,7 @@ describe('AppController', () => {

AuthTestSuite();
TenantTestSuite();
});
OwnerTestSuite();
CoworkingTestSuite();
BookingTestSuite();
});
112 changes: 111 additions & 1 deletion backend/test/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export default function AuthTestSuite() {
password: 'VeryStrongPassword123!',
};

const randomAccessToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5';

describe('POST /auth/register', () => {
it('should return 400 if payload is empty', async () => {
await pactum
Expand Down Expand Up @@ -99,7 +102,7 @@ export default function AuthTestSuite() {
user_id: expect.any(Number),
nik: null,
ktp_picture: null,
balance: "0",
balance: '0',
bank_name: null,
card_number: null,
status: 'PENDING',
Expand All @@ -120,5 +123,112 @@ export default function AuthTestSuite() {
});
});
});

describe('POST /auth/login', () => {
it('should return 400 if payload is empty', async () => {
await pactum.spec().post('/auth/login').withJson({}).expectStatus(400);
});

it('should return 401 if payload is invalid', async () => {
const res = await pactum
.spec()
.post('/auth/login')
.withJson({
...validLoginPayload,
password: 'weakpassword',
})
.expectStatus(401);

expect(res.body).toStrictEqual({
message: expect.any(String),
});
});

it('should return 200 if login tenant is successfull', async () => {
const res = await pactum
.spec()
.post('/auth/login')
.withJson(validLoginPayload)
.expectStatus(200)
.stores('accessToken', 'res.body.accessToken')
.stores('refresh_token', 'res.headers.set-cookie[0]');

expect(res.body).toStrictEqual({
firstName: validRegisterPayload.firstName,
userId: expect.any(Number),
userType: validRegisterPayload.userType,
message: 'Login success',
accessToken: expect.any(String),
});
});

it('should return 200 if login owner is successfull', async () => {
const res = await pactum
.spec()
.post('/auth/login')
.withJson({ ...validLoginPayload, email: 'test2@gmail.com' })
.expectStatus(200)
.stores('accessTokenOwner', 'res.body.accessToken');

expect(res.body).toStrictEqual({
firstName: validRegisterPayload.firstName,
userId: expect.any(Number),
userType: 'OWNER',
message: 'Login success',
accessToken: expect.any(String),
});
});
});

describe('GET /auth/refresh-token', () => {
it('should return 401 if access is denied', async () => {
const res = await pactum
.spec()
.get('/auth/refresh-token')
.expectStatus(401);

expect(res.body).toStrictEqual('Access denied');
});

it('should return 200 if refresh-token is successfull', async () => {
const res = await pactum
.spec()
.get('/auth/refresh-token')
.withHeaders('cookie', '$S{refresh_token}')
.expectStatus(200);

console.log('res.body', res.body);

expect(res.body).toStrictEqual({
accessToken: expect.any(String),
});
});
});

describe('DELETE /auth/logout', () => {
it('should return 401 if user is not logged in', async () => {
const res = await pactum
.spec()
.delete('/auth/logout')
.expectStatus(401);

expect(res.body).toStrictEqual({
message: 'Access denied',
});
});

it('should return 204 if logout is successfull', async () => {
const res = await pactum
.spec()
.delete('/auth/logout')
.withBearerToken(`$S{accessToken}`)
.withHeaders('cookie', '$S{refresh_token}')
.expectStatus(200);

expect(res.body).toStrictEqual({
message: 'Logout success',
});
});
});
});
}
40 changes: 40 additions & 0 deletions backend/test/booking.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, expect, it } from 'bun:test';
import pactum from 'pactum';

export default function BookingTestSuite() {
describe('BookingController', () => {
const validBookPayload = {
date: "2023-12-18",
startHour: "11",
endHour: "13",
totalPrice: "275000"
};

describe('POST /bookings/{spaceId}/book', () => {
it('should return 404 if coworking space is not found', async () => {
const res = await pactum
.spec()
.post('/bookings/{spaceId}/book')
.withBearerToken(`$S{accessToken}`)
.withPathParams({
spaceId: 9999,
})
.withJson({ ...validBookPayload})
.expectStatus(404);
});

// it('should return 200 if booking is successful', async () => {
// const res = await pactum
// .spec()
// .post('/bookings/{spaceId}/book')
// .withBearerToken(`$S{accessToken}`)
// .withPathParams({
// spaceId: 2,
// })
// .withJson({ ...validBookPayload})
// .expectStatus(200);
// console.log(res.body)
// });
});
});
}
62 changes: 62 additions & 0 deletions backend/test/coworking-space.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, expect, it } from 'bun:test';
import pactum from 'pactum';

export default function CoworkingTestSuite() {
describe('CoworkingSpaceController', () => {
const validProfilePayload = {
email: 'test@gmail.com',
firstName: 'Test2',
lastName: 'User2',
phoneNumber: '62811223344',
};

describe('GET /coworking-spaces', () => {
it('should return 200 if get profile is successfull', async () => {
const res = await pactum
.spec()
.get('/coworking-spaces')
.withBearerToken(`$S{accessToken}`)
.expectStatus(200);

expect(res.body).toStrictEqual({
coworkingSpaces: [
[], {
isFirstPage: true,
isLastPage: true,
currentPage: 1,
previousPage: null,
nextPage: null,
pageCount: 0,
totalCount: 0
}
]
});
});
});

describe('GET /coworking-spaces/{space_id}', () => {
it('should return 404 if coworking space is not found', async () => {
const res = await pactum
.spec()
.get('/coworking-spaces/{space_id}')
.withBearerToken(`$S{accessToken}`)
.withPathParams({
space_id: 99999,
})
.expectStatus(404);
})

it('should return 200 if get coworking space by id is successfull', async () => {
const res = await pactum
.spec()
.get(`/coworking-spaces/`)
.withBearerToken(`$S{accessToken}`)
.withPathParams({
space_id: 1,
})
.expectStatus(200);
console.log(res.body)
});
});
});
}
103 changes: 103 additions & 0 deletions backend/test/owner.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { describe, expect, it } from 'bun:test';
import pactum from 'pactum';

export default function OwnerTestSuite() {
describe('OwnerController', () => {
const validOwnerPayload = {
email: 'test2@gmail.com',
firstName: 'Test',
lastName: 'User',
phoneNumber: '62811223355',
};
const validEditOwnerPayload = {
bank_name: "Mandiri",
card_number: "1815850071",
};

const formData = new FormData();
formData.append('bank_name', validEditOwnerPayload.bank_name);
formData.append('card_number', validEditOwnerPayload.card_number);

describe('GET /owners/info', () => {
it('should return 200 if get owner information is successfull', async () => {
const res = await pactum
.spec()
.get('/owners/info')
.withBearerToken(`$S{accessTokenOwner}`)
.expectStatus(200);
expect(res.body).toStrictEqual({
owner: {
owner_id: expect.any(Number),
user_id: expect.any(Number),
nik: null,
ktp_picture: null,
balance: expect.any(String),
bank_name: null,
card_number: null,
status: "PENDING",
user: {
email: validOwnerPayload.email,
first_name: validOwnerPayload.firstName,
last_name: validOwnerPayload.lastName,
phone_number: validOwnerPayload.phoneNumber,
}
}
});
});
});

describe('GET /owners/info', () => {
it('should return 200 if get owner information is successfull', async () => {
const res = await pactum
.spec()
.get('/owners/facilities')
.withBearerToken(`$S{accessTokenOwner}`)
.expectStatus(200);

expect(res.body).toStrictEqual({
facilities: [
[], {
isFirstPage: true,
isLastPage: true,
currentPage: 1,
previousPage: null,
nextPage: null,
pageCount: 0,
totalCount: 0
}
]
});
});
});

// describe('PUT /owners/info', () => {
// it('should return 200 if edit owner information is successfull', async () => {
// const res = await pactum
// .spec()
// .put('/owners/info')
// .withBearerToken(`$S{accessTokenOwner}`)
// .withMultiPartFormData(formData)
// .expectStatus(200);

// expect(res.body).toStrictEqual({
// owner: {
// owner_id: expect.any(Number),
// user_id: expect.any(Number),
// nik: null,
// ktp_picture: null,
// balance: expect.any(String),
// bank_name: validEditOwnerPayload.bank_name,
// card_number: validEditOwnerPayload.card_number,
// status: "PENDING",
// user: {
// email: validOwnerPayload.email,
// first_name: validOwnerPayload.firstName,
// last_name: validOwnerPayload.lastName,
// phone_number: validOwnerPayload.phoneNumber,
// }
// }
// });
// });
// });
});
}
Loading
Loading