-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Maximilian Øystå Lloyd <[email protected]>
- Loading branch information
Showing
8 changed files
with
174 additions
and
63 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
Empty file.
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,75 @@ | ||
import { describe, expect, it } from 'bun:test'; | ||
import { makeEndpointFactory, makeLoginUrl } from 'lib/urls'; | ||
|
||
const dummyEnv = { | ||
LOGIN_URL: 'https://www.nav.no/login', | ||
APP_URL: 'https://www.nav.no', | ||
MIN_SIDE_URL: 'https://www.nav.no/min-side', | ||
MIN_SIDE_ARBEIDSGIVER_URL: 'https://www.nav.no/min-side-arbeidsgiver', | ||
}; | ||
|
||
describe('URLs', () => { | ||
it('It includes params from the defaultParams function', () => { | ||
const params = { | ||
simple: true, | ||
} as any; | ||
|
||
const callEndpoint = makeEndpointFactory( | ||
() => ({ | ||
...params, | ||
}), | ||
'http://localhost:3000' | ||
); | ||
|
||
expect( | ||
callEndpoint('/user-menu', { | ||
feedback: true, | ||
}) | ||
).toBe('http://localhost:3000/user-menu?simple=true&feedback=true'); | ||
}); | ||
|
||
it('Basic login URL with redirect is created correctly', () => { | ||
const loginUrl = makeLoginUrl({ | ||
environment: dummyEnv, | ||
params: { | ||
level: 'Level3', | ||
redirectToApp: false, | ||
redirectToUrl: 'https://www.nav.no', | ||
context: 'privatperson', | ||
language: 'nb', | ||
}, | ||
}); | ||
|
||
expect(loginUrl).toBe('https://www.nav.no/login?redirect=https://www.nav.no&level=Level3&locale=nb'); | ||
}); | ||
|
||
it('Redirect should be min side arbeidsgiver if context is arbeidsgiver', () => { | ||
const loginUrl = makeLoginUrl({ | ||
environment: dummyEnv, | ||
params: { | ||
level: 'Level3', | ||
redirectToApp: false, | ||
redirectToUrl: '', | ||
context: 'arbeidsgiver', | ||
language: 'nb', | ||
}, | ||
}); | ||
|
||
expect(loginUrl).toBe(`https://www.nav.no/login?redirect=${dummyEnv.MIN_SIDE_ARBEIDSGIVER_URL}&level=Level3&locale=nb`); | ||
}); | ||
|
||
it('Redirect should be min side if no redirectToUrl is set and redirectToApp is false and context is anything but arbeidsgiver', () => { | ||
const loginUrl = makeLoginUrl({ | ||
environment: dummyEnv, | ||
params: { | ||
level: 'Level3', | ||
redirectToApp: false, | ||
redirectToUrl: '', | ||
context: 'privatperson', | ||
language: 'nb', | ||
}, | ||
}); | ||
|
||
expect(loginUrl).toBe(`https://www.nav.no/login?redirect=${dummyEnv.MIN_SIDE_URL}&level=Level3&locale=nb`); | ||
}); | ||
}); |
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,77 @@ | ||
import { formatParams } from './json'; | ||
import { Environment, Language, Params } from './params'; | ||
|
||
type IdPortenLocale = 'nb' | 'nn' | 'en' | 'se'; | ||
|
||
const idPortenLocaleMap: Record<Language, IdPortenLocale> = { | ||
nb: 'nb', | ||
nn: 'nn', | ||
se: 'se', | ||
en: 'en', | ||
pl: 'en', | ||
ru: 'en', | ||
uk: 'en', | ||
}; | ||
|
||
export function getIdPortenLocale(language: Language) { | ||
return idPortenLocaleMap[language]; | ||
} | ||
|
||
export function makeEndpointFactory(defaultParams: () => Params, origin: string) { | ||
return (endpoint: string, params: Partial<Params>) => { | ||
const formattedParams = formatParams({ | ||
...defaultParams(), | ||
...params, | ||
}); | ||
|
||
return `${origin}${endpoint}?${formattedParams}`; | ||
}; | ||
} | ||
|
||
type GetUrlLoginOptions = { | ||
environment: Pick<Environment, 'APP_URL' | 'MIN_SIDE_URL' | 'MIN_SIDE_ARBEIDSGIVER_URL' | 'LOGIN_URL'>; | ||
params: Pick<Params, 'redirectToApp' | 'redirectToUrl' | 'context' | 'level' | 'language'>; | ||
isClientSide?: boolean; | ||
}; | ||
|
||
function makeRedirectUrlLogin({ environment, params, isClientSide = false }: GetUrlLoginOptions) { | ||
const { redirectToUrl, redirectToApp } = params; | ||
|
||
const appUrl = environment.APP_URL; | ||
|
||
if (isClientSide && erNavDekoratoren(appUrl)) { | ||
return appUrl; | ||
} | ||
|
||
if (redirectToUrl) { | ||
return redirectToUrl; | ||
} | ||
|
||
if (redirectToApp) { | ||
return appUrl; | ||
} | ||
|
||
if (params.context === 'arbeidsgiver') { | ||
return environment.MIN_SIDE_ARBEIDSGIVER_URL; | ||
} | ||
|
||
return environment.MIN_SIDE_URL; | ||
} | ||
|
||
export function makeLoginUrl( | ||
options: GetUrlLoginOptions & { | ||
overrideLevel?: string; | ||
} | ||
) { | ||
const redirectUrl = makeRedirectUrlLogin(options); | ||
const idPortenLocale = getIdPortenLocale(options.params.language); | ||
|
||
console.log('idPortenLocale', idPortenLocale); | ||
|
||
const { environment, params } = options; | ||
return `${environment.LOGIN_URL}?redirect=${redirectUrl}&level=${options.overrideLevel || params.level}&locale=${idPortenLocale}`; | ||
} | ||
|
||
export function erNavDekoratoren(url: string) { | ||
return url.includes('dekoratoren') || url.includes('localhost'); | ||
} |