-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25d5be6
commit d5c20c2
Showing
10 changed files
with
237 additions
and
17 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,18 +1,25 @@ | ||
import { createTestingVuetify } from "@@/tests/test-utils/setup"; | ||
import { mount } from "@vue/test-utils"; | ||
import { VBtn } from "vuetify/lib/components/index.mjs"; | ||
import AboutView from "./AboutView.vue"; | ||
|
||
describe("AboutView", () => { | ||
const getWrapper = () => { | ||
const wrapper = mount(AboutView); | ||
const wrapper = mount(AboutView, { | ||
global: { plugins: [createTestingVuetify()] }, | ||
}); | ||
|
||
return { | ||
wrapper, | ||
}; | ||
}; | ||
|
||
it("should improve the code coverage", () => { | ||
it("should increase the counter button on click", async () => { | ||
const { wrapper } = getWrapper(); | ||
|
||
expect(wrapper).toBeDefined(); | ||
const counter = wrapper.getComponent(VBtn); | ||
await counter.trigger("click"); | ||
|
||
expect(counter.text()).toEqual("1"); | ||
}); | ||
}); |
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,7 +1,12 @@ | ||
<template> | ||
<main> | ||
<h3>This is the new and improved Superhero-Dashboard!</h3> | ||
<VBtn @click="counter++">{{ counter }}</VBtn> | ||
</main> | ||
</template> | ||
|
||
<script setup lang="ts"></script> | ||
<script setup lang="ts"> | ||
import { Ref, ref } from "vue"; | ||
const counter: Ref<number> = ref(0); | ||
</script> |
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,121 @@ | ||
import { meResponseFactory } from "@@/tests/test-utils/factory"; | ||
import { useAuthStore } from "@data/auth"; | ||
import { createTestingPinia } from "@pinia/testing"; | ||
import { setActivePinia } from "pinia"; | ||
import { RouteLocationNormalized } from "vue-router"; | ||
import { isAuthenticatedGuard } from "./is-authenticated.guard"; | ||
|
||
jest.mock("./login-redirect-url", () => ({ | ||
getLoginUrlWithRedirect: () => "login-url", | ||
})); | ||
|
||
describe("Authentication Guard", () => { | ||
beforeEach(() => { | ||
setActivePinia(createTestingPinia()); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe("isAuthenticatedGuard", () => { | ||
describe("when authenticated", () => { | ||
const setup = () => { | ||
const to: RouteLocationNormalized = { | ||
fullPath: "/test", | ||
} as RouteLocationNormalized; | ||
const from: RouteLocationNormalized = {} as RouteLocationNormalized; | ||
const next = jest.fn(); | ||
const authStore = useAuthStore(); | ||
|
||
authStore.me = meResponseFactory.build(); | ||
|
||
return { | ||
to, | ||
from, | ||
next, | ||
}; | ||
}; | ||
|
||
it("should pass", () => { | ||
const { to, from, next } = setup(); | ||
|
||
isAuthenticatedGuard(to, from, next); | ||
|
||
expect(next).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe("when the url is public", () => { | ||
const setup = () => { | ||
const to: RouteLocationNormalized = { | ||
fullPath: "/test", | ||
meta: { | ||
isPublic: true, | ||
}, | ||
} as unknown as RouteLocationNormalized; | ||
const from: RouteLocationNormalized = {} as RouteLocationNormalized; | ||
const next = jest.fn(); | ||
const authStore = useAuthStore(); | ||
|
||
authStore.me = null; | ||
|
||
return { | ||
to, | ||
from, | ||
next, | ||
}; | ||
}; | ||
|
||
it("should pass", () => { | ||
const { to, from, next } = setup(); | ||
|
||
isAuthenticatedGuard(to, from, next); | ||
|
||
expect(next).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe("when not authenticated and the url is not public", () => { | ||
const setup = () => { | ||
const to: RouteLocationNormalized = { | ||
fullPath: "/test", | ||
} as RouteLocationNormalized; | ||
const from: RouteLocationNormalized = {} as RouteLocationNormalized; | ||
const next = jest.fn(); | ||
const authStore = useAuthStore(); | ||
|
||
authStore.me = null; | ||
|
||
const assign = jest.fn(); | ||
Object.defineProperty(window, "location", { | ||
configurable: true, | ||
value: { assign }, | ||
}); | ||
|
||
return { | ||
to, | ||
from, | ||
next, | ||
assign, | ||
}; | ||
}; | ||
|
||
it("should redirect to login", () => { | ||
const { to, from, next, assign } = setup(); | ||
|
||
isAuthenticatedGuard(to, from, next); | ||
|
||
expect(assign).toHaveBeenCalledWith("login-url"); | ||
}); | ||
|
||
it("should not pass", () => { | ||
const { to, from, next } = setup(); | ||
|
||
isAuthenticatedGuard(to, from, next); | ||
|
||
expect(next).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
src/router/login-redirect-url.ts → src/router/guards/login-redirect-url.ts
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,87 @@ | ||
import { envsFactory } from "@@/tests/test-utils/factory"; | ||
import { useEnvConfigStore } from "@data/env-config"; | ||
import { createPinia, setActivePinia } from "pinia"; | ||
import { getLoginUrlWithRedirect } from "./login-redirect-url"; | ||
|
||
describe("Login Redirect Util", () => { | ||
beforeEach(() => { | ||
setActivePinia(createPinia()); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe("getLoginUrlWithRedirect", () => { | ||
describe("when redirecting to internal login before redirecting to the target url", () => { | ||
const setup = () => { | ||
const loginUrl = "https://test.com/login"; | ||
const targetPath = "/dashboard"; | ||
const origin = "https://test.com"; | ||
const envConfigStore = useEnvConfigStore(); | ||
|
||
envConfigStore.setEnvs( | ||
envsFactory.build({ | ||
NOT_AUTHENTICATED_REDIRECT_URL: loginUrl, | ||
}) | ||
); | ||
|
||
jest.spyOn(window, "location", "get").mockReturnValue({ | ||
origin: origin, | ||
} as Location); | ||
|
||
return { | ||
loginUrl, | ||
targetPath, | ||
origin, | ||
}; | ||
}; | ||
|
||
it("should redirect to internal login with post-login-redirect to internal target url", () => { | ||
const { loginUrl, targetPath, origin } = setup(); | ||
|
||
const result = getLoginUrlWithRedirect(targetPath); | ||
|
||
expect(result).toEqual( | ||
`${loginUrl}?redirect=${encodeURIComponent(`${origin}${targetPath}`)}` | ||
); | ||
}); | ||
}); | ||
|
||
describe("when redirecting to an external login before redirecting to the target url", () => { | ||
const setup = () => { | ||
const origin = "https://test.com"; | ||
const loginUrl = `https://external-login.thr/login?service=${encodeURIComponent(origin)}`; | ||
const targetPath = "/dashboard"; | ||
const envConfigStore = useEnvConfigStore(); | ||
|
||
envConfigStore.setEnvs( | ||
envsFactory.build({ | ||
NOT_AUTHENTICATED_REDIRECT_URL: loginUrl, | ||
}) | ||
); | ||
|
||
jest.spyOn(window, "location", "get").mockReturnValue({ | ||
origin: origin, | ||
} as Location); | ||
|
||
return { | ||
loginUrl, | ||
targetPath, | ||
origin, | ||
}; | ||
}; | ||
|
||
it("should redirect to external login with post-login-redirect to internal target url", () => { | ||
const { loginUrl, targetPath, origin } = setup(); | ||
|
||
const result = getLoginUrlWithRedirect(targetPath); | ||
|
||
const redirectUri = encodeURIComponent(`${origin}${targetPath}`); | ||
expect(result).toEqual( | ||
`${loginUrl}${encodeURIComponent(`/?redirect=${redirectUri}`)}` | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
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