|
| 1 | +const Parse = require("parse/node"); |
| 2 | +const request = require('request'); |
| 3 | +const AdmZip = require('adm-zip'); |
| 4 | + |
| 5 | +describe('Export router', () => { |
| 6 | + |
| 7 | + const headers = { |
| 8 | + 'Content-Type': 'application/json', |
| 9 | + 'X-Parse-Application-Id': 'test', |
| 10 | + 'X-Parse-Master-Key': 'test' |
| 11 | + }; |
| 12 | + |
| 13 | + const createRecords = (itemCount) => { |
| 14 | + const ExportTest = Parse.Object.extend("ExportTest"); |
| 15 | + |
| 16 | + const items = new Array(itemCount).fill().map((item, index) => { |
| 17 | + |
| 18 | + const exportTest = new ExportTest(); |
| 19 | + |
| 20 | + exportTest.set('field1', `value1-${index}`); |
| 21 | + exportTest.set('field2', `value2-${index}`); |
| 22 | + |
| 23 | + return exportTest; |
| 24 | + }); |
| 25 | + |
| 26 | + |
| 27 | + return Parse.Object.saveAll(items); |
| 28 | + }; |
| 29 | + |
| 30 | + it_exclude_dbs(['postgres'])('should create export progress', (done) => { |
| 31 | + |
| 32 | + reconfigureServer({ |
| 33 | + emailAdapter : { |
| 34 | + sendMail : () => { |
| 35 | + done(); |
| 36 | + } |
| 37 | + } |
| 38 | + }) |
| 39 | + .then(() => { |
| 40 | + return createRecords(3000); |
| 41 | + }) |
| 42 | + .then(() => { |
| 43 | + request.put( |
| 44 | + { |
| 45 | + headers: headers, |
| 46 | + url: 'http://localhost:8378/1/export_data', |
| 47 | + body: JSON.stringify({ |
| 48 | + name: 'ExportTest', |
| 49 | + feedbackEmail: '[email protected]' |
| 50 | + }) |
| 51 | + }, |
| 52 | + () => { |
| 53 | + |
| 54 | + request.get( |
| 55 | + { |
| 56 | + headers: headers, |
| 57 | + url: 'http://localhost:8378/1/export_progress' |
| 58 | + }, |
| 59 | + (err, response, body) => { |
| 60 | + |
| 61 | + const progress = JSON.parse(body); |
| 62 | + |
| 63 | + expect(progress instanceof Array).toBe(true); |
| 64 | + expect(progress.length).toBe(1); |
| 65 | + |
| 66 | + if (progress.length) { |
| 67 | + expect(progress[0].id).toBe('ExportTest'); |
| 68 | + } |
| 69 | + |
| 70 | + }); |
| 71 | + } |
| 72 | + ); |
| 73 | + } |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it_exclude_dbs(['postgres'])('send success export mail', (done) => { |
| 78 | + |
| 79 | + let results = []; |
| 80 | + |
| 81 | + const emailAdapter = { |
| 82 | + sendMail: ({ link, to, subject}) => { |
| 83 | + |
| 84 | + expect(to).toEqual('[email protected]'); |
| 85 | + expect(subject).toEqual('Export completed'); |
| 86 | + |
| 87 | + request.get({ url: link, encoding: null }, function(err, res, zipFile) { |
| 88 | + |
| 89 | + if(err) throw err; |
| 90 | + |
| 91 | + const zip = new AdmZip(zipFile); |
| 92 | + const zipEntries = zip.getEntries(); |
| 93 | + |
| 94 | + expect(zipEntries.length).toEqual(1); |
| 95 | + |
| 96 | + const entry = zipEntries.pop(); |
| 97 | + const text = entry.getData().toString('utf8'); |
| 98 | + const resultsToCompare = JSON.parse(text); |
| 99 | + |
| 100 | + expect(results.length).toEqual(resultsToCompare.length); |
| 101 | + |
| 102 | + done(); |
| 103 | + }); |
| 104 | + } |
| 105 | + } |
| 106 | + reconfigureServer({ |
| 107 | + emailAdapter: emailAdapter |
| 108 | + }) |
| 109 | + .then(() => { |
| 110 | + return createRecords(2176); |
| 111 | + }) |
| 112 | + .then(() => { |
| 113 | + request.get( |
| 114 | + { |
| 115 | + headers: headers, |
| 116 | + url: 'http://localhost:8378/1/classes/ExportTest', |
| 117 | + }, |
| 118 | + (err, response, body) => { |
| 119 | + results = JSON.parse(body); |
| 120 | + |
| 121 | + request.put( |
| 122 | + { |
| 123 | + headers: headers, |
| 124 | + url: 'http://localhost:8378/1/export_data', |
| 125 | + body: JSON.stringify({ |
| 126 | + name: 'ExportTest', |
| 127 | + feedbackEmail: '[email protected]' |
| 128 | + }) |
| 129 | + }, |
| 130 | + (err, response, body) => { |
| 131 | + expect(err).toBe(null); |
| 132 | + expect(body).toEqual('"We are exporting your data. You will be notified by e-mail once it is completed."'); |
| 133 | + } |
| 134 | + ); |
| 135 | + } |
| 136 | + ); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments