From eb327bbbeb1e6dda43a22ab828c3770df35329a2 Mon Sep 17 00:00:00 2001 From: IsaacHatton <74068072+IsaacHatton@users.noreply.github.com> Date: Sun, 17 Sep 2023 08:08:38 +0000 Subject: [PATCH] Create ParentClient tests --- src/core/parentClient.ts | 7 +++---- src/core/parentClient_test.ts | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 src/core/parentClient_test.ts diff --git a/src/core/parentClient.ts b/src/core/parentClient.ts index cc002a0..9cd3af1 100644 --- a/src/core/parentClient.ts +++ b/src/core/parentClient.ts @@ -26,6 +26,7 @@ export class ParentClient extends BaseClient { */ async login(): Promise { 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" ); } diff --git a/src/core/parentClient_test.ts b/src/core/parentClient_test.ts new file mode 100644 index 0000000..8e78287 --- /dev/null +++ b/src/core/parentClient_test.ts @@ -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", + ); + }); + \ No newline at end of file