2024-10-09 00:25:41 +02:00
|
|
|
import assert from "node:assert/strict";
|
2021-05-01 03:31:04 +02:00
|
|
|
|
|
|
|
import type {Page} from "puppeteer";
|
|
|
|
|
2024-11-12 03:59:37 +01:00
|
|
|
import * as common from "./lib/common.ts";
|
2021-05-01 03:31:04 +02:00
|
|
|
|
|
|
|
type Playground = {
|
2021-05-10 18:29:56 +02:00
|
|
|
playground_name: string;
|
2021-05-01 03:31:04 +02:00
|
|
|
pygments_language: string;
|
2023-05-27 05:04:50 +02:00
|
|
|
url_template: string;
|
2021-05-01 03:31:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
async function _add_playground_and_return_status(page: Page, payload: Playground): Promise<string> {
|
|
|
|
await page.waitForSelector(".admin-playground-form", {visible: true});
|
|
|
|
// Let's first ensure that the success/failure status from an earlier step has disappeared.
|
|
|
|
const admin_playground_status_selector = "div#admin-playground-status";
|
2021-05-13 02:51:24 +02:00
|
|
|
await page.waitForSelector(admin_playground_status_selector, {hidden: true});
|
2021-05-01 03:31:04 +02:00
|
|
|
|
2024-04-29 15:10:05 +02:00
|
|
|
await common.select_item_via_typeahead(
|
|
|
|
page,
|
|
|
|
"#playground_pygments_language",
|
|
|
|
payload.pygments_language,
|
|
|
|
payload.pygments_language,
|
|
|
|
);
|
|
|
|
|
2021-05-01 03:31:04 +02:00
|
|
|
// Now we can fill and click the submit button.
|
2024-04-29 15:10:05 +02:00
|
|
|
await common.fill_form(page, "form.admin-playground-form", {
|
|
|
|
playground_name: payload.playground_name,
|
|
|
|
url_template: payload.url_template,
|
|
|
|
});
|
2021-05-01 03:31:04 +02:00
|
|
|
// Not sure why, but page.click() doesn't seem to always click the submit button.
|
|
|
|
// So we resort to using eval with the button ID instead.
|
2023-12-22 01:00:27 +01:00
|
|
|
await page.$eval("button#submit_playground_button", (el) => {
|
|
|
|
el.click();
|
|
|
|
});
|
2021-05-01 03:31:04 +02:00
|
|
|
|
|
|
|
// We return the success/failure status message back to the caller.
|
|
|
|
await page.waitForSelector(admin_playground_status_selector, {visible: true});
|
|
|
|
const admin_playground_status = await common.get_text_from_selector(
|
|
|
|
page,
|
|
|
|
admin_playground_status_selector,
|
|
|
|
);
|
|
|
|
return admin_playground_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function test_successful_playground_creation(page: Page): Promise<void> {
|
|
|
|
const payload = {
|
|
|
|
pygments_language: "Python",
|
2021-05-10 18:29:56 +02:00
|
|
|
playground_name: "Python3 playground",
|
2023-05-27 05:04:50 +02:00
|
|
|
url_template: "https://python.example.com?code={code}",
|
2021-05-01 03:31:04 +02:00
|
|
|
};
|
|
|
|
const status = await _add_playground_and_return_status(page, payload);
|
|
|
|
assert.strictEqual(status, "Custom playground added!");
|
|
|
|
await page.waitForSelector(".playground_row", {visible: true});
|
|
|
|
assert.strictEqual(
|
|
|
|
await common.get_text_from_selector(
|
|
|
|
page,
|
|
|
|
".playground_row span.playground_pygments_language",
|
|
|
|
),
|
|
|
|
"Python",
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
await common.get_text_from_selector(page, ".playground_row span.playground_name"),
|
|
|
|
"Python3 playground",
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
2023-05-27 05:04:50 +02:00
|
|
|
await common.get_text_from_selector(page, ".playground_row span.playground_url_template"),
|
|
|
|
"https://python.example.com?code={code}",
|
2021-05-01 03:31:04 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function test_invalid_playground_parameters(page: Page): Promise<void> {
|
|
|
|
const payload = {
|
|
|
|
pygments_language: "Python",
|
2021-05-10 18:29:56 +02:00
|
|
|
playground_name: "Python3 playground",
|
2023-05-27 05:04:50 +02:00
|
|
|
url_template: "not_a_url_template{",
|
2021-05-01 03:31:04 +02:00
|
|
|
};
|
|
|
|
let status = await _add_playground_and_return_status(page, payload);
|
2023-05-27 05:04:50 +02:00
|
|
|
assert.strictEqual(status, "Failed: Invalid URL template.");
|
2021-05-01 03:31:04 +02:00
|
|
|
|
2023-05-27 05:04:50 +02:00
|
|
|
payload.url_template = "https://python.example.com?code={code}";
|
2021-05-01 03:31:04 +02:00
|
|
|
payload.pygments_language = "py!@%&";
|
|
|
|
status = await _add_playground_and_return_status(page, payload);
|
|
|
|
assert.strictEqual(status, "Failed: Invalid characters in pygments language");
|
|
|
|
}
|
|
|
|
|
2021-05-12 16:45:26 +02:00
|
|
|
async function test_successful_playground_deletion(page: Page): Promise<void> {
|
|
|
|
await page.click(".playground_row button.delete");
|
2022-09-07 20:28:08 +02:00
|
|
|
|
|
|
|
await common.wait_for_micromodal_to_open(page);
|
|
|
|
await page.click("#confirm_delete_code_playgrounds_modal .dialog_submit_button");
|
|
|
|
await common.wait_for_micromodal_to_close(page);
|
|
|
|
|
2021-05-12 16:45:26 +02:00
|
|
|
await page.waitForSelector(".playground_row", {hidden: true});
|
|
|
|
}
|
|
|
|
|
2021-05-01 03:31:04 +02:00
|
|
|
async function playground_test(page: Page): Promise<void> {
|
|
|
|
await common.log_in(page);
|
|
|
|
await common.manage_organization(page);
|
|
|
|
await page.click("li[data-section='playground-settings']");
|
|
|
|
|
|
|
|
await test_successful_playground_creation(page);
|
|
|
|
await test_invalid_playground_parameters(page);
|
2021-05-12 16:45:26 +02:00
|
|
|
await test_successful_playground_deletion(page);
|
2021-05-01 03:31:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
common.run_test(playground_test);
|