Files
web2.0-frontend/tests/testcafe/utils.ts
T
2021-01-15 00:23:30 +02:00

138 lines
4.0 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.
import { Selector, ClientFunction, RequestLogger } from "testcafe";
import axios from "axios";
const API_URL = "https://api.dev.sik.party/api"
export const getSiteRoot = (): string => process.env.SITE_URL || "http://localhost:3000";
// export const getPageUrl = ClientFunction(() => window.location.href.toString());
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;
}
}
export const generateTestForm = async (jwt: string) => (
await 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)
)
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 generateTestEvent = async (formIds = [], jwt: string) => (
await 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)
)
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;
}
}