diff --git a/.github/workflows/jest.yml b/.github/workflows/jest.yml new file mode 100644 index 0000000..2cbda8e --- /dev/null +++ b/.github/workflows/jest.yml @@ -0,0 +1,19 @@ +name: Jest Tests +on: workflow_dispatch +# push: +# branches: [ main, master ] +# pull_request: +# branches: [ main, master ] +jobs: + test: + timeout-minutes: 60 + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: lts/* + - name: Install dependencies + run: npm ci + - name: Run Jest tests + run: npx jest --silent diff --git a/.gitignore b/.gitignore index 53e297f..6c05cf0 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,9 @@ certs/ # macOS-specific files .DS_Store + +# Playwright +/test-results/ +/playwright-report/ +/blob-report/ +/playwright/.cache/ diff --git a/jest.config.cjs b/jest.config.cjs index 2bddfac..b8017d1 100644 --- a/jest.config.cjs +++ b/jest.config.cjs @@ -8,5 +8,11 @@ module.exports = { }, testEnvironment: 'node', rootDir: '.', - testMatch: ['/test/**/*.test.ts'], + testMatch: ['/tests/backend/**/*.test.ts'], + reporters: [ + "default", + ["./node_modules/jest-html-reporter", { + "pageTitle": "Test Report" + }] + ], }; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 6114eb4..8f6edf9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,10 +15,13 @@ "mongodb": "^6.2.0" }, "devDependencies": { + "@playwright/test": "^1.50.1", "@types/jest": "^29.5.12", + "@types/node": "^22.13.4", "@vitejs/plugin-basic-ssl": "^1.1.0", "dotenv": "^16.4.5", "jest": "^29.7.0", + "jest-html-reporter": "^4.0.1", "ts-jest": "^29.1.4", "ts-node": "^10.9.2" } @@ -2055,6 +2058,21 @@ "node": ">=10" } }, + "node_modules/@playwright/test": { + "version": "1.50.1", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.50.1.tgz", + "integrity": "sha512-Jii3aBg+CEDpgnuDxEp/h7BimHcUTDlpEtce89xEumlJ5ef2hqepZ+PWp1DDpYC/VO9fmWVI1IlEaoI5fK9FXQ==", + "dev": true, + "dependencies": { + "playwright": "1.50.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/@rollup/rollup-android-arm-eabi": { "version": "4.13.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.13.1.tgz", @@ -2420,11 +2438,11 @@ "integrity": "sha512-IfYcSBWE3hLpBg8+X2SEa8LVkJdJEkT2Ese2aaLs3ptGdVtABxndrMaxuFlQ1qdFf9Q5rDvDpxI3WwgvKFAsQA==" }, "node_modules/@types/node": { - "version": "20.8.9", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.8.9.tgz", - "integrity": "sha512-UzykFsT3FhHb1h7yD4CA4YhBHq545JC0YnEz41xkipN88eKQtL6rSgocL5tbAP6Ola9Izm/Aw4Ora8He4x0BHg==", + "version": "22.13.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.13.4.tgz", + "integrity": "sha512-ywP2X0DYtX3y08eFVx5fNIw7/uIv8hYUKgXoK8oayJlLnKcRfEYCxWMVE1XagUdVtCJlZT1AU4LXEABW+L1Peg==", "dependencies": { - "undici-types": "~5.26.4" + "undici-types": "~6.20.0" } }, "node_modules/@types/stack-utils": { @@ -3850,6 +3868,15 @@ "resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.28.tgz", "integrity": "sha512-aylIc7Z9y4yzHYAJNuESG3hfhC+0Ibp/MAMiaOZgNv4pmEdFyfZhhhny4MNiAfWdBQ1RQ2mfDWmM1x8SvGyp8g==" }, + "node_modules/dateformat": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-3.0.2.tgz", + "integrity": "sha512-EelsCzH0gMC2YmXuMeaZ3c6md1sUJQxyb1XXc4xaisi/K6qKukqZhKPrEQyRkdNIncgYyLoDTReq0nNyuKerTg==", + "dev": true, + "engines": { + "node": "*" + } + }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -6060,6 +6087,49 @@ "fsevents": "^2.3.2" } }, + "node_modules/jest-html-reporter": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/jest-html-reporter/-/jest-html-reporter-4.0.1.tgz", + "integrity": "sha512-AvE1/z59BbkNFC/GQa8eL7x4V8uN1vjW2QKF8+OqG89dppCnF3jgEU3Ix5ia8SA0c9or8YFhsILAjGtgi3I/mg==", + "dev": true, + "dependencies": { + "@jest/reporters": "^29.7.0", + "@jest/test-result": "^29.7.0", + "@jest/types": "^29.6.3", + "dateformat": "3.0.2", + "mkdirp": "^1.0.3", + "strip-ansi": "6.0.1", + "xmlbuilder": "15.0.0" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "jest": "19.x - 29.x", + "typescript": "^3.7.x || ^4.3.x || ^5.x" + } + }, + "node_modules/jest-html-reporter/node_modules/ansi-regex": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", + "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/jest-html-reporter/node_modules/strip-ansi": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", + "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", + "dev": true, + "dependencies": { + "ansi-regex": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/jest-leak-detector": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/jest-leak-detector/-/jest-leak-detector-29.7.0.tgz", @@ -8784,6 +8854,50 @@ "pathe": "^1.1.0" } }, + "node_modules/playwright": { + "version": "1.50.1", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.50.1.tgz", + "integrity": "sha512-G8rwsOQJ63XG6BbKj2w5rHeavFjy5zynBA9zsJMMtBoe/Uf757oG12NXz6e6OirF7RCrTVAKFXbLmn1RbL7Qaw==", + "dev": true, + "dependencies": { + "playwright-core": "1.50.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "fsevents": "2.3.2" + } + }, + "node_modules/playwright-core": { + "version": "1.50.1", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.50.1.tgz", + "integrity": "sha512-ra9fsNWayuYumt+NiM069M6OkcRb1FZSK8bgi66AtpFoWkg2+y0bJSNmkFrWhMbEBbVKC/EruAHH3g0zmtwGmQ==", + "dev": true, + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/playwright/node_modules/fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "dev": true, + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/postcss": { "version": "8.4.38", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.38.tgz", @@ -10557,9 +10671,9 @@ "integrity": "sha512-Y7HYmWaFwPUmkoQCUIAYpKqkOf+SbVj/2fJJZ4RJMCfZp0rTGwRbzQD+HghfnhKOjL9E01okqz+ncJskGYfBNw==" }, "node_modules/undici-types": { - "version": "5.26.5", - "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", - "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==" + "version": "6.20.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.20.0.tgz", + "integrity": "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==" }, "node_modules/unherit": { "version": "3.0.1", @@ -11414,6 +11528,15 @@ "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", "dev": true }, + "node_modules/xmlbuilder": { + "version": "15.0.0", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-15.0.0.tgz", + "integrity": "sha512-KLu/G0DoWhkncQ9eHSI6s0/w+T4TM7rQaLhtCaL6tORv8jFlJPlnGumsgTcGfYeS1qZ/IHqrvDG7zJZ4d7e+nw==", + "dev": true, + "engines": { + "node": ">=8.0" + } + }, "node_modules/y18n": { "version": "5.0.8", "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", diff --git a/package.json b/package.json index 8d2c79b..35bac5b 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "build": "astro build", "preview": "astro preview", "astro": "astro", - "test": "jest --detectOpenHandles --silent=false --verbose=true" + "test": "jest --silent && playwright test" }, "dependencies": { "@astrojs/node": "^8.2.5", @@ -19,10 +19,13 @@ "mongodb": "^6.2.0" }, "devDependencies": { + "@playwright/test": "^1.50.1", "@types/jest": "^29.5.12", + "@types/node": "^22.13.4", "@vitejs/plugin-basic-ssl": "^1.1.0", "dotenv": "^16.4.5", "jest": "^29.7.0", + "jest-html-reporter": "^4.0.1", "ts-jest": "^29.1.4", "ts-node": "^10.9.2" } diff --git a/playwright.config.ts b/playwright.config.ts new file mode 100644 index 0000000..e10fb6a --- /dev/null +++ b/playwright.config.ts @@ -0,0 +1,80 @@ +import { defineConfig, devices } from '@playwright/test'; + +/** + * Read environment variables from file. + * https://github.com/motdotla/dotenv + */ +// import dotenv from 'dotenv'; +// import path from 'path'; +// dotenv.config({ path: path.resolve(__dirname, '.env') }); + +/** + * See https://playwright.dev/docs/test-configuration. + */ +export default defineConfig({ + testDir: './tests/frontend', + /* Run tests in files in parallel */ + fullyParallel: true, + /* Fail the build on CI if you accidentally left test.only in the source code. */ + forbidOnly: !!process.env.CI, + /* Retry on CI only */ + retries: process.env.CI ? 2 : 0, + /* Opt out of parallel tests on CI. */ + workers: process.env.CI ? 1 : undefined, + /* Reporter to use. See https://playwright.dev/docs/test-reporters */ + reporter: 'html', + /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ + use: { + /* Base URL to use in actions like `await page.goto('/')`. */ + // baseURL: 'http://127.0.0.1:3000', + + /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ + trace: 'on-first-retry', + ignoreHTTPSErrors: true + }, + + /* Configure projects for major browsers */ + projects: [ + { + name: 'chromium', + use: { ...devices['Desktop Chrome'] }, + }, + + { + name: 'firefox', + use: { ...devices['Desktop Firefox'] }, + }, + + { + name: 'webkit', + use: { ...devices['Desktop Safari'] }, + }, + + /* Test against mobile viewports. */ + // { + // name: 'Mobile Chrome', + // use: { ...devices['Pixel 5'] }, + // }, + // { + // name: 'Mobile Safari', + // use: { ...devices['iPhone 12'] }, + // }, + + /* Test against branded browsers. */ + // { + // name: 'Microsoft Edge', + // use: { ...devices['Desktop Edge'], channel: 'msedge' }, + // }, + // { + // name: 'Google Chrome', + // use: { ...devices['Desktop Chrome'], channel: 'chrome' }, + // }, + ], + + /* Run your local dev server before starting the tests */ + // webServer: { + // command: 'npm run start', + // url: 'http://127.0.0.1:3000', + // reuseExistingServer: !process.env.CI, + // }, +}); diff --git a/src/lib/classes/managers/LocationManager.ts b/src/lib/classes/managers/LocationManager.ts index 0ad21ce..a44c2dd 100644 --- a/src/lib/classes/managers/LocationManager.ts +++ b/src/lib/classes/managers/LocationManager.ts @@ -260,5 +260,7 @@ class LocationManager { } const locationManager = LocationManager.getInstance(); -await locationManager.init(); +(async () => { + await locationManager.init(); +})(); export default locationManager; \ No newline at end of file diff --git a/src/lib/db/mongodb.ts b/src/lib/db/mongodb.ts index b51d222..71f6a23 100644 --- a/src/lib/db/mongodb.ts +++ b/src/lib/db/mongodb.ts @@ -6,13 +6,24 @@ const options = { tlsCertificateKeyFile: config.MONGODB_CERT }; -const mongo = new MongoClient(uri, options); -await mongo.connect(); +export const connect = async () => { + return await mongo.connect(); +} export const disconnect = async () => { mongo.close(); } +const mongo = new MongoClient(uri, options); +(async () => { + try { + await connect(); + console.log("Connected to MongoDB"); + } catch (error) { + console.error("MongoDB connection error:", error); + } +})(); + export const getDB = async (dbName = config.MONGODB_DB) => { return mongo.db(dbName); } diff --git a/src/lib/db/ships.ts b/src/lib/db/ships.ts index 558ac3c..a0094f0 100644 --- a/src/lib/db/ships.ts +++ b/src/lib/db/ships.ts @@ -5,7 +5,7 @@ export const getAllShips = async () => { return (await Ships()).find({}).toArray() as unknown as Array; } -export const getResourceById = async (id: string) => { +export const getShipById = async (id: string) => { return (await Ships()).findOne({ id }) as unknown as DBShip; diff --git a/tests/backend/api.test.ts b/tests/backend/api.test.ts new file mode 100644 index 0000000..65a965d --- /dev/null +++ b/tests/backend/api.test.ts @@ -0,0 +1,135 @@ +import config from '../../config.json'; + +describe('/api/auth', () => { + test('GET /testAccessToken', async () => { + const token = "A.MTczOTY1ODI1NTA5MA.NjUzZTk2NzdlNWMzZDM2YjE1YmE3NGVi.hQW5wy0J1siFhTYvdU5GJw" + const response = await fetch('https://localhost:4321/api/auth/testAccessToken', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}` + } + }); + + const data = await response.json(); + expect(data.code).toBe(200); + }); + + test('POST /generateAccessToken', async () => { + const response = await fetch('https://localhost:4321/api/auth/generateAccessToken', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${config.MASTER_ACCESSTOKEN}` + }, + body: JSON.stringify({ username: "test" }) + }); + + const data = await response.json(); + + expect(data.code).toBe(404); + }); +}); + +describe('/api/fleet', () => { + test('GET /getDistance', async () => { + const response = await fetch('https://localhost:4321/api/fleet/getDistance?source=661d1163567111a5b4be8829&destination=66cf55650df1e5fe8a5c20d1', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + } + }); + + const data = await response.json(); + expect(data.code).toBe(200); + expect(data.distance).toBe(120); + }); + + test('GET /getDistance (missing parameters)', async () => { + const response = await fetch('https://localhost:4321/api/fleet/getDistance', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + } + }); + + const data = await response.json(); + expect(data.code).toBe(400); + }); + + test('GET /getDistance (source not found)', async () => { + const response = await fetch('https://localhost:4321/api/fleet/getDistance?source=123&destination=66cf55650df1e5fe8a5c20d1', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + } + }); + + const data = await response.json(); + expect(data.code).toBe(404); + }); + + test('GET /getDistance (destination not found)', async () => { + const response = await fetch('https://localhost:4321/api/fleet/getDistance?source=661d1163567111a5b4be8829&destination=123', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + } + }); + + const data = await response.json(); + expect(data.code).toBe(404); + }); + + test('GET /status', async () => { + const token = "A.MTczOTY1ODI1NTA5MA.NjUzZTk2NzdlNWMzZDM2YjE1YmE3NGVi.hQW5wy0J1siFhTYvdU5GJw" + const response = await fetch('https://localhost:4321/api/fleet/status', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}` + } + }); + + const data = await response.json(); + expect(data.code).toBe(200); + }); +}); + +describe('/api/lang', () => { + test('GET .json', async () => { + const response = await fetch('https://localhost:4321/api/lang.json', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + } + }); + + const data: { id: string, name: string }[] = await response.json(); + expect(data.find(lang => lang.id === "en")).toBeTruthy(); + }); + + test('GET /en.json', async () => { + const response = await fetch('https://localhost:4321/api/lang/en.json', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + } + }); + + const data = await response.json(); + expect(data.code).toBe(200); + }); + + test('GET /123.json (not found)', async () => { + const response = await fetch('https://localhost:4321/api/lang/123.json', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + } + }); + + const data = await response.json(); + expect(data.code).toBe(404); + }); +}); \ No newline at end of file diff --git a/tests/backend/database.test.ts b/tests/backend/database.test.ts new file mode 100644 index 0000000..d109bee --- /dev/null +++ b/tests/backend/database.test.ts @@ -0,0 +1,235 @@ +import { getUserById, getUserByNickOrEmail } from '../../src/lib/db/users'; +import { connect } from '../../src/lib/db/mongodb'; +import { ObjectId } from 'mongodb'; +import { getAllSystems, getSystemById } from '../../src/lib/db/systems'; +import { getAllStructures, getStructureById } from '../../src/lib/db/structures'; +import { getSpyReportById, getSpyReportBySystemId } from '../../src/lib/db/spyReports'; +import { getAllShips, getShipById } from '../../src/lib/db/ships'; +import { getAllSectors, getSectorById } from '../../src/lib/db/sectors'; +import { getAllResources, getResourceById } from '../../src/lib/db/resources'; +import { getAllResearch, getResearchById } from '../../src/lib/db/research'; +import { getAllPlanets, getPlanetById } from '../../src/lib/db/planets'; +import { getMailById, getMailsByTo } from '../../src/lib/db/mails'; +import { getAllGalaxies, getGalaxyById } from '../../src/lib/db/galaxies'; +import { getAllFleet, getAllFleetByUser } from '../../src/lib/db/fleet'; +import { getAllDefenses, getDefenseById } from '../../src/lib/db/defenses'; +import { getAllBuildings, getBuildingById } from '../../src/lib/db/buildings'; +import { getAccessToken } from '../../src/lib/db/accessTokens'; +import { createHash } from 'crypto'; + +beforeEach(async () => { + await connect(); +}); + +describe('db_users', () => { + test('getUserByNickOrEmail', async () => { + const user = await getUserByNickOrEmail("gargamel"); + expect(user).not.toBeNull(); + expect(user?.username).toBe("gargamel"); + expect(user?.email).toBe("gargamel@smerfy.pl"); + }); + + test('getUserById', async () => { + const user = await getUserById(new ObjectId("653e9677e5c3d36b15ba74eb")); + expect(user).not.toBeNull(); + expect(user?.username).toBe("gargamel"); + expect(user?.email).toBe("gargamel@smerfy.pl"); + }); +}); + +describe('db_systems', () => { + test('getAllSystems', async () => { + const systems = await getAllSystems(); + expect(systems).not.toBeNull(); + expect(systems.length).toBeGreaterThan(0); + }); + + test('getSystemById', async () => { + const system = await getSystemById(new ObjectId("66cf55650df1e5fe8a5c20d1")); + expect(system).not.toBeNull(); + expect(system?.name).toBe("HelioNad"); + }); +}); + +describe('db_structures', () => { + test('getAllStructures', async () => { + const structures = await getAllStructures(); + expect(structures).not.toBeNull(); + expect(structures.length).toBeGreaterThan(0); + }); + + test('getStructureById', async () => { + const structure = await getStructureById("dyson-sphere"); + expect(structure).not.toBeNull(); + }); +}); + +describe('db_spyReports', () => { + test('getSpyReportById', async () => { + const spyReport = await getSpyReportById(new ObjectId("67aa16abb3cf8ee89d0f00d9")); + expect(spyReport).not.toBeNull(); + expect(spyReport.victimId).toStrictEqual(new ObjectId("653e9677e5c3d36b15ba74eb")); + }); + + test('getSpyReportBySystemId', async () => { + const spyReport = await getSpyReportBySystemId(new ObjectId("66cf55650df1e5fe8a5c20d1")); + expect(spyReport).not.toBeNull(); + expect(spyReport.victimId).toStrictEqual(new ObjectId("653e9677e5c3d36b15ba74eb")); + }); +}); + +describe('db_ships', () => { + test('getAllShips', async () => { + const ships = await getAllShips(); + expect(ships).not.toBeNull(); + expect(ships.length).toBeGreaterThan(0); + }); + + test('getShipById', async () => { + const ship = await getShipById("transporter"); + expect(ship).not.toBeNull(); + }); +}); + +describe('db_sectors', () => { + test('getAllSectors', async () => { + const sectors = await getAllSectors(); + expect(sectors).not.toBeNull(); + expect(sectors.length).toBe(32); + }); + + test('getSectorById', async () => { + const sector = await getSectorById(new ObjectId("66cf55f20df1e5fe8a5c20d3")); + expect(sector).not.toBeNull(); + expect(sector.name).toBe("Voidcaller Reach"); + }); +}); + +describe('db_resources', () => { + test('getAllResources', async () => { + const resources = await getAllResources(); + expect(resources).not.toBeNull(); + expect(resources.length).toBe(15); + }); + + test('getResourceById', async () => { + const resource = await getResourceById("coal"); + expect(resource).not.toBeNull(); + expect(resource.type).toBe("solid"); + expect(resource.icon).toBe("/images/resources/coal.png"); + }); +}); + +describe('db_research', () => { + test('getAllResearch', async () => { + const research = await getAllResearch(); + expect(research).not.toBeNull(); + expect(research.length).toBeGreaterThan(0); + }); + + test('getResearchById', async () => { + const research = await getResearchById("combat-utilities"); + expect(research).not.toBeNull(); + expect(research.onetime).not.toBeTruthy(); + }); +}); + +describe('db_planets', () => { + test('getAllPlanets', async () => { + const planets = await getAllPlanets(); + expect(planets).not.toBeNull(); + expect(planets.length).toBeGreaterThan(0); + }); + + test('getPlanetById', async () => { + const planet = await getPlanetById(new ObjectId("661d1163567111a5b4be8829")); + expect(planet).not.toBeNull(); + expect(planet.name).toBe("Lyoko"); + expect(planet.owner).toStrictEqual(new ObjectId("653e9677e5c3d36b15ba74eb")); + }); +}); + +describe('db_mails', () => { + test('getMailById', async () => { + const mail = await getMailById(new ObjectId("673f16a8e140c989f59f54c3")); + expect(mail).not.toBeNull(); + expect(mail.from).toStrictEqual(new ObjectId("65401221e5c3d36b15ba74ed")); + expect(mail.to).toStrictEqual(new ObjectId("653e9677e5c3d36b15ba74eb")); + expect(mail.subject).toBe("Shrek is love, Shrek is life"); + }); + + test('getMailsByTo', async () => { + const mails = await getMailsByTo(new ObjectId("653e9677e5c3d36b15ba74eb")); + expect(mails).not.toBeNull(); + expect(mails.length).toBeGreaterThan(0); + }); +}); + +describe('db_galaxies', () => { + test('getAllGalaxies', async () => { + const galaxies = await getAllGalaxies(); + expect(galaxies).not.toBeNull(); + expect(galaxies.length).toBe(4); + }); + + test('getGalaxyById', async () => { + const galaxy = await getGalaxyById(new ObjectId("66cf553a0df1e5fe8a5c20cc")); + expect(galaxy).not.toBeNull(); + expect(galaxy.name).toBe("Luminara"); + }); +}); + +describe('db_fleet', () => { + test('getAllFleet', async () => { + const fleet = await getAllFleet(); + expect(fleet).not.toBeNull(); + expect(fleet.length).toBeGreaterThan(0); + }); + + test('getAllFleetByUser', async () => { + const fleet = await getAllFleetByUser(new ObjectId("653e9677e5c3d36b15ba74eb")); + expect(fleet).not.toBeNull(); + expect(fleet.length).toBeGreaterThan(0); + }); +}); + +describe('db_defenses', () => { + test('getAllDefenses', async () => { + const defenses = await getAllDefenses(); + expect(defenses).not.toBeNull(); + expect(defenses.length).toBeGreaterThan(0); + }); + + test('getDefenseById', async () => { + const defense = await getDefenseById("laser-turret"); + expect(defense).not.toBeNull(); + }); +}); + +describe('db_buildings', () => { + test('getAllBuildings', async () => { + const buildings = await getAllBuildings(); + expect(buildings).not.toBeNull(); + expect(buildings.length).toBeGreaterThan(0); + }); + + test('getBuildingById', async () => { + const building = await getBuildingById("iron-mine"); + expect(building).not.toBeNull(); + expect(building.category).toBe("mines"); + }); +}); + +describe('db_accessTokens', () => { + test('getAccessToken', async () => { + const rawToken = "A.MTczOTY1ODI1NTA5MA.NjUzZTk2NzdlNWMzZDM2YjE1YmE3NGVi.hQW5wy0J1siFhTYvdU5GJw"; + const token = await getAccessToken(rawToken); + expect(token).not.toBeNull(); + expect(token?.type).toBe("A"); + expect(token?.user).toStrictEqual(new ObjectId("653e9677e5c3d36b15ba74eb")); + expect(token?.createdFrom).toBe("loginForm"); + const random = rawToken.split(".")[3]; + const hash = createHash("sha256").update(random).digest("hex"); + expect(token?.entropy).toBe(hash); + }); +}); \ No newline at end of file diff --git a/tests/frontend/login.spec.ts b/tests/frontend/login.spec.ts new file mode 100644 index 0000000..b8aee3f --- /dev/null +++ b/tests/frontend/login.spec.ts @@ -0,0 +1,47 @@ +import { test, expect, } from '@playwright/test'; + +test.use({ + ignoreHTTPSErrors: true +}); + +test('successfull_login', async ({ page }) => { + await page.goto('https://localhost:4321/login'); + await page.locator("[name='username']").click(); + await page.locator("[name='username']").fill('gargamel'); + await page.locator("[name='username']").press('Tab'); + await page.locator("[name='password']").fill('klakier1'); + await page.locator(".login-submit").click(); + await expect(page.locator('#fleet-status')).toBeVisible(); +}); + +test('invalid_username', async ({ page }) => { + await page.goto('https://localhost:4321/login'); + await page.locator("[name='username']").click(); + await page.locator("[name='username']").fill('wihwkdietaielgba'); + await page.locator("[name='username']").press('Tab'); + await page.locator("[name='password']").fill('qwerty123'); + await page.locator(".login-submit").click(); + await expect(page.getByText('Invalid username or password')).toBeVisible(); +}); + +test('invalid_password', async ({ page }) => { + await page.goto('https://localhost:4321/login'); + await page.locator("[name='username']").click(); + await page.locator("[name='username']").fill('gargamel'); + await page.locator("[name='username']").press('Tab'); + await page.locator("[name='password']").fill('qwerty123'); + await page.locator(".login-submit").click(); + await expect(page.getByText('Invalid username or password')).toBeVisible(); +}); + +test('logout', async ({ page }) => { + await page.goto('https://localhost:4321/login'); + await page.locator("[name='username']").click(); + await page.locator("[name='username']").fill('gargamel'); + await page.locator("[name='username']").press('Tab'); + await page.locator("[name='password']").fill('klakier1'); + await page.locator(".login-submit").click(); + await page.getByRole('link', { name: '[profile]' }).click(); + await page.getByRole('link', { name: '[log out]' }).click(); + await expect(page.locator("[name='username']")).toBeVisible(); +}); \ No newline at end of file