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

feat: parent client tests & consistant error messages (#37)

This commit is contained in:
Isaac Hatton 2023-09-17 17:19:48 +01:00 committed by GitHub
parent a022c8a8ad
commit 8a11040524
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 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,35 @@
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",
);
});
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 username and password", async () => {
const client = new ParentClient("invalid", "invalid");
await assertRejects(
async () => {
await client.login();
},
Error,
"Unauthenticated: ClassCharts didn't return authentication cookies",
);
});