generated from HugoMendes98/Nx-NestJS-Angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): can login, redirect after login or be redirected to login…
… if needed
- Loading branch information
Mendes Hugo
committed
Sep 6, 2023
1 parent
17e259b
commit e7df474
Showing
23 changed files
with
630 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,96 @@ | ||
import { DbE2eHelper } from "~/app/backend/e2e/db-e2e/db-e2e.helper"; | ||
import { BASE_SEED } from "~/lib/common/seeds"; | ||
|
||
describe("Auth login", () => { | ||
describe("Auth", () => { | ||
const dbHelper = DbE2eHelper.getHelper("base"); | ||
const db = dbHelper.db as typeof BASE_SEED; | ||
|
||
const [workflow] = db.workflows; | ||
|
||
const pathLogin = "/auth/login"; | ||
/** a path to a protected content */ | ||
const pathProtected = `/workflows/${workflow._id}?param1=1¶m2=2`; | ||
|
||
before(() => dbHelper.refresh()); | ||
|
||
beforeEach(() => { | ||
cy.clearCookies(); | ||
cy.visit("/auth/login"); | ||
cy.authDisconnect(); | ||
cy.visit(pathLogin); | ||
}); | ||
|
||
/* ==== Test Created with Cypress Studio ==== */ | ||
it("should fail with unknown user", () => { | ||
/* ==== Generated with Cypress Studio ==== */ | ||
cy.get("#mat-input-0").type("not@existing.user"); | ||
cy.get("#mat-input-1").type("password"); | ||
cy.get(".mdc-button").click(); | ||
cy.get("#mat-mdc-error-2 > span").should("be.visible"); | ||
/* ==== End Cypress Studio ==== */ | ||
}); | ||
describe("Login", () => { | ||
/* ==== Test Created with Cypress Studio ==== */ | ||
it("should fail with unknown user", () => { | ||
/* ==== Generated with Cypress Studio ==== */ | ||
cy.get("#mat-input-0").type("not@existing.user"); | ||
cy.get("#mat-input-1").type("password"); | ||
cy.get(".mdc-button").click(); | ||
cy.get("#mat-mdc-error-2 > span").should("be.visible"); | ||
/* ==== End Cypress Studio ==== */ | ||
}); | ||
|
||
/* ==== Test Created with Cypress Studio ==== */ | ||
it("should fail with wrong password", () => { | ||
const [{ email, password }] = db.users; | ||
|
||
/* ==== Generated with Cypress Studio ==== */ | ||
cy.get("#mat-input-0").type(email); | ||
cy.get("#mat-input-1").type(`${password}abc`); | ||
cy.get(".mdc-button").click(); | ||
cy.get("#mat-mdc-error-2 > span").should("be.visible"); | ||
/* ==== End Cypress Studio ==== */ | ||
}); | ||
|
||
/* ==== Test Created with Cypress Studio ==== */ | ||
it("should login and redirect to default", () => { | ||
const [{ email, password }] = db.users; | ||
|
||
/* ==== Test Created with Cypress Studio ==== */ | ||
it("should fail with wrong password", () => { | ||
/* ==== Generated with Cypress Studio ==== */ | ||
const [{ email, password }] = db.users; | ||
|
||
/* ==== Generated with Cypress Studio ==== */ | ||
cy.get("#mat-input-0").type(email); | ||
cy.get("#mat-input-1").type(`${password}abc`); | ||
cy.get(".mdc-button").click(); | ||
cy.get("#mat-mdc-error-2 > span").should("be.visible"); | ||
/* ==== End Cypress Studio ==== */ | ||
/* ==== Generated with Cypress Studio ==== */ | ||
cy.get("#mat-input-0").type(email); | ||
cy.get("#mat-input-1").type(password); | ||
|
||
cy.get(".mdc-button").click(); | ||
cy.get(".mat-mdc-card-title").should("contain.text", `Hello ${email}!`); | ||
// /* ==== End Cypress Studio ==== */ | ||
|
||
cy.location("pathname").should("eq", "/"); | ||
}); | ||
}); | ||
|
||
/* ==== Test Created with Cypress Studio ==== */ | ||
it("should login and redirect to default", () => { | ||
/* ==== Generated with Cypress Studio ==== */ | ||
const [{ email, password }] = db.users; | ||
describe("Auth guard", () => { | ||
it("should redirect to the login page when accessing protected content and redirect to this page after login", () => { | ||
const [{ email, password }] = db.users; | ||
|
||
cy.visit(pathProtected); | ||
|
||
cy.get("#mat-input-0").type(email); | ||
cy.get("#mat-input-1").type(password); | ||
cy.location("pathname").should("eq", pathLogin); | ||
cy.location("search").should("eq", `?redirectUrl=${encodeURIComponent(pathProtected)}`); | ||
|
||
cy.get(".mdc-button").click(); | ||
cy.get(".mat-mdc-card-title").should("contain.text", `Hello ${email}!`); | ||
// /* ==== End Cypress Studio ==== */ | ||
cy.get("#mat-input-0").type(email); | ||
cy.get("#mat-input-1").type(password); | ||
cy.get("#mat-input-1").type("{enter}"); | ||
|
||
// eslint-disable-next-line cypress/no-unnecessary-waiting -- Wait for redirection | ||
cy.wait(1000); | ||
cy.location("pathname").should("eq", "/"); | ||
cy.url().should("contains", pathProtected); | ||
}); | ||
|
||
it("should load the protected page when already connected", () => { | ||
const [{ email, password }] = db.users; | ||
|
||
cy.authConnectAs(email, password); | ||
cy.visit(pathProtected); | ||
cy.url().should("contains", pathProtected); | ||
}); | ||
}); | ||
|
||
// describe("Auth interceptor", () => { | ||
// it.only("should redirect when a request has a 401 error", () => { | ||
// const [{ email, password }] = db.users; | ||
// cy.authConnectAs(email, password); | ||
// cy.visit(pathProtected); | ||
// | ||
// // TODO: something that makes a request | ||
// cy.authDisconnect(); | ||
// // TODO: something that makes a request and redirect | ||
// }); | ||
// }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { TestBed } from "@angular/core/testing"; | ||
import { CanActivateFn } from "@angular/router"; | ||
|
||
import { authGuard } from "./auth.guard"; | ||
|
||
describe("authGuard", () => { | ||
const executeGuard: CanActivateFn = (...guardParameters) => | ||
TestBed.runInInjectionContext(() => authGuard(...guardParameters)); | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
}); | ||
|
||
it("should be created", () => { | ||
expect(executeGuard).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { inject } from "@angular/core"; | ||
import { CanActivateFn, CanActivateChildFn, Router } from "@angular/router"; | ||
import { map } from "rxjs"; | ||
|
||
import { AuthService } from "./auth.service"; | ||
|
||
/** | ||
* The guard that checks that the user is connected | ||
* | ||
* @param route same as in {@link CanActivateFn} | ||
* @param state same as in {@link CanActivateFn} | ||
* @returns same as in {@link CanActivateFn} | ||
* | ||
* @see {@link CanActivateFn} | ||
* @see {@link CanActivateChildFn} | ||
*/ | ||
export const authGuard: CanActivateChildFn & CanActivateFn = (route, state) => { | ||
const service = inject(AuthService); | ||
const router = inject(Router); | ||
|
||
return service.userState$.pipe( | ||
map(({ type }) => | ||
type === "connected" ? true : AuthService.createLoginUrlTree(router, state.url) | ||
) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { TestBed } from "@angular/core/testing"; | ||
import { ApiModule } from "~/lib/ng/lib/api"; | ||
|
||
import { AuthInterceptor } from "./auth.interceptor"; | ||
import { AuthModule } from "./auth.module"; | ||
|
||
describe("AuthInterceptor", () => { | ||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
imports: [ApiModule.forRoot({ client: { url: "" } }), AuthModule] | ||
}) | ||
); | ||
|
||
it("should be created", () => { | ||
const interceptor: AuthInterceptor = TestBed.inject(AuthInterceptor); | ||
expect(interceptor).toBeTruthy(); | ||
}); | ||
}); |
Oops, something went wrong.