Initial test playground project upload

This commit is contained in:
2023-07-14 23:46:50 +02:00
commit 3b7396ef30
10 changed files with 1628 additions and 0 deletions
Vendored
BIN
View File
Binary file not shown.
+33
View File
@@ -0,0 +1,33 @@
name: Deployment
on:
push:
branches:
- main
- dev
jobs:
test:
environment: testing
runs-on: ubuntu-latest
steps:
- name: Get Code
uses: actions/checkout@v3
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/.npm
key: npm-deps-${{ hashFiles('**/package-lock.json') }}
- name: Install dependencies
run: npm ci
- name: Run server
run: npm start & npx wait-on http://127.0.0.1:XYZ
- name: Run tests
run: npm test
- name: Output information
run: echo "..."
deploy:
needs: test
runs-on: ubuntu-latest
steps:
- name: Output information
run: |
echo "..."
+4
View File
@@ -0,0 +1,4 @@
node_modules/
/test-results/
/playwright-report/
/playwright/.cache/
+12
View File
@@ -0,0 +1,12 @@
import bodyParser from 'body-parser';
import express from 'express';
import eventRoutes from './routes/events.js';
const app = express();
app.use(bodyParser.json());
app.use(eventRoutes);
app.listen(process.env.PORT);
+25
View File
@@ -0,0 +1,25 @@
import { MongoClient } from 'mongodb';
const clusterAddress = process.env.MONGODB_CLUSTER_ADDRESS;
const dbUser = process.env.MONGODB_USERNAME;
const dbPassword = process.env.MONGODB_PASSWORD;
const dbName = process.env.MONGODB_DB_NAME;
const uri = `mongodb+srv://${dbUser}:${dbPassword}@${clusterAddress}/?retryWrites=true&w=majority`;
const client = new MongoClient(uri);
console.log('Trying to connect to db');
try {
await client.connect();
await client.db(dbName).command({ ping: 1 });
console.log('Connected successfully to server');
} catch (error) {
console.log('Connection failed.');
await client.close();
console.log('Connection closed.');
}
const database = client.db(dbName);
export default database;
+1453
View File
File diff suppressed because it is too large Load Diff
+23
View File
@@ -0,0 +1,23 @@
{
"name": "backend",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "index.js",
"scripts": {
"start": "node app.js",
"test": "npx playwright test"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.0",
"express": "^4.18.1",
"mongodb": "^4.9.1"
},
"devDependencies": {
"@playwright/test": "^1.25.1",
"playwright": "^1.25.1"
}
}
+32
View File
@@ -0,0 +1,32 @@
/**
* @see https://playwright.dev/docs/test-configuration
* @type {import('@playwright/test').PlaywrightTestConfig}
*/
const config = {
testDir: './tests',
/* Maximum time one test can run for. */
timeout: 30 * 1000,
expect: {
/**
* Maximum time expect() should wait for the condition to be met.
* For example in `await expect(locator).toHaveText();`
*/
timeout: 5000,
},
/* 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: {
baseURL: `http://127.0.0.1:${process.env.PORT}`,
},
};
export default config;
+21
View File
@@ -0,0 +1,21 @@
import { Router } from 'express';
import db from '../data/database.js';
const router = Router();
router.get('/', async (req, res) => {
const allEvents = await db.collection('events').find().toArray();
res.json({ events: allEvents });
});
router.post('/', async (req, res) => {
const eventData = req.body;
const result = await db.collection('events').insertOne({...eventData});
res.status(201).json({
message: 'Event created.',
event: { ...eventData, id: result.insertedId },
});
});
export default router;
+25
View File
@@ -0,0 +1,25 @@
// @ts-check
import { test, expect } from '@playwright/test';
test('event creation', async ({ request }) => {
const testTitle = 'Test event';
const response = await request.post('/', {
data: {
title: testTitle,
},
});
expect(response.ok()).toBeTruthy();
const resDataRaw = await response.body();
const resData = JSON.parse(resDataRaw.toString());
expect(resData).toHaveProperty('event.id');
expect(resData.event.title).toBe(testTitle);
});
test('getting events', async ({ request }) => {
const response = await request.get('/');
expect(response.ok()).toBeTruthy();
const resDataRaw = await response.body();
const resData = JSON.parse(resDataRaw.toString());
expect(resData).toHaveProperty('events');
expect(resData.events.length).toBeGreaterThan(0);
});