这是indexloc提供的服务,不要输入任何密码
Skip to content

Add test for verifyAuthKeyPrompt #31 #85

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

Merged
merged 9 commits into from
Jun 3, 2023
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