Added test project requiring mongodb and added new workflow to test Gitea Actions - Service Containers

This commit is contained in:
2023-07-17 16:39:34 +02:00
parent e833c393fb
commit 5c635fd321
12 changed files with 1650 additions and 0 deletions
Vendored
BIN
View File
Binary file not shown.
Binary file not shown.
+36
View File
@@ -0,0 +1,36 @@
name: Test Gitea Actions with Service Containers
on:
push:
branches:
- master
env:
# Global Env Variables readable in all jobs of this workflow
MONGODB_CLUSTER_ADDRESS: mongodb
MONGODB_USERNAME: root
MONGODB_PASSWORD: supersecretpassword
MONGODB_DB_NAME: actions-environment
PORT: 8080
jobs:
build:
runs-on: ubuntu-latest
services:
mongodb:
image: mongodb
env:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: supersecretpassword
steps:
- name: Get Code
uses: actions/checkout@v3
- name: Install NodeJS
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm ci
- name: Run server
run: npm start & npx wait-on http://127.0.0.1:$WEB_PORT
- name: Run tests
run: npm test
+4
View File
@@ -0,0 +1,4 @@
node_modules/
/test-results/
/playwright-report/
/playwright/.cache/
+17
View File
@@ -0,0 +1,17 @@
FROM node:16
ENV MONGODB_CONNECTION_PROTOCOL mongodb+srv
ENV MONGODB_DB_NAME gha-demo1
ENV MONGODB_CLUSTER_ADDRESS cluster0.ntrwp.mongodb.net
ENV MONGODB_USERNAME maximilian
ENV MONGODB_PASSWORD eI3R3UfNlJgWJe17
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
CMD ["npm", "start"]
+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);
+27
View File
@@ -0,0 +1,27 @@
import { MongoClient } from 'mongodb';
const connectionProtocol = process.env.MONGODB_CONNECTION_PROTOCOL;
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 = `${connectionProtocol}://${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.');
process.exit(1);
}
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);
});