Skip to content

Commit

Permalink
Merge pull request #85 from ttsukagoshi/test-setDeeplAuthKey
Browse files Browse the repository at this point in the history
Add test for verifyAuthKeyPrompt #31
  • Loading branch information
ttsukagoshi authored Jun 3, 2023
2 parents 813c9e6 + c7f272f commit bfa08e0
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 14 deletions.
4 changes: 1 addition & 3 deletions __tests__/onInstallonOpen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
100 changes: 100 additions & 0 deletions __tests__/verifyAuthKeyPrompt.test.ts
Original file line number Diff line number Diff line change
@@ -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));
});
}
);
38 changes: 27 additions & 11 deletions src/sheetsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*/
Expand Down

0 comments on commit bfa08e0

Please sign in to comment.