Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use host in login [PHX-2805] #2371

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/cmds/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ import { copyright } from '../utils/copyright'

const APP_ID =
'9f86a1d54f3d6f85c159468f5919d6e5d27716b3ed68fd01bd534e3dea2df864'
const REDIRECT_URI = 'https://www.contentful.com/developers/cli-oauth-page/'
const oAuthURL = `https://be.contentful.com/oauth/authorize?response_type=token&client_id=${APP_ID}&redirect_uri=${REDIRECT_URI}&scope=content_management_manage&action=cli`

export const getOauthURL = (host?: string) => {
const REDIRECT_URI = 'https://www.contentful.com/developers/cli-oauth-page/'
let oAuthURL = `https://be.contentful.com/oauth/authorize?response_type=token&client_id=${APP_ID}&redirect_uri=${REDIRECT_URI}&scope=content_management_manage&action=cli`

if (host) {
const url = host.replace('api.', '')
oAuthURL = `https://be.${url}/oauth/authorize?response_type=token&client_id=${APP_ID}&redirect_uri=${REDIRECT_URI}&scope=content_management_manage&action=cli`
}

return oAuthURL
}

export const command = 'login'

Expand All @@ -37,6 +47,7 @@ export const builder = (yargs: Argv) =>

interface Context {
managementToken?: string
host?: string
}

interface LoginProps {
Expand All @@ -48,7 +59,7 @@ export const login = async ({
context,
managementToken: managementTokenFlag
}: LoginProps) => {
const { managementToken } = context
const { managementToken, host } = context

let token
if (managementTokenFlag) {
Expand Down Expand Up @@ -82,6 +93,8 @@ export const login = async ({
return
}

const oAuthURL = getOauthURL(host)

// We open the browser window only on Windows and OSX since this might fail or open the wrong browser on Linux.
if (['win32', 'darwin'].includes(process.platform)) {
await open(oAuthURL, {
Expand Down
30 changes: 30 additions & 0 deletions test/unit/cmds/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,36 @@ test('login - without error', async () => {
expect(result).toBe(mockedRcConfig.managementToken)
})

test('login - uses host from config', async () => {
await loginHandler({
context: {
host: 'api.eu.contentful.com'
}
})

if (['win32', 'darwin'].includes(process.platform)) {
expect(open).toHaveBeenCalled()
expect(
mocks.open.mock.calls[0][0].includes(
'https://be.eu.contentful.com/oauth/'
)
).toBeTruthy()
}
})

test('login - uses default host without host in config', async () => {
await loginHandler({
context: {}
})

if (['win32', 'darwin'].includes(process.platform)) {
expect(open).toHaveBeenCalled()
expect(
mocks.open.mock.calls[0][0].includes('https://be.contentful.com/oauth')
).toBeTruthy()
}
})

test('login - user abort', async () => {
mocks.confirmation.mockResolvedValueOnce(false)

Expand Down