mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-01-20 22:52:46 -05:00
test(unit): add unit test for add user
This commit is contained in:
parent
eaa0e3f8bd
commit
d9a008e881
2 changed files with 97 additions and 21 deletions
|
@ -2,8 +2,8 @@ const express = require('express');
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
const Middleware = require('../web/middleware');
|
const Middleware = require('../web/middleware');
|
||||||
const match = Middleware.match;
|
const match = Middleware.match;
|
||||||
const validate_name = Middleware.validate_name;
|
const validateName = Middleware.validate_name;
|
||||||
const validate_pkg = Middleware.validate_package;
|
const validatePkg = Middleware.validate_package;
|
||||||
const encodeScopePackage = Middleware.encodeScopePackage;
|
const encodeScopePackage = Middleware.encodeScopePackage;
|
||||||
|
|
||||||
const whoami = require('./api/whoami');
|
const whoami = require('./api/whoami');
|
||||||
|
@ -21,12 +21,12 @@ module.exports = function(config, auth, storage) {
|
||||||
|
|
||||||
// validate all of these params as a package name
|
// validate all of these params as a package name
|
||||||
// this might be too harsh, so ask if it causes trouble
|
// this might be too harsh, so ask if it causes trouble
|
||||||
app.param('package', validate_pkg);
|
app.param('package', validatePkg);
|
||||||
app.param('filename', validate_name);
|
app.param('filename', validateName);
|
||||||
app.param('tag', validate_name);
|
app.param('tag', validateName);
|
||||||
app.param('version', validate_name);
|
app.param('version', validateName);
|
||||||
app.param('revision', validate_name);
|
app.param('revision', validateName);
|
||||||
app.param('token', validate_name);
|
app.param('token', validateName);
|
||||||
|
|
||||||
// these can't be safely put into express url for some reason
|
// these can't be safely put into express url for some reason
|
||||||
// TODO: For some reason? what reason?
|
// TODO: For some reason? what reason?
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
import request from 'supertest';
|
import request from 'supertest';
|
||||||
|
import _ from 'lodash';
|
||||||
|
import path from 'path';
|
||||||
|
import rimraf from 'rimraf';
|
||||||
|
|
||||||
import configDefault from '../partials/config';
|
import configDefault from '../partials/config';
|
||||||
import Config from '../../../src/lib/config';
|
import Config from '../../../src/lib/config';
|
||||||
import Storage from '../../../src/lib/storage';
|
import Storage from '../../../src/lib/storage';
|
||||||
|
@ -12,37 +16,109 @@ describe('endpoint unit test', () => {
|
||||||
let storage;
|
let storage;
|
||||||
let auth;
|
let auth;
|
||||||
let app;
|
let app;
|
||||||
|
jest.setTimeout(500000);
|
||||||
|
|
||||||
beforeAll(function() {
|
beforeAll(function(done) {
|
||||||
config = new Config(configDefault);
|
const store = path.join(__dirname, '../partials/store/test-storage');
|
||||||
storage = new Storage(config);
|
rimraf(store, () => {
|
||||||
auth = new Auth(config);
|
const configForTest = _.clone(configDefault);
|
||||||
app = indexAPI(config, auth, storage);
|
configForTest.auth = {
|
||||||
|
htpasswd: {
|
||||||
|
file: './test-storage/htpasswd-test'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
configForTest.self_path = store;
|
||||||
|
config = new Config(configForTest);
|
||||||
|
storage = new Storage(config);
|
||||||
|
auth = new Auth(config);
|
||||||
|
app = indexAPI(config, auth, storage);
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('ping unit test', () => {
|
describe('should test ping api', () => {
|
||||||
test('test /-/ping', (done) => {
|
test('should test endpoint /-/ping', (done) => {
|
||||||
request(app)
|
request(app)
|
||||||
.get('/-/ping')
|
.get('/-/ping')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect(200, done)
|
.expect(200)
|
||||||
|
.end(function(err, res) {
|
||||||
|
if (err) {
|
||||||
|
return done(err);
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('whoami unit test', () => {
|
describe('should test whoami api', () => {
|
||||||
test('test /-/whoami', (done) => {
|
test('should test /-/whoami endpoint', (done) => {
|
||||||
request(app)
|
request(app)
|
||||||
.get('/-/whoami')
|
.get('/-/whoami')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect(200, done)
|
.expect(200)
|
||||||
|
.end(function(err, res) {
|
||||||
|
if (err) {
|
||||||
|
return done(err);
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('test /whoami', (done) => {
|
test('should test /whoami endpoint', (done) => {
|
||||||
request(app)
|
request(app)
|
||||||
.get('/-/whoami')
|
.get('/-/whoami')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect(200, done)
|
.expect(200)
|
||||||
|
.end(function(err, res) {
|
||||||
|
if (err) {
|
||||||
|
return done(err);
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('should test user api', () => {
|
||||||
|
const credentials = { name: 'Jota', password: 'secretPass' };
|
||||||
|
|
||||||
|
test('test add a new user', (done) => {
|
||||||
|
|
||||||
|
|
||||||
|
request(app)
|
||||||
|
.put('/-/user/org.couchdb.user:jota')
|
||||||
|
.send(credentials)
|
||||||
|
.expect('Content-Type', /json/)
|
||||||
|
.expect(201)
|
||||||
|
.end(function(err, res) {
|
||||||
|
if (err) {
|
||||||
|
return done(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(res.body.ok).toBeDefined();
|
||||||
|
expect(res.body.ok).toMatch(`user '${credentials.name}' created`);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('test fails add a new user', (done) => {
|
||||||
|
|
||||||
|
request(app)
|
||||||
|
.put('/-/user/org.couchdb.user:jota')
|
||||||
|
.send(credentials)
|
||||||
|
.expect('Content-Type', /json/)
|
||||||
|
.expect(409)
|
||||||
|
.end(function(err, res) {
|
||||||
|
if (err) {
|
||||||
|
return done(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(res.body.error).toBeDefined();
|
||||||
|
expect(res.body.error).toMatch(/this user already exists/);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue