mirror of
https://github.com/classchartsapi/classcharts-api-js.git
synced 2026-05-14 03:56:59 +00:00
feat: parent client tests & consistant error messages (#37)
This commit is contained in:
parent
a022c8a8ad
commit
8a11040524
2 changed files with 38 additions and 4 deletions
|
|
@ -26,6 +26,7 @@ export class ParentClient extends BaseClient {
|
||||||
*/
|
*/
|
||||||
async login(): Promise<void> {
|
async login(): Promise<void> {
|
||||||
if (!this.email) throw new Error("Email not provided");
|
if (!this.email) throw new Error("Email not provided");
|
||||||
|
if (!this.password) throw new Error("Password not provided");
|
||||||
const formData = new URLSearchParams();
|
const formData = new URLSearchParams();
|
||||||
formData.append("_method", "POST");
|
formData.append("_method", "POST");
|
||||||
formData.append("email", this.email);
|
formData.append("email", this.email);
|
||||||
|
|
@ -42,11 +43,9 @@ export class ParentClient extends BaseClient {
|
||||||
redirect: "manual",
|
redirect: "manual",
|
||||||
});
|
});
|
||||||
if (response.status != 302 || !response.headers.has("set-cookie")) {
|
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(
|
throw new Error(
|
||||||
"Unauthenticated: ClassCharts returned an error: " +
|
"Unauthenticated: ClassCharts didn't return authentication cookies",
|
||||||
response.status +
|
|
||||||
" " +
|
|
||||||
response.statusText,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
35
src/core/parentClient_test.ts
Normal file
35
src/core/parentClient_test.ts
Normal 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",
|
||||||
|
);
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue