fmt, purged useless deps and removed mongoMS
Unit Tests with docker compose / unit-test (pull_request) Failing after 7m21s
Details
Unit Tests with docker compose / unit-test (pull_request) Failing after 7m21s
Details
This commit is contained in:
parent
49b159f9dc
commit
9cd61c101c
|
@ -4,12 +4,10 @@
|
|||
"type": "module",
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.11",
|
||||
"@types/mongodb-memory-server": "^2.3.0",
|
||||
"@types/supertest": "^6.0.1",
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/express-list-endpoints": "^6.0.3",
|
||||
"@types/jsonwebtoken": "^9.0.5",
|
||||
"@types/mongoose": "^5.11.97",
|
||||
"bun-types": "latest",
|
||||
"jest": "^29.7.0",
|
||||
"mongodb-memory-server": "^9.1.3",
|
||||
|
@ -30,10 +28,5 @@
|
|||
"express-list-endpoints": "^6.0.0",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"mongoose": "^8.0.3"
|
||||
},
|
||||
"config": {
|
||||
"mongodbMemoryServer": {
|
||||
"debug": "1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
40
src/app.ts
40
src/app.ts
|
@ -18,24 +18,24 @@ app.post("/images", authControler.authorize, imageController.addImage);
|
|||
app.post("/login", authControler.login);
|
||||
|
||||
export const startApp = async () => {
|
||||
const port = process.env.PORT || 8080;
|
||||
const mongo_uri: string = process.env.MONGODB_URI || "";
|
||||
const mongo_user = process.env.MONGODB_USER;
|
||||
const mongo_pass = process.env.MONGODB_PASS;
|
||||
|
||||
try {
|
||||
await mongoose.connect(mongo_uri, {
|
||||
authSource: "admin",
|
||||
user: mongo_user,
|
||||
pass: mongo_pass,
|
||||
});
|
||||
app.listen(port, () =>
|
||||
console.log(`Express server listening on port ${port}`)
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
const port = process.env.PORT || 8080;
|
||||
const mongo_uri: string = process.env.MONGODB_URI || "";
|
||||
const mongo_user = process.env.MONGODB_USER;
|
||||
const mongo_pass = process.env.MONGODB_PASS;
|
||||
|
||||
export default app;
|
||||
try {
|
||||
await mongoose.connect(mongo_uri, {
|
||||
authSource: "admin",
|
||||
user: mongo_user,
|
||||
pass: mongo_pass,
|
||||
});
|
||||
app.listen(port, () =>
|
||||
console.log(`Express server listening on port ${port}`)
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
export default app;
|
||||
|
|
|
@ -23,11 +23,9 @@ class ImageController {
|
|||
} 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`,
|
||||
});
|
||||
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 });
|
||||
|
|
10
src/index.ts
10
src/index.ts
|
@ -3,10 +3,6 @@ import { startApp } from "./app";
|
|||
|
||||
await startApp();
|
||||
|
||||
|
||||
try{
|
||||
await populateDatabase();
|
||||
}
|
||||
catch {
|
||||
|
||||
}
|
||||
try {
|
||||
await populateDatabase();
|
||||
} catch {}
|
||||
|
|
|
@ -5,16 +5,19 @@ export interface Auth extends Document {
|
|||
secret: String;
|
||||
}
|
||||
|
||||
const AuthSchema = new mongoose.Schema({
|
||||
app: {
|
||||
type: String,
|
||||
required: true,
|
||||
index: true,
|
||||
const AuthSchema = new mongoose.Schema(
|
||||
{
|
||||
app: {
|
||||
type: String,
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
secret: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
secret: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
}, { collection: "authorizations" });
|
||||
{ collection: "authorizations" }
|
||||
);
|
||||
|
||||
export default mongoose.model("authorizations", AuthSchema);
|
||||
|
|
|
@ -6,24 +6,27 @@ export interface Image extends Document {
|
|||
tags?: String[];
|
||||
}
|
||||
|
||||
const ImageSchema = new mongoose.Schema({
|
||||
url: {
|
||||
type: String,
|
||||
required: true,
|
||||
index: true,
|
||||
unique: true
|
||||
},
|
||||
status: {
|
||||
type: String,
|
||||
enum: {
|
||||
values: ["consumed", "unavailable", "available"],
|
||||
const ImageSchema = new mongoose.Schema(
|
||||
{
|
||||
url: {
|
||||
type: String,
|
||||
required: true,
|
||||
index: true,
|
||||
unique: true,
|
||||
},
|
||||
status: {
|
||||
type: String,
|
||||
enum: {
|
||||
values: ["consumed", "unavailable", "available"],
|
||||
},
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
tags: {
|
||||
type: [String],
|
||||
},
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
tags: {
|
||||
type: [String],
|
||||
},
|
||||
}, { collection: "images" });
|
||||
{ collection: "images" }
|
||||
);
|
||||
|
||||
export default mongoose.model<Image>("images", ImageSchema);
|
||||
|
|
|
@ -1,4 +1,12 @@
|
|||
import { afterAll, afterEach, beforeAll, describe, expect, it, mock } from "bun:test";
|
||||
import {
|
||||
afterAll,
|
||||
afterEach,
|
||||
beforeAll,
|
||||
describe,
|
||||
expect,
|
||||
it,
|
||||
mock,
|
||||
} from "bun:test";
|
||||
import request, { Response } from "supertest";
|
||||
import app, { startApp } from "../src/app";
|
||||
import imageService from "../src/services/ImageService";
|
||||
|
@ -10,164 +18,162 @@ const imageServiceOriginal = imageService;
|
|||
let token: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
if (!process.env.DEDICATED_MONGODB_SERVER)
|
||||
await memoryServer.start();
|
||||
await startApp();
|
||||
await populateDatabase();
|
||||
//if (!process.env.DEDICATED_MONGODB_SERVER) await memoryServer.start();
|
||||
await startApp();
|
||||
await populateDatabase();
|
||||
|
||||
const tok = await request(app)
|
||||
.post("/login")
|
||||
.send({ app: "tester", secret: "test" });
|
||||
token = tok.body.token;
|
||||
const tok = await request(app)
|
||||
.post("/login")
|
||||
.send({ app: "tester", secret: "test" });
|
||||
token = tok.body.token;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
if (!process.env.DEDICATED_MONGODB_SERVER)
|
||||
await memoryServer.stop();
|
||||
})
|
||||
|
||||
/* afterAll(async () => {
|
||||
if (!process.env.DEDICATED_MONGODB_SERVER) await memoryServer.stop();
|
||||
});
|
||||
*/
|
||||
afterEach(() => {
|
||||
mock.restore();
|
||||
mock.module("../src/services/ImageService", () => ({
|
||||
default: imageServiceOriginal,
|
||||
}));
|
||||
mock.restore();
|
||||
mock.module("../src/services/ImageService", () => ({
|
||||
default: imageServiceOriginal,
|
||||
}));
|
||||
});
|
||||
|
||||
describe("/login works as instended", async () => {
|
||||
let correctRespose: Response;
|
||||
beforeAll(async () => {
|
||||
correctRespose = await request(app)
|
||||
.post("/login")
|
||||
.send({ app: "tester", secret: "test" });
|
||||
});
|
||||
|
||||
it("should return 200 for correct login", async () => {
|
||||
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);
|
||||
});
|
||||
});
|
||||
let correctRespose: Response;
|
||||
beforeAll(async () => {
|
||||
correctRespose = await request(app)
|
||||
.post("/login")
|
||||
.send({ app: "tester", secret: "test" });
|
||||
});
|
||||
|
||||
it("should return 200 for correct login", async () => {
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET / shows all of the endpoints", async () => {
|
||||
const res = await request(app).get("/");
|
||||
const res = await request(app).get("/");
|
||||
|
||||
it("should be", async () => {
|
||||
expect(res.body).toHaveProperty("endpoints");
|
||||
});
|
||||
it("should be", async () => {
|
||||
expect(res.body).toHaveProperty("endpoints");
|
||||
});
|
||||
|
||||
it("should be an array", () => {
|
||||
expect(Array.isArray(res.body.endpoints)).toBeTrue();
|
||||
});
|
||||
it("should be an array", () => {
|
||||
expect(Array.isArray(res.body.endpoints)).toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
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", () => {
|
||||
expect(Array.isArray(res.body.images)).toBeTrue();
|
||||
});
|
||||
it("should be an array", () => {
|
||||
expect(Array.isArray(res.body.images)).toBeTrue();
|
||||
});
|
||||
|
||||
it("should return a 200", async () => {
|
||||
expect(res.statusCode).toBe(200);
|
||||
});
|
||||
it("should return a 200", async () => {
|
||||
expect(res.statusCode).toBe(200);
|
||||
});
|
||||
});
|
||||
|
||||
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)
|
||||
.post("/images")
|
||||
.set("authorization", `Bearer ${token}`)
|
||||
.send({
|
||||
url: "https://test.url.com/1",
|
||||
status: "available",
|
||||
tags: ["2girls", "touhou"],
|
||||
});
|
||||
expect(res.status).toBe(201);
|
||||
});
|
||||
it("should return 201 for new image", async () => {
|
||||
const res = await request(app)
|
||||
.post("/images")
|
||||
.set("authorization", `Bearer ${token}`)
|
||||
.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 () => {
|
||||
await request(app)
|
||||
.post("/images")
|
||||
.set("authorization", `Bearer ${token}`)
|
||||
.send({
|
||||
url: "https://test.url.com/2",
|
||||
status: "available",
|
||||
tags: ["2girls", "touhou"],
|
||||
});
|
||||
it("should return 409 for a repeated images", async () => {
|
||||
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)
|
||||
.post("/images")
|
||||
.set("authorization", `Bearer ${token}`)
|
||||
.send({
|
||||
url: "https://test.url.com/2",
|
||||
status: "available",
|
||||
tags: ["2girls", "touhou"],
|
||||
});
|
||||
const res = await request(app)
|
||||
.post("/images")
|
||||
.set("authorization", `Bearer ${token}`)
|
||||
.send({
|
||||
url: "https://test.url.com/2",
|
||||
status: "available",
|
||||
tags: ["2girls", "touhou"],
|
||||
});
|
||||
|
||||
expect(res.status).toBe(409);
|
||||
});
|
||||
expect(res.status).toBe(409);
|
||||
});
|
||||
|
||||
it("should return 500 for an error on the service", async () => {
|
||||
mock.module("../src/services/ImageService", () => ({
|
||||
default: {
|
||||
add: () => {
|
||||
throw new Error("This is an expected testing error");
|
||||
},
|
||||
},
|
||||
}));
|
||||
it("should return 500 for an error on the service", async () => {
|
||||
mock.module("../src/services/ImageService", () => ({
|
||||
default: {
|
||||
add: () => {
|
||||
throw new Error("This is an expected testing error");
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
const res = await request(app)
|
||||
.post("/images")
|
||||
.set("authorization", `Bearer ${token}`)
|
||||
.send({
|
||||
url: "https://test.url.com/3",
|
||||
status: "available",
|
||||
tags: ["2girls", "touhou"],
|
||||
});
|
||||
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);
|
||||
});
|
||||
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);
|
||||
});
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -16,4 +16,4 @@ class MemoryServer {
|
|||
}
|
||||
}
|
||||
|
||||
export default new MemoryServer();
|
||||
export default new MemoryServer();
|
||||
|
|
|
@ -2,14 +2,14 @@ import authModel from "../src/models/AuthModel";
|
|||
import imageModel from "../src/models/ImageModel";
|
||||
|
||||
export default async function () {
|
||||
await imageModel.create({
|
||||
url: "https://example.com",
|
||||
status: "consumed",
|
||||
tags: ["2girls", "sleeping"],
|
||||
});
|
||||
await imageModel.create({
|
||||
url: "https://example.com",
|
||||
status: "consumed",
|
||||
tags: ["2girls", "sleeping"],
|
||||
});
|
||||
|
||||
await authModel.create({
|
||||
app: "tester",
|
||||
secret: "test",
|
||||
});
|
||||
await authModel.create({
|
||||
app: "tester",
|
||||
secret: "test",
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue