added a test for /
This commit is contained in:
parent
f483f62c42
commit
c5e41d6f62
19
src/index.ts
19
src/index.ts
|
@ -10,14 +10,14 @@ app.get("/", (_, res) => {
|
|||
res.json({ message: "Blazing fast 🚀" });
|
||||
});
|
||||
|
||||
app.get("/images", async (req, res) => {
|
||||
app.get("/images", async (_, res) => {
|
||||
try {
|
||||
const allImages = await ImageModel.find();
|
||||
res.json(allImages);
|
||||
res.json({ message: allImages });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error });
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
app.post("/images", async (req, res) => {
|
||||
try {
|
||||
|
@ -25,7 +25,7 @@ app.post("/images", async (req, res) => {
|
|||
// 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);
|
||||
res.status(201).json({ message: image });
|
||||
} catch (error: any) {
|
||||
if (error.code == 11000) {
|
||||
// Should return 409 Conflict for existing urls
|
||||
|
@ -34,7 +34,7 @@ app.post("/images", async (req, res) => {
|
|||
// 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
|
||||
|
||||
|
@ -48,14 +48,15 @@ const start = async () => {
|
|||
await mongoose.connect(mongo_uri, {
|
||||
authSource: "admin",
|
||||
user: mongo_user,
|
||||
pass: mongo_pass
|
||||
pass: mongo_pass,
|
||||
});
|
||||
app.listen(port, () => console.log(`Express server listening on port ${port}`));
|
||||
app.listen(port, () =>
|
||||
console.log(`Express server listening on port ${port}`)
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
start();
|
||||
|
||||
|
|
|
@ -1,12 +1,20 @@
|
|||
import { beforeAll, describe, expect, it } from "bun:test";
|
||||
import { describe, expect, it, mock } from "bun:test";
|
||||
import request from "supertest";
|
||||
import { app } from "../src";
|
||||
|
||||
describe("GET / shows what it should",async () => {
|
||||
const res = await request(app).get("/");
|
||||
|
||||
it("should be", async () => {
|
||||
expect(res.body).toHaveProperty("message", "Blazing fast 🚀");
|
||||
});
|
||||
})
|
||||
|
||||
describe("GET /images works properly", async () => {
|
||||
const res = await request(app).get("/images");
|
||||
|
||||
it("should be an array", async () => {
|
||||
expect(Array.isArray(res.body)).toBeTrue();
|
||||
expect(Array.isArray(res.body.message)).toBeTrue();
|
||||
});
|
||||
|
||||
it("should return a 200", async () => {
|
||||
|
|
Loading…
Reference in New Issue