1
0
Fork 0
mirror of https://github.com/classchartsapi/classcharts-api-js.git synced 2026-05-14 19:59:37 +00:00

Create ParentClient tests

This commit is contained in:
IsaacHatton 2023-09-17 08:08:38 +00:00
parent a022c8a8ad
commit eb327bbbeb
2 changed files with 41 additions and 4 deletions

View file

@ -26,6 +26,7 @@ export class ParentClient extends BaseClient {
*/
async login(): Promise<void> {
if (!this.email) throw new Error("Email not provided");
if (!this.password) throw new Error("Password not provided");
const formData = new URLSearchParams();
formData.append("_method", "POST");
formData.append("email", this.email);
@ -42,11 +43,9 @@ export class ParentClient extends BaseClient {
redirect: "manual",
});
if (response.status != 302 || !response.headers.has("set-cookie")) {
await response.body?.cancel(); // Make deno tests happy by closing the body, unsure whether this is needed for the actual library
throw new Error(
"Unauthenticated: ClassCharts returned an error: " +
response.status +
" " +
response.statusText,
"Unauthenticated: ClassCharts didn't return authentication cookies"
);
}

View file

@ -0,0 +1,38 @@
import { assertRejects } from "~/deps_dev.ts";
import { ParentClient } from "~/src/core/parentClient.ts";
Deno.test("Throws when no email is provided", async () => {
const client = new ParentClient("","password");
await assertRejects(
async () => {
await client.login();
},
Error,
"Email not provided",
);
});
// throws when no password is provided
Deno.test("Throws when no password is provided", async () => {
const client = new ParentClient("email","");
await assertRejects(
async () => {
await client.login();
},
Error,
"Password not provided",
);
}
);
Deno.test("Throws with invalid login", async () => {
const client = new ParentClient("invalid","invalid");
await assertRejects(
async () => {
await client.login();
},
Error,
"Unauthenticated: ClassCharts didn't return authentication cookies",
);
});