v1.0.0 #28

Merged
bizcochito merged 147 commits from develop into main 2024-01-14 19:49:34 +00:00
10 changed files with 85 additions and 84 deletions
Showing only changes of commit 0d2e8dfe36 - Show all commits

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
node_modules/
bun.lockb
bun.lockb

View File

@ -1,4 +1,4 @@
version: '3'
version: "3"
services:
mongodb:

View File

@ -29,4 +29,4 @@
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.0.3"
}
}
}

View File

@ -3,36 +3,40 @@ import imageService from "../services/ImageService";
import mongoose, { mongo } from "mongoose";
class ImageController {
async getAllImages(req: 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" });
}
async getAllImages(req: 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" });
}
}
async addImage(req: Request, res: Response): Promise<void> {
try {
// Should add auth here before doing stuff
// Thowing a 401 if not auth provided
// Throwing a 403 for incorrect auth
const image = await imageService.add(req.body);
res.status(201).json({ image });
} 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` });
} else if (error instanceof mongoose.Error.ValidationError) {
// Should return 400 Bad request for invalid requests
res.status(400).json({ error: error.message });
} else {
// Return 500 in other case
res.status(500).json({ error: error });
}
}
async addImage(req: Request, res: Response): Promise<void> {
try {
// Should add auth here before doing stuff
// Thowing a 401 if not auth provided
// Throwing a 403 for incorrect auth
const image = await imageService.add(req.body);
res.status(201).json({ image });
} 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`,
});
} else if (error instanceof mongoose.Error.ValidationError) {
// Should return 400 Bad request for invalid requests
res.status(400).json({ error: error.message });
} else {
// Return 500 in other case
res.status(500).json({ error: error });
}
}
}
}
export default new ImageController();
export default new ImageController();

View File

@ -9,14 +9,13 @@ export const app = express();
app.use(express.json());
app.get("/", (_, res) => {
const endpoints = listEndpoints(app);
res.json({ endpoints });
const endpoints = listEndpoints(app);
res.json({ endpoints });
});
app.get("/images", imageController.getAllImages);
app.post("/images", authControler.authorize, imageController.addImage);
app.post("/login", authControler.login)
app.post("/login", authControler.login);
const start = async () => {
// Set the default port to 8080, or use the PORT environment variable

View File

@ -1,8 +1,8 @@
import mongoose, { Document } from "mongoose";
export interface Auth extends Document {
app: String,
secret: String
app: String;
secret: String;
}
const AuthSchema = new mongoose.Schema({

View File

@ -1,26 +1,26 @@
import mongoose, { Document } from "mongoose";
export interface Image extends Document {
url: String;
enum: "consumed" | "unavailable" | "available";
tags?: String[];
url: String;
enum: "consumed" | "unavailable" | "available";
tags?: String[];
}
const ImageSchema = new mongoose.Schema({
url: {
type: String,
required: true
url: {
type: String,
required: true,
},
status: {
type: String,
enum: {
values: ["consumed", "unavailable", "available"],
},
status: {
type: String,
enum: {
values: ["consumed", "unavailable", "available"],
},
required: true
},
tags: {
type: [String]
}
required: true,
},
tags: {
type: [String],
},
});
export default mongoose.model<Image>('images', ImageSchema);
export default mongoose.model<Image>("images", ImageSchema);

View File

@ -1,14 +1,14 @@
import imageModel, { Image } from "../models/ImageModel";
class ImageService {
async findAll(): Promise<Image[]> {
const allImages = await imageModel.find();
return allImages;
}
async add(image: Image): Promise<Image> {
const newImage = await imageModel.create(image);
return newImage;
}
async findAll(): Promise<Image[]> {
const allImages = await imageModel.find();
return allImages;
}
async add(image: Image): Promise<Image> {
const newImage = await imageModel.create(image);
return newImage;
}
}
export default new ImageService();
export default new ImageService();

View File

@ -41,29 +41,28 @@ describe("GET /images works properly", async () => {
});
describe("POST /images works properly", () => {
it("should return 401 for unauthenticated requests", async () => {
const res = await request(app)
.post("/images")
.send({
url: "https://test.url.com/0",
status: "available",
tags: ["2girls", "touhou"],
});
expect(res.status).toBe(401);
it("should return 401 for unauthenticated requests", async () => {
const res = await request(app)
.post("/images")
.send({
url: "https://test.url.com/0",
status: "available",
tags: ["2girls", "touhou"],
});
expect(res.status).toBe(401);
});
it("should return 403 for invalid tokens", async () => {
const res = await request(app)
.post("/images")
.set("authorization", `Bearer token`)
.send({
url: "https://test.url.com/0",
status: "available",
tags: ["2girls", "touhou"],
});
expect(res.status).toBe(403);
it("should return 403 for invalid tokens", async () => {
const res = await request(app)
.post("/images")
.set("authorization", `Bearer token`)
.send({
url: "https://test.url.com/0",
status: "available",
tags: ["2girls", "touhou"],
});
expect(res.status).toBe(403);
});
it("should return 201 for new image", async () => {
const res = await request(app)

View File

@ -19,4 +19,3 @@ describe("/login", async () => {
expect(res.status).toBe(403);
});
});