You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

175 lines
6.5 KiB

import app from "../server.js";
import request from "supertest";
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NSwidXNlcm5hbWUiOiJ0ZXN0IiwiaWF0IjoxNzUxNzk2MjM2fQ.XmaVA_NQcpW7fxRtDWOinMyQXPaFixpp3ib_mzo6M6c"
const videoId = 14
const commentId = 1
describe("POST COMMENT", async function() {
it("Should return 401 if token is missing", async function() {
const comment = {
"content": "Lorem ipsum dolor sit amet",
"video": videoId
}
const req = await request(app).post("/api/comments/").send(comment);
expect(req.status).toBe(401);
})
it("Should return 400 if video is missing", async function() {
const comment = {
"content": "Lorem ipsum dolor sit amet",
}
const req = await request(app).post("/api/comments/").send(comment).set("Authorization", "Bearer " + token);
expect(req.status).toBe(400);
})
it("Should return 400 if content is missing", async function() {
const comment = {
'video': videoId
}
const req = await request(app).post("/api/comments/").send(comment).set("Authorization", "Bearer " + token);
expect(req.status).toBe(400);
})
it("Should return 400 if video is not number", async function() {
const comment = {
"content": "Lorem ipsum dolor sit amet",
"video": "lorem"
}
const req = await request(app).post("/api/comments/").send(comment).set("Authorization", "Bearer " + token);
expect(req.status).toBe(400);
})
it("Should return 404 if video doesn't exist", async function() {
const comment = {
"content": "Lorem ipsum dolor sit amet",
"author": 5,
"video": 404
}
const req = await request(app).post("/api/comments/").send(comment).set("Authorization", "Bearer " + token);
expect(req.status).toBe(404);
})
})
describe("GET COMMENTS FROM VIDEO ID", async function() {
it("Should return 401 if token is missing", async function() {
const req = await request(app).get("/api/comments/video/" + videoId).send();
expect(req.status).toBe(401);
})
it("Should return 404 if video doesn't exist", async function() {
const req = await request(app).get("/api/comments/video/404").send().set("Authorization", "Bearer " + token)
expect(req.status).toBe(404);
})
it("Should return 200 if OK", async function() {
const req = await request(app).get("/api/comments/video/" + videoId).send().set("Authorization", "Bearer " + token)
expect(req.status).toBe(200);
})
})
describe("GET COMMENT BY ID", async function() {
it("Should return 401 if token is missing", async function() {
const req = await request(app).get("/api/comments/1").send()
expect(req.status).toBe(401);
})
it("Should return 400 if id is not number", async function() {
const req = await request(app).get("/api/comments/sacha").send().set("Authorization", "Bearer " + token)
expect(req.status).toBe(400);
})
it("Should return 404 if comment doesn't exist", async function() {
const req = await request(app).get("/api/comments/404").send().set("Authorization", "Bearer " + token)
expect(req.status).toBe(404);
})
it("Should return 200 if OK", async function() {
const req = await request(app).get("/api/comments/1").send().set("Authorization", "Bearer " + token)
expect(req.status).toBe(200);
})
})
describe("UPDATE COMMENT", async function() {
it("Should return 401 if token is missing", async function() {
const comment = {
"content": "Lorem ipsum dolor sit amet",
"video": videoId
}
const req = await request(app).put("/api/comments/" + commentId).send(comment);
expect(req.status).toBe(401);
})
it("Should return 400 if video is missing", async function() {
const comment = {
"content": "Lorem ipsum dolor sit amet",
}
const req = await request(app).put("/api/comments/" + commentId).send(comment).set("Authorization", "Bearer " + token);
expect(req.status).toBe(400);
})
it("Should return 400 if content is missing", async function() {
const comment = {
'video': videoId
}
const req = await request(app).put("/api/comments/" + commentId).send(comment).set("Authorization", "Bearer " + token);
expect(req.status).toBe(400);
})
it("Should return 400 if video is not number", async function() {
const comment = {
"content": "Lorem ipsum dolor sit amet",
"video": "lorem"
}
const req = await request(app).put("/api/comments/" + commentId).send(comment).set("Authorization", "Bearer " + token);
expect(req.status).toBe(400);
})
it("Should return 403 if user is not the author", async function() {
const comment = {
"content": "Lorem ipsum dolor sit amet",
"video": videoId
}
const req = await request(app).put("/api/comments/2").send(comment).set("Authorization", "Bearer " + token);
expect(req.status).toBe(403);
})
it("Should return 404 if comment doesn't exist", async function() {
const comment = {
"content": "Lorem ipsum dolor sit amet",
"video": videoId
}
const req = await request(app).put("/api/comments/12").send(comment).set("Authorization", "Bearer " + token);
expect(req.status).toBe(404);
})
it("Should return 404 if video doesn't exist", async function() {
const comment = {
"content": "Lorem ipsum dolor sit amet",
"author": 5,
"video": 404
}
const req = await request(app).put("/api/comments/" + commentId).send(comment).set("Authorization", "Bearer " + token);
expect(req.status).toBe(404);
})
})
describe("DELETE COMMENT", async function() {
it("Should return 401 if token is missing", async function() {
const req = await request(app).del("/api/comments/" + commentId).send();
expect(req.status).toBe(401);
})
it("Should return 403 if user is not the author", async function() {
const req = await request(app).del("/api/comments/2").send();
expect(req.status).toBe(401);
})
it("Should return 404 if comment doesn't exist", async function() {
const req = await request(app).del("/api/comments/404" + commentId).send().set("Authorization", "Bearer " + token);
expect(req.status).toBe(404);
})
})