Skip to content

Commit

Permalink
testing controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Dim authored and Tony Dim committed Sep 23, 2023
1 parent 97194c7 commit df50bd9
Show file tree
Hide file tree
Showing 5 changed files with 517 additions and 0 deletions.
174 changes: 174 additions & 0 deletions tests/authController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
require('dotenv').config();
const request = require('supertest');
const sequelize = require('../db/db');
const app = require('../app');

beforeAll(async () => {
try {
await sequelize.sync();
} catch (error) {
console.error('Database sync error', error);
}
});

describe('Email Validation API', () => {
it('should validate an email address', async () => {
const validEmail = '[email protected]';
const response = await request(app)
.post('/api/auth/validate')
.send({email: validEmail});

expect(response.status).toEqual(200);
});

it('should reject an invalid email address', async () => {
const invalidEmail = 'invalid-email';
const response = await request(app)
.post('/api/auth/validate')
.send({ email: invalidEmail });

expect(response.status).toEqual(400);
})
})


describe ('Create User API', () => {
it('should create a new user', async () => {
const newUser = {
first_name: 'test',
last_name: 'create',
email: '[email protected]',
phone: '02012345678',
password: 'testpassword_hash',
is_admin: false,
profile_pic: 'https://cdn-icons-png.flaticon.com/512/147/147142.png',
org_id: 976542562576,
lunch_credit_balance: 1000,
bank_code: '100011',
bank_name: 'KeyStone',
bank_number: '9292929292'
};
const response = await request(app)
.post('/api/auth/signup')
.send({user: newUser});
expect(response.status).toEqual(200);
expect(response.body.data).toMatchObject(newUser);
});

it('should return a 400 error for missing data', async () => {
const newUser = {
first_name: 'test'
}; // Missing required params last_name through to bank_name
const response = await request(app)
.post('/api/auth/signup')
.send({user: newUser});

expect(response.status).toEqual(400);
});
});

describe('User Login API', () => {
it('should allow a valid user to login', async () => {
const user = {
email: '[email protected]',
password: 'testpassword_hash'
};
const response = await request(app)
.post('/api/auth/login')
.send(user);

expect(response.status).toEqual(200);
expect(response.body).toHaveProperty('message', 'User authenticated sucessfully');
});

it('should return a 400 error for missing data', async () => {
const user = {
email: '[email protected]'
}; // Missing 'password'
const response = await request(app)
.post('/api/auth/login')
.send(user);

expect(response.status).toBe(400);
expect(response.body).toHaveProperty('message', 'Fill all required fields');
});

it('should return a 404 error for invalid credentials', async () => {
const user = {
email: '[email protected]',
password: 'password_hash'
}; // email does not exist in DB
const response = await request(app)
.post('/api/auth/login')
.send(user);

expect(response.status).toEqual(404);
expect(response.body).toHaveProperty('message', 'Invalid credentials');
});

it('should return a 401 error for invalid password', async () => {
const user = {
email: '[email protected]',
password: 'invalidPassword_hash'
}; // password does not exist nor correlate with DB
const response = await request(app)
.post('/api/auth/login')
.send(user);

expect(response.status).toEqual(401);
expect(response.body).toHaveProperty('message', 'Invalid credentials');
});
});

describe('Logout API', () => {
it('should allow an authenticated user to log out', async () => {
const token = 'userToken';
const response = await request(app)
.post('/api/auth/logout')
.set('token', token);

expect(response.status).toEqual(200);
expect(response.body).toHaveProperty('message', 'User logged out successfully');
});

it('should return a 401 error for missing token', async () => {
const response = await request(app)
.post('/api/auth/logout');

expect(response.status).toBe(401);
expect(response.body).toHaveProperty('message', 'Token is missing');
});
});

describe('Create Org and User API', () => {
it('should create an organization and admin user', async () => {
const adminUser = {
first_name: 'adminName',
last_name: 'adminLastName',
email: '[email protected]',
phone: '01012345678',
password: '5f4dcc3b5aa765d61d8327deb882cf99',
is_admin: true,
org_id: 976542562576,
lunch_credit_balance: 10000,
};
const response = await request(app)
.post('/api/auth/signup/org-user')
.send(adminUser);

expect(response.status).toEqual(200);
expect(response.body).toHaveProperty('message', 'Org and User registered successfully');
expect(response.body.data).toMatchObject(adminUser);
});

it('should return a 400 error for missing data', async () => {
const adminUser = {
fisrt_name: 'adminName'
}; // Missing required params email, password, org_name and lunch_price
const response = await request(app)
.post('/api/auth/signup/org-user')
.send(adminUser);

expect(response.status).toEqual(400);
});
});
55 changes: 55 additions & 0 deletions tests/createPasswordController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require('dotenv').config();
const request = require('supertest');
const sequelize = require('../db/db');
const app = require('../app');

beforeAll(async () => {
try {
await sequelize.sync();
} catch (error) {
console.error('Database sync error', error);
}
});

describe('Update Password API', () => {
it('should update the user password', async () => {
const testUserId = '092820892';
const password = 'updatedPassword_hash';
const confirmPassword = 'updatedPassword_hash';

const response = await request(app)
.query({testUserId})
.patch('/api/users/update-password')
.send({ password, confirmPassword });

expect(response.status).toEqual(200);
expect(response.status).toHaveProperty('message', 'Password created successfully');
});

it('should return a 400 error if passwords do not match', async () => {
const testUserId = '092820892';
const password = 'updatedPassword_hash';
const confirmPassword = passwordUpdate_hash; // differs from entry in password field

const response = await request(app)
.query({testUserId})
.patch('/api/users/update-password')
.send({ password, confirmPassword });

expect(response.status).toEqual(400);
expect(response.body).toHaveProperty('message', 'Passwords do not match');
});

it('should return a 400 error for missing data', async () => {
const testUserId = '092820892';
const password = updatedPassword_hash; // Missing confirmPassword

const response = await request(app)
.query({testUserId})
.patch('/api/users/update-password')
.send({ password });

expect(response.status).toEqual(400);
expect(response.body).toHaveProperty('message', 'Fill all required fields');
});
});
113 changes: 113 additions & 0 deletions tests/lunchController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
require('dotenv').config();
const request = require('supertest');
const sequelize = require('../db/db');
const app = require('../app');

beforeAll(async () => {
try {
await sequelize.sync();
} catch (error) {
console.error('Database sync error', error);
}
});

describe('Retrieve available lunches for a user', () => {

it('should return an empty array for a user with no lunch data', async () => {
const testUserId = '9339'; //user is with no lunch data

const response = await request(app)
.get(`/api/lunch/${testUserId}`);

expect(response.status).toBe(200);
expect(response.body).toHaveProperty('message', 'No lunches found for this user');
});

it('should retrieve all available lunches for a user by the user id', async () => {
const testUserId = '29292';

const response = await request(app)
.query({testUserId})
.get(`/api/lunch/${testUserId}`);

expect(response.status).toBe(200);
expect(response.body).toHaveProperty('message', 'Lunches retrieved successfully');
});

it('should return a 500 error for missing user ID', async () => {
const testUserId = '';
const response = await request(app)
.get(`/api/lunch/${testUserId}`);

expect(response.status).toBe(500);
});

});

describe('Send Lunch API', () => {
it('should send lunch to a user', async () => {
const testReceiverId = '9302302'; //user id you want to send lunch to
const testQuantity = 2;
const testNote = 'Well deserved';

const response = await request(app)
.post('/api/lunch/send')
.send({ testReceiverId, testQuantity, testNote });

expect(response.status).toBe(201);
expect(response.body).toHaveProperty('message', 'Lunch sent successfully');
});

});

describe('Redeem Lunch API', () => {
it('should redeem lunch into bank account', async () => {
const bank_code = '100110';
const bank_name = 'KeyStone';
const bank_number = '0123456789';
const amount = 2;
const email = '[email protected]';
const user_id = 203930303;

const response = await request(app)
.query({user_id})
.post('/api/lunch/redeem')
.send({ bank_code, bank_name, bank_number, amount, email });

expect(response.status).toBe(201);
expect(response.body).toHaveProperty('message', 'Withdrawal request created successfully');
});

it('should return a 400 error for missing data', async () => {
const bank_code = '100110'; // Missing bank_name, bank_number, amount and email
const user_id = 203930303;

const response = await request(app)
.query({user_id})
.post('/api/lunch/redeem')
.send({ bank_code });

expect(response.status).toBe(400);
expect(response.body).toHaveProperty('message', 'Please provide all required fields');
});
});

describe('Get Lunch Details API', () => {
it('should retrieve lunch details by lunch id', async () => {
const testLunchId = '38292';

const response = await request(app)
.get(`/api/lunch/${testLunchId}`);

expect(response.status).toBe(200);
});

it('should return a 404 error for missing lunch details', async () => {
const testLunchId = '0101010' // Lunch Id which doesn't contain lunch data

const response = await request(app)
.get(`/api/lunch/${testLunchId}`);

expect(response.status).toBe(404);
})
});
Loading

0 comments on commit df50bd9

Please sign in to comment.