Skip to content

Commit

Permalink
feat: accept eventsource init dict
Browse files Browse the repository at this point in the history
  • Loading branch information
john-u committed Apr 20, 2021
1 parent 2af5fec commit 45eb35f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"@oclif/errors": "1.3.3",
"@smartthings/core-sdk": "^1.3.3",
"@types/eventsource": "^1.1.5",
"axios": "^0.21.1",
"chalk": "^4.1.0",
"cli-table": "^0.3.1",
Expand All @@ -40,7 +41,6 @@
"devDependencies": {
"@oclif/dev-cli": "^1.22.2",
"@types/cli-table": "^0.3.0",
"@types/eventsource": "^1.1.5",
"@types/express": "^4.17.6",
"@types/jest": "^26.0.14",
"@types/js-yaml": "^3.12.3",
Expand Down
15 changes: 8 additions & 7 deletions packages/lib/src/__tests__/login-authenticator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('LoginAuthenticator', () => {
'deviceId': 'device id',
},
}
const codeVerifierRegex = /\bcode_verifier=[\w|-]+\b/
interface AuthTokenResponse {
access_token: string
refresh_token: string
Expand Down Expand Up @@ -163,20 +164,20 @@ describe('LoginAuthenticator', () => {
expect(mkdirMock).toHaveBeenCalledWith('/full/path/to/file', { recursive: true })
})

it('sets _credentialsFile properly', function() {
it('sets _credentialsFile properly', function () {
LoginAuthenticator.init(credentialsFilename)

expect((global as { _credentialsFile?: string })._credentialsFile).toBe(credentialsFilename)
})
})

describe('constructor', () => {
it('throws exception when init not called', function() {
it('throws exception when init not called', function () {
expect(() => new LoginAuthenticator(profileName, clientIdProvider))
.toThrow('LoginAuthenticator credentials file not set.')
})

it('constructs without errors', function() {
it('constructs without errors', function () {
LoginAuthenticator.init(credentialsFilename)

const loginAuthenticator = new LoginAuthenticator(profileName, clientIdProvider)
Expand Down Expand Up @@ -229,7 +230,7 @@ describe('LoginAuthenticator', () => {
const postData = postMock.mock.calls[0][1]
expect(postData).toMatch(/\bgrant_type=authorization_code\b/)
expect(postData).toMatch(/\bclient_id=client-id\b/)
expect(postData).toMatch(/\bcode_verifier=\w+\b/)
expect(postData).toMatch(codeVerifierRegex)
expect(postData).toMatch(/\bcode=auth-code\b/)
expect(postData).toMatch(/\bredirect_uri=http%3A%2F%2Flocalhost%3A7777%2Ffinish\b/)
const postConfig = postMock.mock.calls[0][2]
Expand Down Expand Up @@ -334,7 +335,7 @@ describe('LoginAuthenticator', () => {
const postData = postMock.mock.calls[0][1]
expect(postData).toMatch(/\bgrant_type=authorization_code\b/)
expect(postData).toMatch(/\bclient_id=client-id\b/)
expect(postData).toMatch(/\bcode_verifier=\w+\b/)
expect(postData).toMatch(codeVerifierRegex)
expect(postData).toMatch(/\bcode=auth-code\b/)
expect(postData).toMatch(/\bredirect_uri=http%3A%2F%2Flocalhost%3A7777%2Ffinish\b/)
const postConfig = postMock.mock.calls[0][2]
Expand Down Expand Up @@ -411,7 +412,7 @@ describe('LoginAuthenticator', () => {
const postData2 = postMock.mock.calls[1][1]
expect(postData2).toMatch(/\bgrant_type=authorization_code\b/)
expect(postData2).toMatch(/\bclient_id=client-id\b/)
expect(postData2).toMatch(/\bcode_verifier=\w+\b/)
expect(postData2).toMatch(codeVerifierRegex)
expect(postData2).toMatch(/\bcode=auth-code\b/)
expect(postData2).toMatch(/\bredirect_uri=http%3A%2F%2Flocalhost%3A7777%2Ffinish\b/)
const postConfig2 = postMock.mock.calls[1][2]
Expand Down Expand Up @@ -457,7 +458,7 @@ describe('LoginAuthenticator', () => {
const postData = postMock.mock.calls[0][1]
expect(postData).toMatch(/\bgrant_type=authorization_code\b/)
expect(postData).toMatch(/\bclient_id=client-id\b/)
expect(postData).toMatch(/\bcode_verifier=\w+\b/)
expect(postData).toMatch(codeVerifierRegex)
expect(postData).toMatch(/\bcode=auth-code\b/)
expect(postData).toMatch(/\bredirect_uri=http%3A%2F%2Flocalhost%3A7777%2Ffinish\b/)
const postConfig = postMock.mock.calls[0][2]
Expand Down
20 changes: 20 additions & 0 deletions packages/lib/src/__tests__/sse-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Config } from '@oclif/config'
import { v4 as uuidv4 } from 'uuid'
import { SseCommand } from '../sse-command'
import * as sseUtil from '../sse-util'
import EventSource from 'eventsource'


jest.mock('eventsource')
Expand Down Expand Up @@ -42,6 +43,25 @@ describe('SseCommand', () => {
expect(() => { sseCommand.source }).not.toThrow()
})

it('adds auth header with token to eventsource by default', async () => {
await sseCommand.setup({}, [], flags)
await sseCommand.initSource('localhost')

expect(EventSource).toBeCalledWith(
'localhost',
expect.objectContaining({ headers: expect.objectContaining({ Authorization: `Bearer ${flags.token}` }) }),
)
})

it('accepts source init dict and uses it instead of default', async () => {
await sseCommand.setup({}, [], flags)

const initDict = { headers: { 'Cookie': 'test=test' } }
await sseCommand.initSource('localhost', initDict)

expect(EventSource).toBeCalledWith('localhost', initDict)
})

it('registers signal handler on initialization', async () => {
await sseCommand.init()

Expand Down
6 changes: 3 additions & 3 deletions packages/lib/src/sse-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export abstract class SseCommand extends APICommand {
throw new Error('SseCommand not initialized properly')
}

async initSource(url: string): Promise<void> {
const sourceInitDict = { headers: { 'Authorization': `Bearer ${this.token}` } }
this._source = new EventSource(url, sourceInitDict)
async initSource(url: string, sourceInitDict?: EventSource.EventSourceInitDict): Promise<void> {
const eventSourceInitDict = sourceInitDict ?? { headers: { 'Authorization': `Bearer ${this.token}` } }
this._source = new EventSource(url, eventSourceInitDict)

// eslint-disable-next-line @typescript-eslint/no-explicit-any
this._source.onerror = (error: any) => {
Expand Down

0 comments on commit 45eb35f

Please sign in to comment.