160 lines
4.1 KiB
TypeScript
160 lines
4.1 KiB
TypeScript
/* 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}/api-token-auth/`;
|
||
|
||
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: string) {
|
||
try {
|
||
const resp = await axios.post(eventURL, data, {
|
||
headers: {
|
||
Authorization: `JWT ${jwt}`,
|
||
},
|
||
});
|
||
return resp.data;
|
||
} catch (err) {
|
||
console.error(err);
|
||
throw err;
|
||
}
|
||
}
|
||
|
||
export async function deleteEvent(id: string, jwt: string) {
|
||
try {
|
||
const resp = await axios.delete(`${eventURL}${id}/`, {
|
||
headers: {
|
||
Authorization: `JWT ${jwt}`,
|
||
},
|
||
});
|
||
return resp.data;
|
||
} catch (err) {
|
||
console.error(err);
|
||
throw err;
|
||
}
|
||
}
|
||
|
||
const formURL = `${API_URL}/signupForm/`;
|
||
|
||
export async function createForm(data, jwt: string) {
|
||
try {
|
||
const resp = await axios.post(formURL, data, {
|
||
headers: {
|
||
Authorization: `JWT ${jwt}`,
|
||
},
|
||
});
|
||
return resp.data;
|
||
} catch (err) {
|
||
console.error(err);
|
||
throw err;
|
||
}
|
||
}
|
||
|
||
export async function deleteForm(id: string, jwt: string) {
|
||
try {
|
||
const resp = await axios.delete(`${formURL}${id}/`, {
|
||
headers: {
|
||
Authorization: `JWT ${jwt}`,
|
||
},
|
||
});
|
||
return resp.data;
|
||
} catch (err) {
|
||
console.error(err);
|
||
throw err;
|
||
}
|
||
}
|
||
|
||
export const generateTestForm = async (jwt: 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: "XS_Ox5Rry", name: "Nimi", type: "name", options: [], required: true,
|
||
}, {
|
||
id: "Ve02XSEEx", name: "S-Posti", type: "email", options: [], required: true,
|
||
}, {
|
||
id: "luMqnz5y9", name: "Olen", type: "radiobutton", options: ["Nuori", "Vanha", "Testaaja"],
|
||
}],
|
||
id: 14,
|
||
isOpen: true,
|
||
schema: {
|
||
type: "object",
|
||
required: ["XS_Ox5Rry", "Ve02XSEEx"],
|
||
properties: {
|
||
XS_Ox5Rry: { type: "string", title: "Nimi" },
|
||
Ve02XSEEx: {
|
||
type: ["string"], title: "S-Posti", format: "email", pattern: "^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$", default: null,
|
||
},
|
||
luMqnz5y9: {
|
||
type: "string", title: "Olen", pattern: "^Nuori$|^Vanha$|^Testaaja$", enum: ["Nuori", "Vanha", "Testaaja"],
|
||
},
|
||
},
|
||
},
|
||
}, jwt)
|
||
);
|
||
|
||
export const generateTestEvent = async (formIds = [], jwt: 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)
|
||
);
|
||
|
||
export const sleep = async (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|