Skip to content

Commit

Permalink
Add login options 2e2 test
Browse files Browse the repository at this point in the history
  • Loading branch information
blacelle committed Sep 21, 2024
1 parent 5f828c0 commit d3bc9e3
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 13 deletions.
8 changes: 7 additions & 1 deletion js/e2e-tests/fake-player.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ export default {

return response;
},

async showLoginOptions(page) {
await page.getByRole("link", { name: /You need to login/ }).click();
},

async login(page) {
await page.getByRole("link", { name: /You need to login/ }).click();
await this.showLoginOptions(page);
await page.getByRole("link", { name: /github/ }).click();
// How can we login automatically?
},

async playOptimization(page) {
Expand Down
28 changes: 28 additions & 0 deletions js/e2e-tests/localhost-login-dev.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { test, expect, request } from "@playwright/test";

import fakePlayer from "./fake-player.mjs";

const url = "http://localhost:8080";

//test.beforeAll(async ({ request }) => {
// // Create a new repository
// const response = await fakePlayer.clear(request, url);
// expect(response.ok()).toBeTruthy();
//});

// We just check the login page is working OK
test('showLoginOptions', async ({ page }) => {
await page.goto(url);
await fakePlayer.showLoginOptions(page);
});

test('login', async ({ page }) => {
await page.goto(url);
await fakePlayer.login(page);
});

// We can not login automatically without fakeUser
//test("play-optimization", async ({ page }) => {
// await page.goto(url);
// await fakePlayer.playOptimization(page);
//});
5 changes: 3 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
"lint": "eslint --fix src/main/resources/static",
"lint_debug": "eslint --fix src/main/resources/static --debug",
"preview": "vite preview",
"pw_localhost8080": "npx playwright test localhost8080 --project=chromium",
"pw_localhost8080_ui": "npx playwright test localhost8080 --project=chromium --ui"
"pw_localhost8080": "SPRING_ACTIVE_PROFILES=unsafe,inmemory,fakeuser npx playwright test localhost8080 --project=chromium",
"pw_localhost8080_nofakeuser": "SPRING_ACTIVE_PROFILES=unsafe,inmemory npx playwright test localhost-login --project=chromium",
"pw_localhost8080_ui": "SPRING_ACTIVE_PROFILES=unsafe,inmemory,fakeuser npx playwright test localhost8080 --project=chromium --ui"
},
"type": "module",
"version": "0.0.0"
Expand Down
5 changes: 4 additions & 1 deletion js/playwright.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { defineConfig, devices } from "@playwright/test";
*/
// require('dotenv').config({ path: path.resolve(__dirname, '.env') });

const commandSpringBoot = `(mvn install; cd ../server; mvn spring-boot:run -Dspring-boot.run.profiles=${process.env.SPRING_ACTIVE_PROFILES};)`;
console.log("commandSpringBoot", commandSpringBoot);

/**
* @see https://playwright.dev/docs/test-configuration
*/
Expand Down Expand Up @@ -92,7 +95,7 @@ const config = defineConfig({
// `mvn install` to ensure `js` is fresh (especially important in local dev)
// `cd ../server` to relocate in the backend folder
// `mvn spring-boot:run` to effectively starts a server
command: "(mvn install; cd ../server; mvn spring-boot:run;)",
command: commandSpringBoot,
// url: 'http://127.0.0.1:3000',
url: "http://127.0.0.1:8080",
reuseExistingServer: !process.env.CI,
Expand Down
8 changes: 5 additions & 3 deletions js/src/main/resources/static/ui/js/login-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ export default {
LoginOptions,
},
computed: {
...mapState(useKumiteStore, ["nbAccountFetching"]),
...mapState(useKumiteStore, ["nbAccountFetching", "account", "needsToLogin"]),
...mapState(useKumiteStore, {
user(store) {
return store.account || { error: "not_loaded" };
return store.account || { error: "not_loaded", raw: {error: "not_loaded"} };
},
}),
},
setup() {
const store = useKumiteStore();

console.log("user", store.account || { error: "not_loaded", raw: {error: "not_loaded"} });

store.loadUser();

Expand All @@ -30,6 +32,6 @@ export default {
<LoginOptions />
</div>
</div>
<div>Welcome {{user.raw.name}}. ?Logout?</div>
<div v-else>Welcome {{user.raw.name}}. ?Logout?</div>
`,
};
4 changes: 2 additions & 2 deletions js/src/main/resources/static/ui/js/login-providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
loginProviders: [],
});

async function theData(url) {
async function fetchTheUrl(url) {
try {
isLoading.value = true;
const response = await fetch(url);
Expand All @@ -22,7 +22,7 @@ export default {
}
}

theData("/api/v1/login/v1/providers");
fetchTheUrl("/api/login/v1/providers");

return { isLoading, loginProviders };
},
Expand Down
9 changes: 5 additions & 4 deletions js/src/main/resources/static/ui/js/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const useKumiteStore = defineStore("kumite", {
nbBoardFetching: 0,

// Currently connected account
account: {},
account: { raw: {}},
tokens: {},
// Initially, we assume we are logged-in as we may have a session cookie
// May be turned to true by 401 on `loadUser()`
Expand All @@ -54,6 +54,7 @@ export const useKumiteStore = defineStore("kumite", {
nbBoardOperating: 0,
}),
getters: {
isLoggedIn: (store) => Object.keys(store.account.raw).length > 0,
// There will be a way to choose a different playerId amongst the account playerIds
playingPlayerId: (store) => store.account.playerId,
// Default headers: we authenticate ourselves
Expand Down Expand Up @@ -151,7 +152,7 @@ export const useKumiteStore = defineStore("kumite", {

// @throws UserNeedsToLoginError if not logged-in
async ensureUser() {
if (Object.keys(this.account || {}).length !== 0) {
if (this.isLoggedIn) {
// We have loaded a user: we assume it does not need to login
return Promise.resolve(this.account);
} else {
Expand Down Expand Up @@ -211,8 +212,8 @@ export const useKumiteStore = defineStore("kumite", {
}
}

return this.ensureUser().then(() => {
console.log("We do have a User. Let's fetch tokens");
return this.ensureUser().then(user => {
console.log("We do have a User. Let's fetch tokens", user);
return fetchFromUrl(`/api/login/v1/oauth2/token?player_id=${this.playingPlayerId}`);
});
},
Expand Down

0 comments on commit d3bc9e3

Please sign in to comment.