Skip to content

Commit 1e4c248

Browse files
committed
[skip ci] migrate 06 examples
1 parent 301530e commit 1e4c248

4 files changed

Lines changed: 66 additions & 37 deletions

File tree

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// @ts-nocheck
2+
import { test, mock } from "node:test";
3+
import assert from "node:assert/strict";
4+
15
async function checkAuth(request, res) {
26
if (!request.session.data) {
37
return res.status(401).json();
@@ -15,21 +19,23 @@ const mockRequest = (sessionData) => {
1519

1620
const mockResponse = () => {
1721
const res = {};
18-
res.status = jest.fn().mockReturnValue(res);
19-
res.json = jest.fn().mockReturnValue(res);
22+
res.status = mock.fn();
23+
res.status.mock.mockImplementation(() => res);
24+
res.json = mock.fn();
25+
res.status.mock.mockImplementation(() => res);
2026
return res;
2127
};
2228

2329
test("should 401 if session data is not set", async () => {
2430
const request = mockRequest();
2531
const res = mockResponse();
2632
await checkAuth(request, res);
27-
expect(res.status).toHaveBeenCalledWith(401);
33+
assert.deepEqual(res.status.mock.calls[0].arguments, [401]);
2834
});
2935
test("should 200 with username from session if session data is set", async () => {
3036
const request = mockRequest({ username: "hugo" });
3137
const res = mockResponse();
3238
await checkAuth(request, res);
33-
expect(res.status).toHaveBeenCalledWith(200);
34-
expect(res.json).toHaveBeenCalledWith({ username: "hugo" });
39+
assert.deepEqual(res.status.mock.calls[0].arguments, [200]);
40+
assert.deepEqual(res.json.mock.calls[0].arguments, [{ username: "hugo" }]);
3541
});
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { test, mock } from "node:test";
2+
import assert from "node:assert/strict";
3+
14
const apiKeyToUser = {
25
"76b1e728-1c14-43f9-aa06-6de5cbc064c2": "hugo",
36
};
@@ -34,34 +37,36 @@ const mockRequest = (authHeader, sessionData) => ({
3437

3538
const mockResponse = () => {
3639
const res = {};
37-
res.status = jest.fn().mockReturnValue(res);
38-
res.json = jest.fn().mockReturnValue(res);
40+
res.status = mock.fn();
41+
res.status.mock.mockImplementation(() => res);
42+
res.json = mock.fn();
43+
res.status.mock.mockImplementation(() => res);
3944
return res;
4045
};
4146

4247
test("should set req.session.data if API key is in authorization and is valid", async () => {
4348
const req = mockRequest("76b1e728-1c14-43f9-aa06-6de5cbc064c2");
4449
const res = mockResponse();
4550
await headerAuth(req, res, () => {});
46-
expect(req.session.data).toEqual({ username: "hugo" });
51+
assert.deepEqual(req.session.data, { username: "hugo" });
4752
});
4853
test("should not do anything if req.session.data is already set", async () => {
4954
const req = mockRequest("76b1e728-1c14-43f9-aa06-6de5cbc064c2", {
5055
username: "guest",
5156
});
5257
const res = mockResponse();
5358
await headerAuth(req, res, () => {});
54-
expect(req.session.data).toEqual({ username: "guest" });
59+
assert.deepEqual(req.session.data, { username: "guest" });
5560
});
5661
test("should not do anything if authorization header is not present", async () => {
5762
const req = mockRequest(undefined);
5863
const res = mockResponse();
5964
await headerAuth(req, res, () => {});
60-
expect(req.session.data).toBeUndefined();
65+
assert.equal(req.session.data, undefined);
6166
});
6267
test("should not do anything if api key is invalid", async () => {
6368
const req = mockRequest("invalid-api-key");
6469
const res = mockResponse();
6570
await headerAuth(req, res, () => {});
66-
expect(req.session.data).toBeUndefined();
71+
assert.equal(req.session.data, undefined);
6772
});
Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
const bcrypt = require("bcrypt");
1+
import { test, mock } from "node:test";
2+
import assert from "node:assert/strict";
3+
4+
import bcrypt from "bcrypt";
25

36
const users = [
47
{
@@ -52,28 +55,34 @@ const mockRequest = (sessionData, body) => ({
5255

5356
const mockResponse = () => {
5457
const res = {};
55-
res.status = jest.fn().mockReturnValue(res);
56-
res.json = jest.fn().mockReturnValue(res);
58+
res.status = mock.fn();
59+
res.status.mock.mockImplementation(() => res);
60+
res.json = mock.fn();
61+
res.status.mock.mockImplementation(() => res);
5762
return res;
5863
};
5964

6065
test("should 400 if username is missing from body", async () => {
6166
const req = mockRequest({}, { password: "boss" });
6267
const res = mockResponse();
6368
await login(req, res);
64-
expect(res.status).toHaveBeenCalledWith(400);
65-
expect(res.json).toHaveBeenCalledWith({
66-
message: "username and password are required",
67-
});
69+
assert.deepEqual(res.status.mock.calls[0].arguments, [400]);
70+
assert.deepEqual(res.json.mock.calls[0].arguments, [
71+
{
72+
message: "username and password are required",
73+
},
74+
]);
6875
});
6976
test("should 400 if password is missing from body", async () => {
7077
const req = mockRequest({}, { username: "hugo" });
7178
const res = mockResponse();
7279
await login(req, res);
73-
expect(res.status).toHaveBeenCalledWith(400);
74-
expect(res.json).toHaveBeenCalledWith({
75-
message: "username and password are required",
76-
});
80+
assert.deepEqual(res.status.mock.calls[0].arguments, [400]);
81+
assert.deepEqual(res.json.mock.calls[0].arguments, [
82+
{
83+
message: "username and password are required",
84+
},
85+
]);
7786
});
7887
test("should 401 with message if user with passed username does not exist", async () => {
7988
const req = mockRequest(
@@ -85,10 +94,12 @@ test("should 401 with message if user with passed username does not exist", asyn
8594
);
8695
const res = mockResponse();
8796
await login(req, res);
88-
expect(res.status).toHaveBeenCalledWith(401);
89-
expect(res.json).toHaveBeenCalledWith({
90-
message: "No user with matching username",
91-
});
97+
assert.deepEqual(res.status.mock.calls[0].arguments, [401]);
98+
assert.deepEqual(res.json.mock.calls[0].arguments, [
99+
{
100+
message: "No user with matching username",
101+
},
102+
]);
92103
});
93104
test("should 401 with message if passed password does not match stored password", async () => {
94105
const req = mockRequest(
@@ -100,10 +111,12 @@ test("should 401 with message if passed password does not match stored password"
100111
);
101112
const res = mockResponse();
102113
await login(req, res);
103-
expect(res.status).toHaveBeenCalledWith(401);
104-
expect(res.json).toHaveBeenCalledWith({
105-
message: "Wrong password",
106-
});
114+
assert.deepEqual(res.status.mock.calls[0].arguments, [401]);
115+
assert.deepEqual(res.json.mock.calls[0].arguments, [
116+
{
117+
message: "Wrong password",
118+
},
119+
]);
107120
});
108121
test("should 201 and set session.data with username if user exists and right password provided", async () => {
109122
const req = mockRequest(
@@ -115,9 +128,9 @@ test("should 201 and set session.data with username if user exists and right pas
115128
);
116129
const res = mockResponse();
117130
await login(req, res);
118-
expect(res.status).toHaveBeenCalledWith(201);
119-
expect(res.json).toHaveBeenCalled();
120-
expect(req.session.data).toEqual({
131+
assert.deepEqual(res.status.mock.calls[0].arguments, [201]);
132+
assert.deepEqual(res.json.mock.calls[0].arguments, []);
133+
assert.deepEqual(req.session.data, {
121134
username: "guest",
122135
});
123136
});
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { test, mock } from "node:test";
2+
import assert from "node:assert/strict";
3+
14
async function logout(request, res) {
25
request.session.data = null;
36
return res.status(200).json();
@@ -11,20 +14,22 @@ const mockRequest = (sessionData) => {
1114

1215
const mockResponse = () => {
1316
const res = {};
14-
res.status = jest.fn().mockReturnValue(res);
15-
res.json = jest.fn().mockReturnValue(res);
17+
res.status = mock.fn();
18+
res.status.mock.mockImplementation(() => res);
19+
res.json = mock.fn();
20+
res.status.mock.mockImplementation(() => res);
1621
return res;
1722
};
1823

1924
test("should set session.data to null", async () => {
2025
const request = mockRequest({ username: "hugo" });
2126
const res = mockResponse();
2227
await logout(request, res);
23-
expect(request.session.data).toBeNull();
28+
assert.equal(request.session.data, null);
2429
});
2530
test("should 200", async () => {
2631
const request = mockRequest({ username: "hugo" });
2732
const res = mockResponse();
2833
await logout(request, res);
29-
expect(res.status).toHaveBeenCalledWith(200);
34+
assert.deepEqual(res.status.mock.calls[0].arguments, [200]);
3035
});

0 commit comments

Comments
 (0)