From e12504703dfb81e265a31c83d1ae35756bab0c9f Mon Sep 17 00:00:00 2001 From: Dao Lam Date: Mon, 18 Mar 2019 16:03:09 -0700 Subject: [PATCH] fix: split tokengen (#55) --- index.js | 7 ++++--- test/index.test.js | 10 ++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 7f47e60..e7326c3 100644 --- a/index.js +++ b/index.js @@ -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' }); @@ -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}`); @@ -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) { diff --git a/test/index.test.js b/test/index.test.js index 9040997..8faffc8 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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' @@ -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, @@ -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); @@ -335,7 +333,7 @@ describe('index test', () => { jobId: testJob.id }]); assert.calledWith(redisMock.hdel, 'periodicBuildConfigs', testJob.id); - + assert.calledOnce(userTokenGen); assert.calledWith(reqMock, options); }); });