Compare commits

..

No commits in common. "9e064fc56e5c2988a91d2436622dd953477db08e" and "c6e194789906f938067fd58adb19713c58ae5579" have entirely different histories.

9 changed files with 81 additions and 239 deletions

View File

@ -27,7 +27,6 @@ services:
MONGODB_URI: "mongodb://mongodb:27017/bot" MONGODB_URI: "mongodb://mongodb:27017/bot"
MONGODB_USER: "root" MONGODB_USER: "root"
MONGODB_PASS: "password" MONGODB_PASS: "password"
JWTSECRET: "cooljwtsecret"
volumes: volumes:
- ./:/usr/src/app:ro - ./:/usr/src/app:ro

View File

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

View File

@ -22,11 +22,9 @@
"dependencies": { "dependencies": {
"@types/express": "^4.17.21", "@types/express": "^4.17.21",
"@types/express-list-endpoints": "^6.0.3", "@types/express-list-endpoints": "^6.0.3",
"@types/jsonwebtoken": "^9.0.5",
"@types/mongoose": "^5.11.97", "@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",
"mongoose": "^8.0.3" "mongoose": "^8.0.3"
} }
} }

View File

@ -1,49 +0,0 @@
import jwt from "jsonwebtoken";
import AuthService from "../services/AuthService";
import { Request, Response, NextFunction } from "express";
const authTokenSecret = process.env.JWTSECRET || "badsecret";
class AuthControler {
async login(req: Request, res: Response) {
// Read app and secret from request body
const { app, secret } = req.body;
// Filter app from the apps by app and secret
const authenticated = await AuthService.find(app, secret);
if (authenticated) {
console.log("Authenticated app ", authenticated.app);
// Generate an access token
const accessToken = jwt.sign(
{ app: authenticated.app },
authTokenSecret,
{ expiresIn: "1h" }
);
res.json({
token: accessToken,
});
} else {
res.status(403).send("Credentials incorrect");
}
}
authorize(req: Request, res: Response, next: NextFunction) {
const authHeader = req.headers.authorization;
if (authHeader) {
const token = authHeader.split(" ")[1];
jwt.verify(token, authTokenSecret, (err, app) => {
if (err) {
return res.status(403).json("Invalid token provided");
}
next();
});
} else {
res.status(401).json("No Authorization header provided");
}
}
}
export default new AuthControler();

View File

@ -2,7 +2,6 @@ import express from "express";
import mongoose from "mongoose"; import mongoose from "mongoose";
import listEndpoints from "express-list-endpoints"; import listEndpoints from "express-list-endpoints";
import imageController from "./controllers/ImageController"; import imageController from "./controllers/ImageController";
import authControler from "./controllers/AuthControler";
export const app = express(); export const app = express();
@ -14,8 +13,7 @@ app.get("/", (_, res) => {
}); });
app.get("/images", imageController.getAllImages); app.get("/images", imageController.getAllImages);
app.post("/images", authControler.authorize, imageController.addImage); app.post("/images", imageController.addImage);
app.post("/login", authControler.login)
// Set the default port to 8080, or use the PORT environment variable // Set the default port to 8080, or use the PORT environment variable

View File

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

View File

@ -1,10 +0,0 @@
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();
return auth;
}
}
export default new AuthService();

View File

@ -1,135 +1,88 @@
import { afterEach, beforeAll, describe, expect, it, mock } from "bun:test"; import { afterEach, describe, expect, it, mock } from "bun:test";
import request from "supertest"; import request from "supertest";
import { app } from "../src"; import { app } from "../src";
import imageService from "../src/services/ImageService"; import imageService from "../src/services/ImageService";
const imageServiceOriginal = imageService; const imageServiceOriginal = imageService;
const tok = await request(app)
.post("/login")
.send({ app: "tester", secret: "test" });
const token = tok.body.token;
afterEach(() => { afterEach(() => {
mock.restore(); mock.restore();
mock.module("../src/services/ImageService", () => ({ mock.module("../src/services/ImageService", () => ({ default: imageServiceOriginal }));
default: imageServiceOriginal, })
}));
});
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("/");
it("should be", async () => { it("should be", async () => {
expect(res.body).toHaveProperty("endpoints"); expect(res.body).toHaveProperty("endpoints");
}); });
it("should be an array", () => { it("should be an array", () => {
expect(Array.isArray(res.body.endpoints)).toBeTrue(); expect(Array.isArray(res.body.endpoints)).toBeTrue();
}); })
}); })
describe("GET /images works properly", async () => { describe("GET /images works properly", async () => {
const res = await request(app).get("/images"); const res = await request(app).get("/images");
it("should be an array", () => { it("should be an array", () => {
expect(Array.isArray(res.body.images)).toBeTrue(); expect(Array.isArray(res.body.images)).toBeTrue();
}); });
it("should return a 200", async () => { it("should return a 200", async () => {
expect(res.statusCode).toBe(200); expect(res.statusCode).toBe(200);
}); });
}); });
describe("POST /images works properly", () => { describe("POST /images works properly", () => {
it("should return 201 for new image", async () => {
it("should return 401 for unauthenticated requests", async () => { const res = await request(app).post("/images").send({
const res = await request(app) url: "https://test.url.com/1",
.post("/images")
.send({
url: "https://test.url.com/0",
status: "available", status: "available",
tags: ["2girls", "touhou"], tags: ["2girls", "touhou"]
}); });
expect(res.status).toBe(401); expect(res.status).toSatisfy(status => [201].includes(status));
}); });
it("should return 403 for invalid tokens", async () => { it("should return 409 for a repeated images", async () => {
const res = await request(app) await request(app).post("/images").send({
.post("/images") url: "https://test.url.com/2",
.set("authorization", `Bearer token`)
.send({
url: "https://test.url.com/0",
status: "available", status: "available",
tags: ["2girls", "touhou"], tags: ["2girls", "touhou"]
}); });
expect(res.status).toBe(403);
});
it("should return 201 for new image", async () => { const res = await request(app).post("/images").send({
const res = await request(app) url: "https://test.url.com/2",
.post("/images") status: "available",
.set("authorization", `Bearer ${token}`) tags: ["2girls", "touhou"]
.send({ });
url: "https://test.url.com/1",
status: "available",
tags: ["2girls", "touhou"],
});
expect(res.status).toBe(201);
});
it("should return 409 for a repeated images", async () => { expect(res.status).toBe(409);
await request(app) });
.post("/images")
.set("authorization", `Bearer ${token}`)
.send({
url: "https://test.url.com/2",
status: "available",
tags: ["2girls", "touhou"],
});
const res = await request(app) it("should return 500 for an error on the service", async () => {
.post("/images") mock.module("../src/services/ImageService", () => ({
.set("authorization", `Bearer ${token}`) default: {
.send({ add: () => { throw new Error("This is an expected testing error"); }
url: "https://test.url.com/2", }
status: "available", }));
tags: ["2girls", "touhou"],
});
expect(res.status).toBe(409); const res = await request(app).post("/images").send({
}); url: "https://test.url.com/3",
status: "available",
tags: ["2girls", "touhou"]
});
expect(res.status).toBe(500);
});
it("should return 500 for an error on the service", async () => { it("should return 400 for malformed requests", async () => {
mock.module("../src/services/ImageService", () => ({ mock.restore();
default: { const res = await request(app).post("/images").send({
add: () => { url: "https://test.url.com/4",
throw new Error("This is an expected testing error"); status: "wrong",
}, tags: ["2girls", "touhou"]
}, });
})); expect(res.status).toBe(400);
});
const res = await request(app) });
.post("/images")
.set("authorization", `Bearer ${token}`)
.send({
url: "https://test.url.com/3",
status: "available",
tags: ["2girls", "touhou"],
});
expect(res.status).toBe(500);
});
it("should return 400 for malformed requests", async () => {
mock.restore();
const res = await request(app)
.post("/images")
.set("authorization", `Bearer ${token}`)
.send({
url: "https://test.url.com/4",
status: "wrong",
tags: ["2girls", "touhou"],
});
expect(res.status).toBe(400);
});
});

View File

@ -1,22 +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);
});
});