diff --git a/src/commands/app/init.js b/src/commands/app/init.js index f19b68bf..e35029eb 100644 --- a/src/commands/app/init.js +++ b/src/commands/app/init.js @@ -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 ' 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] @@ -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 } diff --git a/test/commands/app/init.test.js b/test/commands/app/init.test.js index e64f4d22..b9c127ec 100644 --- a/test/commands/app/init.test.js +++ b/test/commands/app/init.test.js @@ -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() } @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) + }) })