0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-02-17 23:45:29 -05:00

refactor: plugins auth endpoints

This commit is contained in:
Juan Picado @jotadeveloper 2018-06-22 20:31:43 +02:00
parent 5ce09a2d4a
commit d5a29f72b5
No known key found for this signature in database
GPG key ID: 18AC54485952D158
3 changed files with 95 additions and 66 deletions

View file

@ -37,3 +37,8 @@ export const HTTP_STATUS = {
export const PORT_SERVER_1 = '55551';
export const PORT_SERVER_2 = '55552';
export const PORT_SERVER_3 = '55551';
export const PACKAGE_ERROR = {
NO_PACKAGE: 'no such package available',
NOT_ALLOWED: 'not allowed to access package',
};

View file

@ -1,6 +1,17 @@
import assert from 'assert';
import {HTTP_STATUS, PACKAGE_ERROR} from "../../../src/lib/constants";
export default function(server2) {
// credentials
const USER1 = 'authtest';
const USER2 = 'authtest2';
const CORRECT_PASSWORD = 'blahblah';
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';
export default function(server2){
const requestAuthFail = (user, pass, message, statusCode) => {
return server2.auth(user, pass)
.status(statusCode)
@ -9,7 +20,7 @@ export default function(server2){
return server2.whoami();
})
.then(function(username) {
assert.equal(username, null);
expect(username).toBeUndefined();
});
};
const requestAuthOk = (user, pass, regex, statusCode) => {
@ -20,76 +31,87 @@ export default function(server2){
return server2.whoami();
})
.then(function(username) {
assert.equal(username, user);
expect(username).toBe(user);
});
};
describe('test default authentication', () => {
describe('plugin authentication', () => {
describe('test users authentication', () => {
test('should not authenticate user1 with wrong password', () => {
return requestAuthFail(USER1, WRONG_PASSWORD, 'i don\'t like your password', HTTP_STATUS.UNAUTHORIZED);
});
test('should not authenticate user2 with wrong password', () => {
return requestAuthFail(USER2, WRONG_PASSWORD, 'i don\'t like your password', HTTP_STATUS.UNAUTHORIZED);
});
test('should right user2 password handled by plugin', () => {
return requestAuthOk(USER2, CORRECT_PASSWORD, new RegExp(USER2), HTTP_STATUS.CREATED);
});
test('should right user1 password handled by plugin', () => {
return requestAuthOk(USER1, CORRECT_PASSWORD, new RegExp(USER1), HTTP_STATUS.CREATED);
});
test('should not authenticate with wrong password', () => {
return requestAuthFail('authtest', 'wrongpass1', 'i don\'t like your password', 401);
});
test('should right password handled by plugin', () => {
return requestAuthOk('authtest2', 'blahblah', /'authtest2'/, 201);
describe('test package access authorization', () => {
describe(`access with user ${USER1} on server2`, () => {
beforeAll(function() {
return server2.auth(USER1, CORRECT_PASSWORD)
.status(HTTP_STATUS.CREATED)
.body_ok(new RegExp(USER1));
});
test(`should fails (404) on access ${UNEXISTING_PKG_NAME}`, () => {
return server2.getPackage(UNEXISTING_PKG_NAME)
.status(HTTP_STATUS.NOT_FOUND)
.body_error(PACKAGE_ERROR.NO_PACKAGE);
});
test(`should fails (403) access ${ONLY_ACCESS_BY_USER_2}`, () => {
return server2.getPackage(ONLY_ACCESS_BY_USER_2)
.status(HTTP_STATUS.FORBIDDEN)
.body_error(PACKAGE_ERROR.NOT_ALLOWED);
});
test(`should fails (404) access ${AUTH_PKG_ACCESS_NAME}`, () => {
return server2.getPackage(AUTH_PKG_ACCESS_NAME)
.status(HTTP_STATUS.NOT_FOUND)
.body_error(PACKAGE_ERROR.NO_PACKAGE);
});
});
describe(`access with user ${USER2} on server2`, () => {
beforeAll(function() {
return server2.auth(USER2, CORRECT_PASSWORD)
.status(HTTP_STATUS.CREATED)
.body_ok(new RegExp(USER2));
});
test(`should fails (403) on access ${UNEXISTING_PKG_NAME}`, () => {
return server2.getPackage(UNEXISTING_PKG_NAME)
.status(HTTP_STATUS.FORBIDDEN)
.body_error(PACKAGE_ERROR.NOT_ALLOWED);
});
test(`should fails (403) on access ${DENY_PKG_NAME}`, () => {
return server2.getPackage(DENY_PKG_NAME)
.status(HTTP_STATUS.FORBIDDEN)
.body_error(PACKAGE_ERROR.NOT_ALLOWED);
});
test(`should fails (404) access ${AUTH_PKG_ACCESS_NAME}`, () => {
return server2.getPackage(AUTH_PKG_ACCESS_NAME)
.status(HTTP_STATUS.NOT_FOUND)
.body_error(PACKAGE_ERROR.NO_PACKAGE);
});
});
});
});
describe('test access authorization', () => {
describe('access with user authtest', () => {
beforeAll(function() {
return server2.auth('authtest', 'blahblah')
.status(201)
.body_ok(/'authtest'/);
});
test('access test-auth-allow', () => {
return server2.getPackage('test-auth-allow')
.status(404)
.body_error('no such package available');
});
test('access test-deny', () => {
return server2.getPackage('test-deny')
.status(403)
.body_error('not allowed to access package');
});
test('access test-auth-regular', () => {
return server2.getPackage('test-auth-regular')
.status(404)
.body_error('no such package available');
});
});
describe('access with user authtest2', () => {
beforeAll(function() {
return server2.auth('authtest2', 'blahblah')
.status(201)
.body_ok(/'authtest2'/);
});
test('access test-auth-allow', () => {
return server2.getPackage('test-auth-allow')
.status(403)
.body_error('not allowed to access package');
});
test('access test-auth-deny', () => {
return server2.getPackage('test-auth-deny')
.status(403)
.body_error('not allowed to access package');
});
test('access test-auth-regular', () => {
return server2.getPackage('test-auth-regular')
.status(404)
.body_error('no such package available');
});
});
});
}

View file

@ -72,6 +72,7 @@ packages:
access: $all
publish: $all
## start test auth.js
'test-auth-regular':
access: $authenticated
@ -80,6 +81,7 @@ packages:
'test-deny':
access: authtest2
## end test auth.js
'*':
access: test $anonymous