Skip to content

Commit

Permalink
refactor(oauth): use got in place of node-fetch (#138)
Browse files Browse the repository at this point in the history
* refactor(oauth): use got in place of node-fetch

* test(oauth): update cache eviction test
  • Loading branch information
jwulf authored Apr 29, 2024
1 parent b6302d0 commit f81bdea
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 113 deletions.
62 changes: 1 addition & 61 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@
"long": "^4.0.0",
"lossless-json": "^4.0.1",
"neon-env": "^0.1.3",
"node-fetch": "^2.7.0",
"promise-retry": "^1.1.1",
"reflect-metadata": "^0.2.1",
"stack-trace": "0.0.10",
Expand Down
23 changes: 16 additions & 7 deletions src/__tests__/oauth/OAuthProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const jwt = new JSONWebToken(strategy)
const payload = { id: 1 }

const access_token = jwt.generate(payload)
const access_token2 = jwt.generate(payload)

jest.setTimeout(10000)
let server: http.Server
Expand Down Expand Up @@ -194,6 +193,16 @@ test('In-memory cache is populated and evicted after timeout', (done) => {
},
})

const strategy = new HS256Strategy({
ttl: 2000,
secret: 'YOUR_SECRET',
})

const jwt = new JSONWebToken(strategy)
const payload = { id: 1 }

const access_token = jwt.generate(payload)

let requestCount = 0
server = http
.createServer((req, res) => {
Expand All @@ -206,7 +215,7 @@ test('In-memory cache is populated and evicted after timeout', (done) => {
req.on('end', () => {
res.writeHead(200, { 'Content-Type': 'application/json' })
const expiresIn = 2 // seconds
const token = requestCount % 2 === 0 ? access_token : access_token2
const token = `${access_token}${requestCount}`
res.end(`{"access_token": "${token}", "expires_in": ${expiresIn}}`)
requestCount++
expect(body).toEqual(
Expand All @@ -218,13 +227,13 @@ test('In-memory cache is populated and evicted after timeout', (done) => {
.listen(serverPort3002)

o.getToken('ZEEBE').then(async (token) => {
expect(token).toBe(access_token)
expect(token).toBe(`${access_token}0`)
await delay(500)
const token2 = await o.getToken('ZEEBE')
expect(token2).toBe(access_token)
expect(token2).toBe(`${access_token}0`)
await delay(1600)
const token3 = await o.getToken('ZEEBE')
expect(token3).toBe(access_token2)
expect(token3).toBe(`${access_token}1`)
done()
})
})
Expand Down Expand Up @@ -296,7 +305,7 @@ test('Uses a custom audience for an Operate token, if one is configured', (done)
o.getToken('OPERATE')
})

test.only('Passes scope, if provided', () => {
test('Passes scope, if provided', () => {
const serverPort3004 = 3004
const o = new OAuthProvider({
config: {
Expand Down Expand Up @@ -510,7 +519,7 @@ test('Passes no audience for Modeler API when self-hosted', (done) => {

req.on('end', () => {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(`{"token": "${access_token}"}`)
res.end(`{"access_token": "${access_token}"}`)
expect(body).toEqual(
'client_id=clientId17&client_secret=clientSecret&grant_type=client_credentials'
)
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/operate/operate-integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ afterAll(async () => {
restoreZeebeLogging()
})

jest.setTimeout(15000)
jest.setTimeout(20000)
describe('Operate Integration', () => {
xtest('It can get the Incident', async () => {
const c = new OperateApiClient()
Expand Down Expand Up @@ -61,7 +61,7 @@ test('getJSONVariablesforProcess works', async () => {
})

// Wait for Operate to catch up.
await new Promise((res) => setTimeout(() => res(null), 12000))
await new Promise((res) => setTimeout(() => res(null), 15000))
// Make sure that the process instance exists in Operate.
const process = await c.getProcessInstance(p.processInstanceKey)
// If this fails, it is probably a timing issue.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ZeebeGrpcClient } from '../../../zeebe/index'
import { cancelProcesses } from '../../../zeebe/lib/cancelProcesses'
import { DeployResourceResponse, ProcessDeployment } from '../../../zeebe/types'

jest.setTimeout(15000)

suppressZeebeLogging()

let res: DeployResourceResponse<ProcessDeployment> | undefined
Expand All @@ -21,15 +23,12 @@ afterAll(async () => {
const zbc = new ZeebeGrpcClient()

test('ZeebeGrpcClient can migrate a process instance', async () => {
expect(true).toBe(true)
// Deploy a process model

res = await zbc.deployResource({
processFilename: './src/__tests__/testdata/MigrateProcess-Version-1.bpmn',
})

// Create an instance of the process model

const processInstance = await zbc.createProcessInstance({
bpmnProcessId: 'migrant-work',
variables: {},
Expand Down Expand Up @@ -62,6 +61,7 @@ test('ZeebeGrpcClient can migrate a process instance', async () => {
})

// Migrate the process instance to the updated process model

await zbc.migrateProcessInstance({
processInstanceKey: processInstance.processInstanceKey,
migrationPlan: {
Expand Down Expand Up @@ -105,7 +105,7 @@ test('ZeebeGrpcClient can migrate a process instance', async () => {
},
})
})

await zbc.close()
expect(instanceKey).toBe(processInstance.processInstanceKey)
expect(processVersion).toBe('2')
})
Loading

0 comments on commit f81bdea

Please sign in to comment.