2018-06-21 23:33:20 +02:00
|
|
|
import {buildToken} from "../../../src/lib/utils";
|
2018-06-24 10:11:52 +02:00
|
|
|
import {API_ERROR, HTTP_STATUS, TOKEN_BASIC} from "../../../src/lib/constants";
|
|
|
|
import {CREDENTIALS} from "../config.functional";
|
2018-06-21 23:33:20 +02:00
|
|
|
|
2017-12-02 01:50:09 +01:00
|
|
|
export default function(server) {
|
2015-04-21 18:41:50 +02:00
|
|
|
|
2017-12-02 01:50:09 +01:00
|
|
|
describe('package access control', () => {
|
2019-02-03 10:43:55 +01:00
|
|
|
jest.setTimeout(20000000);
|
2018-06-21 23:33:20 +02:00
|
|
|
const buildAccesToken = (auth) => {
|
|
|
|
return buildToken(TOKEN_BASIC, `${(new Buffer(auth).toString('base64'))}`);
|
2017-07-02 00:05:58 +02:00
|
|
|
};
|
2015-04-21 18:41:50 +02:00
|
|
|
|
2017-07-02 00:05:58 +02:00
|
|
|
/**
|
|
|
|
* Check whether the user is allowed to fetch packages
|
|
|
|
* @param auth {object} disable auth
|
|
|
|
* @param pkg {string} package name
|
2019-02-03 10:43:55 +01:00
|
|
|
* @param status {boolean}
|
2017-07-02 00:05:58 +02:00
|
|
|
*/
|
2019-02-03 10:43:55 +01:00
|
|
|
function checkAccess(auth, pkg, status) {
|
2017-12-02 01:50:09 +01:00
|
|
|
test(
|
2019-02-03 10:43:55 +01:00
|
|
|
`${(status ? 'allows' : 'forbids')} access ${auth} to ${pkg}`, () => {
|
2018-06-21 23:33:20 +02:00
|
|
|
server.authstr = auth ? buildAccesToken(auth) : undefined;
|
|
|
|
const req = server.getPackage(pkg);
|
2018-06-24 10:11:52 +02:00
|
|
|
|
2019-02-03 10:43:55 +01:00
|
|
|
if (status === HTTP_STATUS.NOT_FOUND) {
|
2018-06-24 10:11:52 +02:00
|
|
|
return req.status(HTTP_STATUS.NOT_FOUND).body_error(API_ERROR.NO_PACKAGE);
|
2019-02-03 10:43:55 +01:00
|
|
|
} else if (status === HTTP_STATUS.FORBIDDEN) {
|
2018-06-24 10:11:52 +02:00
|
|
|
return req.status(HTTP_STATUS.FORBIDDEN).body_error(API_ERROR.NOT_ALLOWED);
|
2017-12-02 01:50:09 +01:00
|
|
|
}
|
2015-04-21 18:41:50 +02:00
|
|
|
}
|
2017-12-02 01:50:09 +01:00
|
|
|
);
|
2015-04-21 18:41:50 +02:00
|
|
|
}
|
|
|
|
|
2017-07-02 00:05:58 +02:00
|
|
|
/**
|
|
|
|
* Check whether the user is allowed to publish packages
|
|
|
|
* @param auth {object} disable auth
|
|
|
|
* @param pkg {string} package name
|
2019-02-03 10:43:55 +01:00
|
|
|
* @param status {boolean}
|
2017-07-02 00:05:58 +02:00
|
|
|
*/
|
2019-02-03 10:43:55 +01:00
|
|
|
function checkPublish(auth, pkg, status) {
|
|
|
|
test(`${(status ? 'allows' : 'forbids')} publish ${auth} to ${pkg}`, () => {
|
2018-06-21 23:33:20 +02:00
|
|
|
server.authstr = auth ? buildAccesToken(auth) : undefined;
|
2017-08-06 21:54:15 +02:00
|
|
|
const req = server.putPackage(pkg, require('../fixtures/package')(pkg));
|
2019-02-03 10:43:55 +01:00
|
|
|
if (status === HTTP_STATUS.NOT_FOUND) {
|
|
|
|
return req.status(HTTP_STATUS.NOT_FOUND).body_error(API_ERROR.PACKAGE_CANNOT_BE_ADDED);
|
|
|
|
} else if (status === HTTP_STATUS.FORBIDDEN) {
|
|
|
|
return req.status(HTTP_STATUS.FORBIDDEN).body_error(API_ERROR.NOT_ALLOWED_PUBLISH);
|
|
|
|
} else if (status === HTTP_STATUS.CREATED) {
|
|
|
|
return req.status(HTTP_STATUS.CREATED);
|
|
|
|
} else if (status === HTTP_STATUS.CONFLICT) {
|
|
|
|
return req.status(HTTP_STATUS.CONFLICT);
|
2015-04-21 18:41:50 +02:00
|
|
|
}
|
2017-04-19 21:15:28 +02:00
|
|
|
});
|
2015-04-21 18:41:50 +02:00
|
|
|
}
|
2017-07-02 00:05:58 +02:00
|
|
|
|
|
|
|
// credentials
|
|
|
|
const badCredentials = 'test:badpass';
|
|
|
|
// test user is logged by default
|
2018-06-21 23:33:20 +02:00
|
|
|
const validCredentials = `${CREDENTIALS.user}:${CREDENTIALS.password}`;
|
2017-07-02 00:05:58 +02:00
|
|
|
|
|
|
|
// defined on server1 configuration
|
2017-04-19 21:15:28 +02:00
|
|
|
const testAccessOnly = 'test-access-only';
|
|
|
|
const testPublishOnly = 'test-publish-only';
|
|
|
|
const testOnlyTest = 'test-only-test';
|
|
|
|
const testOnlyAuth = 'test-only-auth';
|
2015-04-21 18:41:50 +02:00
|
|
|
|
2018-09-22 12:54:21 +02:00
|
|
|
describe('all are allowed to access', () => {
|
2019-02-03 10:43:55 +01:00
|
|
|
checkAccess(validCredentials, testAccessOnly, HTTP_STATUS.NOT_FOUND);
|
|
|
|
checkAccess(undefined, testAccessOnly, HTTP_STATUS.NOT_FOUND);
|
|
|
|
checkAccess(badCredentials, testAccessOnly, HTTP_STATUS.NOT_FOUND);
|
|
|
|
checkPublish(validCredentials, testAccessOnly, HTTP_STATUS.FORBIDDEN);
|
|
|
|
checkPublish(undefined, testAccessOnly, HTTP_STATUS.FORBIDDEN);
|
|
|
|
checkPublish(badCredentials, testAccessOnly, HTTP_STATUS.FORBIDDEN);
|
2018-09-22 12:54:21 +02:00
|
|
|
});
|
2017-07-02 00:05:58 +02:00
|
|
|
|
2018-09-22 12:54:21 +02:00
|
|
|
describe('all are allowed to publish', () => {
|
2019-02-03 10:43:55 +01:00
|
|
|
checkAccess(validCredentials, testPublishOnly, HTTP_STATUS.FORBIDDEN);
|
|
|
|
checkAccess(undefined, testPublishOnly, HTTP_STATUS.FORBIDDEN);
|
|
|
|
checkAccess(badCredentials, testPublishOnly, HTTP_STATUS.FORBIDDEN);
|
|
|
|
checkPublish(validCredentials, testPublishOnly, HTTP_STATUS.CREATED);
|
|
|
|
checkPublish(undefined, testPublishOnly, HTTP_STATUS.CONFLICT);
|
|
|
|
checkPublish(badCredentials, testPublishOnly, HTTP_STATUS.CONFLICT);
|
2018-09-22 12:54:21 +02:00
|
|
|
});
|
2015-04-21 18:41:50 +02:00
|
|
|
|
2018-09-22 12:54:21 +02:00
|
|
|
describe('only user "test" is allowed to publish and access', () => {
|
2019-02-03 10:43:55 +01:00
|
|
|
checkAccess(validCredentials, testOnlyTest, HTTP_STATUS.NOT_FOUND);
|
|
|
|
checkAccess(undefined, testOnlyTest, HTTP_STATUS.FORBIDDEN);
|
|
|
|
checkAccess(badCredentials, testOnlyTest, HTTP_STATUS.FORBIDDEN);
|
|
|
|
checkPublish(validCredentials, testOnlyTest, HTTP_STATUS.CREATED);
|
|
|
|
checkPublish(undefined, testOnlyTest, HTTP_STATUS.FORBIDDEN);
|
|
|
|
checkPublish(badCredentials, testOnlyTest, HTTP_STATUS.FORBIDDEN);
|
2018-09-22 12:54:21 +02:00
|
|
|
});
|
2015-04-21 18:41:50 +02:00
|
|
|
|
2018-09-22 12:54:21 +02:00
|
|
|
describe('only authenticated users are allowed', () => {
|
2019-02-03 10:43:55 +01:00
|
|
|
checkAccess(validCredentials, testOnlyAuth, HTTP_STATUS.NOT_FOUND);
|
|
|
|
checkAccess(undefined, testOnlyAuth, HTTP_STATUS.FORBIDDEN);
|
|
|
|
checkAccess(badCredentials, testOnlyAuth, HTTP_STATUS.FORBIDDEN);
|
|
|
|
checkPublish(validCredentials, testOnlyAuth, HTTP_STATUS.CREATED);
|
|
|
|
checkPublish(undefined, testOnlyAuth, HTTP_STATUS.FORBIDDEN);
|
|
|
|
checkPublish(badCredentials, testOnlyAuth, HTTP_STATUS.FORBIDDEN);
|
2018-09-22 12:54:21 +02:00
|
|
|
});
|
2017-04-19 21:15:28 +02:00
|
|
|
});
|
2017-12-02 01:50:09 +01:00
|
|
|
}
|