Skip to content

Commit

Permalink
adding tests for open int auth
Browse files Browse the repository at this point in the history
  • Loading branch information
pellicceama committed Dec 4, 2024
1 parent d0c3246 commit ab271dc
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion packages/fetch-links/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from 'axios'
import type {HTTPMethod} from './link.js'
import {applyLinks} from './link.js'
import {axiosLink} from './links/axiosLink.js'
import {fetchLink, logLink, retryLink, throwLink} from './links/index.js'
import {authLink, echoLink, fetchLink, logLink, retryLink, throwLink} from './links/index.js'
import {modifyRequest, modifyResponse} from './modifyRequestResponse.js'

const req = new Request('https://httpbin.org/anything', {method: 'GET'})
Expand Down Expand Up @@ -152,3 +152,66 @@ describe.each([

// TODO: Test error handling for both 4xx and 5xx errors
})

describe('authLink', () => {
test('openInt user auth fails with no resourceId', async () => {
try {
await applyLinks(req, [authLink({openInt: {token: '123'}}, 'https://httpbin.org'), fetchLink()])
} catch (e) {
expect(e).toBeInstanceOf(Error)
}
})

test('OpenInt user auth succeeds with resourceId', async () => {
const res = await applyLinks(req, [authLink({openInt: {token: '123', resourceId: '123'}}, 'https://httpbin.org'), echoLink()])
const json: any = await res.json()
expect(json.headers['authorization']).toEqual('Bearer 123')
expect(json.headers['x-resource-id']).toEqual('123')
expect(json.headers['x-resource-connector-name']).toEqual(undefined)
expect(json.headers['x-resource-connector-config-id']).toEqual(undefined)
expect(json.headers['x-resource-end-user-id']).toEqual(undefined)
expect(json.url).toContain('openint.dev')
})

test('openInt admin auth', async () => {
const res = await applyLinks(req, [authLink({openInt: {apiKey: '123', resourceId: '456'}}, 'https://httpbin.org'), echoLink()])
const json: any = await res.json()
expect(json.headers['x-apikey']).toEqual('123')
expect(json.headers['x-resource-id']).toEqual('456')
expect(json.headers['x-resource-connector-name']).toEqual(undefined)
expect(json.headers['x-resource-connector-config-id']).toEqual(undefined)
expect(json.headers['x-resource-end-user-id']).toEqual(undefined)
expect(json.url).toContain('openint.dev')
})

test('openInt user auth fails with token and no connectorName', async () => {
try {
await applyLinks(req, [authLink({openInt: {token: '123'}}, 'https://httpbin.org'), fetchLink()])
} catch (e) {
expect(e).toBeInstanceOf(Error)
}
})

test('openInt user auth succeeds with token and connectorName', async () => {
const res = await applyLinks(req, [authLink({openInt: {token: '123', connectorName: 'myConnector'}}, 'https://httpbin.org'), echoLink()])
const json: any = await res.json()
expect(json.headers['authorization']).toEqual('Bearer 123')
expect(json.headers['x-resource-connector-name']).toEqual('myConnector')
})

test('openInt admin auth fails with apiKey and no endUserId or connectorName', async () => {
try {
await applyLinks(req, [authLink({openInt: {apiKey: '123'}}, 'https://httpbin.org'), fetchLink()])
} catch (e) {
expect(e).toBeInstanceOf(Error)
}
})

test('openInt admin auth succeeds with apiKey, endUserId, and connectorName', async () => {
const res = await applyLinks(req, [authLink({openInt: {apiKey: '123', endUserId: '789', connectorName: 'myConnector'}}, 'https://httpbin.org'), echoLink()])
const json: any = await res.json()
expect(json.headers['x-apikey']).toEqual('123')
expect(json.headers['x-resource-end-user-id']).toEqual('789')
expect(json.headers['x-resource-connector-name']).toEqual('myConnector')
})
})

0 comments on commit ab271dc

Please sign in to comment.