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.
 
 
 
 

190 lines
7.8 KiB

import vitest, {describe} from "vitest";
import app from "../server.js";
import request from "supertest";
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NSwidXNlcm5hbWUiOiJ0ZXN0IiwiaWF0IjoxNzUxNzk2MjM2fQ.XmaVA_NQcpW7fxRtDWOinMyQXPaFixpp3ib_mzo6M6c"
describe("CREATE CHANNEL", async function() {
it("Should return 401 if token isn't valid", async function() {
const channel = {
"name": "Astria",
"description": "A channel already exists",
"owner": 5
}
const req = await request(app).post("/api/channels/").send(channel);
expect(req.statusCode).toBe(401);
})
it("Should return 400 if owner is not number", async function() {
const channel = {
"name": "Machin",
"description": "kljsdfjklsdfjkl",
"owner": "astria"
}
const req = await request(app).post("/api/channels/").send(channel).set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(400);
})
it("Should return 404 if owner don't exist", async function() {
const channel = {
"name": "Machin",
"description": "kljsdfjklsdfjkl",
"owner": 55
}
const req = await request(app).post("/api/channels/").send(channel).set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(404);
})
it("Should return 403 if user isn't owner of owner", async function() {
const channel = {
"name": "Machin",
"description": "kljsdfjklsdfjkl",
"owner": 3
}
const req = await request(app).post("/api/channels/").send(channel).set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(403);
})
it("Should return 400 if channel name already exists", async function() {
const channel = {
"name": "Sacha",
"description": "A channel already exists",
"owner": 5
}
const req = await request(app).post("/api/channels/").send(channel).set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(400);
})
it("Should return 400 if channel name is empty", async function() {
const channel = {
"description": "A channel already exists",
"owner": 5
}
const req = await request(app).post("/api/channels/").send(channel).set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(400);
})
it("Should return 400 if owner is empty", async function() {
const channel = {
"name": "Astria",
"description": "A channel already exists",
}
const req = await request(app).post("/api/channels/").send(channel).set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(400);
})
})
describe("GET CHANNEL BY ID", async function() {
it("Should return 401 if token is not valid", async function() {
const req = await request(app).get("/api/channels/2").send();
expect(req.statusCode).toBe(401);
})
it("Should return 400 if id is not number", async function() {
const req = await request(app).get("/api/channels/sacha").send().set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(400);
})
it("Should return 404 if channel does not exist", async function() {
const req = await request(app).get("/api/channels/200").send().set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(404);
})
it("Should return 200 if OK", async function() {
const req = await request(app).get("/api/channels/2").send().set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(200);
})
})
describe("GET ALL CHANNELS", async function() {
it("Should be 401 if token is not valid", async function() {
const req = await request(app).get("/api/channels/").send();
expect(req.statusCode).toBe(401);
})
it("Should return 200 if OK", async function() {
const req = await request(app).get("/api/channels/").send().set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(200);
})
})
describe("UPDATE CHANNEL", async function() {
it("Should be 401 if token is not valid", async function() {
const channel = {
"name": "Machin",
"description": "kljsdfjklsdfjkl",
}
const req = await request(app).put("/api/channels/2").send(channel);
expect(req.statusCode).toBe(401);
})
it("Should return 400 if name is empty", async function() {
const channel = {
"name": null,
"description": "A channel already exists",
}
const req = await request(app).put("/api/channels/2").send(channel).set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(400);
})
it("Should return 404 if channel does not exist", async function() {
const channel = {
"name": "Machin",
"description": "kljsdfjklsdfjkl",
}
const req = await request(app).put("/api/channels/404").send(channel).set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(404);
})
it("Should return 403 if user is not the owner of the channel", async function() {
const channel = {
"name": "Machin",
"description": "kljsdfjklsdfjkl",
}
const req = await request(app).put("/api/channels/3").send(channel).set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(403);
})
it("Should return 400 if channel name is changed but already used", async function() {
const channel = {
"name": "Astria",
"description": "kljsdfjklsdfjkl",
}
const req = await request(app).put("/api/channels/2").send(channel).set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(400);
})
})
describe("DELETE CHANNEL", async function() {
it("Should be 401 if token is not valid", async function() {
const req = await request(app).del("/api/channels/2").send();
expect(req.statusCode).toBe(401);
})
it("Should return 400 if id is not number", async function() {
const req = await request(app).del("/api/channels/astria").send().set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(400);
})
it("Should return 404 if channel does not exist", async function() {
const req = await request(app).del("/api/channels/404").send().set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(404);
})
it("Should return 403 if user is not the owner of the channel", async function() {
const req = await request(app).del("/api/channels/3").send().set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(403);
})
})
describe("TOGGLE SUBSCRIPTION", async function() {
it("Should return 401 if token is not valid", async function() {
const req = await request(app).post("/api/channels/2/subscribe").send();
expect(req.statusCode).toBe(401);
})
it("Should return 400 if id is not number", async function() {
const req = await request(app).post("/api/channels/sacha/subscribe").send().set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(400);
})
it("Should return 404 if channel does not exist", async function() {
const req = await request(app).post("/api/channels/404/subscribe").send().set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(404);
})
it("Should return 200 if OK", async function() {
const body = {
"userId": 5
}
const req = await request(app).post("/api/channels/2/subscribe").send(body).set("Authorization", "Bearer " + token);
expect(req.statusCode).toBe(200);
})
})