Skip to content

Commit

Permalink
style: correct eslint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sebtiz13 committed Sep 17, 2024
1 parent 30575a1 commit 11c9f7b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
12 changes: 3 additions & 9 deletions src/Kuzzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ export class Kuzzle extends KuzzleEventEmitter {
this.protocol = protocol;

this._protectedEvents = {
afterLogin: {},
connected: {},
disconnected: {},
error: {},
loginAttempt: {},
afterLogin: {},
reconnected: {},
tokenExpired: {},
};
Expand Down Expand Up @@ -582,18 +582,12 @@ export class Kuzzle extends KuzzleEventEmitter {
listener: () => void
): this;

on(
eventName: "beforeLogout",
listener: () => void
): this;
on(eventName: "beforeLogout", listener: () => void): this;
on(
eventName: "logoutAttempt" | "afterLogout",
listener: (status: { success: true }) => void
): this;
on(
eventName: "beforeLogin",
listener: () => void
): this;
on(eventName: "beforeLogin", listener: () => void): this;
on(
eventName: "loginAttempt" | "afterLogin",
listener: (data: { success: boolean; error: string }) => void
Expand Down
10 changes: 8 additions & 2 deletions src/controllers/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,14 @@ export class AuthController extends BaseController {
/**
* @deprecated logout `logoutAttempt` is legacy event. Use afterLogout instead.
*/
this.kuzzle.emit("logoutAttempt", { success: false, error: (error as Error).message });
this.kuzzle.emit("afterLogout", { success: false, error: (error as Error).message });
this.kuzzle.emit("logoutAttempt", {
error: (error as Error).message,
success: false,
});
this.kuzzle.emit("afterLogout", {
error: (error as Error).message,
success: false,
});

throw error;
}
Expand Down
63 changes: 39 additions & 24 deletions test/controllers/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,28 +506,33 @@ describe("Auth Controller", () => {
});
});

it('should trigger a login events the user is logged in', async () => {
it("should trigger a login events the user is logged in", async () => {
kuzzle.emit = sinon.stub();

await kuzzle.auth
.login("strategy", credentials, "expiresIn")
.then(() => {
should(kuzzle.emit).be.calledWith("beforeLogin");
should(kuzzle.emit).be.calledWith("afterLogin", { success: true });
should(kuzzle.emit).be.calledWith("loginAttempt", { success: true });
});
await kuzzle.auth.login("strategy", credentials, "expiresIn").then(() => {
should(kuzzle.emit).be.calledWith("beforeLogin");
should(kuzzle.emit).be.calledWith("afterLogin", { success: true });
should(kuzzle.emit).be.calledWith("loginAttempt", { success: true });
});
kuzzle.emit.reset();

kuzzle.query.rejects();
await kuzzle.auth.login("strategy", credentials, "expiresIn")
await kuzzle.auth
.login("strategy", credentials, "expiresIn")
.catch(() => {
should(kuzzle.emit).be.calledWith("beforeLogin");
should(kuzzle.emit).be.calledWith("afterLogin", { success: false, error: "Error" });
should(kuzzle.emit).be.calledWith("loginAttempt", { success: false, error: "Error" });
should(kuzzle.emit).be.calledWith("afterLogin", {
success: false,
error: "Error",
});
should(kuzzle.emit).be.calledWith("loginAttempt", {
success: false,
error: "Error",
});
});
});

it('should trigger a login events the user is logged in with cookieAuthentication enabled', async () => {
it("should trigger a login events the user is logged in with cookieAuthentication enabled", async () => {
kuzzle.emit = sinon.stub();
kuzzle.cookieAuthentication = true;
kuzzle.query.resolves({
Expand All @@ -536,22 +541,26 @@ describe("Auth Controller", () => {
},
});

await kuzzle.auth
.login("strategy", credentials, "expiresIn")
.then(() => {
should(kuzzle.emit).be.calledWith("beforeLogin");
should(kuzzle.emit).be.calledWith("afterLogin", { success: true });
should(kuzzle.emit).be.calledWith("loginAttempt", { success: true });
});
await kuzzle.auth.login("strategy", credentials, "expiresIn").then(() => {
should(kuzzle.emit).be.calledWith("beforeLogin");
should(kuzzle.emit).be.calledWith("afterLogin", { success: true });
should(kuzzle.emit).be.calledWith("loginAttempt", { success: true });
});
kuzzle.emit.reset();

kuzzle.query.rejects();
await should(kuzzle.auth.login("strategy", credentials, "expiresIn"))
.be.rejected()
.catch(() => {
should(kuzzle.emit).be.calledWith("beforeLogin");
should(kuzzle.emit).be.calledWith("afterLogin", { success: false, error: "Error" });
should(kuzzle.emit).be.calledWith("loginAttempt", { success: false, error: "Error" });
should(kuzzle.emit).be.calledWith("afterLogin", {
success: false,
error: "Error",
});
should(kuzzle.emit).be.calledWith("loginAttempt", {
success: false,
error: "Error",
});
});
});

Expand Down Expand Up @@ -616,11 +625,14 @@ describe("Auth Controller", () => {
// ? Fail logout
kuzzle.query.rejects();
await kuzzle.auth.logout().catch(() => {
should(kuzzle.emit).be.calledWith("logoutAttempt", { success: false, error: "Error" });
should(kuzzle.emit).be.calledWith("logoutAttempt", {
success: false,
error: "Error",
});
});
});

it('should trigger logout events when the user is logged out', async () => {
it("should trigger logout events when the user is logged out", async () => {
kuzzle.emit = sinon.stub();
await kuzzle.auth.logout().then(() => {
should(kuzzle.emit).be.calledWith("beforeLogout");
Expand All @@ -631,7 +643,10 @@ describe("Auth Controller", () => {
// ? Fail logout
kuzzle.query.rejects();
await kuzzle.auth.logout().catch(() => {
should(kuzzle.emit).be.calledWith("afterLogout", { success: false, error: "Error" });
should(kuzzle.emit).be.calledWith("afterLogout", {
success: false,
error: "Error",
});
});
});
});
Expand Down

0 comments on commit 11c9f7b

Please sign in to comment.