2018-01-31 11:02:35 +00:00
|
|
|
const app = require('../../../server/server');
|
2018-12-21 11:50:28 +00:00
|
|
|
const routes = require('../auth');
|
2018-01-29 13:28:31 +00:00
|
|
|
|
2018-10-17 04:59:43 +00:00
|
|
|
describe('Auth routes', () => {
|
2018-11-22 14:44:33 +00:00
|
|
|
beforeEach(async () => {
|
2018-10-17 04:59:43 +00:00
|
|
|
await app.models.User.destroyById(102);
|
2018-01-30 14:49:22 +00:00
|
|
|
});
|
|
|
|
|
2018-11-22 14:44:33 +00:00
|
|
|
afterAll(async () => {
|
2018-10-17 04:59:43 +00:00
|
|
|
await app.models.User.destroyById(102);
|
2018-09-21 13:01:51 +00:00
|
|
|
});
|
2018-10-17 04:59:43 +00:00
|
|
|
|
2018-01-29 20:20:04 +00:00
|
|
|
let User = app.models.User;
|
2018-01-29 13:28:31 +00:00
|
|
|
let loginFunction;
|
2018-01-29 20:20:04 +00:00
|
|
|
let logoutFunction;
|
2018-01-29 13:28:31 +00:00
|
|
|
let res;
|
|
|
|
let req;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(app, 'post');
|
2018-01-29 20:20:04 +00:00
|
|
|
spyOn(app, 'get').and.callThrough();
|
2018-01-29 13:28:31 +00:00
|
|
|
routes(app);
|
|
|
|
loginFunction = app.post.calls.mostRecent().args[1];
|
2018-01-29 20:20:04 +00:00
|
|
|
logoutFunction = app.get.calls.argsFor(2)[1];
|
2018-01-29 13:28:31 +00:00
|
|
|
res = {};
|
|
|
|
req = {body: {}};
|
|
|
|
});
|
|
|
|
|
2018-03-08 11:23:51 +00:00
|
|
|
describe('when the user doesnt exist but the client does and the password is correct', () => {
|
|
|
|
it('should create the user login and return the token', done => {
|
|
|
|
spyOn(User, 'upsertWithWhere').and.callThrough();
|
|
|
|
req.body.user = 'PetterParker';
|
|
|
|
req.body.password = 'nightmare';
|
|
|
|
res.json = response => {
|
|
|
|
expect(User.upsertWithWhere).toHaveBeenCalledWith(jasmine.any(Object), jasmine.any(Object), jasmine.any(Function));
|
|
|
|
expect(response.token).toBeDefined();
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
loginFunction(req, res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-01-29 13:28:31 +00:00
|
|
|
describe('when the user exists and the password is correct', () => {
|
|
|
|
it('should login and return the token', done => {
|
2018-10-11 07:14:26 +00:00
|
|
|
req.body.user = 'employee';
|
2018-01-29 13:28:31 +00:00
|
|
|
req.body.password = 'nightmare';
|
|
|
|
res.json = response => {
|
|
|
|
expect(response.token).toBeDefined();
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
loginFunction(req, res);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should define the url to continue upon login', done => {
|
2018-10-11 07:14:26 +00:00
|
|
|
req.body.user = 'employee';
|
2018-01-29 13:28:31 +00:00
|
|
|
req.body.password = 'nightmare';
|
|
|
|
req.body.location = 'http://localhost:5000/auth/?apiKey=salix&continue="continueURL"';
|
|
|
|
res.json = response => {
|
|
|
|
expect(response.continue).toBeDefined();
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
loginFunction(req, res);
|
|
|
|
});
|
2018-01-29 13:39:08 +00:00
|
|
|
|
|
|
|
it('should define the loginUrl upon login', done => {
|
2018-10-11 07:14:26 +00:00
|
|
|
req.body.user = 'employee';
|
2018-01-29 13:39:08 +00:00
|
|
|
req.body.password = 'nightmare';
|
|
|
|
req.body.location = 'http://localhost:5000/auth/?apiKey=salix';
|
|
|
|
res.json = response => {
|
|
|
|
expect(response.loginUrl).toBeDefined();
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
loginFunction(req, res);
|
|
|
|
});
|
2018-01-29 20:20:04 +00:00
|
|
|
|
|
|
|
it('should logout after login', done => {
|
|
|
|
spyOn(User, 'logout').and.callThrough();
|
|
|
|
req.accessToken = {id: 'testingTokenId'};
|
|
|
|
logoutFunction(req, res);
|
|
|
|
res.redirect = url => {
|
|
|
|
expect(User.logout).toHaveBeenCalledWith('testingTokenId', jasmine.any(Function));
|
|
|
|
expect(url).toBe('/');
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
});
|
2018-01-29 13:28:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('when the user is incorrect', () => {
|
|
|
|
it('should return a 401 unauthorized', done => {
|
2018-01-29 20:20:04 +00:00
|
|
|
req.body.user = 'IDontExist';
|
2018-01-29 13:28:31 +00:00
|
|
|
req.body.password = 'TotallyWrongPassword';
|
|
|
|
res.status = status => {
|
|
|
|
expect(status).toBe(401);
|
|
|
|
};
|
|
|
|
|
|
|
|
res.json = response => {
|
|
|
|
expect(response.message).toBe('Login failed');
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
loginFunction(req, res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|