Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
feat: new registry api (#1597)
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu authored Oct 21, 2020
1 parent 24a0039 commit 39c3223
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 1 deletion.
7 changes: 7 additions & 0 deletions controllers/registry/user/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

// https://docs.npmjs.com/cli/ping
module.exports = function* () {
this.status = 200;
this.body = {};
};
9 changes: 9 additions & 0 deletions controllers/registry/user/whoami.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

// https://docs.npmjs.com/cli/whoami
module.exports = function* () {
this.status = 200;
this.body = {
username: this.user.name,
};
};
2 changes: 1 addition & 1 deletion docs/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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';
5 changes: 5 additions & 0 deletions middleware/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions routes/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down
78 changes: 78 additions & 0 deletions test/controllers/registry/user/ping.test.js
Original file line number Diff line number Diff line change
@@ -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 changes: 37 additions & 0 deletions test/controllers/registry/user/whoami.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
});

0 comments on commit 39c3223

Please sign in to comment.