Merge pull request 'post-images-endpoint' (#2) from post-images-endpoint into main
Reviewed-on: #2
This commit is contained in:
commit
215469eae7
|
@ -9,7 +9,13 @@ bun install
|
|||
To run:
|
||||
|
||||
```bash
|
||||
bun run index.ts
|
||||
docker compose up
|
||||
```
|
||||
|
||||
For testing, remember:
|
||||
```bash
|
||||
docker compose down -v
|
||||
docker compose run bot-api bun run test
|
||||
```
|
||||
|
||||
This project was created using `bun init` in bun v1.0.13. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
|
||||
|
|
|
@ -16,7 +16,7 @@ db.createUser({
|
|||
db = new Mongo().getDB("bot");
|
||||
|
||||
db.images.createIndex({ "status": 1 });
|
||||
db.images.createIndex({ "image": 1 }, { "unique": true });
|
||||
db.images.createIndex({ "url": 1 }, { "unique": true });
|
||||
db.images.insert({
|
||||
url: "https://example.com",
|
||||
status: "consumed",
|
||||
|
|
18
src/index.ts
18
src/index.ts
|
@ -6,7 +6,6 @@ export const app = express();
|
|||
|
||||
app.use(express.json());
|
||||
|
||||
// Hello World GET endpoint
|
||||
app.get("/", (_, res) => {
|
||||
res.json({ message: "Blazing fast 🚀" });
|
||||
});
|
||||
|
@ -20,6 +19,23 @@ app.get("/images", async (req, res) => {
|
|||
}
|
||||
})
|
||||
|
||||
app.post("/images", async (req, res) => {
|
||||
try {
|
||||
// Should add auth here before doing stuff
|
||||
// Thowing a 401 if not auth provided
|
||||
// Throwing a 403 for incorrect auth
|
||||
const image = await ImageModel.create(req.body);
|
||||
res.status(201).json(image);
|
||||
} catch (error: any) {
|
||||
if (error.code == 11000){
|
||||
// Should return 409 Conflict for existing urls
|
||||
res.status(409).json({ message: "Existing URL" });
|
||||
}
|
||||
// Should return 400 Bad request for invalid requests
|
||||
res.status(400).json({ message: error });
|
||||
}
|
||||
})
|
||||
|
||||
// Set the default port to 8080, or use the PORT environment variable
|
||||
|
||||
const start = async () => {
|
||||
|
|
|
@ -1,22 +1,51 @@
|
|||
import { describe, expect, it } from "bun:test";
|
||||
import { beforeAll, describe, expect, it } from "bun:test";
|
||||
import request from "supertest";
|
||||
import { app } from "../src";
|
||||
|
||||
describe("GET /images works properly", () => {
|
||||
describe("GET /images works properly", async () => {
|
||||
const res = await request(app).get("/images");
|
||||
|
||||
it("should be an array", async () => {
|
||||
const res = await request(app).get("/images");
|
||||
expect(Array.isArray(res.body)).toBeTrue();
|
||||
});
|
||||
|
||||
it("should return a 200", async () => {
|
||||
expect(res.statusCode).toBe(200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /images works properly", () => {
|
||||
it("should return 201 or 409", async () => {
|
||||
it("should return 201 for new image", async () => {
|
||||
const res = await request(app).post("/images").send({
|
||||
url: "https://test.url.com/123",
|
||||
url: "https://test.url.com/1",
|
||||
status: "available",
|
||||
tags: ["2girls", "touhou"]
|
||||
});
|
||||
expect(res.status).toSatisfy(status => [201, 409].includes(status));
|
||||
expect(res.status).toSatisfy(status => [201].includes(status));
|
||||
});
|
||||
|
||||
it("should return 409 for a repeated images", async () => {
|
||||
await request(app).post("/images").send({
|
||||
url: "https://test.url.com/2",
|
||||
status: "available",
|
||||
tags: ["2girls", "touhou"]
|
||||
});
|
||||
|
||||
const res = await request(app).post("/images").send({
|
||||
url: "https://test.url.com/2",
|
||||
status: "available",
|
||||
tags: ["2girls", "touhou"]
|
||||
});
|
||||
|
||||
expect(res.status).toSatisfy(status => [409].includes(status));
|
||||
});
|
||||
|
||||
it("should return 400 for malformed requests", async () => {
|
||||
const res = await request(app).post("/images").send({
|
||||
url: "https://test.url.com/3",
|
||||
status: "wrong",
|
||||
tags: ["2girls", "touhou"]
|
||||
});
|
||||
expect(res.status).toSatisfy(status => [400].includes(status));
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue