|
| 1 | +/*eslint no-unused-expressions: 0, prefer-arrow-callback: 0, no-console:0 */ |
| 2 | +/* globals before: false */ |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +const supertest = require('supertest'); |
| 7 | +const chai = require('chai'); |
| 8 | + |
| 9 | +const expect = chai.expect; |
| 10 | +chai.config.includeStack = true; |
| 11 | +const config = require('wild-config'); |
| 12 | + |
| 13 | +const server = supertest.agent(`http://127.0.0.1:${config.api.port}`); |
| 14 | + |
| 15 | +describe('Messages tests', function () { |
| 16 | + this.timeout(10000); // eslint-disable-line no-invalid-this |
| 17 | + |
| 18 | + let user; |
| 19 | + let testMailbox; |
| 20 | + let trashId; |
| 21 | + |
| 22 | + before(async () => { |
| 23 | + // ensure that we have an existing user account |
| 24 | + const userCreationResponse = await server |
| 25 | + .post('/users') |
| 26 | + .send({ |
| 27 | + username: 'messagestestsuser', |
| 28 | + password: 'secretpassword', |
| 29 | + address: 'messagestestsuser@web.zone.test', |
| 30 | + name: 'messages user', |
| 31 | + recipients: 10000, |
| 32 | + pubKey: '-----BEGIN PGP PUBLIC KEY BLOCK-----\nVersion: Keybase OpenPGP v1.0.0\nComment: https://keybase.io/crypto\n\nxo0EYb0PqAEEANJtI/ivwudfCMmxm+a77Fll5YwSzaaI2nqhcp6pMRJ4l0aafsX3\nBcXUQpsyyELelt2xFtwTNygR4RFWVTn4OoXmO5zFtWCSegAwSyUNK7R/GXi2GTKk\nkYtxUwGcNKBkfY7yAn5KsaeuZL1feDXUGt0YHUmBds5i+6ylI+i4tNbRABEBAAHN\nH1dpbGQgRHVjayA8dGVzdEB3aWxkZHVjay5lbWFpbD7CrQQTAQoAFwUCYb0PqAIb\nLwMLCQcDFQoIAh4BAheAAAoJEJVLs8wf5gSCzBoD/3gz32OfJM1D4IrmKVwyLKxC\n1P81kL7E6ICWD2A0JF9EkojsMHl+/zagwoJejBQhmzTNkFmui5zwmdLGforKl303\ntB0l9vCTb5+eDDHOTUatJrvlw76Fz2ZjIh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/2vz4LKVK9apyJI\neaZ+D7dt5Ipu1p0lWtglqL0xtjOSWuwHFwHuiRYg6eyhGN1RylFpuiKi5KykhrBS\nuBL/BHrk6AP/boRA+KIlb6s19KHNt54Kl8n8G4ZApCwZbUc2jzvbP5DZL5rcjlHd\ns4i4XE+uIJxsiX3iJZtVXzhTKuQlaoEljlhPs/TZYUmxeJ3TdV4o7emWiZ4gE8EQ\nhfxV37ew/GoYm6yME3tAZLIXbv2+bj6HZ4eE8bAMmPvpcQ+UwNJXvnk=\n=dR+x\n-----END PGP PUBLIC KEY BLOCK-----', |
| 33 | + encryptMessages: false |
| 34 | + }) |
| 35 | + .expect(200); |
| 36 | + expect(userCreationResponse.body.success).to.be.true; |
| 37 | + expect(userCreationResponse.body.id).to.exist; |
| 38 | + |
| 39 | + user = userCreationResponse.body.id; |
| 40 | + |
| 41 | + const mailboxesResponse = await server.get(`/users/${user}/mailboxes`).send(); |
| 42 | + // inboxMailbox = mailboxesResponse.body.results[0].id; // INBOX id |
| 43 | + |
| 44 | + const mailboxResponse = await server.post(`/users/${user}/mailboxes`).send({ path: '/testpath', hidden: false, retention: 10000 }).expect(200); |
| 45 | + testMailbox = mailboxResponse.body.id; |
| 46 | + trashId = mailboxesResponse.body.results[4].id; |
| 47 | + |
| 48 | + // Instantiate some random messages |
| 49 | + const initialMessages = []; |
| 50 | + const initialMessagesCount = 500; |
| 51 | + for (let i = 0; i < initialMessagesCount; i++) { |
| 52 | + const messageCreationPromise = server.post(`/users/${user}/mailboxes/${testMailbox}/messages`).send({ |
| 53 | + draft: true, |
| 54 | + headers: [{ key: 'List-Id', value: `${i}` }], |
| 55 | + to: [{ address: 'noreply@to.com' }, { address: 'to3@to.com' }], |
| 56 | + subject: `Test message ${i}`, |
| 57 | + text: `${i}Test message ${i}` |
| 58 | + }); |
| 59 | + |
| 60 | + initialMessages.push(messageCreationPromise); |
| 61 | + } |
| 62 | + await Promise.all(initialMessages); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should POST /users/:user/mailboxes/:mailbox/messages/:message/submit expect success / normal submit', async () => { |
| 66 | + const messageResponse = await server |
| 67 | + .post(`/users/${user}/mailboxes/${testMailbox}/messages`) |
| 68 | + .send({ |
| 69 | + draft: true, |
| 70 | + headers: [{ key: 'List-Id', value: '123' }], |
| 71 | + to: [{ address: 'noreply@to.com' }, { address: 'to2@to.com' }, { address: 'to3@to.com' }], |
| 72 | + attachments: [ |
| 73 | + { |
| 74 | + content: |
| 75 | + 'zZW1wZXIgcGxhY2VyYXQsIGZhdWNpYnVzIGluIG9kaW8uIERvbmVjIGxhY2luaWEgYXJjdSBhYyB2ZWxpdCBjb25kaW1lbnR1bSBsb2JvcnRpcy4gTWF1cmlzIGVnZXQgZGlnbmlzc2ltIGp1c3RvLiBNYWVjZW5hcyBzZWQgbGVvIHV0IHNlbSBpbXBlcmRpZXQgY29uZGltZW50dW0uIEN1cmFiaXR1ciB2ZW5lbmF0aXMsIG51bGxhIGV1IGNvbmR', |
| 76 | + contentType: 'text/plain' |
| 77 | + } |
| 78 | + ], |
| 79 | + from: { name: 'messagestestsuser@web.zone.test', address: 'messagestestsuser@web.zone.test' }, |
| 80 | + subject: 'test message', |
| 81 | + text: 'This is a test message with attachment' |
| 82 | + }) |
| 83 | + .expect(200); |
| 84 | + const message = messageResponse.body.message.id; |
| 85 | + |
| 86 | + const data = await server.post(`/users/${user}/mailboxes/${testMailbox}/messages/${message}/submit`).send(); |
| 87 | + |
| 88 | + const body = data.body; |
| 89 | + expect(body.success).to.be.true; |
| 90 | + expect(body.message).to.not.be.empty; |
| 91 | + expect(body.message.id).to.eq(1); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should POST /users/:user/mailboxes/:mailbox/messages/:message/submit expect success / encrypted mailbox', async () => { |
| 95 | + // set test mailbox as encrypted |
| 96 | + await server.put(`/users/${user}/mailboxes/${testMailbox}`).send({ encryptMessages: true }).expect(200); |
| 97 | + |
| 98 | + const messageResponse = await server |
| 99 | + .post(`/users/${user}/mailboxes/${testMailbox}/messages`) |
| 100 | + .send({ |
| 101 | + draft: false, |
| 102 | + headers: [{ key: 'List-Id', value: '123' }], |
| 103 | + to: [{ address: 'noreply@to.com' }, { address: 'to2@to.com' }, { address: 'to3@to.com' }], |
| 104 | + attachments: [ |
| 105 | + { |
| 106 | + content: |
| 107 | + 'zZW1wZXIgcGxhY2VyYXQsIGZhdWNpYnVzIGluIG9kaW8uIERvbmVjIGxhY2luaWEgYXJjdSBhYyB2ZWxpdCBjb25kaW1lbnR1bSBsb2JvcnRpcy4gTWF1cmlzIGVnZXQgZGlnbmlzc2ltIGp1c3RvLiBNYWVjZW5hcyBzZWQgbGVvIHV0IHNlbSBpbXBlcmRpZXQgY29uZGltZW50dW0uIEN1cmFiaXR1ciB2ZW5lbmF0aXMsIG51bGxhIGV1IGNvbmR', |
| 108 | + contentType: 'text/plain' |
| 109 | + } |
| 110 | + ], |
| 111 | + from: { name: 'messagestestsuser@web.zone.test', address: 'messagestestsuser@web.zone.test' }, |
| 112 | + subject: 'Encrypted test message', |
| 113 | + text: 'This is an encrypted test message' |
| 114 | + }) |
| 115 | + .expect(200); |
| 116 | + const message = messageResponse.body.message.id; |
| 117 | + |
| 118 | + const messageData = await server.get(`/users/${user}/mailboxes/${testMailbox}/messages/${message}`).send(); |
| 119 | + |
| 120 | + expect(messageData.body.encrypted).to.be.true; |
| 121 | + expect(messageData.body.contentType.value).to.eq('multipart/encrypted'); |
| 122 | + expect(messageData.body.text).to.be.empty; |
| 123 | + expect(messageData.body.html).to.not.exist; |
| 124 | + expect(messageData.body.attachments.length).to.be.eq(2); |
| 125 | + |
| 126 | + await server.put(`/users/${user}/mailboxes/${testMailbox}`).send({ encryptMessages: false }).expect(200); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should POST /users/:user/search expect success / pagination pages 1 -> 2 -> 3 -> 2 -> 1', async () => { |
| 130 | + const orderSearch = 'desc'; |
| 131 | + const from = 'messagestests'; // Partial match |
| 132 | + const limit = 10; |
| 133 | + |
| 134 | + const search = await server.get(`/users/${user}/search?order=${orderSearch}&mailbox=${testMailbox}&from=${from}&limit=${limit}`).send({}); |
| 135 | + |
| 136 | + const search2 = await server |
| 137 | + .get(`/users/${user}/search?next=${search.body.nextCursor}&order=${orderSearch}&mailbox=${testMailbox}&from=${from}&limit=${limit}`) |
| 138 | + .send({}); |
| 139 | + |
| 140 | + const search3 = await server |
| 141 | + .get(`/users/${user}/search?next=${search2.body.nextCursor}&order=${orderSearch}&mailbox=${testMailbox}&from=${from}&limit=${limit}`) |
| 142 | + .send({}); |
| 143 | + |
| 144 | + const search4 = await server |
| 145 | + .get(`/users/${user}/search?previous=${search3.body.previousCursor}&order=${orderSearch}&mailbox=${testMailbox}&from=${from}&limit=${limit}`) |
| 146 | + .send({}); |
| 147 | + |
| 148 | + expect(search4.body.results).to.deep.eq(search2.body.results); // Check if page 2 is equal to original page 2 after moving back from page 3 |
| 149 | + |
| 150 | + const search5 = await server |
| 151 | + .get(`/users/${user}/search?previous=${search4.body.previousCursor}&order=${orderSearch}&mailbox=${testMailbox}&from=${from}&limit=${limit}`) |
| 152 | + .send({}); // page 2 -> page 1 |
| 153 | + expect(search5.body.results).to.deep.eq(search.body.results); // Check if page 1 is equal to original page 1 after moving back from page 2 |
| 154 | + }); |
| 155 | + |
| 156 | + it('should GET /users/:user/mailboxes/:mailbox/messages expect success / pagination pages 1 -> 2 -> 3 -> 2 -> 1', async () => { |
| 157 | + const order = 'desc'; |
| 158 | + |
| 159 | + const res = await server.get(`/users/${user}/mailboxes/${testMailbox}/messages?order=${order}`).send({}); |
| 160 | + |
| 161 | + const res2 = await server.get(`/users/${user}/mailboxes/${testMailbox}/messages?next=${res.body.nextCursor}&order=${order}`).send({}); |
| 162 | + |
| 163 | + const res3 = await server.get(`/users/${user}/mailboxes/${testMailbox}/messages?next=${res2.body.nextCursor}&order=${order}`).send({}); |
| 164 | + |
| 165 | + const res4 = await server.get(`/users/${user}/mailboxes/${testMailbox}/messages?previous=${res3.body.previousCursor}&order=${order}`).send({}); |
| 166 | + |
| 167 | + expect(res4.body.results).to.deep.eq(res2.body.results); // Check if page 2 is equal to original page 2 after moving back from page 3 |
| 168 | + |
| 169 | + const res5 = await server.get(`/users/${user}/mailboxes/${testMailbox}/messages?previous=${res4.body.previousCursor}&page=100?order=${order}`).send({}); // page 2 -> page 1 |
| 170 | + expect(res5.body.results).to.deep.eq(res.body.results); // Check if page 1 is equal to original page 1 after moving back from page 2 |
| 171 | + }); |
| 172 | + |
| 173 | + it('should PUT /users/:user/mailboxes/:mailbox/messages expect success / move lots of messages to trash, should not timeout', async () => { |
| 174 | + const performanceMessages = []; |
| 175 | + // add even more messages |
| 176 | + const perfMessagesCount = 250; |
| 177 | + for (let i = 0; i < perfMessagesCount; i++) { |
| 178 | + const messageCreationPromise = server.post(`/users/${user}/mailboxes/${testMailbox}/messages`).send({ |
| 179 | + draft: true, |
| 180 | + headers: [{ key: 'List-Id', value: `${i}` }], |
| 181 | + to: [{ address: 'to4@to.com' }], |
| 182 | + subject: `Test message move to trash ${i}`, |
| 183 | + text: `${i} Test message move to trash ${i}` |
| 184 | + }); |
| 185 | + |
| 186 | + performanceMessages.push(messageCreationPromise); |
| 187 | + } |
| 188 | + await Promise.all(performanceMessages); |
| 189 | + |
| 190 | + // Move all messages to trash |
| 191 | + await server.put(`/users/${user}/mailboxes/${testMailbox}/messages`).send({ message: '1:*', moveTo: trashId }); |
| 192 | + }); |
| 193 | +}); |
0 commit comments