|
| 1 | +const request = require("supertest"); |
| 2 | +const { MongoMemoryServer } = require("mongodb-memory-server"); |
| 3 | +const { connectMongoDB, closeMongoDB, getDB } = require("../../db"); |
| 4 | +const createApp = require("../../app"); |
| 5 | +const config = require("../../config"); |
| 6 | +const { ObjectId } = require("mongodb"); |
| 7 | +const path = require("path"); |
| 8 | +const fs = require("fs"); |
| 9 | +const { Readable } = require("stream"); |
| 10 | +const { handleIncomingEmail } = require("../../services/emailService"); |
| 11 | + |
| 12 | +let mongoServer; |
| 13 | +let app; |
| 14 | + |
| 15 | +beforeAll(async () => { |
| 16 | + mongoServer = await MongoMemoryServer.create(); |
| 17 | + config.mongoURL = mongoServer.getUri(); |
| 18 | + await connectMongoDB(); |
| 19 | + app = createApp(); |
| 20 | +}); |
| 21 | + |
| 22 | +afterAll(async () => { |
| 23 | + await closeMongoDB(); |
| 24 | + await mongoServer.stop(); |
| 25 | +}); |
| 26 | + |
| 27 | +describe("API Routes", () => { |
| 28 | + beforeEach(async () => { |
| 29 | + const db = getDB(); |
| 30 | + await db.collection("test@example.com").deleteMany({}); |
| 31 | + await db.collection("other@example.com").deleteMany({}); |
| 32 | + }); |
| 33 | + |
| 34 | + const insertEmails = async (db, emails, collection) => { |
| 35 | + await db.collection(collection).insertMany(emails); |
| 36 | + }; |
| 37 | + |
| 38 | + const sendTestEmail = async (testEmail) => { |
| 39 | + const emailStream = new Readable(); |
| 40 | + emailStream.push( |
| 41 | + Buffer.from( |
| 42 | + Object.entries(testEmail) |
| 43 | + .map(([k, v]) => `${k}: ${v}`) |
| 44 | + .join("\r\n") |
| 45 | + ) |
| 46 | + ); |
| 47 | + emailStream.push(null); |
| 48 | + |
| 49 | + const session = { |
| 50 | + envelope: { |
| 51 | + rcptTo: [{ address: testEmail.to }], |
| 52 | + }, |
| 53 | + }; |
| 54 | + |
| 55 | + await handleIncomingEmail(emailStream, session); |
| 56 | + const db = getDB(); |
| 57 | + return db.collection(testEmail.to).findOne({ subject: testEmail.subject }); |
| 58 | + }; |
| 59 | + |
| 60 | + test("GET /api/all-emails should return emails for all users", async () => { |
| 61 | + const db = getDB(); |
| 62 | + await insertEmails(db, [{ from: { text: "sender@example.com" }, subject: "Test Email 1", date: new Date() }], "test@example.com"); |
| 63 | + await insertEmails(db, [{ from: { text: "sender@example.com" }, subject: "Test Email 2", date: new Date() }], "other@example.com"); |
| 64 | + |
| 65 | + const response = await request(app).get("/api/all-emails"); |
| 66 | + |
| 67 | + expect(response.status).toBe(200); |
| 68 | + expect(response.body.length).toBe(2); |
| 69 | + }); |
| 70 | + |
| 71 | + test("GET /api/emails-list/:emailId should return emails for a specific user", async () => { |
| 72 | + const db = getDB(); |
| 73 | + const emails = [ |
| 74 | + { subject: "Test Email 1", from: { text: "SendTestEmail <noreply@sendtestemail.com>" }, date: new Date(), readStatus: false }, |
| 75 | + { subject: "Test Email 2", from: { text: "SendTestEmail <noreply@sendtestemail.com>" }, date: new Date(), readStatus: true }, |
| 76 | + ]; |
| 77 | + await insertEmails(db, emails, "harshal@myserver.pw"); |
| 78 | + |
| 79 | + const response = await request(app).get("/api/emails-list/harshal@myserver.pw"); |
| 80 | + |
| 81 | + expect(response.status).toBe(200); |
| 82 | + expect(response.body.length).toBe(2); |
| 83 | + expect(response.body[0].subject).toBe("Test Email 1"); |
| 84 | + expect(response.body[1].subject).toBe("Test Email 2"); |
| 85 | + }); |
| 86 | + |
| 87 | + test("GET /api/email/:emailID/:email_id should return a specific email and update readStatus", async () => { |
| 88 | + const testEmail = { |
| 89 | + from: "SendTestEmail <noreply@sendtestemail.com>", |
| 90 | + to: "harshal@myserver.pw", |
| 91 | + subject: "Test Email", |
| 92 | + text: "This is a test email.", |
| 93 | + }; |
| 94 | + |
| 95 | + const savedEmail = await sendTestEmail(testEmail); |
| 96 | + |
| 97 | + let response = await request(app).get(`/api/email/${testEmail.to}/${savedEmail._id.toString()}`); |
| 98 | + response = await request(app).get(`/api/email/${testEmail.to}/${savedEmail._id.toString()}`); |
| 99 | + |
| 100 | + expect(response.status).toBe(200); |
| 101 | + expect(response.body[0].subject).toBe("Test Email"); |
| 102 | + expect(response.body[0].readStatus).toBe(true); |
| 103 | + |
| 104 | + const db = getDB(); |
| 105 | + const updatedEmail = await db.collection(testEmail.to).findOne({ _id: savedEmail._id }); |
| 106 | + expect(updatedEmail.readStatus).toBe(true); |
| 107 | + |
| 108 | + await db.collection(testEmail.to).deleteOne({ _id: savedEmail._id }); |
| 109 | + }); |
| 110 | + |
| 111 | + test("DELETE /api/email/:emailID/:email_id should delete an email", async () => { |
| 112 | + const db = getDB(); |
| 113 | + const insertResult = await db.collection("harshal@myserver.pw").insertOne({ |
| 114 | + subject: "Test Email", |
| 115 | + from: { text: "SendTestEmail <noreply@sendtestemail.com>" }, |
| 116 | + date: new Date(), |
| 117 | + }); |
| 118 | + |
| 119 | + const response = await request(app).delete(`/api/email/harshal@myserver.pw/${insertResult.insertedId.toString()}`); |
| 120 | + |
| 121 | + expect(response.status).toBe(200); |
| 122 | + expect(response.body.message).toBe("Email deleted successfully"); |
| 123 | + |
| 124 | + const deletedEmail = await db.collection("harshal@myserver.pw").findOne({ _id: insertResult.insertedId }); |
| 125 | + expect(deletedEmail).toBeNull(); |
| 126 | + }); |
| 127 | + |
| 128 | + test("GET /api/attachment/:directory/:filename should serve attachment files", async () => { |
| 129 | + const testEmail = { |
| 130 | + from: "sender@example.com", |
| 131 | + to: "recipient@example.com", |
| 132 | + subject: "Test Email with Attachment", |
| 133 | + text: "This is a test email with an attachment.", |
| 134 | + attachments: [ |
| 135 | + { |
| 136 | + filename: "test.txt", |
| 137 | + content: "This is a test attachment content.", |
| 138 | + }, |
| 139 | + ], |
| 140 | + }; |
| 141 | + |
| 142 | + const emailStream = new Readable(); |
| 143 | + emailStream.push( |
| 144 | + `From: ${testEmail.from}\r\n` + |
| 145 | + `To: ${testEmail.to}\r\n` + |
| 146 | + `Subject: ${testEmail.subject}\r\n` + |
| 147 | + `Content-Type: multipart/mixed; boundary="boundary"\r\n\r\n` + |
| 148 | + `--boundary\r\n` + |
| 149 | + `Content-Type: text/plain\r\n\r\n` + |
| 150 | + `${testEmail.text}\r\n\r\n` + |
| 151 | + `--boundary\r\n` + |
| 152 | + `Content-Type: text/plain; name=${testEmail.attachments[0].filename}\r\n` + |
| 153 | + `Content-Disposition: attachment; filename=${testEmail.attachments[0].filename}\r\n` + |
| 154 | + `Content-Transfer-Encoding: base64\r\n\r\n` + |
| 155 | + `${Buffer.from(testEmail.attachments[0].content).toString("base64")}\r\n\r\n` + |
| 156 | + `--boundary--\r\n` |
| 157 | + ); |
| 158 | + emailStream.push(null); |
| 159 | + |
| 160 | + const session = { |
| 161 | + envelope: { |
| 162 | + rcptTo: [{ address: testEmail.to }], |
| 163 | + }, |
| 164 | + }; |
| 165 | + |
| 166 | + await handleIncomingEmail(emailStream, session); |
| 167 | + |
| 168 | + const db = getDB(); |
| 169 | + const savedEmail = await db.collection(testEmail.to).findOne({ subject: testEmail.subject }); |
| 170 | + |
| 171 | + expect(savedEmail).toBeTruthy(); |
| 172 | + expect(savedEmail.attachments.length).toBe(1); |
| 173 | + const [attachment] = savedEmail.attachments; |
| 174 | + |
| 175 | + const response = await request(app).get(`/api/attachment/${attachment.directory}/${attachment.filename}`); |
| 176 | + |
| 177 | + expect(response.status).toBe(200); |
| 178 | + expect(response.text).toBe("This is a test attachment content."); |
| 179 | + |
| 180 | + await db.collection(testEmail.to).deleteOne({ _id: savedEmail._id }); |
| 181 | + const attachmentPath = path.join(__dirname, "../../attachments", attachment.directory, attachment.filename); |
| 182 | + if (fs.existsSync(attachmentPath)) { |
| 183 | + fs.unlinkSync(attachmentPath); |
| 184 | + fs.rmdirSync(path.dirname(attachmentPath)); |
| 185 | + } |
| 186 | + }); |
| 187 | +}); |
0 commit comments