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:
parent
a022c8a8ad
commit
eb327bbbeb
2 changed files with 41 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,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
38
src/core/parentClient_test.ts
Normal file
38
src/core/parentClient_test.ts
Normal 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",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue