v1.0.0 #28
|
@ -1,19 +1,19 @@
|
|||
name: Gitea Actions Demo
|
||||
name: Unit Tests with docker compose
|
||||
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
|
||||
on: [pull_request]
|
||||
|
||||
jobs:
|
||||
Explore-Gitea-Actions:
|
||||
unit-test:
|
||||
container:
|
||||
image: oven/bun:1
|
||||
image: docker:dind
|
||||
steps:
|
||||
- run: echo "🎉 The job was automatically triggered by a ${{ gitea.event_name }} event."
|
||||
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!"
|
||||
- run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}."
|
||||
- name: Starting docker daemon
|
||||
run: docker-init -- dockerd --host=unix:///var/run/docker.sock &
|
||||
- name: Installing necessary packages
|
||||
run: apk add npm git curl bash
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v3
|
||||
- run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner."
|
||||
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
|
||||
- name: List files in the repository
|
||||
run: |
|
||||
ls ${{ gitea.workspace }}
|
||||
- name: Install project dependencies
|
||||
run: npm install
|
||||
- name: Run docker-compose
|
||||
run: docker compose down -v && docker compose run bot-api bun test
|
|
@ -2,7 +2,7 @@ version: "3"
|
|||
|
||||
services:
|
||||
mongodb:
|
||||
image: mongo
|
||||
image: mongo:bionic
|
||||
container_name: mongodb
|
||||
ports:
|
||||
- "27017:27017"
|
||||
|
@ -12,7 +12,6 @@ services:
|
|||
MONGO_INITDB_DATABASE: bot
|
||||
volumes:
|
||||
- mongodb_data:/data/db
|
||||
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
|
||||
|
||||
bot-api:
|
||||
image: oven/bun:1
|
||||
|
|
|
@ -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",
|
||||
});
|
|
@ -3,7 +3,10 @@
|
|||
"module": "index.ts",
|
||||
"type": "module",
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/express-list-endpoints": "^6.0.3",
|
||||
"@types/jest": "^29.5.11",
|
||||
"@types/jsonwebtoken": "^9.0.5",
|
||||
"@types/supertest": "^6.0.1",
|
||||
"bun-types": "latest",
|
||||
"jest": "^29.7.0",
|
||||
|
@ -20,10 +23,6 @@
|
|||
"test": "docker compose down -v && docker compose run bot-api bun test"
|
||||
},
|
||||
"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-list-endpoints": "^6.0.0",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
|
|
|
@ -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;
|
|
@ -13,7 +13,7 @@ class AuthControler {
|
|||
const authenticated = await AuthService.find(app, secret);
|
||||
|
||||
if (authenticated) {
|
||||
console.log("Authenticated app ", authenticated.app);
|
||||
console.log("Authenticated app", authenticated.app);
|
||||
// Generate an access token
|
||||
const accessToken = jwt.sign(
|
||||
{ app: authenticated.app },
|
||||
|
|
|
@ -3,12 +3,11 @@ import imageService from "../services/ImageService";
|
|||
import mongoose, { mongo } from "mongoose";
|
||||
|
||||
class ImageController {
|
||||
async getAllImages(req: Request, res: Response): Promise<void> {
|
||||
async getAllImages(_: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const images = await imageService.findAll();
|
||||
res.json({ images });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ error: "Internal Server Error" });
|
||||
}
|
||||
}
|
||||
|
@ -23,11 +22,9 @@ class ImageController {
|
|||
} catch (error: any) {
|
||||
if (error instanceof mongo.MongoServerError && error.code === 11000) {
|
||||
// Should return 409 Conflict for existing urls
|
||||
res
|
||||
.status(409)
|
||||
.json({
|
||||
error: `the image with URL ${error.keyValue.url} already exists`,
|
||||
});
|
||||
res.status(409).json({
|
||||
error: `the image with URL ${error.keyValue.url} already exists`,
|
||||
});
|
||||
} else if (error instanceof mongoose.Error.ValidationError) {
|
||||
// Should return 400 Bad request for invalid requests
|
||||
res.status(400).json({ error: error.message });
|
||||
|
|
46
src/index.ts
46
src/index.ts
|
@ -1,42 +1,8 @@
|
|||
import express from "express";
|
||||
import mongoose from "mongoose";
|
||||
import listEndpoints from "express-list-endpoints";
|
||||
import imageController from "./controllers/ImageController";
|
||||
import authControler from "./controllers/AuthControler";
|
||||
import populateDatabase from "../tests/populateDatabase";
|
||||
import { startApp } from "./app";
|
||||
|
||||
export const app = express();
|
||||
await startApp();
|
||||
|
||||
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);
|
||||
|
||||
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();
|
||||
try {
|
||||
await populateDatabase();
|
||||
} catch {}
|
||||
|
|
|
@ -5,15 +5,19 @@ export interface Auth extends Document {
|
|||
secret: String;
|
||||
}
|
||||
|
||||
const AuthSchema = new mongoose.Schema({
|
||||
app: {
|
||||
type: String,
|
||||
required: true,
|
||||
const AuthSchema = new mongoose.Schema(
|
||||
{
|
||||
app: {
|
||||
type: String,
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
secret: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
secret: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
});
|
||||
{ collection: "authorizations" }
|
||||
);
|
||||
|
||||
export default mongoose.model("authorizations", AuthSchema);
|
||||
|
|
|
@ -6,21 +6,27 @@ export interface Image extends Document {
|
|||
tags?: String[];
|
||||
}
|
||||
|
||||
const ImageSchema = new mongoose.Schema({
|
||||
url: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
status: {
|
||||
type: String,
|
||||
enum: {
|
||||
values: ["consumed", "unavailable", "available"],
|
||||
const ImageSchema = new mongoose.Schema(
|
||||
{
|
||||
url: {
|
||||
type: String,
|
||||
required: true,
|
||||
index: true,
|
||||
unique: true,
|
||||
},
|
||||
status: {
|
||||
type: String,
|
||||
enum: {
|
||||
values: ["consumed", "unavailable", "available"],
|
||||
},
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
tags: {
|
||||
type: [String],
|
||||
},
|
||||
required: true,
|
||||
},
|
||||
tags: {
|
||||
type: [String],
|
||||
},
|
||||
});
|
||||
{ collection: "images" }
|
||||
);
|
||||
|
||||
export default mongoose.model<Image>("images", ImageSchema);
|
||||
|
|
|
@ -2,7 +2,7 @@ import AuthModel, { Auth } from "../models/AuthModel";
|
|||
|
||||
class AuthService {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,29 @@
|
|||
import { afterEach, beforeAll, describe, expect, it, mock } from "bun:test";
|
||||
import request from "supertest";
|
||||
import { app } from "../src";
|
||||
import {
|
||||
afterEach,
|
||||
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 populateDatabase from "./populateDatabase";
|
||||
|
||||
const imageServiceOriginal = imageService;
|
||||
const tok = await request(app)
|
||||
.post("/login")
|
||||
.send({ app: "tester", secret: "test" });
|
||||
const token = tok.body.token;
|
||||
|
||||
let token: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
await startApp();
|
||||
await populateDatabase();
|
||||
|
||||
const tok = await request(app)
|
||||
.post("/login")
|
||||
.send({ app: "tester", secret: "test" });
|
||||
token = tok.body.token;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
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 () => {
|
||||
const res = await request(app).get("/");
|
||||
|
||||
|
@ -38,6 +76,21 @@ describe("GET /images works properly", async () => {
|
|||
it("should return a 200", async () => {
|
||||
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", () => {
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
|
@ -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",
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue