2021-03-14 02:42:46 -05:00
|
|
|
import { HTTP_STATUS, API_ERROR } from '../../../src/lib/constants';
|
2018-06-22 13:31:43 -05:00
|
|
|
|
2021-03-14 02:42:46 -05:00
|
|
|
export default function (server2) {
|
2018-06-22 13:31:43 -05:00
|
|
|
// credentials
|
|
|
|
const USER1 = 'authtest';
|
|
|
|
const USER2 = 'authtest2';
|
2018-06-23 01:35:06 -05:00
|
|
|
const CORRECT_PASSWORD = 'blahblah-password';
|
2018-06-22 13:31:43 -05:00
|
|
|
const WRONG_PASSWORD = 'wrongpass1';
|
|
|
|
// package names
|
|
|
|
const DENY_PKG_NAME = 'test-auth-deny';
|
|
|
|
const AUTH_PKG_ACCESS_NAME = 'test-auth-regular';
|
|
|
|
const ONLY_ACCESS_BY_USER_2 = 'test-deny';
|
|
|
|
const UNEXISTING_PKG_NAME = 'test-auth-allow';
|
2015-04-11 08:09:19 -05:00
|
|
|
|
2018-01-27 20:40:07 -05:00
|
|
|
const requestAuthFail = (user, pass, message, statusCode) => {
|
2021-03-14 02:42:46 -05:00
|
|
|
return server2
|
|
|
|
.auth(user, pass)
|
2018-01-27 20:40:07 -05:00
|
|
|
.status(statusCode)
|
2017-08-06 14:54:15 -05:00
|
|
|
.body_error(message)
|
2021-03-14 02:42:46 -05:00
|
|
|
.then(function () {
|
2017-08-06 14:54:15 -05:00
|
|
|
return server2.whoami();
|
|
|
|
})
|
2021-03-14 02:42:46 -05:00
|
|
|
.then(function (username) {
|
2018-06-22 13:31:43 -05:00
|
|
|
expect(username).toBeUndefined();
|
2017-08-06 14:54:15 -05:00
|
|
|
});
|
|
|
|
};
|
2018-01-27 20:40:07 -05:00
|
|
|
const requestAuthOk = (user, pass, regex, statusCode) => {
|
2021-03-14 02:42:46 -05:00
|
|
|
return server2
|
|
|
|
.auth(user, pass)
|
2018-01-27 20:40:07 -05:00
|
|
|
.status(statusCode)
|
2017-08-06 14:54:15 -05:00
|
|
|
.body_ok(regex)
|
2021-03-14 02:42:46 -05:00
|
|
|
.then(function () {
|
2017-08-06 14:54:15 -05:00
|
|
|
return server2.whoami();
|
|
|
|
})
|
2021-03-14 02:42:46 -05:00
|
|
|
.then(function (username) {
|
2018-06-22 13:31:43 -05:00
|
|
|
expect(username).toBe(user);
|
2017-08-06 14:54:15 -05:00
|
|
|
});
|
|
|
|
};
|
2015-04-11 08:09:19 -05:00
|
|
|
|
2018-06-22 13:31:43 -05:00
|
|
|
describe('plugin authentication', () => {
|
|
|
|
describe('test users authentication', () => {
|
|
|
|
test('should not authenticate user1 with wrong password', () => {
|
2021-03-14 02:42:46 -05:00
|
|
|
return requestAuthFail(
|
|
|
|
USER1,
|
|
|
|
WRONG_PASSWORD,
|
|
|
|
"i don't like your password",
|
|
|
|
HTTP_STATUS.UNAUTHORIZED
|
|
|
|
);
|
2017-04-19 14:15:28 -05:00
|
|
|
});
|
2015-04-11 08:09:19 -05:00
|
|
|
|
2018-06-22 13:31:43 -05:00
|
|
|
test('should not authenticate user2 with wrong password', () => {
|
2021-03-14 02:42:46 -05:00
|
|
|
return requestAuthFail(
|
|
|
|
USER2,
|
|
|
|
WRONG_PASSWORD,
|
|
|
|
"i don't like your password",
|
|
|
|
HTTP_STATUS.UNAUTHORIZED
|
|
|
|
);
|
2017-04-19 14:15:28 -05:00
|
|
|
});
|
2015-04-11 08:09:19 -05:00
|
|
|
|
2018-06-22 13:31:43 -05:00
|
|
|
test('should right user2 password handled by plugin', () => {
|
|
|
|
return requestAuthOk(USER2, CORRECT_PASSWORD, new RegExp(USER2), HTTP_STATUS.CREATED);
|
2017-04-19 14:15:28 -05:00
|
|
|
});
|
2015-04-11 08:09:19 -05:00
|
|
|
|
2018-06-22 13:31:43 -05:00
|
|
|
test('should right user1 password handled by plugin', () => {
|
|
|
|
return requestAuthOk(USER1, CORRECT_PASSWORD, new RegExp(USER1), HTTP_STATUS.CREATED);
|
2017-04-19 14:15:28 -05:00
|
|
|
});
|
2018-06-22 13:31:43 -05:00
|
|
|
});
|
2015-04-11 08:09:19 -05:00
|
|
|
|
2018-06-22 13:31:43 -05:00
|
|
|
describe('test package access authorization', () => {
|
2019-07-16 01:40:01 -05:00
|
|
|
describe(`access with user ${USER1} on server2`, () => {
|
2021-03-14 02:42:46 -05:00
|
|
|
beforeAll(function () {
|
|
|
|
return server2
|
|
|
|
.auth(USER1, CORRECT_PASSWORD)
|
2019-07-16 01:40:01 -05:00
|
|
|
.status(HTTP_STATUS.CREATED)
|
|
|
|
.body_ok(new RegExp(USER1));
|
2018-06-22 13:31:43 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test(`should fails (404) on access ${UNEXISTING_PKG_NAME}`, () => {
|
2021-03-14 02:42:46 -05:00
|
|
|
return server2
|
|
|
|
.getPackage(UNEXISTING_PKG_NAME)
|
2019-07-16 01:40:01 -05:00
|
|
|
.status(HTTP_STATUS.NOT_FOUND)
|
|
|
|
.body_error(API_ERROR.NO_PACKAGE);
|
2018-06-22 13:31:43 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test(`should fails (403) access ${ONLY_ACCESS_BY_USER_2}`, () => {
|
2021-03-14 02:42:46 -05:00
|
|
|
return server2
|
|
|
|
.getPackage(ONLY_ACCESS_BY_USER_2)
|
2019-07-16 01:40:01 -05:00
|
|
|
.status(HTTP_STATUS.FORBIDDEN)
|
|
|
|
.body_error(API_ERROR.NOT_ALLOWED);
|
2018-06-22 13:31:43 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test(`should fails (404) access ${AUTH_PKG_ACCESS_NAME}`, () => {
|
2021-03-14 02:42:46 -05:00
|
|
|
return server2
|
|
|
|
.getPackage(AUTH_PKG_ACCESS_NAME)
|
2019-07-16 01:40:01 -05:00
|
|
|
.status(HTTP_STATUS.NOT_FOUND)
|
|
|
|
.body_error(API_ERROR.NO_PACKAGE);
|
2018-06-22 13:31:43 -05:00
|
|
|
});
|
2017-04-19 14:15:28 -05:00
|
|
|
});
|
2015-04-11 08:09:19 -05:00
|
|
|
|
2018-06-22 13:31:43 -05:00
|
|
|
describe(`access with user ${USER2} on server2`, () => {
|
2021-03-14 02:42:46 -05:00
|
|
|
beforeAll(function () {
|
|
|
|
return server2
|
|
|
|
.auth(USER2, CORRECT_PASSWORD)
|
2019-07-16 01:40:01 -05:00
|
|
|
.status(HTTP_STATUS.CREATED)
|
|
|
|
.body_ok(new RegExp(USER2));
|
2018-06-22 13:31:43 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test(`should fails (403) on access ${UNEXISTING_PKG_NAME}`, () => {
|
2021-03-14 02:42:46 -05:00
|
|
|
return server2
|
|
|
|
.getPackage(UNEXISTING_PKG_NAME)
|
2019-07-16 01:40:01 -05:00
|
|
|
.status(HTTP_STATUS.FORBIDDEN)
|
|
|
|
.body_error(API_ERROR.NOT_ALLOWED);
|
2018-06-22 13:31:43 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test(`should fails (403) on access ${DENY_PKG_NAME}`, () => {
|
2021-03-14 02:42:46 -05:00
|
|
|
return server2
|
|
|
|
.getPackage(DENY_PKG_NAME)
|
2019-07-16 01:40:01 -05:00
|
|
|
.status(HTTP_STATUS.FORBIDDEN)
|
|
|
|
.body_error(API_ERROR.NOT_ALLOWED);
|
2018-06-22 13:31:43 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test(`should fails (404) access ${AUTH_PKG_ACCESS_NAME}`, () => {
|
2021-03-14 02:42:46 -05:00
|
|
|
return server2
|
|
|
|
.getPackage(AUTH_PKG_ACCESS_NAME)
|
2019-07-16 01:40:01 -05:00
|
|
|
.status(HTTP_STATUS.NOT_FOUND)
|
|
|
|
.body_error(API_ERROR.NO_PACKAGE);
|
2018-06-22 13:31:43 -05:00
|
|
|
});
|
2017-04-19 14:15:28 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-12-02 05:19:08 -05:00
|
|
|
}
|