Skip to content

Commit

Permalink
feat: ACNA-1240 - Added ability to accept Dev Terms of Service (#509)
Browse files Browse the repository at this point in the history
* Added ability to accept dev terms.
  • Loading branch information
florind-ens authored Mar 14, 2022
1 parent cc6230a commit ed46cbe
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/commands/app/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ class InitCommand extends AddCommand {
this.log(chalk.blue(chalk.bold(`Project initialized for Workspace ${workspace.name}, you can run 'aio app use -w <workspace>' to switch workspace.`)))
}

async ensureDevTermAccepted (consoleCLI, orgId) {
const isTermAccepted = await consoleCLI.checkDevTermsForOrg(orgId)
if (!isTermAccepted) {
const terms = await consoleCLI.getDevTermsForOrg()
const confirmDevTerms = await consoleCLI.prompt.promptConfirm(terms.text)
if (!confirmDevTerms) {
this.error('The Developer Terms of Service were declined')
} else {
const accepted = await consoleCLI.acceptDevTermsForOrg(orgId)
if (!accepted) {
this.error('The Developer Terms of Service could not be accepted')
}
this.log(`The Developer Terms of Service were successfully accepted for org ${orgId}`)
}
}
}

async selectExtensionPoints (flags, orgSupportedServices = null) {
if (!flags.extensions) {
return [implPromptChoices.find(i => i.value.name === 'application').value]
Expand Down Expand Up @@ -167,6 +184,7 @@ class InitCommand extends AddCommand {
async selectConsoleOrg (consoleCLI) {
const organizations = await consoleCLI.getOrganizations()
const selectedOrg = await consoleCLI.promptForSelectOrganization(organizations)
await this.ensureDevTermAccepted(consoleCLI, selectedOrg.id)
return selectedOrg
}

Expand Down
67 changes: 66 additions & 1 deletion test/commands/app/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const mockConsoleCLIInstance = {
subscribeToServices: jest.fn(),
getWorkspaceConfig: jest.fn(),
createWorkspace: jest.fn(),
checkDevTermsForOrg: jest.fn(),
getDevTermsForOrg: jest.fn(),
acceptDevTermsForOrg: jest.fn(),
prompt: {
promptConfirm: jest.fn()
}
Expand Down Expand Up @@ -426,13 +429,13 @@ describe('run', () => {
const fakeServicePropertiesNoAssetCompute = [{ sdkCode: 'another' }]

test('with login, select excshell', async () => {
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(true)
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(fakeProject)
mockConsoleCLIInstance.getWorkspaces.mockResolvedValue(fakeWorkspaces)
mockConsoleCLIInstance.getServicePropertiesFromWorkspace.mockResolvedValue(fakeServicePropertiesNoAssetCompute)
mockConsoleCLIInstance.getEnabledServicesForOrg.mockResolvedValue(fakeSupportedOrgServices)
mockConsoleCLIInstance.getWorkspaceConfig.mockResolvedValue(fakeConfig)

mockExtensionPrompt.mockReturnValue({ res: excshellSelection })
await TheCommand.run([])
expect(mockGenInstantiate).toHaveBeenCalledTimes(3)
Expand Down Expand Up @@ -464,6 +467,7 @@ describe('run', () => {
})

test('with login, select asset-compute', async () => {
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(true)
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(fakeProject)
mockConsoleCLIInstance.getWorkspaces.mockResolvedValue(fakeWorkspaces)
Expand Down Expand Up @@ -508,6 +512,7 @@ describe('run', () => {
})

test('with login, select excshell, no asset compute service in org', async () => {
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(true)
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(fakeProject)
mockConsoleCLIInstance.getWorkspaces.mockResolvedValue(fakeWorkspaces)
Expand Down Expand Up @@ -559,6 +564,7 @@ describe('run', () => {
})

test('with login, select excshell, create new project', async () => {
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(true)
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(null) // null = user selects to create a project
mockConsoleCLIInstance.createProject.mockResolvedValue(fakeProject)
Expand Down Expand Up @@ -599,6 +605,7 @@ describe('run', () => {

test('with login, --extension excshell, create new project', async () => {
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(true)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(null) // null = user selects to create a project
mockConsoleCLIInstance.createProject.mockResolvedValue(fakeProject)
mockConsoleCLIInstance.getWorkspaces.mockResolvedValue(fakeWorkspaces)
Expand Down Expand Up @@ -638,6 +645,7 @@ describe('run', () => {
})

test('with login, select excshell, -w dev', async () => {
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(true)
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(fakeProject)
mockConsoleCLIInstance.getWorkspaces.mockResolvedValue(fakeWorkspaces)
Expand Down Expand Up @@ -677,6 +685,7 @@ describe('run', () => {
})

test('with login, select excshell, -w notexists, promptConfirm true', async () => {
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(true)
mockConsoleCLIInstance.prompt.promptConfirm.mockResolvedValue(true)
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(fakeProject)
Expand All @@ -693,6 +702,7 @@ describe('run', () => {

test('with login, select excshell, -w notexists, promptConfirm false, should throw', async () => {
const workspaceName = 'notexists'
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(true)
mockConsoleCLIInstance.prompt.promptConfirm.mockResolvedValue(false)
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(fakeProject)
Expand All @@ -707,6 +717,7 @@ describe('run', () => {
})
test('with login, select excshell, -w notexists, --confirm-new-workspace', async () => {
const notexistsWorkspace = 'notexists'
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(true)
mockConsoleCLIInstance.prompt.promptConfirm.mockResolvedValue(true)
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(fakeProject)
Expand All @@ -721,4 +732,58 @@ describe('run', () => {
expect(mockConsoleCLIInstance.prompt.promptConfirm).not.toHaveBeenCalled()
expect(mockConsoleCLIInstance.createWorkspace).toHaveBeenCalledWith(expect.anything(), expect.anything(), expect.objectContaining({ name: 'notexists', title: '' }))
})
test('with login, developer terms not accepted, accept', async () => {
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(fakeProject)
mockConsoleCLIInstance.getWorkspaces.mockResolvedValue(fakeWorkspaces)
mockConsoleCLIInstance.getServicePropertiesFromWorkspace.mockResolvedValue(fakeServicePropertiesNoAssetCompute)
mockConsoleCLIInstance.getEnabledServicesForOrg.mockResolvedValue(fakeSupportedOrgServices)
mockConsoleCLIInstance.getWorkspaceConfig.mockResolvedValue(fakeConfig)
mockExtensionPrompt.mockReturnValue({ res: excshellSelection })
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(false)
mockConsoleCLIInstance.getDevTermsForOrg.mockResolvedValue({ text: 'pls, accept this', locale: 'eng' })
mockConsoleCLIInstance.acceptDevTermsForOrg.mockResolvedValueOnce(true)
mockConsoleCLIInstance.prompt.promptConfirm.mockResolvedValueOnce(true)
await TheCommand.run([])
expect(mockConsoleCLIInstance.checkDevTermsForOrg).toHaveBeenCalledTimes(1)
expect(mockConsoleCLIInstance.prompt.promptConfirm).toHaveBeenCalledTimes(1)
expect(mockConsoleCLIInstance.getDevTermsForOrg).toHaveBeenCalledTimes(1)
expect(mockConsoleCLIInstance.acceptDevTermsForOrg).toHaveBeenCalledTimes(1)
})
test('with login, developer terms not accepted, declined by user', async () => {
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(fakeProject)
mockConsoleCLIInstance.getWorkspaces.mockResolvedValue(fakeWorkspaces)
mockConsoleCLIInstance.getServicePropertiesFromWorkspace.mockResolvedValue(fakeServicePropertiesNoAssetCompute)
mockConsoleCLIInstance.getEnabledServicesForOrg.mockResolvedValue(fakeSupportedOrgServices)
mockConsoleCLIInstance.getWorkspaceConfig.mockResolvedValue(fakeConfig)
mockExtensionPrompt.mockReturnValue({ res: excshellSelection })
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(false)
mockConsoleCLIInstance.getDevTermsForOrg.mockResolvedValue({ text: 'pls, accept this', locale: 'eng' })
mockConsoleCLIInstance.acceptDevTermsForOrg.mockResolvedValueOnce(true)
mockConsoleCLIInstance.prompt.promptConfirm.mockResolvedValueOnce(false)
await expect(TheCommand.run([])).rejects.toThrow('The Developer Terms of Service were declined')
expect(mockConsoleCLIInstance.checkDevTermsForOrg).toHaveBeenCalledTimes(1)
expect(mockConsoleCLIInstance.getDevTermsForOrg).toHaveBeenCalledTimes(1)
expect(mockConsoleCLIInstance.prompt.promptConfirm).toHaveBeenCalledTimes(1)
expect(mockConsoleCLIInstance.acceptDevTermsForOrg).toHaveBeenCalledTimes(0)
})
test('with login, developer terms not accepted, acceptDevTermsForOrg: accepted = false', async () => {
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(fakeProject)
mockConsoleCLIInstance.getWorkspaces.mockResolvedValue(fakeWorkspaces)
mockConsoleCLIInstance.getServicePropertiesFromWorkspace.mockResolvedValue(fakeServicePropertiesNoAssetCompute)
mockConsoleCLIInstance.getEnabledServicesForOrg.mockResolvedValue(fakeSupportedOrgServices)
mockConsoleCLIInstance.getWorkspaceConfig.mockResolvedValue(fakeConfig)
mockExtensionPrompt.mockReturnValue({ res: excshellSelection })
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(false)
mockConsoleCLIInstance.getDevTermsForOrg.mockResolvedValue({ text: 'pls, accept this', locale: 'eng' })
mockConsoleCLIInstance.acceptDevTermsForOrg.mockResolvedValueOnce(false)
mockConsoleCLIInstance.prompt.promptConfirm.mockResolvedValueOnce(true)
await expect(TheCommand.run([])).rejects.toThrow('The Developer Terms of Service could not be accepted')
expect(mockConsoleCLIInstance.checkDevTermsForOrg).toHaveBeenCalledTimes(1)
expect(mockConsoleCLIInstance.getDevTermsForOrg).toHaveBeenCalledTimes(1)
expect(mockConsoleCLIInstance.prompt.promptConfirm).toHaveBeenCalledTimes(1)
expect(mockConsoleCLIInstance.acceptDevTermsForOrg).toHaveBeenCalledTimes(1)
})
})

0 comments on commit ed46cbe

Please sign in to comment.