Skip to content

Commit

Permalink
test: add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-cucu committed Mar 4, 2024
1 parent fe5c84c commit c418d14
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
10 changes: 8 additions & 2 deletions api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,17 @@ class Client {
try {
response = await this._admin.login(args);
} catch (error) {
if (error === INVALIDCREDENTIALS_ERROR) {
if (
error instanceof Error &&
error.message === INVALIDCREDENTIALS_ERROR
) {
throw new Error(
"Have you been granted permission to a model on this controller?"
);
} else if (response === PERMISSIONDENIED_ERROR) {
} else if (
error instanceof Error &&
error.message === PERMISSIONDENIED_ERROR
) {
throw new Error(
"Ensure that you've been given 'login' permission on this controller."
);
Expand Down
40 changes: 37 additions & 3 deletions api/tests/test-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("connect", () => {
ws.close("bad wolf");
});

function validateLoginFailure(error: string) {
function validateLoginFailure(error: Error, message: string) {
requestEqual(ws.lastRequest, {
type: "Admin",
request: "Login",
Expand All @@ -88,7 +88,7 @@ describe("connect", () => {
},
version: 3,
});
expect(error).toStrictEqual(new Error("bad wolf"));
expect(error).toStrictEqual(new Error(message));
}

it("handles admin login failures", (done) => {
Expand All @@ -108,14 +108,48 @@ describe("connect", () => {
?.login({ username: "who", password: "secret" })
.then(() => fail)
.catch((error) => {
validateLoginFailure(error);
validateLoginFailure(error, "bad wolf");
done();
});
ws.reply({ error: "bad wolf" });
});
ws.open();
});

it("invalid credentials login failure via promise", (done) => {
connect("wss://1.2.3.4", options).then((juju: Client) => {
juju
?.login({ username: "who", password: "secret" })
.then(() => fail)
.catch((error) => {
validateLoginFailure(
error,
"Have you been granted permission to a model on this controller?"
);
done();
});
ws.reply({ error: "invalid entity name or password" });
});
ws.open();
});

it("permission denied login failure via promise", (done) => {
connect("wss://1.2.3.4", options).then((juju: Client) => {
juju
?.login({ username: "who", password: "secret" })
.then(() => fail)
.catch((error) => {
validateLoginFailure(
error,
"Ensure that you've been given 'login' permission on this controller."
);
done();
});
ws.reply({ error: "permission denied" });
});
ws.open();
});

function validateRedirectionLoginFailure(error: Error) {
requestEqual(ws.lastRequest, {
type: "Admin",
Expand Down

0 comments on commit c418d14

Please sign in to comment.