mail/back/test/test-utils/test-attrsUtils.ts
2023-04-04 18:00:27 +02:00

75 lines
2.1 KiB
TypeScript

import { AttrsWithEnvelope, User } from "../../interfaces/mail/attrs.interface";
import names from "./names";
interface Options {
subject?: string;
from?: User[];
sender?: User[];
replyTo?: User[];
to?: User[];
cc?: User[];
bcc?: User[];
inReplyTo?: string;
messageId?: string;
date?: string;
flags?: string[];
uid?: number;
modseq?: number;
}
export function generateAttrs(options: Options): AttrsWithEnvelope {
const attrs = {
"size": 42,
"envelope": {
date: "2023-03-21T15:25:42.000Z",
subject: options.subject ?? "subject" + randomString(10),
from: options.from ?? undefined,
sender: options.sender ?? undefined,
replyTo: options.replyTo ?? undefined,
to: options.to ?? undefined,
cc: options.cc ?? undefined,
bcc: options.bcc ?? undefined,
inReplyTo: options.inReplyTo ?? undefined,
messageId: options.messageId ?? randomString(10),
},
"date": options.date ?? new Date().toDateString(),
"flags": options.flags ?? [],
"uid": options.uid ?? randomInt(3),
"modseq": options.modseq ?? randomInt(7),
};
return attrs;
}
export interface UserTest {
user: User;
id: number;
}
export function generateUsers(nb: number): UserTest[] {
const users: {user: User, id: number}[] = [];
for (let i = 0; i < nb; i++) {
users.push({
user: {
name: "",
mailbox: names[i],
host: "provider.com",
},
id: i,
});
}
return users;
}
function randomString(length: number): string {
let result = "";
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function randomInt(length: number): number {
return parseInt((Math.random() * Math.pow(10, length)).toFixed());
}