Skip to content

Commit d7c7eec

Browse files
authored
refactor: some of the unit tests for the user api (#1254)
1 parent 8b76b01 commit d7c7eec

File tree

1 file changed

+42
-76
lines changed

1 file changed

+42
-76
lines changed

server/tests/unit/api/user.spec.ts

+42-76
Original file line numberDiff line numberDiff line change
@@ -342,87 +342,53 @@ describe("User API", () => {
342342
});
343343
});
344344

345-
it("should not register user if email is already in use", async () => {
346-
await request(app)
347-
.post("/api/user/register")
348-
.set("Authorization", `Bearer ${token}`)
349-
.send({ email: "test@localhost", username: "registered", password: "test1234" })
350-
.expect(400)
351-
.expect("Content-Type", /json/)
352-
.then(resp => {
353-
expect(resp.body.success).toBe(false);
354-
expect(resp.body.error).toBeDefined();
355-
expect(resp.body.error.name).toEqual("AlreadyInUse");
356-
expect(onUserLogInSpy).not.toBeCalled();
357-
});
358-
});
359-
360-
it("should not register user if username is already in use", async () => {
361-
await request(app)
362-
.post("/api/user/register")
363-
.set("Authorization", `Bearer ${token}`)
364-
.send({
345+
const denyCases: [string, any, number, any][] = [
346+
[
347+
"should not register user if email is already in use",
348+
{ email: "test@localhost", username: "registered", password: "test1234" },
349+
400,
350+
{ name: "AlreadyInUse", fields: ["email"] },
351+
],
352+
[
353+
"should not register user if username is already in use",
354+
{
365355
email: "register@localhost",
366356
username: "test user",
367357
password: "test1234",
368-
})
369-
.expect(400)
370-
.expect("Content-Type", /json/)
371-
.then(resp => {
372-
expect(resp.body.success).toBe(false);
373-
expect(resp.body.error).toBeDefined();
374-
expect(resp.body.error.name).toEqual("AlreadyInUse");
375-
expect(onUserLogInSpy).not.toBeCalled();
376-
});
377-
});
378-
379-
it("should not register user if email is invalid", async () => {
380-
await request(app)
358+
},
359+
400,
360+
{ name: "AlreadyInUse" },
361+
],
362+
[
363+
"should not register user if email is invalid",
364+
{ email: "bad", username: "bad email user", password: "test1234" },
365+
400,
366+
{ name: "ValidationError" },
367+
],
368+
[
369+
"should not register user if username is invalid",
370+
{ email: "badusername@localhost", username: "", password: "test1234" },
371+
400,
372+
{ name: "ValidationError" },
373+
],
374+
[
375+
"should not register user if password is not good enough",
376+
{ email: "badpassword@localhost", username: "bad password", password: "a" },
377+
400,
378+
{ name: "ValidationError" },
379+
],
380+
];
381+
382+
it.each(denyCases)("%s", async (_name, body, respCode, error) => {
383+
const resp = await request(app)
381384
.post("/api/user/register")
382385
.set("Authorization", `Bearer ${token}`)
383-
.send({ email: "bad", username: "bad email user", password: "test1234" })
384-
.expect(400)
385-
.expect("Content-Type", /json/)
386-
.then(resp => {
387-
expect(resp.body.success).toBe(false);
388-
expect(resp.body.error).toBeDefined();
389-
expect(resp.body.error.name).toEqual("ValidationError");
390-
expect(onUserLogInSpy).not.toBeCalled();
391-
});
392-
});
393-
394-
it("should not register user if username is invalid", async () => {
395-
await request(app)
396-
.post("/api/user/register")
397-
.set("Authorization", `Bearer ${token}`)
398-
.send({ email: "badusername@localhost", username: "", password: "test1234" })
399-
.expect(400)
400-
.expect("Content-Type", /json/)
401-
.then(resp => {
402-
expect(resp.body.success).toBe(false);
403-
expect(resp.body.error).toBeDefined();
404-
expect(resp.body.error.name).toEqual("ValidationError");
405-
expect(onUserLogInSpy).not.toBeCalled();
406-
});
407-
});
408-
409-
it("should not register user if password is not good enough", async () => {
410-
await request(app)
411-
.post("/api/user/register")
412-
.set("Authorization", `Bearer ${token}`)
413-
.send({
414-
email: "badpassword@localhost",
415-
username: "bad password",
416-
password: "a",
417-
})
418-
.expect(400)
419-
.expect("Content-Type", /json/)
420-
.then(resp => {
421-
expect(resp.body.success).toBe(false);
422-
expect(resp.body.error).toBeDefined();
423-
expect(resp.body.error.name).toEqual("ValidationError");
424-
expect(onUserLogInSpy).not.toBeCalled();
425-
});
386+
.send(body)
387+
.expect(respCode)
388+
.expect("Content-Type", /json/);
389+
expect(resp.body.success).toBe(false);
390+
expect(resp.body.error).toMatchObject(error);
391+
expect(onUserLogInSpy).not.toBeCalled();
426392
});
427393

428394
it("should not register user if registration is disabled", async () => {

0 commit comments

Comments
 (0)