feat: new registry api (#1597)
This commit is contained in:
7
controllers/registry/user/ping.js
Normal file
7
controllers/registry/user/ping.js
Normal file
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
// https://docs.npmjs.com/cli/ping
|
||||
module.exports = function* () {
|
||||
this.status = 200;
|
||||
this.body = {};
|
||||
};
|
||||
9
controllers/registry/user/whoami.js
Normal file
9
controllers/registry/user/whoami.js
Normal 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,
|
||||
};
|
||||
};
|
||||
@@ -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';
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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);
|
||||
|
||||
78
test/controllers/registry/user/ping.test.js
Normal file
78
test/controllers/registry/user/ping.test.js
Normal 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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
37
test/controllers/registry/user/whoami.test.js
Normal file
37
test/controllers/registry/user/whoami.test.js
Normal 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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user