forked from TomDoesTech/Testing-Express-REST-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.test.ts
More file actions
131 lines (105 loc) · 3.47 KB
/
Copy pathuser.test.ts
File metadata and controls
131 lines (105 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import mongoose from "mongoose";
import supertest from "supertest";
import createServer from "../utils/server";
import * as UserService from "../service/user.service";
import * as SessionService from "../service/session.service";
import { createUserSessionHandler } from "../controller/session.controller";
const app = createServer();
const userId = new mongoose.Types.ObjectId().toString();
const userPayload = {
_id: userId,
email: "jane.doe@example.com",
name: "Jane Doe",
};
const userInput = {
email: "test@example.com",
name: "Jane Doe",
password: "Password123",
passwordConfirmation: "Password123",
};
const sessionPayload = {
_id: new mongoose.Types.ObjectId().toString(),
user: userId,
valid: true,
userAgent: "PostmanRuntime/7.28.4",
createdAt: new Date("2021-09-30T13:31:07.674Z"),
updatedAt: new Date("2021-09-30T13:31:07.674Z"),
__v: 0,
};
describe("user", () => {
// user registration
describe("user registration", () => {
describe("given the username and password are valid", () => {
it("should return the user payload", async () => {
const createUserServiceMock = jest
.spyOn(UserService, "createUser")
// @ts-ignore
.mockReturnValueOnce(userPayload);
const { statusCode, body } = await supertest(app)
.post("/api/users")
.send(userInput);
expect(statusCode).toBe(200);
expect(body).toEqual(userPayload);
expect(createUserServiceMock).toHaveBeenCalledWith(userInput);
});
});
describe("given the passwords do not match", () => {
it("should return a 400", async () => {
const createUserServiceMock = jest
.spyOn(UserService, "createUser")
// @ts-ignore
.mockReturnValueOnce(userPayload);
const { statusCode } = await supertest(app)
.post("/api/users")
.send({ ...userInput, passwordConfirmation: "doesnotmatch" });
expect(statusCode).toBe(400);
expect(createUserServiceMock).not.toHaveBeenCalled();
});
});
describe("given the user service throws", () => {
it("should return a 409 error", async () => {
const createUserServiceMock = jest
.spyOn(UserService, "createUser")
.mockRejectedValueOnce("Oh no! :(");
const { statusCode } = await supertest(createServer())
.post("/api/users")
.send(userInput);
expect(statusCode).toBe(409);
expect(createUserServiceMock).toHaveBeenCalled();
});
});
});
describe("create user session", () => {
describe("given the username and password are valid", () => {
it("should return a signed accessToken & refresh token", async () => {
jest
.spyOn(UserService, "validatePassword")
// @ts-ignore
.mockReturnValue(userPayload);
jest
.spyOn(SessionService, "createSession")
// @ts-ignore
.mockReturnValue(sessionPayload);
const req = {
get: () => {
return "a user agent";
},
body: {
email: "test@example.com",
password: "Password123",
},
};
const send = jest.fn();
const res = {
send,
};
// @ts-ignore
await createUserSessionHandler(req, res);
expect(send).toHaveBeenCalledWith({
accessToken: expect.any(String),
refreshToken: expect.any(String),
});
});
});
});
});