-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.ts
58 lines (53 loc) · 1.58 KB
/
index.ts
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
import { AccountModel } from '@src/infra/db/mongoose/models';
import { hash } from 'bcrypt';
import env from '@src/main/config/env';
import { sign } from 'jsonwebtoken';
import faker from 'faker';
import nodemailer from 'nodemailer';
export const throwError = (): any => {
throw new Error();
};
type mockCreateAccountResponse = {
email: string;
password: string;
accountId: string;
};
export const mockCreateAccountOnDb =
async (): Promise<mockCreateAccountResponse> => {
const email = faker.internet.email();
const password = faker.internet.password();
const hashedPassword = await hash(password, 12);
const date = new Date();
date.setMinutes(date.getMinutes() + 5);
const doc = new AccountModel({
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
email,
password: hashedPassword,
forgotPasswordAccessToken: 123456,
forgotPasswordExpiresIn: date,
});
await doc.save();
return { email, password, accountId: doc._id };
};
export const mockAuthenticateUser = async (): Promise<string> => {
const { accountId } = await mockCreateAccountOnDb();
const accessToken = sign({ accountId }, env.jwtSecret);
await AccountModel.updateOne(
{ _id: accountId },
{
sessions: [
{
accessToken,
createdAt: new Date(),
updatedAt: new Date(),
},
],
}
);
return accessToken;
};
export const mockNodeMailer = (): jest.Mocked<typeof nodemailer> => {
const mockedNodeMailer = nodemailer as jest.Mocked<typeof nodemailer>;
return mockedNodeMailer;
};