Files
web2.0-frontend/tests/testcafe/utils.ts
T
2022-07-04 11:13:02 +03:00

160 lines
4.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* eslint-disable no-console */
import { Selector, ClientFunction, RequestLogger } from "testcafe";
import axios from "axios";
const API_URL = "https://api.dev.sahkoinsinoorikilta.fi/api";
export const getSiteRoot = (): string => process.env.NEXT_PUBLIC_SITE_URL || "http://localhost:3000";
export const getPageUrl = ClientFunction(() => window.location.pathname);
export const getPostRequestLogger = (url: string) => RequestLogger({ url: `${API_URL}/${url}`, method: "post" }, {
// logResponseHeaders: true,
logResponseBody: true,
stringifyResponseBody: true,
});
const openTime = new Date("1994-01-14T22:51:00+02:00");
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
const USERNAME = "admin";
const PASSWORD = "password123";
export const doLogin = async (t: TestController) => {
await t.typeText(Selector("#login-username"), USERNAME);
await t.typeText(Selector("#login-password"), PASSWORD);
await t.click(Selector("#login-submit"));
};
export async function generateToken(): Promise<string> {
const tokenUrl = `${API_URL}/token/`;
try {
const resp = await axios.post(tokenUrl, {
username: USERNAME,
password: PASSWORD,
});
return resp.data.token;
} catch (err) {
console.error(err);
throw err;
}
}
const eventURL = `${API_URL}/events/`;
export async function createEvent(data, jwt_access: string) {
try {
const resp = await axios.post(eventURL, data, {
headers: {
Authorization: `Bearer ${jwt_access}`,
},
});
return resp.data;
} catch (err) {
console.error(err);
throw err;
}
}
export async function deleteEvent(id: string, jwt_access: string) {
try {
const resp = await axios.delete(`${eventURL}${id}/`, {
headers: {
Authorization: `Bearer ${jwt_access}`,
},
});
return resp.data;
} catch (err) {
console.error(err);
throw err;
}
}
const formURL = `${API_URL}/signupForm/`;
export async function createForm(data, jwt_access: string) {
try {
const resp = await axios.post(formURL, data, {
headers: {
Authorization: `Bearer ${jwt_access}`,
},
});
return resp.data;
} catch (err) {
console.error(err);
throw err;
}
}
export async function deleteForm(id: string, jwt_access: string) {
try {
const resp = await axios.delete(`${formURL}${id}/`, {
headers: {
Authorization: `Bearer ${jwt_access}`,
},
});
return resp.data;
} catch (err) {
console.error(err);
throw err;
}
}
export const generateTestForm = async (jwt_access: string) => (
createForm({
title_fi: "Testi Ilmo",
title_en: "Test Signup",
visible: true,
quota: 0,
start_time: openTime,
end_time: tomorrow,
email_content: "E2E Test",
questions: [{
id: "Kv0IRYUWE", type: "name", options: { enum: [], enumNames_en: [], enumNames_fi: [] }, required: true, title_en: "Name", title_fi: "Nimi",
}, {
id: "_9o78DbdZ", type: "email", options: { enum: [], enumNames_en: [], enumNames_fi: [] }, required: true, title_en: "Email", title_fi: "S-Posti",
}, {
id: "-Zk6tCy7U", type: "radiobutton", options: { enum: ["Nuori", "Vanha", "Testaaja"], enumNames_en: ["Yung", "Old", "Tester"], enumNames_fi: ["Nuori", "Vanha", "Testaaja"] }, title_en: "I am", title_fi: "Olen",
}],
id: 14,
isOpen: true,
schema: {
type: "object",
required: ["Kv0IRYUWE", "_9o78DbdZ"],
properties: {
Kv0IRYUWE: { type: "string", title: "Nimi" },
_9o78DbdZ: {
type: ["string"], title: "S-Posti", format: "email", pattern: "^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$", default: null,
},
"-Zk6tCy7U": {
type: "string", title: "Olen", pattern: "^Nuori$|^Vanha$|^Testaaja$", enum: ["Nuori", "Vanha", "Testaaja"],
},
},
},
}, jwt_access)
);
export const generateTestEvent = async (formIds = [], jwt_access: string) => (
createEvent({
tags: [1],
visible: true,
start_time: openTime,
end_time: tomorrow,
title_fi: "title_fi",
description_fi: "desc_fi",
content_fi: "content_fi",
location_fi: "location_fi",
title_en: "title_en",
description_en: "desc_en",
content_en: "content_en",
location_en: "location_en",
image: null,
signupForm: formIds,
signup_id: formIds,
tag_id: [1],
}, jwt_access)
);
export const sleep = async (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));