Skip to content

Commit

Permalink
fix: split tokengen (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
d2lam authored Mar 18, 2019
1 parent 8fc85fe commit e125047
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ExecutorQueue extends Executor {
this.periodicBuildTable = `${this.prefix}periodicBuildConfigs`;
this.frozenBuildTable = `${this.prefix}frozenBuildConfigs`;
this.tokenGen = null;
this.userTokenGen = null;
this.pipelineFactory = config.pipelineFactory;

const redisConnection = Object.assign({}, config.redisConnection, { pkg: 'ioredis' });
Expand Down Expand Up @@ -186,7 +187,7 @@ class ExecutorQueue extends Executor {
async postBuildEvent({ pipeline, job, apiUri, eventId }) {
const pipelineInstance = await this.pipelineFactory.get(pipeline.id);
const admin = await pipelineInstance.getFirstAdmin();
const jwt = this.tokenGen(admin.username, {}, pipeline.scmContext);
const jwt = this.userTokenGen(admin.username, {}, pipeline.scmContext);

winston.info(`POST event for pipeline ${pipeline.id}:${job.name}` +
`using user ${admin.username}`);
Expand Down Expand Up @@ -278,8 +279,8 @@ class ExecutorQueue extends Executor {
{ separator: '>' });

// Save tokenGen to current executor object so we can access it in postBuildEvent
if (!this.tokenGen) {
this.tokenGen = tokenGen;
if (!this.userTokenGen) {
this.userTokenGen = tokenGen;
}

if (isUpdate) {
Expand Down
10 changes: 4 additions & 6 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const partialTestConfig = {
};
const partialTestConfigToString = Object.assign({}, partialTestConfig, {
blockedBy: blockedBy.toString() });
const tokenGen = sinon.stub().returns('123456abc');
const userTokenGen = sinon.stub().returns('admintoken');
const testDelayedConfig = {
pipeline: testPipeline,
job: testJob,
apiUri: 'http://localhost',
tokenGen
tokenGen: userTokenGen
};
const testAdmin = {
username: 'admin'
Expand Down Expand Up @@ -312,7 +312,7 @@ describe('index test', () => {
url: 'http://localhost/v4/events',
method: 'POST',
headers: {
Authorization: 'Bearer 123456abc',
Authorization: 'Bearer admintoken',
'Content-Type': 'application/json'
},
json: true,
Expand All @@ -325,8 +325,6 @@ describe('index test', () => {
retryStrategy: executor.requestRetryStrategy
};

executor.tokenGen = tokenGen;

return executor.startPeriodic(testDelayedConfig).then(() => {
assert.calledOnce(queueMock.connect);
assert.notCalled(redisMock.hset);
Expand All @@ -335,7 +333,7 @@ describe('index test', () => {
jobId: testJob.id
}]);
assert.calledWith(redisMock.hdel, 'periodicBuildConfigs', testJob.id);

assert.calledOnce(userTokenGen);
assert.calledWith(reqMock, options);
});
});
Expand Down

0 comments on commit e125047

Please sign in to comment.