diff --git a/__tests__/onInstallonOpen.test.ts b/__tests__/onInstallonOpen.test.ts index c0782c8..4ef62a3 100644 --- a/__tests__/onInstallonOpen.test.ts +++ b/__tests__/onInstallonOpen.test.ts @@ -50,9 +50,7 @@ class MockUi { this.isAddonMenu = true; return this; } - addToUi(): void { - console.log(JSON.stringify(ADDON_MENU)); - } + addToUi(): void {} createAddonMenu(): this { this.isAddonMenu = true; return this; diff --git a/__tests__/verifyAuthKeyPrompt.test.ts b/__tests__/verifyAuthKeyPrompt.test.ts new file mode 100644 index 0000000..dbbc441 --- /dev/null +++ b/__tests__/verifyAuthKeyPrompt.test.ts @@ -0,0 +1,100 @@ +import { verifyAuthKeyPrompt } from '../src/sheetsl'; + +const verifyAuthKeyPromptSuccessPatterns = [ + { + testName: 'Success', + input: { + promptResponse: { + getSelectedButton: () => 'ok', + getResponseText: () => 'ThisIsAnApiAuthKey:fx', + }, + ui: { + Button: { + OK: 'ok', // Don't change this value. + }, + }, + }, + expectedOutput: { + getSelectedButton: () => 'ok', + getResponseText: () => 'ThisIsAnApiAuthKey:fx', + }, + }, +] as any[]; + +const verifyAuthKeyPromptErrorPatterns = [ + { + testName: 'Error: canceled auth key setting', + input: { + promptResponse: { + getSelectedButton: () => 'canceled', + getResponseText: () => 'ThisIsAnApiAuthKey:fx', + }, + ui: { + Button: { + OK: 'ok', // Don't change this value. + }, + }, + }, + expectedErrorMessage: + '[SheetsL] Canceled: Setting of DeepL Authentication Key has been canceled.', + }, + { + testName: 'Error: empty auth key', + input: { + promptResponse: { + getSelectedButton: () => 'ok', + getResponseText: () => '', + }, + ui: { + Button: { + OK: 'ok', // Don't change this value. + }, + }, + }, + expectedErrorMessage: + '[SheetsL] You must enter a valid DeepL API Authentication Key.', + }, + { + testName: 'Error: auth key is null', + input: { + promptResponse: { + getSelectedButton: () => 'ok', + getResponseText: () => null, + }, + ui: { + Button: { + OK: 'ok', // Don't change this value. + }, + }, + }, + expectedErrorMessage: + '[SheetsL] You must enter a valid DeepL API Authentication Key.', + }, +] as any[]; + +describe.each(verifyAuthKeyPromptSuccessPatterns)( + 'verifyAuthKeyPrompt Test: success patterns', + ({ testName, input, expectedOutput }) => { + test(`${testName}: getSelectedButton`, () => { + expect( + verifyAuthKeyPrompt(input.promptResponse, input.ui).getSelectedButton() + ).toBe(expectedOutput.getSelectedButton()); + }); + test(`${testName}: getResponseText`, () => { + expect( + verifyAuthKeyPrompt(input.promptResponse, input.ui).getResponseText() + ).toBe(expectedOutput.getResponseText()); + }); + } +); + +describe.each(verifyAuthKeyPromptErrorPatterns)( + 'verifyAuthKeyPrompt Test: error patterns', + ({ testName, input, expectedErrorMessage }) => { + test(testName, () => { + expect(() => { + verifyAuthKeyPrompt(input.promptResponse, input.ui); + }).toThrowError(new Error(expectedErrorMessage)); + }); + } +); diff --git a/src/sheetsl.ts b/src/sheetsl.ts index 661db30..e6204aa 100644 --- a/src/sheetsl.ts +++ b/src/sheetsl.ts @@ -96,17 +96,7 @@ export function setDeeplAuthKey(): void { 'Enter your DeepL API Authentication Key', ui.ButtonSet.OK_CANCEL ); - if (promptResponse.getSelectedButton() !== ui.Button.OK) { - throw new Error( - `[${ADDON_NAME}] Canceled: Setting of DeepL Authentication Key has been canceled.` - ); - } - const apiKey = promptResponse.getResponseText(); - if (!apiKey || apiKey === '') { - throw new Error( - `[${ADDON_NAME}] You must enter a valid DeepL API Authentication Key.` - ); - } + const apiKey = verifyAuthKeyPrompt(promptResponse, ui).getResponseText(); PropertiesService.getUserProperties().setProperty( UP_KEY_DEEPL_API_KEY, apiKey @@ -120,6 +110,32 @@ export function setDeeplAuthKey(): void { } } +/** + * Verify the prompt response in setDeeplAuthKey and return an error + * if the prompt is canceled or if an invalid DeepL API Authentication Key + * was entered. + * @param promptResponse Response object for the user prompt in setDeeplAuthKey + * to enter the user's DeepL API Authentication Key. + * @returns The entered prompt response object. + */ +export function verifyAuthKeyPrompt( + promptResponse: GoogleAppsScript.Base.PromptResponse, + ui: GoogleAppsScript.Base.Ui +): GoogleAppsScript.Base.PromptResponse { + if (promptResponse.getSelectedButton() !== ui.Button.OK) { + throw new Error( + `[${ADDON_NAME}] Canceled: Setting of DeepL Authentication Key has been canceled.` + ); + } + const apiKey = promptResponse.getResponseText(); + if (!apiKey || apiKey === '') { + throw new Error( + `[${ADDON_NAME}] You must enter a valid DeepL API Authentication Key.` + ); + } + return promptResponse; +} + /** * Delete the stored DeepL API authentication key in user property. */