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

Commit

Permalink
feat: support custom tokenService (#1658)
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu authored Jul 28, 2021
1 parent e258241 commit d28af7f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 3 additions & 1 deletion controllers/registry/token/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ module.exports = function* createToken() {
return;
}

var token = yield tokenService.createToken(this.user.name, {
var tokenServiceImpl = this.tokenService || tokenService;

var token = yield tokenServiceImpl.createToken(this.user.name, {
readonly: !!readonly,
cidrWhitelist: cidrWhitelist || [],
});
Expand Down
1 change: 0 additions & 1 deletion controllers/sync_module_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,6 @@ function* _listStarUsers(modName) {
}

function* _saveNpmUser(username) {
var user = yield npmSerivce.getUser(username);
if (!user) {
var existsUser = yield User.findByName(username);
if (existsUser && existsUser.isNpmUser) {
Expand Down
25 changes: 25 additions & 0 deletions test/controllers/registry/token/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var request = require('supertest');
var app = require('../../../../servers/registry');
var tokenService = require('../../../../services/token');
var TestUtil = require('../../../utils');
var create = require('../../../../controllers/registry/token/create');

describe('test/controllers/registry/token/create.test.js', function () {
describe('POST /-/npm/v1/tokens', function () {
Expand Down Expand Up @@ -76,4 +77,28 @@ describe('test/controllers/registry/token/create.test.js', function () {
});
});
});

describe('inject tokenService', () => {
it('should work', function* () {
const ctx = {
request: {
body: {
readonly: false,
cidr_whitelist: [],
password: TestUtil.admin,
},
},
user: {
name: TestUtil.admin,
},
tokenService: {
createToken(userId) {
return `${userId}:token`
}
},
};
yield Reflect.apply(create, ctx, []);
ctx.body.should.equal(`${TestUtil.admin}:token`);
});
});
});

0 comments on commit d28af7f

Please sign in to comment.