0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-23 22:27:34 -05:00
verdaccio/test/unit/api.spec.js

125 lines
3 KiB
JavaScript
Raw Normal View History

import request from 'supertest';
2018-01-25 17:14:03 -05:00
import _ from 'lodash';
import path from 'path';
import rimraf from 'rimraf';
2018-01-25 17:31:47 -05:00
import configDefault from './partials/config';
import Config from '../../src/lib/config';
import Storage from '../../src/lib/storage';
import Auth from '../../src/lib/auth';
import indexAPI from '../../src/api/index';
2018-01-25 17:31:47 -05:00
require('../../src/lib/logger').setup([]);
describe('endpoint unit test', () => {
let config;
let storage;
let auth;
let app;
2018-01-25 17:14:03 -05:00
jest.setTimeout(500000);
2018-01-25 17:14:03 -05:00
beforeAll(function(done) {
2018-01-25 17:31:47 -05:00
const store = path.join(__dirname, './partials/store/test-storage');
2018-01-25 17:14:03 -05:00
rimraf(store, () => {
const configForTest = _.clone(configDefault);
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();
});
});
2018-01-25 17:14:03 -05:00
describe('should test ping api', () => {
test('should test endpoint /-/ping', (done) => {
request(app)
.get('/-/ping')
.expect('Content-Type', /json/)
2018-01-25 17:14:03 -05:00
.expect(200)
.end(function(err, res) {
if (err) {
return done(err);
}
done();
});
});
});
2018-01-25 17:14:03 -05:00
describe('should test whoami api', () => {
test('should test /-/whoami endpoint', (done) => {
request(app)
.get('/-/whoami')
.expect('Content-Type', /json/)
2018-01-25 17:14:03 -05:00
.expect(200)
.end(function(err, res) {
if (err) {
return done(err);
}
done();
});
});
2018-01-25 17:14:03 -05:00
test('should test /whoami endpoint', (done) => {
request(app)
.get('/-/whoami')
.expect('Content-Type', /json/)
2018-01-25 17:14:03 -05:00
.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();
});
});
2018-01-25 17:14:03 -05:00
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();
});
});
});
});