unit testing #7

Merged
Suguivy merged 67 commits from testing into develop 2023-12-31 11:11:23 +00:00
14 changed files with 176 additions and 147 deletions

View File

@ -1,19 +1,19 @@
name: Gitea Actions Demo name: Unit Tests with docker compose
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀 run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
on: [pull_request] on: [pull_request]
jobs: jobs:
Explore-Gitea-Actions: unit-test:
container: container:
image: oven/bun:1 image: docker:dind
steps: steps:
- run: echo "🎉 The job was automatically triggered by a ${{ gitea.event_name }} event." - name: Starting docker daemon
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!" run: docker-init -- dockerd --host=unix:///var/run/docker.sock &
- run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}." - name: Installing necessary packages
run: apk add npm git curl bash
- name: Check out repository code - name: Check out repository code
uses: actions/checkout@v3 uses: actions/checkout@v3
- run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner." - name: Install project dependencies
- run: echo "🖥️ The workflow is now ready to test your code on the runner." run: npm install
- name: List files in the repository - name: Run docker-compose
run: | run: docker compose down -v && docker compose run bot-api bun test
ls ${{ gitea.workspace }}

View File

@ -2,7 +2,7 @@ version: "3"
services: services:
mongodb: mongodb:
image: mongo image: mongo:bionic
container_name: mongodb container_name: mongodb
ports: ports:
- "27017:27017" - "27017:27017"
@ -12,7 +12,6 @@ services:
MONGO_INITDB_DATABASE: bot MONGO_INITDB_DATABASE: bot
volumes: volumes:
- mongodb_data:/data/db - mongodb_data:/data/db
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
bot-api: bot-api:
image: oven/bun:1 image: oven/bun:1

View File

@ -1,30 +0,0 @@
db.createUser({
user: "root",
pwd: "password",
roles: [
{
role: "readWrite",
db: "admin",
},
{
role: "readWrite",
db: "bot",
},
],
});
db = new Mongo().getDB("bot");
db.images.createIndex({ status: 1 });
db.images.createIndex({ url: 1 }, { unique: true });
db.images.insert({
url: "https://example.com",
status: "consumed",
tags: ["2girls", "sleeping"],
});
db.authorizations.createIndex({ app: 1 });
db.authorizations.insert({
app: "tester",
secret: "test",
});

View File

@ -3,7 +3,10 @@
"module": "index.ts", "module": "index.ts",
"type": "module", "type": "module",
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.21",
"@types/express-list-endpoints": "^6.0.3",
"@types/jest": "^29.5.11", "@types/jest": "^29.5.11",
"@types/jsonwebtoken": "^9.0.5",
"@types/supertest": "^6.0.1", "@types/supertest": "^6.0.1",
"bun-types": "latest", "bun-types": "latest",
"jest": "^29.7.0", "jest": "^29.7.0",
@ -20,10 +23,6 @@
"test": "docker compose down -v && docker compose run bot-api bun test" "test": "docker compose down -v && docker compose run bot-api bun test"
}, },
"dependencies": { "dependencies": {
"@types/express": "^4.17.21",
"@types/express-list-endpoints": "^6.0.3",
"@types/jsonwebtoken": "^9.0.5",
"@types/mongoose": "^5.11.97",
"express": "^4.18.2", "express": "^4.18.2",
"express-list-endpoints": "^6.0.0", "express-list-endpoints": "^6.0.0",
"jsonwebtoken": "^9.0.2", "jsonwebtoken": "^9.0.2",

41
src/app.ts Normal file
View File

@ -0,0 +1,41 @@
import express from "express";
import listEndpoints from "express-list-endpoints";
import imageController from "./controllers/ImageController";
import authControler from "./controllers/AuthControler";
import mongoose from "mongoose";
export const app = express();
app.use(express.json());
app.get("/", (_, res) => {
const endpoints = listEndpoints(app);
res.json({ endpoints });
});
app.get("/images", imageController.getAllImages);
app.post("/images", authControler.authorize, imageController.addImage);
app.post("/login", authControler.login);
export const startApp = async () => {
const port = process.env.PORT || 8080;
const mongo_uri: string = process.env.MONGODB_URI || "";
const mongo_user = process.env.MONGODB_USER;
const mongo_pass = process.env.MONGODB_PASS;
try {
await mongoose.connect(mongo_uri, {
authSource: "admin",
user: mongo_user,
pass: mongo_pass,
});
app.listen(port, () =>
console.log(`Express server listening on port ${port}`)
);
} catch (error) {
console.error(error);
process.exit(1);
}
};
export default app;

View File

@ -13,7 +13,7 @@ class AuthControler {
const authenticated = await AuthService.find(app, secret); const authenticated = await AuthService.find(app, secret);
if (authenticated) { if (authenticated) {
console.log("Authenticated app ", authenticated.app); console.log("Authenticated app", authenticated.app);
// Generate an access token // Generate an access token
const accessToken = jwt.sign( const accessToken = jwt.sign(
{ app: authenticated.app }, { app: authenticated.app },

View File

@ -3,12 +3,11 @@ import imageService from "../services/ImageService";
import mongoose, { mongo } from "mongoose"; import mongoose, { mongo } from "mongoose";
class ImageController { class ImageController {
async getAllImages(req: Request, res: Response): Promise<void> { async getAllImages(_: Request, res: Response): Promise<void> {
try { try {
const images = await imageService.findAll(); const images = await imageService.findAll();
res.json({ images }); res.json({ images });
} catch (error) { } catch (error) {
console.error(error);
res.status(500).json({ error: "Internal Server Error" }); res.status(500).json({ error: "Internal Server Error" });
} }
} }
@ -23,11 +22,9 @@ class ImageController {
} catch (error: any) { } catch (error: any) {
if (error instanceof mongo.MongoServerError && error.code === 11000) { if (error instanceof mongo.MongoServerError && error.code === 11000) {
// Should return 409 Conflict for existing urls // Should return 409 Conflict for existing urls
res res.status(409).json({
.status(409) error: `the image with URL ${error.keyValue.url} already exists`,
.json({ });
error: `the image with URL ${error.keyValue.url} already exists`,
});
} else if (error instanceof mongoose.Error.ValidationError) { } else if (error instanceof mongoose.Error.ValidationError) {
// Should return 400 Bad request for invalid requests // Should return 400 Bad request for invalid requests
res.status(400).json({ error: error.message }); res.status(400).json({ error: error.message });

View File

@ -1,42 +1,8 @@
import express from "express"; import populateDatabase from "../tests/populateDatabase";
import mongoose from "mongoose"; import { startApp } from "./app";
import listEndpoints from "express-list-endpoints";
import imageController from "./controllers/ImageController";
import authControler from "./controllers/AuthControler";
export const app = express(); await startApp();
app.use(express.json()); try {
await populateDatabase();
app.get("/", (_, res) => { } catch {}
const endpoints = listEndpoints(app);
res.json({ endpoints });
});
app.get("/images", imageController.getAllImages);
app.post("/images", authControler.authorize, imageController.addImage);
app.post("/login", authControler.login);
const start = async () => {
// Set the default port to 8080, or use the PORT environment variable
const port = process.env.PORT || 8080;
const mongo_uri: string = process.env.MONGODB_URI || "";
const mongo_user = process.env.MONGODB_USER;
const mongo_pass = process.env.MONGODB_PASS;
try {
await mongoose.connect(mongo_uri, {
authSource: "admin",
user: mongo_user,
pass: mongo_pass,
});
app.listen(port, () =>
console.log(`Express server listening on port ${port}`)
);
} catch (error) {
console.error(error);
process.exit(1);
}
};
start();

View File

@ -5,15 +5,19 @@ export interface Auth extends Document {
secret: String; secret: String;
} }
const AuthSchema = new mongoose.Schema({ const AuthSchema = new mongoose.Schema(
app: { {
type: String, app: {
required: true, type: String,
required: true,
index: true,
},
secret: {
type: String,
required: true,
},
}, },
secret: { { collection: "authorizations" }
type: String, );
required: true,
},
});
export default mongoose.model("authorizations", AuthSchema); export default mongoose.model("authorizations", AuthSchema);

View File

@ -6,21 +6,27 @@ export interface Image extends Document {
tags?: String[]; tags?: String[];
} }
const ImageSchema = new mongoose.Schema({ const ImageSchema = new mongoose.Schema(
url: { {
type: String, url: {
required: true, type: String,
}, required: true,
status: { index: true,
type: String, unique: true,
enum: { },
values: ["consumed", "unavailable", "available"], status: {
type: String,
enum: {
values: ["consumed", "unavailable", "available"],
},
required: true,
index: true,
},
tags: {
type: [String],
}, },
required: true,
}, },
tags: { { collection: "images" }
type: [String], );
},
});
export default mongoose.model<Image>("images", ImageSchema); export default mongoose.model<Image>("images", ImageSchema);

View File

@ -2,7 +2,7 @@ import AuthModel, { Auth } from "../models/AuthModel";
class AuthService { class AuthService {
async find(app: String, secret: String): Promise<Auth | null> { async find(app: String, secret: String): Promise<Auth | null> {
const auth = await AuthModel.findOne({ app: app, secret: secret }).exec(); const auth = await AuthModel.findOne({ app: app, secret: secret });
return auth; return auth;
} }
} }

View File

@ -1,13 +1,29 @@
import { afterEach, beforeAll, describe, expect, it, mock } from "bun:test"; import {
import request from "supertest"; afterEach,
import { app } from "../src"; beforeAll,
describe,
expect,
it,
mock,
} from "bun:test";
import request, { Response } from "supertest";
import app, { startApp } from "../src/app";
import imageService from "../src/services/ImageService"; import imageService from "../src/services/ImageService";
import populateDatabase from "./populateDatabase";
const imageServiceOriginal = imageService; const imageServiceOriginal = imageService;
const tok = await request(app)
.post("/login") let token: string;
.send({ app: "tester", secret: "test" });
const token = tok.body.token; beforeAll(async () => {
await startApp();
await populateDatabase();
const tok = await request(app)
.post("/login")
.send({ app: "tester", secret: "test" });
token = tok.body.token;
});
afterEach(() => { afterEach(() => {
mock.restore(); mock.restore();
@ -16,6 +32,28 @@ afterEach(() => {
})); }));
}); });
describe("/login works as instended", async () => {
let correctRespose: Response;
beforeAll(async () => {
correctRespose = await request(app)
.post("/login")
.send({ app: "tester", secret: "test" });
});
it("should return 200 for correct login", async () => {
expect(correctRespose.status).toBe(200);
});
it("should contain a token", () => {
expect(correctRespose.body).toHaveProperty("token");
});
it("should return 403 for invalid credentials", async () => {
const res = await request(app).post("/login").send({});
expect(res.status).toBe(403);
});
});
describe("GET / shows all of the endpoints", async () => { describe("GET / shows all of the endpoints", async () => {
const res = await request(app).get("/"); const res = await request(app).get("/");
@ -38,6 +76,21 @@ describe("GET /images works properly", async () => {
it("should return a 200", async () => { it("should return a 200", async () => {
expect(res.statusCode).toBe(200); expect(res.statusCode).toBe(200);
}); });
it("should return 500 for an error on the service", async () => {
mock.module("../src/services/ImageService", () => ({
default: {
add: () => {
throw new Error("This is an expected testing error");
},
},
}));
const res = await request(app)
.get("/images");
expect(res.status).toBe(500);
});
}); });
describe("POST /images works properly", () => { describe("POST /images works properly", () => {

View File

@ -1,21 +0,0 @@
import { describe, expect, it, mock } from "bun:test";
import request from "supertest";
import { app } from "../src";
describe("/login", async () => {
const correctRespose = await request(app).post("/login").send({
app: "tester",
secret: "test",
});
it("should return 200 for correct login", () => {
expect(correctRespose.status).toBe(200);
});
it("should contain a token", () => {
expect(correctRespose.body).toHaveProperty("token");
});
it("should return 403 for invalid credentials", async () => {
const res = await request(app).post("/login").send({});
expect(res.status).toBe(403);
});
});

15
tests/populateDatabase.ts Normal file
View File

@ -0,0 +1,15 @@
import authModel from "../src/models/AuthModel";
import imageModel from "../src/models/ImageModel";
export default async function () {
await imageModel.create({
url: "https://example.com",
status: "consumed",
tags: ["2girls", "sleeping"],
});
await authModel.create({
app: "tester",
secret: "test",
});
}