0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-04-15 03:02:51 -05:00

refactor: more unit test constants usage

This commit is contained in:
Juan Picado @jotadeveloper 2018-06-21 23:33:20 +02:00
parent 2c8c8f0295
commit 970c0461df
No known key found for this signature in database
GPG key ID: 18AC54485952D158
3 changed files with 34 additions and 45 deletions

View file

@ -1,4 +1,3 @@
import assert from 'assert';
import _ from 'lodash';
import {HEADERS} from '../../../src/lib/constants';
@ -12,7 +11,7 @@ export default function(express) {
'Content-Type': HEADERS.JSON
}],
endpoint: "http://localhost:55550/api/notify",
content: '{"color":"green","message":"New package published: * {{ name }}*. Publisher name: * {{ publisher.name }} *.","notify":true,"message_format":"text"}'
content: `{"color":"green","message":"New package published: * {{ name }}*. Publisher name: * {{ publisher.name }} *.","notify":true,"message_format":"text"}`
}
};
@ -45,15 +44,11 @@ export default function(express) {
notify(metadata, config, publisherInfo).then(function (body) {
const jsonBody = parseBody(body);
assert.ok(
`New package published: * ${metadata.name}*. Publisher name: * ${
publisherInfo.name
} *.` === jsonBody.message,
"Body notify message should be equal"
);
expect(
`New package published: * ${metadata.name}*. Publisher name: * ${publisherInfo.name} *.`).toBe(jsonBody.message, "Body notify message should be equal");
done();
}, function (err) {
assert.fail(err);
expect(err).toBeDefined();
done();
});
});
@ -70,15 +65,12 @@ export default function(express) {
notify(metadata, configMultipleHeader, publisherInfo).then(function (body) {
const jsonBody = parseBody(body);
assert.ok(
`New package published: * ${metadata.name}*. Publisher name: * ${
publisherInfo.name
} *.` === jsonBody.message,
"Body notify message should be equal"
);
expect(
`New package published: * ${metadata.name}*. Publisher name: * ${publisherInfo.name} *.`)
.toBe(jsonBody.message, "Body notify message should be equal");
done();
}, function (err) {
assert.fail(err);
expect(err).toBeDefined();
done();
});
});
@ -106,16 +98,13 @@ export default function(express) {
notify(metadata, multipleNotificationsEndpoint, publisherInfo).then(function (body) {
body.forEach(function(notification) {
const jsonBody = parseBody(notification);
assert.ok(
`New package published: * ${metadata.name}*. Publisher name: * ${
publisherInfo.name
} *.` === jsonBody.message,
"Body notify message should be equal"
);
expect(
`New package published: * ${metadata.name}*. Publisher name: * ${publisherInfo.name} *.`)
.toBe(jsonBody.message, "Body notify message should be equal");
});
done();
}, function (err) {
assert.fail(err);
expect(err).toBeDefined();
done();
});
});
@ -128,7 +117,7 @@ export default function(express) {
configFail.notify.endpoint = "http://localhost:55550/api/notify/bad";
notify(metadata, configFail, publisherInfo).then(function () {
assert.equal(false, 'This service should fails with status code 400');
expect(false).toBe('This service should fails with status code 400');
done();
}, function (err) {
expect(err).toEqual('bad response');
@ -147,16 +136,12 @@ export default function(express) {
notify(metadata, config, publisherInfo).then(
function(body) {
const jsonBody = parseBody(body);
assert.ok(
`New package published: * ${metadata.name}*. Publisher name: * ${
metadata.publisher.name
} *.` === jsonBody.message,
"Body notify message should be equal"
);
expect(`New package published: * ${metadata.name}*. Publisher name: * ${metadata.publisher.name} *.`)
.toBe(jsonBody.message, "Body notify message should be equal");
done();
},
function(err) {
assert.fail(err);
expect(err).toBeDefined();
done();
}
);

View file

@ -1,8 +1,12 @@
import {buildToken} from "../../../src/lib/utils";
import {HTTP_STATUS, TOKEN_BASIC} from "../../../src/lib/constants";
import {CREDENTIALS} from "../config.func";
export default function(server) {
describe('package access control', () => {
const buildToken = (auth) => {
return `Basic ${(new Buffer(auth).toString('base64'))}`;
const buildAccesToken = (auth) => {
return buildToken(TOKEN_BASIC, `${(new Buffer(auth).toString('base64'))}`);
};
/**
@ -13,14 +17,13 @@ export default function(server) {
*/
function checkAccess(auth, pkg, ok) {
test(
(ok ? 'allows' : 'forbids') +' access ' + auth + ' to ' + pkg,
() => {
server.authstr = auth ? buildToken(auth) : undefined;
let req = server.getPackage(pkg);
(ok ? 'allows' : 'forbids') + ' access ' + auth + ' to ' + pkg, () => {
server.authstr = auth ? buildAccesToken(auth) : undefined;
const req = server.getPackage(pkg);
if (ok) {
return req.status(404).body_error(/no such package available/);
return req.status(HTTP_STATUS.NOT_FOUND).body_error(/no such package available/);
} else {
return req.status(403).body_error(/not allowed to access package/);
return req.status(HTTP_STATUS.FORBIDDEN).body_error(/not allowed to access package/);
}
}
);
@ -34,12 +37,12 @@ export default function(server) {
*/
function checkPublish(auth, pkg, ok) {
test(`${(ok ? 'allows' : 'forbids')} publish ${auth} to ${pkg}`, () => {
server.authstr = auth ? buildToken(auth) : undefined;
server.authstr = auth ? buildAccesToken(auth) : undefined;
const req = server.putPackage(pkg, require('../fixtures/package')(pkg));
if (ok) {
return req.status(404).body_error(/this package cannot be added/);
return req.status(HTTP_STATUS.NOT_FOUND).body_error(/this package cannot be added/);
} else {
return req.status(403).body_error(/not allowed to publish package/);
return req.status(HTTP_STATUS.FORBIDDEN).body_error(/not allowed to publish package/);
}
});
}
@ -47,7 +50,7 @@ export default function(server) {
// credentials
const badCredentials = 'test:badpass';
// test user is logged by default
const validCredentials = 'test:test';
const validCredentials = `${CREDENTIALS.user}:${CREDENTIALS.password}`;
// defined on server1 configuration
const testAccessOnly = 'test-access-only';

View file

@ -6,6 +6,7 @@ import smartRequest from './request';
import type {IServerBridge} from '../types';
import {HEADERS, HTTP_STATUS, TOKEN_BASIC} from '../../src/lib/constants';
import {buildToken} from "../../src/lib/utils";
import {CREDENTIALS} from "../functional/config.func";
const buildAuthHeader = (user, pass): string => {
return buildToken(TOKEN_BASIC, new Buffer(`${user}:${pass}`).toString('base64'));
@ -19,7 +20,7 @@ export default class Server implements IServerBridge {
constructor(url: string) {
this.url = url.replace(/\/$/, '');
this.userAgent = 'node/v8.1.2 linux x64';
this.authstr = buildAuthHeader('test', 'test');
this.authstr = buildAuthHeader(CREDENTIALS.user, CREDENTIALS.password);
}
request(options: any): any {
@ -47,7 +48,7 @@ export default class Server implements IServerBridge {
json: {
name: name,
password: password,
email: 'test@example.com',
email: `${CREDENTIALS.user}@example.com`,
_id: `org.couchdb.user:${name}`,
type: 'user',
roles: [],