-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Similar to `openService` API, but returns URL instead of open the URL immediately.
- Loading branch information
Showing
7 changed files
with
303 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
import qs from 'qs'; | ||
|
||
import { MY_ACCOUNT_DOMAINS, VAULT_DOMAINS, LINK_KIT_DOMAINS } from '../../server-paths'; | ||
import { MtLinkSdk } from '../..'; | ||
import openServiceUrl from '../open-service-url'; | ||
import { generateConfigs } from '../../helper'; | ||
|
||
describe('api', () => { | ||
describe('open-service-url', () => { | ||
const clientId = 'clientId'; | ||
|
||
test('myaccount', () => { | ||
const url = openServiceUrl(new MtLinkSdk().storedOptions, 'myaccount'); | ||
|
||
const query = qs.stringify({ | ||
configs: generateConfigs() | ||
}); | ||
|
||
expect(url).toBe(`${MY_ACCOUNT_DOMAINS.production}/?${query}`); | ||
}); | ||
|
||
test('myaccount/change-language', () => { | ||
const url = openServiceUrl(new MtLinkSdk().storedOptions, 'myaccount', { | ||
view: 'settings/change-language' | ||
}); | ||
|
||
const query = qs.stringify({ | ||
configs: generateConfigs() | ||
}); | ||
|
||
expect(url).toBe(`${MY_ACCOUNT_DOMAINS.production}/settings/change-language?${query}`); | ||
}); | ||
|
||
test('vault', () => { | ||
const url = openServiceUrl(new MtLinkSdk().storedOptions, 'vault', { | ||
showRememberMe: false | ||
}); | ||
|
||
const query = qs.stringify({ | ||
configs: generateConfigs({ | ||
showRememberMe: false | ||
}) | ||
}); | ||
|
||
expect(url).toBe(`${VAULT_DOMAINS.production}?${query}`); | ||
}); | ||
|
||
test('vault/services-list', () => { | ||
const url = openServiceUrl(new MtLinkSdk().storedOptions, 'vault', { | ||
view: 'services-list', | ||
type: 'bank', | ||
group: 'grouping_testing', | ||
search: 'vault', | ||
showRememberMe: false | ||
}); | ||
|
||
const query = qs.stringify({ | ||
configs: generateConfigs({ | ||
showRememberMe: false | ||
}), | ||
group: 'grouping_testing', | ||
type: 'bank', | ||
search: 'vault' | ||
}); | ||
|
||
expect(url).toBe(`${VAULT_DOMAINS.production}/services?${query}`); | ||
}); | ||
|
||
test('vault/service-connection', () => { | ||
const url = openServiceUrl(new MtLinkSdk().storedOptions, 'vault', { | ||
view: 'service-connection', | ||
entityKey: 'fauxbank_test_bank', | ||
showRememberMe: false | ||
}); | ||
|
||
const query = qs.stringify({ | ||
configs: generateConfigs({ | ||
showRememberMe: false | ||
}) | ||
}); | ||
|
||
expect(url).toBe(`${VAULT_DOMAINS.production}/service/fauxbank_test_bank?${query}`); | ||
}); | ||
|
||
test('vault/connection-setting', () => { | ||
const url = openServiceUrl(new MtLinkSdk().storedOptions, 'vault', { | ||
view: 'connection-setting', | ||
credentialId: '123', | ||
showRememberMe: false | ||
}); | ||
|
||
const query = qs.stringify({ | ||
configs: generateConfigs({ | ||
showRememberMe: false | ||
}) | ||
}); | ||
|
||
expect(url).toBe(`${VAULT_DOMAINS.production}/connection/123?${query}`); | ||
}); | ||
|
||
test('vault/customer-support', () => { | ||
const url = openServiceUrl(new MtLinkSdk().storedOptions, 'vault', { | ||
view: 'customer-support', | ||
showRememberMe: false | ||
}); | ||
|
||
const query = qs.stringify({ | ||
configs: generateConfigs({ | ||
showRememberMe: false | ||
}) | ||
}); | ||
|
||
expect(url).toBe(`${VAULT_DOMAINS.production}/customer-support?${query}`); | ||
}); | ||
|
||
test('link-kit', () => { | ||
const url = openServiceUrl(new MtLinkSdk().storedOptions, 'link-kit'); | ||
|
||
const query = qs.stringify({ | ||
configs: generateConfigs() | ||
}); | ||
|
||
expect(url).toBe(`${LINK_KIT_DOMAINS.production}?${query}`); | ||
}); | ||
|
||
test('calling after init will includes client id', () => { | ||
const cobrandClientId = 'cobrandClientId'; | ||
const locale = 'locale'; | ||
|
||
const mtLinkSdk = new MtLinkSdk(); | ||
mtLinkSdk.init(clientId, { | ||
locale, | ||
cobrandClientId | ||
}); | ||
|
||
const url = openServiceUrl(mtLinkSdk.storedOptions, 'myaccount'); | ||
|
||
const query = qs.stringify({ | ||
client_id: clientId, | ||
cobrand_client_id: cobrandClientId, | ||
locale, | ||
configs: generateConfigs() | ||
}); | ||
|
||
expect(url).toBe(`${MY_ACCOUNT_DOMAINS.production}/?${query}`); | ||
}); | ||
|
||
test('invalid service id', () => { | ||
expect(() => { | ||
openServiceUrl(new MtLinkSdk().storedOptions, 'invalid'); | ||
}).toThrow('[mt-link-sdk] Invalid `serviceId` in `openServiceUrl/openService`, got: invalid'); | ||
}); | ||
|
||
test('saml_subject_id is passed when initialized', () => { | ||
const instance = new MtLinkSdk(); | ||
instance.init('clientId', { samlSubjectId: 'samlSubjectId' }); | ||
|
||
const url = openServiceUrl(instance.storedOptions, 'myaccount'); | ||
|
||
const query = qs.stringify({ | ||
client_id: 'clientId', | ||
saml_subject_id: 'samlSubjectId', | ||
configs: generateConfigs() | ||
}); | ||
|
||
expect(url).toBe(`${MY_ACCOUNT_DOMAINS.production}/?${query}`); | ||
}); | ||
|
||
test('undefined saml_subject_id should not be passed down', () => { | ||
const instance = new MtLinkSdk(); | ||
instance.init('clientId', { samlSubjectId: undefined }); | ||
|
||
const url = openServiceUrl(instance.storedOptions, 'myaccount'); | ||
|
||
const query = qs.stringify({ | ||
client_id: 'clientId', | ||
configs: generateConfigs() | ||
}); | ||
|
||
expect(url).toBe(`${MY_ACCOUNT_DOMAINS.production}/?${query}`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { stringify } from 'qs'; | ||
|
||
import { generateConfigs, mergeConfigs } from '../helper'; | ||
import { MY_ACCOUNT_DOMAINS, VAULT_DOMAINS, LINK_KIT_DOMAINS } from '../server-paths'; | ||
import { | ||
StoredOptions, | ||
ServiceId, | ||
OpenServicesUrlConfigsOptions, | ||
ConnectionSettingType, | ||
ServiceConnectionType, | ||
ServicesListType | ||
} from '../typings'; | ||
|
||
interface QueryData { | ||
client_id?: string; | ||
cobrand_client_id?: string; | ||
locale?: string; | ||
configs: string; | ||
saml_subject_id?: string; | ||
} | ||
|
||
export default function openServiceUrl( | ||
storedOptions: StoredOptions, | ||
serviceId: ServiceId, | ||
options: OpenServicesUrlConfigsOptions = {} | ||
): string { | ||
const { clientId, mode, cobrandClientId, locale, samlSubjectId } = storedOptions; | ||
const { view = '', ...rest } = options; | ||
|
||
const getQueryValue = (needStringify = true): string | QueryData => { | ||
const query: QueryData = { | ||
client_id: clientId, | ||
cobrand_client_id: cobrandClientId, | ||
locale, | ||
saml_subject_id: samlSubjectId, | ||
configs: generateConfigs(mergeConfigs(storedOptions, rest)) | ||
}; | ||
|
||
if (!needStringify) { | ||
return query; | ||
} | ||
|
||
return stringify(query); | ||
}; | ||
|
||
switch (serviceId) { | ||
case 'vault': | ||
if (!view) { | ||
return `${VAULT_DOMAINS[mode]}?${getQueryValue()}`; | ||
} | ||
|
||
switch (view) { | ||
case 'services-list': | ||
// eslint-disable-next-line no-case-declarations | ||
const { group, type, search } = options as ServicesListType; | ||
|
||
return `${VAULT_DOMAINS[mode]}/services?${stringify({ | ||
...(getQueryValue(false) as QueryData), | ||
group, | ||
type, | ||
search | ||
})}`; | ||
|
||
case 'service-connection': | ||
// eslint-disable-next-line no-case-declarations | ||
const { entityKey } = options as ServiceConnectionType; | ||
|
||
return `${VAULT_DOMAINS[mode]}/service/${entityKey}?${getQueryValue()}`; | ||
|
||
case 'connection-setting': | ||
// eslint-disable-next-line no-case-declarations | ||
const { credentialId } = options as ConnectionSettingType; | ||
|
||
return `${VAULT_DOMAINS[mode]}/connection/${credentialId}?${getQueryValue()}`; | ||
|
||
case 'customer-support': | ||
default: | ||
return `${VAULT_DOMAINS[mode]}/customer-support?${getQueryValue()}`; | ||
} | ||
|
||
case 'myaccount': | ||
return `${MY_ACCOUNT_DOMAINS[mode]}/${view}?${getQueryValue()}`; | ||
|
||
case 'link-kit': | ||
return `${LINK_KIT_DOMAINS[mode]}?${getQueryValue()}`; | ||
|
||
default: | ||
throw new Error(`[mt-link-sdk] Invalid \`serviceId\` in \`openServiceUrl/openService\`, got: ${serviceId}`); | ||
} | ||
} |
Oops, something went wrong.