added a test for /
This commit is contained in:
parent
f483f62c42
commit
c5e41d6f62
81
src/index.ts
81
src/index.ts
|
@ -7,55 +7,56 @@ export const app = express();
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
app.get("/", (_, res) => {
|
app.get("/", (_, res) => {
|
||||||
res.json({ message: "Blazing fast 🚀" });
|
res.json({ message: "Blazing fast 🚀" });
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/images", async (req, res) => {
|
app.get("/images", async (_, res) => {
|
||||||
try {
|
try {
|
||||||
const allImages = await ImageModel.find();
|
const allImages = await ImageModel.find();
|
||||||
res.json(allImages);
|
res.json({ message: allImages });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ message: error });
|
res.status(500).json({ message: error });
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
app.post("/images", async (req, res) => {
|
app.post("/images", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
// Should add auth here before doing stuff
|
// Should add auth here before doing stuff
|
||||||
// Thowing a 401 if not auth provided
|
// Thowing a 401 if not auth provided
|
||||||
// Throwing a 403 for incorrect auth
|
// Throwing a 403 for incorrect auth
|
||||||
const image = await ImageModel.create(req.body);
|
const image = await ImageModel.create(req.body);
|
||||||
res.status(201).json(image);
|
res.status(201).json({ message: image });
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error.code == 11000){
|
if (error.code == 11000) {
|
||||||
// Should return 409 Conflict for existing urls
|
// Should return 409 Conflict for existing urls
|
||||||
res.status(409).json({ message: "Existing URL" });
|
res.status(409).json({ message: "Existing URL" });
|
||||||
}
|
|
||||||
// Should return 400 Bad request for invalid requests
|
|
||||||
res.status(400).json({ message: error });
|
|
||||||
}
|
}
|
||||||
})
|
// 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
|
// Set the default port to 8080, or use the PORT environment variable
|
||||||
|
|
||||||
const start = async () => {
|
const start = async () => {
|
||||||
const port = process.env.PORT || 8080;
|
const port = process.env.PORT || 8080;
|
||||||
const mongo_uri: string = process.env.MONGODB_URI || "";
|
const mongo_uri: string = process.env.MONGODB_URI || "";
|
||||||
const mongo_user = process.env.MONGODB_USER;
|
const mongo_user = process.env.MONGODB_USER;
|
||||||
const mongo_pass = process.env.MONGODB_PASS;
|
const mongo_pass = process.env.MONGODB_PASS;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await mongoose.connect(mongo_uri, {
|
await mongoose.connect(mongo_uri, {
|
||||||
authSource: "admin",
|
authSource: "admin",
|
||||||
user: mongo_user,
|
user: mongo_user,
|
||||||
pass: mongo_pass
|
pass: mongo_pass,
|
||||||
});
|
});
|
||||||
app.listen(port, () => console.log(`Express server listening on port ${port}`));
|
app.listen(port, () =>
|
||||||
} catch (error) {
|
console.log(`Express server listening on port ${port}`)
|
||||||
console.error(error);
|
);
|
||||||
process.exit(1);
|
} catch (error) {
|
||||||
}
|
console.error(error);
|
||||||
}
|
process.exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
start();
|
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 request from "supertest";
|
||||||
import { app } from "../src";
|
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 () => {
|
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", async () => {
|
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 () => {
|
it("should return a 200", async () => {
|
||||||
|
|
Loading…
Reference in New Issue