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

test: increased test coverage for API Endpoints #802

Open
wants to merge 2 commits into
base: main
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
2 changes: 1 addition & 1 deletion src/service/routes/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ router.post('/:id/cancel', async (req, res) => {
console.log(`user ${req.user.username} not authorised to cancel push request for ${id}`);
res.status(401).send({
message:
'User ${req.user.username)} not authorised to cancel push requests on this project.',
`User ${req.user.username} not authorised to cancel push requests on this project.`,
});
}
} else {
Expand Down
150 changes: 150 additions & 0 deletions test/testAuthRoutes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Import the dependencies for testing
const chai = require('chai');
const chaiHttp = require('chai-http');
const db = require('../src/db');
const service = require('../src/service');

chai.use(chaiHttp);
chai.should();
const expect = chai.expect;

describe('Test Auth Routes', async () => {
let app;
let cookie;

before(async function () {
app = await service.start();
await db.deleteUser('login-test-user');
});

describe('test GET / method', async function () {
it('should return auth endpoints', async function () {
const res = await chai.request(app)
.get('/api/auth');

expect(res.status).to.be.equal(200);
expect(res.body).to.deep.equal({
login: {
action: 'post',
uri: '/api/auth/login',
},
profile: {
action: 'get',
uri: '/api/auth/profile',
},
logout: {
action: 'post',
uri: '/api/auth/logout',
}
});
})
});



describe('test login / logout', async function () {
// Test to get all students record
it('should get 401 not logged in', async function () {
const res = await chai.request(app).get('/api/auth/profile');

res.should.have.status(401);
});

it('should be able to login', async function () {
const res = await chai.request(app).post('/api/auth/login').send({
username: 'admin',
password: 'admin',
});

expect(res).to.have.cookie('connect.sid');
res.should.have.status(200);

// Get the connect cooie
res.headers['set-cookie'].forEach((x) => {
if (x.startsWith('connect')) {
cookie = x.split(';')[0];
}
});
});

it('should now return success', async function () {
const res = await chai.request(app).get('/api/auth/success').set('Cookie', `${cookie}`);
res.should.have.status(200);
});

it('should now be able to access the user login metadata', async function () {
const res = await chai.request(app).get('/api/auth/userLoggedIn').set('Cookie', `${cookie}`);
res.should.have.status(200);
});

it('should now be able to access the profile', async function () {
const res = await chai.request(app).get('/api/auth/profile').set('Cookie', `${cookie}`);
res.should.have.status(200);
});

it('should now be able to add git account to user', async function () {
const userAccount = { "username": "admin", "gitAccount": "test", "email": "[email protected]", "admin": true };

const res = await chai.request(app)
.post('/api/auth/gitAccount')
.set('Cookie', `${cookie}`)
.send({
username: 'admin',
gitAccount: userAccount
});
res.should.have.status(200);
});

it('should get error when user id/name is not sent in req.bosy', async function () {
const userAccount = { "username": "admin", "gitAccount": "test", "email": "[email protected]", "admin": true };

const res = await chai.request(app)
.post('/api/auth/gitAccount')
.set('Cookie', `${cookie}`)
.send({
gitAccount: userAccount
});
res.should.have.status(500);
});


it('should now be able to logout', async function () {
const res = await chai.request(app).post('/api/auth/logout').set('Cookie', `${cookie}`);
res.should.have.status(200);
});

it('should not get login success', async function () {
const res = await chai.request(app).get('/api/auth/success').set('Cookie', `${cookie}`);
res.should.have.status(401);
});

it('should not be able to add git account without login', async function () {
const userAccount = { "username": "admin", "gitAccount": "test", "email": "[email protected]", "admin": true };

const res = await chai.request(app)
.post('/api/auth/gitAccount')
.set('Cookie', `${cookie}`)
.send({
username: 'admin',
gitAccount: userAccount
});
res.should.have.status(401);
});

it('test cannot access profile page', async function () {
const res = await chai.request(app).get('/api/auth/profile').set('Cookie', `${cookie}`);

res.should.have.status(401);
});

it('test cannot get login status', async function () {
const res = await chai.request(app).get('/api/auth/userLoggedIn').set('Cookie', `${cookie}`);

res.should.have.status(401);
});
});

after(async function () {
await service.httpServer.close();
});
});
4 changes: 3 additions & 1 deletion test/testClearBareClone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const expect = chai.expect;
const timestamp = Date.now();

describe('clear bare and local clones', async () => {
it('pull remote generates a local .remote folder', async () => {
it('pull remote generates a local .remote folder', async function () {
// eslint-disable-next-line no-invalid-this
this.timeout(5000);
const action = new Action('123', 'type', 'get', timestamp, 'finos/git-proxy');
action.url = 'https://github.com/finos/git-proxy';
await pullRemote({}, action);
Expand Down
70 changes: 0 additions & 70 deletions test/testLogin.test.js

This file was deleted.

53 changes: 0 additions & 53 deletions test/testPush.test.js

This file was deleted.

Loading
Loading