Skip to content

Commit

Permalink
Merge pull request #202 from skedify/feature/SKED-6368
Browse files Browse the repository at this point in the history
feature/SKED-6368: Fix SDK not correctly applying proxy url as Realm
  • Loading branch information
timothy-suy authored Sep 23, 2020
2 parents f513c9a + ca09085 commit c1e2491
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 19 deletions.
100 changes: 100 additions & 0 deletions src/Skedify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,106 @@ describe('API', () => {
expect(await matchRequest(SDK.subjects())).toMatchSnapshot()
})

it('should not use a proxy url when none is given', async () => {
const SDK2 = new API({
auth_provider,
locale: 'nl-BE',
})

installSkedifySDKMock(SDK2, { mockProxyCall: false })

// No proxy url is returned
// Fallback/Default url should be used
mockMatchingURLResponse(/integrations/)
mockMatchingURLResponse(/subjects/, [])

await SDK2.subjects()

expect(mockedRequests()).toMatchSnapshot()

uninstallSkedifySDKMock(SDK2)
})

it('should use the proxy url when given', async () => {
const SDK2 = new API({
auth_provider,
locale: 'nl-BE',
})

installSkedifySDKMock(SDK2, { mockProxyCall: false })

// A proxy url is returned
// The proxy url should be used for the request
mockMatchingURLResponse(/integrations/, {
settings: { url: 'https://api-proxy.example.com' },
})
mockMatchingURLResponse(/subjects/, [])

await SDK2.subjects()

expect(mockedRequests()).toMatchSnapshot()

uninstallSkedifySDKMock(SDK2)
})

it('should use the proxy url when given for the token grant', async () => {
const auth_provider_token = API.createAuthProviderString('token', {
token_type: 'Bearer',
access_token: 'some-access-token-goes-here',
realm: 'https://api.example.com',
})

const SDK2 = new API({
auth_provider: auth_provider_token,
locale: 'nl-BE',
})

installSkedifySDKMock(SDK2, { mockProxyCall: false })

// A proxy url is returned
// The proxy url should be used for the request
mockMatchingURLResponse(/integrations/, {
settings: { url: 'https://api-proxy.example.com' },
})
// See grant/index.js
mockMatchingURLResponse(/identity/, [])
mockMatchingURLResponse(/subjects/, [])

await SDK2.subjects()

expect(mockedRequests()).toMatchSnapshot()

uninstallSkedifySDKMock(SDK2)
})

it('should not use a proxy url when none is given for the token grant', async () => {
const auth_provider_token = API.createAuthProviderString('token', {
token_type: 'Bearer',
access_token: 'some-access-token-goes-here',
realm: 'https://api.example.com',
})

const SDK2 = new API({
auth_provider: auth_provider_token,
locale: 'nl-BE',
})

installSkedifySDKMock(SDK2, { mockProxyCall: false })

// No proxy url is returned
// Fallback/Default url should be used
mockMatchingURLResponse(/integrations/)
// See grant/index.js
mockMatchingURLResponse(/identity/, [])
mockMatchingURLResponse(/subjects/, [])

await SDK2.subjects()

expect(mockedRequests()).toMatchSnapshot()

uninstallSkedifySDKMock(SDK2)
})

describe('API/Response', () => {
it('should normalize the response when the call succeeds', async () => {
mockResponse([])
Expand Down
150 changes: 150 additions & 0 deletions src/__snapshots__/Skedify.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,81 @@ Object {
}
`;

exports[`API should not use a proxy url when none is given 1`] = `
Array [
Object {
"data": undefined,
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "nl-BE, nl;q=0.667, *;q=0.333",
"Authorization": "Bearer fake_example_access_token",
"Cache-Control": "no-store",
"Pragma": "no-cache",
},
"method": "get",
"params": undefined,
"url": "https://api.example.com/integrations/proxy",
},
Object {
"data": undefined,
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "nl-BE, nl;q=0.667, *;q=0.333",
"Authorization": "Bearer fake_example_access_token",
"Cache-Control": "no-store",
"Pragma": "no-cache",
},
"method": "get",
"params": undefined,
"url": "https://api.example.com/subjects",
},
]
`;

exports[`API should not use a proxy url when none is given for the token grant 1`] = `
Array [
Object {
"data": undefined,
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "nl-BE, nl;q=0.667, *;q=0.333",
"Authorization": "Bearer some-access-token-goes-here",
"Cache-Control": "no-store",
"Pragma": "no-cache",
},
"method": "get",
"params": undefined,
"url": "https://api.example.com/integrations/proxy",
},
Object {
"data": undefined,
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "nl-BE, nl;q=0.667, *;q=0.333",
"Authorization": "Bearer some-access-token-goes-here",
"Cache-Control": "no-store",
"Pragma": "no-cache",
},
"method": "get",
"params": undefined,
"url": "https://api.example.com/identity",
},
Object {
"data": undefined,
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "nl-BE, nl;q=0.667, *;q=0.333",
"Authorization": "Bearer some-access-token-goes-here",
"Cache-Control": "no-store",
"Pragma": "no-cache",
},
"method": "get",
"params": undefined,
"url": "https://api.example.com/subjects",
},
]
`;

exports[`API should reflect configuration changes in the request (auth_provider) 1`] = `
Object {
"data": undefined,
Expand Down Expand Up @@ -1527,6 +1602,81 @@ Object {

exports[`API should throw an error when an interceptor is added but it is not a function 1`] = `"[RESOURCE]: You tried to call \`.addResponseInterceptor(null)\` but it must receive a function."`;

exports[`API should use the proxy url when given 1`] = `
Array [
Object {
"data": undefined,
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "nl-BE, nl;q=0.667, *;q=0.333",
"Authorization": "Bearer fake_example_access_token",
"Cache-Control": "no-store",
"Pragma": "no-cache",
},
"method": "get",
"params": undefined,
"url": "https://api.example.com/integrations/proxy",
},
Object {
"data": undefined,
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "nl-BE, nl;q=0.667, *;q=0.333",
"Authorization": "Bearer fake_example_access_token",
"Cache-Control": "no-store",
"Pragma": "no-cache",
},
"method": "get",
"params": undefined,
"url": "https://api-proxy.example.com/subjects",
},
]
`;

exports[`API should use the proxy url when given for the token grant 1`] = `
Array [
Object {
"data": undefined,
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "nl-BE, nl;q=0.667, *;q=0.333",
"Authorization": "Bearer some-access-token-goes-here",
"Cache-Control": "no-store",
"Pragma": "no-cache",
},
"method": "get",
"params": undefined,
"url": "https://api.example.com/integrations/proxy",
},
Object {
"data": undefined,
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "nl-BE, nl;q=0.667, *;q=0.333",
"Authorization": "Bearer some-access-token-goes-here",
"Cache-Control": "no-store",
"Pragma": "no-cache",
},
"method": "get",
"params": undefined,
"url": "https://api-proxy.example.com/identity",
},
Object {
"data": undefined,
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "nl-BE, nl;q=0.667, *;q=0.333",
"Authorization": "Bearer some-access-token-goes-here",
"Cache-Control": "no-store",
"Pragma": "no-cache",
},
"method": "get",
"params": undefined,
"url": "https://api-proxy.example.com/subjects",
},
]
`;

exports[`API/Auth Providers should be possible to use the \`public_client\` strategy 1`] = `
Object {
"data": "my subjects data",
Expand Down
1 change: 1 addition & 0 deletions src/build/Skedify.testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function installSkedifySDKMock(instance, options = {}) {
Object.assign(
{
mockAccessTokensCall: true,
mockProxyCall: true,
},
options
)
Expand Down
24 changes: 19 additions & 5 deletions src/createIdentityProvider/identityProviders/createGrant.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,25 @@ function defaultAuthorizationMethod({
.get(`${Realm}/integrations/proxy`, {
headers: { Authorization },
})
.then(({ data: response }) => ({
Realm: response.data.url,
Authorization,
Expiration,
}))
.then(({ data: response }) => {
if (
response.data &&
response.data.settings &&
response.data.settings.url
) {
return {
Realm: response.data.settings.url,
Authorization,
Expiration,
}
}

return {
Realm,
Authorization,
Expiration,
}
})
})

// Make sure to create a new access_token when the current one is going to expire
Expand Down
21 changes: 17 additions & 4 deletions src/createIdentityProvider/identityProviders/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,23 @@ export const IDPS = {
.get(`${Realm}/integrations/proxy`, {
headers: { Authorization },
})
.then(({ data: response }) => ({
Realm: response.data.url,
Authorization,
}))
.then(({ data: response }) => {
if (
response.data &&
response.data.settings &&
response.data.settings.url
) {
return {
Realm: response.data.settings.url,
Authorization,
}
}

return {
Realm,
Authorization,
}
})
})

// Check if the identity call works for grant_type = token. This way we
Expand Down
24 changes: 14 additions & 10 deletions test/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,20 @@ const PROXY_CALL = {
status: 200,
data: {
data: {
// We need to use a getter to delay the execution of this function
// We need access to the mostRecent request, but if there is no most recent
// Request we can't access its url. Therefor once we are sure the /integrations/proxy
// Is executed, we can safely run the mostRecent() function.
get url() {
return mostRecentRequest().url.replace('/integrations/proxy', '')
settings: {
// We need to use a getter to delay the execution of this function
// We need access to the mostRecent request, but if there is no most recent
// Request we can't access its url. Therefor once we are sure the /integrations/proxy
// Is executed, we can safely run the mostRecent() function.
get url() {
return mostRecentRequest().url.replace('/integrations/proxy', '')
},
},
},
},
},
}

const SETUP_REQUESTS = [{ ...ACCESS_TOKEN_CALL }, { ...PROXY_CALL }]

export function mockedRequests() {
const { requests, ignoredRequests } = storage()

Expand All @@ -126,9 +126,10 @@ export function mockedRequests() {
}

export function install(instance, options = {}) {
const { mockAccessTokensCall } = Object.assign(
const { mockAccessTokensCall, mockProxyCall } = Object.assign(
{
mockAccessTokensCall: true,
mockProxyCall: true,
},
options
)
Expand All @@ -143,7 +144,10 @@ export function install(instance, options = {}) {
ignoredRequests: [],
})

const setup_calls = mockAccessTokensCall ? SETUP_REQUESTS : [PROXY_CALL]
const setup_calls = [
...(mockAccessTokensCall ? [ACCESS_TOKEN_CALL] : []),
...(mockProxyCall ? [PROXY_CALL] : []),
]

// Stub setup requests
setup_calls.forEach(({ url, response }) => {
Expand Down

0 comments on commit c1e2491

Please sign in to comment.