Skip to content

Commit e88c6f8

Browse files
committed
Remove handlebars and dockerise test case for user service
1 parent 311e524 commit e88c6f8

File tree

11 files changed

+169
-95
lines changed

11 files changed

+169
-95
lines changed

backend/user-service/config/redis.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ const REDIS_URI = process.env.REDIS_URI || "redis://localhost:6379";
77

88
const client = createClient({ url: REDIS_URI });
99

10-
client.on("error", (err) => console.log(`Error: ${err}`));
10+
export const connectRedis = async () => {
11+
await client.connect();
12+
client.on("error", (err) => console.log(`Error: ${err}`));
13+
};
1114

12-
(async () => await client.connect())();
15+
// client.on("error", (err) => console.log(`Error: ${err}`));
16+
17+
// (async () => await client.connect())();
1318

1419
export default client;

backend/user-service/jest.config.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* https://jestjs.io/docs/configuration
44
*/
55

6-
// import type { Config } from "jest";
7-
import type { JestConfigWithTsJest } from "ts-jest";
6+
import type { Config } from "jest";
7+
// import type { JestConfigWithTsJest } from "ts-jest";
88

9-
const config: JestConfigWithTsJest = {
9+
const config: Config = {
1010
// All imported modules in your tests should be mocked automatically
1111
// automock: false,
1212

@@ -53,7 +53,7 @@ const config: JestConfigWithTsJest = {
5353
// Make calling deprecated APIs throw helpful error messages
5454
// errorOnDeprecated: false,
5555

56-
extensionsToTreatAsEsm: [".ts"],
56+
// extensionsToTreatAsEsm: [".ts"],
5757

5858
// The default configuration for fake timers
5959
// fakeTimers: {
@@ -93,9 +93,9 @@ const config: JestConfigWithTsJest = {
9393
// ],
9494

9595
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
96-
moduleNameMapper: {
97-
"^(\\.{1,2}/.*)\\.js$": "$1",
98-
},
96+
// moduleNameMapper: {
97+
// "^(\\.{1,2}/.*)\\.js$": "$1",
98+
// },
9999

100100
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
101101
// modulePathIgnorePatterns: [],
@@ -151,7 +151,7 @@ const config: JestConfigWithTsJest = {
151151
// snapshotSerializers: [],
152152

153153
// The test environment that will be used for testing
154-
// testEnvironment: "jest-environment-node",
154+
// testEnvironment: "node",
155155

156156
// Options that will be passed to the testEnvironment
157157
// testEnvironmentOptions: {},
@@ -180,28 +180,38 @@ const config: JestConfigWithTsJest = {
180180
// testRunner: "jest-circus/runner",
181181

182182
// A map from regular expressions to paths to transformers
183-
transform: {
184-
"^.+\\.tsx?$": [
185-
"ts-jest",
186-
{
187-
diagnostics: {
188-
ignoreCodes: [1343],
189-
},
190-
astTransformers: {
191-
before: [
192-
{
193-
path: "node_modules/ts-jest-mock-import-meta", // or, alternatively, 'ts-jest-mock-import-meta' directly, without node_modules.
194-
},
195-
],
196-
},
197-
},
198-
],
199-
},
183+
// transform: {
184+
// "^.+\\.tsx?$": [
185+
// "ts-jest",
186+
// {
187+
// diagnostics: {
188+
// ignoreCodes: [1343],
189+
// },
190+
// astTransformers: {
191+
// before: [
192+
// {
193+
// path: "node_modules/ts-jest-mock-import-meta", // or, alternatively, 'ts-jest-mock-import-meta' directly, without node_modules.
194+
// },
195+
// ],
196+
// },
197+
// },
198+
// ],
199+
// },
200+
// transform: {
201+
// "^.+\\.tsx?$": [
202+
// "ts-jest",
203+
// {
204+
// useESM: true,
205+
// astTransformers: { before: [{ path: "ts-jest-mock-import-meta" }] },
206+
// diagnostics: { ignoreCodes: [1343] },
207+
// },
208+
// ],
209+
// "node_modules/nodemailer-express-handlebars/.+\\.(j|t)sx?$": "ts-jest",
210+
// },
200211

201212
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
202213
// transformIgnorePatterns: [
203-
// "/node_modules/",
204-
// "\\.pnp\\.[^\\/]+$"
214+
// "node_modules/?!(nodemailer-express-handlebars/.*)",
205215
// ],
206216

207217
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them

backend/user-service/package-lock.json

Lines changed: 3 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/user-service/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"dotenv": "^16.4.5",
5151
"express": "^4.19.2",
5252
"firebase-admin": "^12.6.0",
53+
"handlebars": "^4.7.8",
5354
"jsonwebtoken": "^9.0.2",
5455
"mongoose": "^8.5.4",
5556
"multer": "^1.4.5-lts.1",

backend/user-service/server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import index from "./app.ts";
33
import dotenv from "dotenv";
44
import { connectToDB } from "./model/repository";
55
import { seedAdminAccount } from "./scripts/seed.ts";
6+
import { connectRedis } from "./config/redis.ts";
67

78
dotenv.config();
89

@@ -23,4 +24,6 @@ if (process.env.NODE_ENV !== "test") {
2324
console.error("Failed to connect to DB");
2425
console.error(err);
2526
});
27+
28+
await connectRedis();
2629
}

backend/user-service/templates/account-verification.hbs

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import mongoose from "mongoose";
2-
import { MongoMemoryServer } from "mongodb-memory-server";
3-
4-
let mongo: MongoMemoryServer;
2+
import redisClient from "../config/redis";
53

64
beforeAll(async () => {
7-
mongo = await MongoMemoryServer.create();
8-
const mongoUri = mongo.getUri();
5+
// mongo = await MongoMemoryServer.create();
6+
// const mongoUri = mongo.getUri();
97

10-
if (mongoose.connection.readyState !== 0) {
11-
await mongoose.disconnect();
12-
}
8+
// if (mongoose.connection.readyState !== 0) {
9+
// await mongoose.disconnect();
10+
// }
11+
12+
const mongoUri =
13+
process.env.MONGO_URI_TEST || "mongodb://mongo:mongo@mongo:27017/";
1314

1415
await mongoose.connect(mongoUri, {});
16+
await redisClient.connect();
17+
redisClient.on("error", (err) => console.log(`Error: ${err}`));
1518
});
1619

1720
afterEach(async () => {
@@ -24,9 +27,10 @@ afterEach(async () => {
2427
});
2528

2629
afterAll(async () => {
27-
if (mongo) {
28-
await mongo.stop();
29-
}
30+
// if (mongo) {
31+
// await mongo.stop();
32+
// }
3033

3134
await mongoose.connection.close();
35+
await redisClient.disconnect();
3236
});

backend/user-service/tests/userRoutes.spec.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const USER_BASE_URL = "/api/users";
1111

1212
faker.seed(0);
1313

14+
const mockSendMail = jest.fn();
15+
1416
jest.mock("../middleware/basic-access-control", () => ({
1517
verifyAccessToken: jest.fn((req, res, next) => {
1618
req.user = {
@@ -48,6 +50,12 @@ jest.mock("../middleware/basic-access-control", () => ({
4850
}),
4951
}));
5052

53+
jest.mock("nodemailer", () => ({
54+
createTransport: jest.fn(() => ({
55+
sendMail: () => mockSendMail(),
56+
})),
57+
}));
58+
5159
const username = faker.internet.userName();
5260
const firstName = faker.person.firstName();
5361
const lastName = faker.person.lastName();
@@ -117,15 +125,13 @@ describe("User routes", () => {
117125
});
118126

119127
it("Create a user with invalid username characters", async () => {
120-
const res = await request
121-
.post(USER_BASE_URL)
122-
.send({
123-
username: "!!!!!!!!!!!!!",
124-
firstName,
125-
lastName,
126-
email,
127-
password,
128-
});
128+
const res = await request.post(USER_BASE_URL).send({
129+
username: "!!!!!!!!!!!!!",
130+
firstName,
131+
lastName,
132+
email,
133+
password,
134+
});
129135

130136
expect(res.status).toBe(400);
131137
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
11
export const ACCOUNT_VERIFICATION_SUBJ = "Account Verification Link";
2+
3+
export const ACCOUNT_VERIFICATION_TEMPLATE = `
4+
<html>
5+
<head>
6+
<title>Account Verification</title>
7+
<meta charset="utf-8" />
8+
</head>
9+
<body>
10+
<p>Hello {{username}}!</p>
11+
<p>
12+
Thank you for signing up with us. Please verify your email address via
13+
this
14+
<span><a href={{verificationLink}}>link</a></span>.
15+
</p>
16+
<p>If you did not sign up with us, please ignore this email.</p>
17+
<p>Regards,</p>
18+
<p>Peerprep G28</p>
19+
</body>
20+
</html>
21+
`;
Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import nodemailer from "nodemailer";
22
import dotenv from "dotenv";
3-
import hbs from "nodemailer-express-handlebars";
4-
import path from "path";
5-
import { fileURLToPath } from "url";
3+
// import path from "path";
4+
// import { fileURLToPath } from "url";
65
import { Options } from "nodemailer/lib/mailer";
6+
// import hbs from "nodemailer-express-handlebars";
7+
import Handlebars from "handlebars";
8+
import { ACCOUNT_VERIFICATION_TEMPLATE } from "./constants";
79

8-
type ExtendedOptions = Options & {
9-
template: string;
10-
context: Record<string, unknown>;
11-
};
10+
// type ExtendedOptions = Options & {
11+
// template: string;
12+
// context: Record<string, unknown>;
13+
// };
1214

1315
dotenv.config();
1416

@@ -21,33 +23,35 @@ const transporter = nodemailer.createTransport({
2123
auth: { user: USER, pass: PASS },
2224
});
2325

24-
const dirname = fileURLToPath(import.meta.url);
26+
// const dirname = fileURLToPath(import.meta.url);
2527

26-
transporter.use(
27-
"compile",
28-
hbs({
29-
viewEngine: {
30-
extname: ".hbs",
31-
layoutsDir: path.resolve(path.dirname(dirname), "../templates/"),
32-
defaultLayout: "",
33-
},
34-
viewPath: path.resolve(path.dirname(dirname), "../templates/"),
35-
extName: ".hbs",
36-
})
37-
);
28+
// transporter.use(
29+
// "compile",
30+
// hbs({
31+
// viewEngine: {
32+
// extname: ".hbs",
33+
// layoutsDir: path.resolve(path.dirname(dirname), "../templates/"),
34+
// defaultLayout: "",
35+
// },
36+
// viewPath: path.resolve(path.dirname(dirname), "../templates/"),
37+
// extName: ".hbs",
38+
// })
39+
// );
3840

3941
export const sendAccVerificationMail = async (
4042
to: string,
4143
subject: string,
4244
username: string,
4345
verificationLink: string
4446
) => {
45-
const options: ExtendedOptions = {
47+
const template = Handlebars.compile(ACCOUNT_VERIFICATION_TEMPLATE);
48+
const replacement = { username, verificationLink };
49+
const html = template(replacement);
50+
const options = {
4651
from: USER,
4752
to,
4853
subject,
49-
template: "account-verification",
50-
context: { username, verificationLink },
54+
html,
5155
};
5256
return transporter.sendMail(options);
5357
};

0 commit comments

Comments
 (0)