feat: new registry api (#1597)

This commit is contained in:
killa
2020-10-21 13:53:44 +08:00
committed by GitHub
parent 24a0039188
commit 39c322332f
7 changed files with 142 additions and 1 deletions

View File

@@ -0,0 +1,7 @@
'use strict';
// https://docs.npmjs.com/cli/ping
module.exports = function* () {
this.status = 200;
this.body = {};
};

View File

@@ -0,0 +1,9 @@
'use strict';
// https://docs.npmjs.com/cli/whoami
module.exports = function* () {
this.status = 200;
this.body = {
username: this.user.name,
};
};

View File

@@ -330,5 +330,5 @@ CREATE TABLE IF NOT EXISTS `token` (
`cidr_whitelist` varchar(500) NOT NULL COMMENT 'ip list, ["127.0.0.1"]',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_token` (`token`),
KEY `idx_user` (`user`)
KEY `idx_user` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='token info';

View File

@@ -3,6 +3,11 @@
var http = require('http');
module.exports = function *login(next) {
if (this.path === '/-/ping' && this.query.write !== 'true') {
yield next;
return;
}
if (this.user.error) {
var status = this.user.error.status;
this.status = http.STATUS_CODES[status]

View File

@@ -29,6 +29,8 @@ var listPackagesByUser = require('../controllers/registry/package/list_by_user')
var addUser = require('../controllers/registry/user/add');
var showUser = require('../controllers/registry/user/show');
var updateUser = require('../controllers/registry/user/update');
var whoami = require('../controllers/registry/user/whoami');
var ping = require('../controllers/registry/user/ping');
var createToken = require('../controllers/registry/token/create');
var delToken = require('../controllers/registry/token/del');
@@ -58,6 +60,9 @@ function routes(app) {
app.get('/-/allversions', listAllPackageVersions);
app.get('/-/whoami', login, whoami);
app.get('/-/ping', login, ping);
// module
// scope package: params: [$name]
app.get(/^\/(@[\w\-\.]+\/[^\/]+)$/, syncByInstall, listAllVersions);

View File

@@ -0,0 +1,78 @@
'use strict';
var should = require('should');
var request = require('supertest');
var mm = require('mm');
var app = require('../../../../servers/registry');
var config = require('../../../../config');
var tokenService = require('../../../../services/token');
var TestUtil = require('../../../utils');
describe('test/controllers/registry/user/ping.test.js', function () {
afterEach(mm.restore);
describe('/-/ping', function () {
var token;
beforeEach(function* () {
mm(config, 'syncModel', 'all');
token = yield tokenService.createToken(TestUtil.admin);
});
afterEach(function* () {
yield tokenService.deleteToken(TestUtil.admin, token.token);
});
describe('with write', function () {
describe('has login', function () {
it('should work', function (done) {
request(app)
.get('/-/ping?write=true')
.set('authorization', 'Bearer ' + token.token)
.expect(200, function (err) {
should.not.exist(err);
done();
});
});
});
describe('has not login', function () {
it('should work', function (done) {
request(app)
.get('/-/ping?write=true')
.set('authorization', 'Bearer mock_token')
.expect(401, function (err) {
should.not.exist(err);
done();
});
});
});
});
describe('with not write', function () {
describe('has login', function () {
it('should work', function (done) {
request(app)
.get('/-/ping')
.set('authorization', 'Bearer ' + token.token)
.expect(200, function (err) {
should.not.exist(err);
done();
});
});
});
describe('has not login', function () {
it('should work', function (done) {
request(app)
.get('/-/ping')
.set('authorization', 'Bearer ' + token.token)
.expect(200, function (err) {
should.not.exist(err);
done();
});
});
});
});
});
});

View File

@@ -0,0 +1,37 @@
'use strict';
var should = require('should');
var request = require('supertest');
var mm = require('mm');
var app = require('../../../../servers/registry');
var config = require('../../../../config');
var tokenService = require('../../../../services/token');
var TestUtil = require('../../../utils');
describe('test/controllers/registry/user/whoami.test.js', function () {
afterEach(mm.restore);
describe('/-/whoami', function () {
var token;
beforeEach(function* () {
mm(config, 'syncModel', 'all');
token = yield tokenService.createToken(TestUtil.admin);
});
afterEach(function* () {
yield tokenService.deleteToken(TestUtil.admin, token.token);
});
it('should work', function (done) {
request(app)
.get('/-/whoami')
.set('authorization', 'Bearer ' + token.token)
.expect(200, function (err, res) {
should.not.exist(err);
res.body.username.should.eql(TestUtil.admin);
done();
});
});
});
});