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.
 
 
 
 

413 lines
13 KiB

import { describe, it, expect } from 'vitest';
import app from '../server.js';
import request from 'supertest';
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NSwidXNlcm5hbWUiOiJ0ZXN0IiwiaWF0IjoxNzUxNzk2MjM2fQ.XmaVA_NQcpW7fxRtDWOinMyQXPaFixpp3ib_mzo6M6c"
describe('USER REGISTER', () => {
it('should return 400 when password is too short', async () => {
const user = {
email: "sachaguerin@gmail.com",
username: "sachaguerin",
password: "Toor!9",
picture: "null"
};
const res = await request(app)
.post('/api/users')
.send(user);
expect(res.status).toBe(400);
});
it('should return 400 when password is too long', async () => {
const user = {
email: "sachaguerin",
username: "sachaguerin",
password: "Toor!95555555555555555555555555555555555555555555555555555555",
picture: "null"
}
const res = await request(app)
.post("/api/users")
.send(user);
expect(res.status).toBe(400);
});
it('should return 400 when password don\'t have capital letters', async () => {
const user = {
email: "sachaguerin",
username: "sachaguerin",
password: "toooooooooor_974",
picture: "null"
}
const res = await request(app).post('/api/users').send(user);
expect(res.status).toBe(400);
});
it('should return 400 when password don\'t have lowercase letters', async () => {
const user = {
email: "sachaguerin",
username: "sachaguerin",
password: "TOOOOOOOOOOOOOOOOOOOORRR_974",
picture: "null"
}
const res = await request(app).post('/api/users').send(user);
expect(res.status).toBe(400);
});
it('should return 400 when password don\'t have symbols', async () => {
const user = {
email: "sachaguerin",
username: "sachaguerin",
password: "Rwqfsfasxc974",
picture: "null"
}
const res = await request(app).post('/api/users').send(user);
expect(res.status).toBe(400);
});
it("Should return 400 if email is invalid", async () => {
const user = {
email: "sachaguerin",
username: "sachaguerin",
password: "Rwqfsfasxc_974",
picture: "null"
}
const res = await request(app).post('/api/users').send(user);
expect(res.status).toBe(400);
})
it("Should return 400 if username isn't string", async () => {
const user = {
email: "sachaguerin",
username: 48,
password: "Rwqfsfasxc_974",
picture: "null"
}
const res = await request(app).post('/api/users').send(user);
expect(res.status).toBe(400);
})
it("Should return 400 if email is already used", async () => {
const user = {
email: "sachaguerin.sg@gmail.com",
username: "sachaguerin",
password: "Rwqfsfasxc_974",
picture: "null"
}
const res = await request(app).post('/api/users').send(user);
expect(res.status).toBe(400);
})
it("Should return 400 if username is already used", async () => {
const user = {
email: "sachaguerin@gmail.com",
username: "astria",
password: "Rwqfsfasxc_974",
picture: "null"
}
const res = await request(app).post('/api/users').send(user);
expect(res.status).toBe(400);
})
it("Should return 400 if email isn't provided", async () => {
const user = {
username: "sachaguerin",
password: "Rwqfsfasxc_974",
picture: "null"
}
const res = await request(app).post('/api/users').send(user);
expect(res.status).toBe(400);
})
it("Should return 400 if username isn't provided", async () => {
const user = {
email: "sachaguerin@gmail.com",
password: "Rwqfsfasxc_974",
picture: "null"
}
const res = await request(app).post('/api/users').send(user);
expect(res.status).toBe(400);
})
it("Should return 400 if password isn't provided", async () => {
const user = {
email: "sachaguerin@gmail.com",
username: "sachaguerin",
picture: "null"
}
const res = await request(app).post('/api/users').send(user);
expect(res.status).toBe(400);
})
it("Should return 400 if picture isn't provided", async () => {
const user = {
email: "sachaguerin@gmail.com",
username: "sachaguerin",
password: "Rwqfsfasxc_974",
}
const res = await request(app).post('/api/users').send(user);
expect(res.status).toBe(400);
})
});
describe('USER LOGIN', () => {
it('Should return 401 if account doesn\'t exist', async () => {
const user = {
username: "sachaguerin",
password: "Rwqfsfasxc__974",
}
const req = await request(app).post('/api/users/login').send(user);
expect(req.status).toBe(401);
})
it('Should return 401 if password is not valid', async () => {
const user = {
username: "astria",
password: "Rwqfsfasxc__974",
}
const req = await request(app).post('/api/users/login').send(user);
expect(req.status).toBe(401);
})
it("Should return 400 if username isn't provided", async () => {
const user = {
password: "Rwqfsfasxc_974",
}
const res = await request(app).post('/api/users/login').send(user);
expect(res.status).toBe(400);
})
it("Should return 400 if password isn't provided", async () => {
const user = {
username: "astria",
}
const res = await request(app).post('/api/users/login').send(user);
expect(res.status).toBe(400);
})
it("Should return 200 if OK", async () => {
const user = {
username: "astria",
password: "Rwqfsfasxc_974",
}
const res = await request(app).post('/api/users/login').send(user);
expect(res.status).toBe(200);
})
})
describe('GET USER BY ID', async () => {
it("Should return 401 if token is not valid", async () => {
const res = await request(app).get("/api/users/5").send();
expect(res.status).toBe(401);
})
it("Should return 404 if user doesn\'t exist", async () => {
const res = await request(app).get("/api/users/3333").send().set("Authorization", "Bearer " + token);
expect(res.status).toBe(404);
})
it("Should return 400 if id is not integer", async () => {
const res = await request(app).get("/api/users/sacha").send().set("Authorization", "Bearer " + token);
expect(res.status).toBe(400);
})
it("Should return 200 if OK", async () => {
const res = await request(app).get("/api/users/3").send().set("Authorization", "Bearer " + token);
expect(res.status).toBe(200);
})
})
describe('GET USER BY USERNAME', async () => {
it("Should return 401 if token is not valid", async () => {
const req = await request(app).get("/api/users/username/astria").send();
expect(req.status).toBe(401);
})
it("Should return 404 if user doesn\'t exist", async () => {
const req = await request(app).get("/api/users/username/congolexicomatision").send().set("Authorization", "Bearer " + token);
expect(req.status).toBe(404);
})
it("Should return 200 if OK", async () => {
const req = await request(app).get("/api/users/username/astria").send().set("Authorization", "Bearer " + token);
expect(req.status).toBe(200);
})
})
describe('UPDATE USER', () => {
it("Should return 401 if token is not valid", async () => {
const user = {
email: "sachaguerin@gmail.com",
username: "sacha",
password: "Rwqfsfasxc__974",
picture: "Sachaguerin",
}
const res = await request(app).put('/api/users/3').send(user);
expect(res.status).toBe(401);
})
it('should return 400 when password is too short', async () => {
const user = {
email: "test@test.com",
username: "test",
password: "Toor!9",
picture: "null"
};
const res = await request(app)
.put('/api/users/5')
.send(user)
.set("Authorization", "Bearer " + token);
expect(res.status).toBe(400);
});
it('should return 400 when password don\'t have capital letters', async () => {
const user = {
email: "test@test.com",
username: "test",
password: "toooooooooor_974",
picture: "null"
}
const res = await request(app).put('/api/users/5').send(user).set("Authorization", "Bearer " + token);
expect(res.status).toBe(400);
});
it('should return 400 when password don\'t have lowercase letters', async () => {
const user = {
email: "test@test.com",
username: "test",
password: "TOOOOOOOOOOOOOOOOOOOORRR_974",
picture: "null"
}
const res = await request(app).put('/api/users/5').send(user).set("Authorization", "Bearer " + token);
expect(res.status).toBe(400);
});
it('should return 400 when password don\'t have symbols', async () => {
const user = {
email: "test@test.com",
username: "test",
password: "Rwqfsfasxc974",
picture: "null"
}
const res = await request(app).put('/api/users/5').send(user).set("Authorization", "Bearer " + token);
expect(res.status).toBe(400);
});
it("Should return 400 if email is invalid", async () => {
const user = {
email: "sachaguerin",
username: "test",
password: "Rwqfsfasxc_974",
picture: "null"
}
const res = await request(app).put('/api/users/5').send(user).set("Authorization", "Bearer " + token);
expect(res.status).toBe(400);
})
it("Should return 403 if the user is not the owner", async () => {
const user = {
email: "test@test.com",
username: "test",
password: "Rwqfsfasxc_974",
picture: "null"
}
const res = await request(app).put('/api/users/3').send(user).set("Authorization", "Bearer " + token);
expect(res.status).toBe(403);
})
it("Should return 400 if mail change but already taken", async () => {
const user = {
email: "sachaguerin.sg@gmail.com",
username: "test",
picture: "null",
password: "Test_974"
}
const res = await request(app).put('/api/users/5').send(user).set("Authorization", "Bearer " + token);
expect(res.status).toBe(400);
})
it("Should return 400 if username changed but already taken", async () => {
const user = {
email: "test@test.com",
username: "astria",
password: "Test_974",
picture: "null"
}
const res = await request(app).put('/api/users/5').send(user).set("Authorization", "Bearer " + token);
expect(res.status).toBe(400);
})
it("Should return 404 if user doesn't exist", async () => {
const user = {
email: "test@test.com",
username: "test",
password: "Rwqfsfasxc_974",
picture: "null"
}
const res = await request(app).put('/api/users/666').send(user).set("Authorization", "Bearer " + token);
expect(res.status).toBe(404);
})
})
describe ('DELETE USER', async function () {
it("Should return 401 if token is not valid", async () => {
const req = await request(app).del('/api/users/5').send();
expect(req.status).toBe(401);
})
it ("Should return 403 if user isn't the owner", async () => {
const req = await request(app).del('/api/users/4').send().set("Authorization", "Bearer " + token);
expect(req.status).toBe(403);
})
it("Should return 400 if id is empty", async () => {
const req = await request(app).del('/api/users').send().set("Authorization", "Bearer " + token);
expect(req.status).toBe(404);
})
it("Should return 400 if id isn't number", async () => {
const req = await request(app).del('/api/users/sacha').send().set("Authorization", "Bearer " + token);
expect(req.status).toBe(400);
})
it("Should return 404 if user doesn't exist", async () => {
const req = await request(app).del('/api/users/666').send().set("Authorization", "Bearer " + token);
expect(req.status).toBe(404);
})
})