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.
 
 
 
 

231 lines
10 KiB

import { describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../server.js';
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NSwidXNlcm5hbWUiOiJ0ZXN0IiwiaWF0IjoxNzUxODA5OTA3fQ.uCJFysaOiXC8qZKykWLi58WDcItKSkQPUIZ7kH-87Tg";
const playlistId = 1;
const playlistId2 = 3;
const videoId = 14;
const falseVideoId = 404;
const falseplaylistId = 404;
const userId = 5;
describe('CREATE PLAYLIST', () => {
it("Should return 401 if token is missing", async () => {
const playlist = {
"name": "Playlist Test",
}
const req = await request(app).post("/api/playlists").send(playlist);
expect(req.statusCode).toBe(401);
})
it("Should return 400 if name is missing", async () => {
const playlist = {}
const req = await request(app).post("/api/playlists").set("Authorization", `Bearer ${token}`).send(playlist);
expect(req.statusCode).toBe(400);
})
it("Should return 400 if name is not alphanumeric", async () => {
const playlist = {
"name": "Playlist Test !@#"
}
const req = await request(app).post("/api/playlists").set("Authorization", `Bearer ${token}`).send(playlist);
expect(req.statusCode).toBe(400);
})
it("Should return 200 if playlist is created", async () => {
const playlist = {
"name": "Playlist Test",
}
const req = await request(app).post("/api/playlists").set("Authorization", `Bearer ${token}`).send(playlist);
expect(req.statusCode).toBe(200);
})
});
describe('ADD VIDEO TO PLAYLIST', () => {
it("Should return 401 if token is missing", async () => {
const req = await request(app).post(`/api/playlists/${playlistId}`).send({ video: videoId });
expect(req.statusCode).toBe(401);
})
it("Should return 404 if playlist does not exist", async () => {
const req = await request(app).post(`/api/playlists/${falseplaylistId}`).set("Authorization", `Bearer ${token}`).send({ video: videoId });
console.log(req.body);
expect(req.statusCode).toBe(404);
})
it("Should return 403 if user is not the owner of the playlist", async () => {
const req = await request(app).post(`/api/playlists/${playlistId2}`).set("Authorization", `Bearer ${token}`).send({ video: videoId });
expect(req.statusCode).toBe(403);
})
it("Should return 400 if video id is missing", async () => {
const req = await request(app).post(`/api/playlists/${playlistId}`).set("Authorization", `Bearer ${token}`).send({});
expect(req.statusCode).toBe(400);
})
it("Should return 400 if video id is not numeric", async () => {
const req = await request(app).post(`/api/playlists/${playlistId}`).set("Authorization", `Bearer ${token}`).send({ video: "test" });
expect(req.statusCode).toBe(400);
})
it("Should return 404 if video does not exist", async () => {
const req = await request(app).post(`/api/playlists/${playlistId}`).set("Authorization", `Bearer ${token}`).send({ video: falseVideoId });
expect(req.statusCode).toBe(404);
})
it("Should return 200 if video is added to playlist", async () => {
const req = await request(app).post(`/api/playlists/${playlistId}`).set("Authorization", `Bearer ${token}`).send({ video: videoId });
expect(req.statusCode).toBe(200);
})
});
describe('GET PLAYLIST BY USER', () => {
it("Should return 401 if token is missing", async () => {
const req = await request(app).get("/api/playlists/user/" + userId);
expect(req.statusCode).toBe(401);
})
it("Should return 400 if user id is not numeric", async () => {
const req = await request(app).get("/api/playlists/user/test").set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(400);
})
it("Should return 404 if user does not exist", async () => {
const req = await request(app).get("/api/playlists/user/404").set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(404);
})
it("Should return 403 if user is not the owner of the account", async () => {
const req = await request(app).get("/api/playlists/user/" + 6).set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(403);
})
it("Should return 200 if playlists are found for user", async () => {
const req = await request(app).get("/api/playlists/user/" + 5).set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(200);
})
});
describe('GET PLAYLIST BY ID', () => {
it("Should return 401 if token is missing", async ()=> {
const req = await request(app).get(`/api/playlists/${playlistId}`);
expect(req.statusCode).toBe(401);
})
it("Should return 400 if playlist id is not numeric", async () => {
const req = await request(app).get("/api/playlists/test").set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(400);
})
it("Should return 404 if playlist does not exist", async () => {
const req = await request(app).get(`/api/playlists/${falseplaylistId}`).set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(404);
})
it("Should return 403 if user is not the owner of the playlist", async () => {
const req = await request(app).get(`/api/playlists/${playlistId2}`).set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(403);
})
it("Should return 200 if playlist is found", async () => {
const req = await request(app).get(`/api/playlists/${playlistId}`).set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(200);
})
})
describe("UPDATE PLAYLIST", () => {
it("Should return 401 if token is missing", async () => {
const req = await request(app).put(`/api/playlists/${playlistId}`).send({ name: "New Playlist Name" });
expect(req.statusCode).toBe(401);
})
it("Should return 400 if playlist id is not numeric", async () => {
const req = await request(app).put("/api/playlists/test").set("Authorization", `Bearer ${token}`).send({ name: "New Playlist Name" });
expect(req.statusCode).toBe(400);
})
it("Should return 404 if playlist does not exist", async () => {
const req = await request(app).put(`/api/playlists/${falseplaylistId}`).set("Authorization", `Bearer ${token}`).send({ name: "New Playlist Name" });
expect(req.statusCode).toBe(404);
})
it("Should return 403 if user is not the owner of the playlist", async () => {
const req = await request(app).put(`/api/playlists/${playlistId2}`).set("Authorization", `Bearer ${token}`).send({ name: "New Playlist Name" });
expect(req.statusCode).toBe(403);
})
it("Should return 400 if name is missing", async () => {
const req = await request(app).put(`/api/playlists/${playlistId}`).set("Authorization", `Bearer ${token}`).send({});
expect(req.statusCode).toBe(400);
})
it("Should return 400 if name is not alphanumeric", async () => {
const req = await request(app).put(`/api/playlists/${playlistId}`).set("Authorization", `Bearer ${token}`).send({ name: "New Playlist Name !@#" });
expect(req.statusCode).toBe(400);
})
it("Should return 200 if playlist is updated", async () => {
const req = await request(app).put(`/api/playlists/${playlistId}`).set("Authorization", `Bearer ${token}`).send({ name: "Updated Playlist Name" });
expect(req.statusCode).toBe(200);
})
})
describe("DELETE VIDEO FROM PLAYLIST", () => {
it("Should return 401 if token is missing", async () => {
const req = await request(app).delete(`/api/playlists/${playlistId}/video/${videoId}`);
expect(req.statusCode).toBe(401);
})
it("Should return 400 if playlist id is not numeric", async () => {
const req = await request(app).delete("/api/playlists/test/video/14").set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(400);
})
it("Should return 400 if video id is not numeric", async () => {
const req = await request(app).delete(`/api/playlists/${playlistId}/video/test`).set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(400);
})
it("Should return 404 if playlist does not exist", async () => {
const req = await request(app).delete(`/api/playlists/${falseplaylistId}/video/${videoId}`).set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(404);
})
it("Should return 403 if user is not the owner of the playlist", async () => {
const req = await request(app).delete(`/api/playlists/${playlistId2}/video/${videoId}`).set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(403);
})
it("Should return 404 if video is not in the playlist", async () => {
const req = await request(app).delete(`/api/playlists/${playlistId}/video/${falseVideoId}`).set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(404);
})
it("Should return 200 if video is deleted from playlist", async () => {
const req = await request(app).delete(`/api/playlists/${playlistId}/video/${videoId}`).set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(200);
})
})
describe("DELETE PLAYLIST", () => {
it("Should return 401 if token is missing", async () => {
const req = await request(app).delete(`/api/playlists/${playlistId}`);
expect(req.statusCode).toBe(401);
})
it("Should return 400 if playlist id is not numeric", async () => {
const req = await request(app).delete("/api/playlists/test").set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(400);
})
it("Should return 404 if playlist does not exist", async () => {
const req = await request(app).delete(`/api/playlists/${falseplaylistId}`).set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(404);
})
it("Should return 403 if user is not the owner of the playlist", async () => {
const req = await request(app).delete(`/api/playlists/${playlistId2}`).set("Authorization", `Bearer ${token}`);
expect(req.statusCode).toBe(403);
})
})