|
| 1 | +import { |
| 2 | + clear as clearDatabase, |
| 3 | + expect, |
| 4 | + faker, |
| 5 | +} from '@lykmapipo/mongoose-test-helpers'; |
| 6 | +import { |
| 7 | + clear as clearHttp, |
| 8 | + testRouter, |
| 9 | +} from '@lykmapipo/express-test-helpers'; |
| 10 | +import fileRouter from '../src/file.http.router'; |
| 11 | + |
| 12 | +const properties = [ |
| 13 | + 'length', |
| 14 | + 'chunkSize', |
| 15 | + 'uploadDate', |
| 16 | + 'md5', |
| 17 | + 'filename', |
| 18 | + 'contentType', |
| 19 | + 'aliases', |
| 20 | + 'metadata', |
| 21 | + '_id', |
| 22 | + '__v', |
| 23 | + 'id', |
| 24 | + 'createdAt', |
| 25 | + 'updatedAt', |
| 26 | +]; |
| 27 | + |
| 28 | +const options = { |
| 29 | + pathSingle: '/files/:bucket/:id', |
| 30 | + pathList: '/files/:bucket', |
| 31 | + pathSchema: '/files/:bucket/schema', |
| 32 | + pathUpload: '/files/:bucket', |
| 33 | + pathDownload: '/files/:bucket/:id/download', |
| 34 | + pathStream: '/files/:bucket/:id/chunks', |
| 35 | +}; |
| 36 | + |
| 37 | +describe('Document HTTP API', () => { |
| 38 | + before(() => clearHttp()); |
| 39 | + before(done => clearDatabase(done)); |
| 40 | + |
| 41 | + let file; |
| 42 | + |
| 43 | + it('should handle HTTP POST on /files/:bucket', done => { |
| 44 | + const upload = { |
| 45 | + bucket: 'documents', |
| 46 | + aliases: faker.random.word(), |
| 47 | + attach: { document: `${__dirname}/fixtures/document.doc` }, |
| 48 | + }; |
| 49 | + const { testUpload } = testRouter(options, fileRouter); |
| 50 | + testUpload(upload) |
| 51 | + .expect('Content-Type', /json/) |
| 52 | + .expect(201, (error, { body }) => { |
| 53 | + expect(error).to.not.exist; |
| 54 | + expect(body).to.exist; |
| 55 | + expect(body._id).to.exist; |
| 56 | + expect(body.filename).to.exist; |
| 57 | + expect(body.contentType).to.exist; |
| 58 | + expect(body.length).to.exist; |
| 59 | + expect(body.chunkSize).to.exist; |
| 60 | + expect(body.uploadDate).to.exist; |
| 61 | + expect(body.md5).to.exist; |
| 62 | + expect(body.stream).to.exist; |
| 63 | + expect(body.download).to.exist; |
| 64 | + file = body; |
| 65 | + done(error, body); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should throw when HTTP POST on /files/:bucket with no file', done => { |
| 70 | + const upload = { |
| 71 | + bucket: 'documents', |
| 72 | + aliases: faker.random.word(), |
| 73 | + }; |
| 74 | + const { testUpload } = testRouter(options, fileRouter); |
| 75 | + testUpload(upload) |
| 76 | + .expect('Content-Type', /json/) |
| 77 | + .expect(400, (error, { body }) => { |
| 78 | + expect(error).to.not.exist; |
| 79 | + expect(body.code).to.be.equal(400); |
| 80 | + expect(body.status).to.be.equal(400); |
| 81 | + expect(body.name).to.be.equal('ValidationError'); |
| 82 | + expect(body.message).to.be.equal('Validation failed'); |
| 83 | + expect(body.description).to.be.equal('Validation failed'); |
| 84 | + expect(body.errors.document.message).to.be.equal( |
| 85 | + 'Path `document` is required.' |
| 86 | + ); |
| 87 | + expect(body.errors.document.name).to.be.equal('ValidatorError'); |
| 88 | + expect(body.errors.document.type).to.be.equal('required'); |
| 89 | + expect(body.errors.document.kind).to.be.equal('required'); |
| 90 | + expect(body.errors.document.path).to.be.equal('document'); |
| 91 | + done(error, body); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should handle HTTP GET on /files/:bucket', done => { |
| 96 | + const { testGet } = testRouter(options, fileRouter); |
| 97 | + const params = { bucket: 'documents' }; |
| 98 | + testGet(params) |
| 99 | + .expect('Content-Type', /json/) |
| 100 | + .expect(200, (error, { body }) => { |
| 101 | + expect(error).to.not.exist; |
| 102 | + expect(body).to.exist; |
| 103 | + expect(body.data).to.exist; |
| 104 | + expect(body.data).to.have.length.at.least(1); |
| 105 | + expect(body.total).to.exist; |
| 106 | + expect(body.total).to.be.at.least(1); |
| 107 | + expect(body.limit).to.exist; |
| 108 | + expect(body.limit).to.be.equal(10); |
| 109 | + expect(body.skip).to.exist; |
| 110 | + expect(body.skip).to.be.equal(0); |
| 111 | + expect(body.page).to.exist; |
| 112 | + expect(body.page).to.be.equal(1); |
| 113 | + expect(body.pages).to.exist; |
| 114 | + expect(body.pages).to.be.at.least(1); |
| 115 | + expect(body.lastModified).to.exist; |
| 116 | + expect(body.hasMore).to.exist; |
| 117 | + done(error, body); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should handle HTTP GET on /files/:bucket/schema', done => { |
| 122 | + const { testGetSchema } = testRouter(options, fileRouter); |
| 123 | + testGetSchema({ bucket: 'documents' }).expect(200, (error, { body }) => { |
| 124 | + expect(error).to.not.exist; |
| 125 | + expect(body).to.exist; |
| 126 | + expect(body).to.have.all.keys('title', 'type', 'properties'); |
| 127 | + expect(body.properties).to.have.any.keys(...properties); |
| 128 | + done(error, body); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should handle HTTP GET on /files/:bucket/:id', done => { |
| 133 | + const { testGet } = testRouter(options, fileRouter); |
| 134 | + const params = { bucket: 'documents', id: file._id }; |
| 135 | + testGet(params) |
| 136 | + .expect('Content-Type', /json/) |
| 137 | + .expect(200, (error, { body }) => { |
| 138 | + expect(error).to.not.exist; |
| 139 | + expect(body).to.exist; |
| 140 | + expect(body._id).to.exist.and.be.eql(file._id); |
| 141 | + expect(body.filename).to.exist.and.be.eql(file.filename); |
| 142 | + expect(body.contentType).to.exist.and.be.eql(file.contentType); |
| 143 | + expect(body.length).to.exist.and.be.eql(file.length); |
| 144 | + expect(body.chunkSize).to.exist.and.be.eql(file.chunkSize); |
| 145 | + expect(body.uploadDate).to.exist.and.be.eql(file.uploadDate); |
| 146 | + expect(body.md5).to.exist.and.be.eql(file.md5); |
| 147 | + done(error, body); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should handle HTTP GET on /files/:bucket/:id/chunks', done => { |
| 152 | + const { testStream } = testRouter(options, fileRouter); |
| 153 | + const params = { bucket: 'documents', id: file._id }; |
| 154 | + testStream(params) |
| 155 | + .expect('Content-Type', 'application/msword') |
| 156 | + .expect(200, (error, response) => { |
| 157 | + expect(error).to.not.exist; |
| 158 | + expect(response).to.exist; |
| 159 | + done(error, response); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should handle HTTP GET on /files/:bucket/:id/download', done => { |
| 164 | + const { testDownload } = testRouter(options, fileRouter); |
| 165 | + const params = { bucket: 'documents', id: file._id }; |
| 166 | + testDownload(params) |
| 167 | + .expect('Content-Type', 'application/msword') |
| 168 | + .expect('Content-Disposition', 'attachment; filename="document.doc"') |
| 169 | + .expect(200, (error, response) => { |
| 170 | + expect(error).to.not.exist; |
| 171 | + expect(response).to.exist; |
| 172 | + done(error, response); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + it('should handle HTTP PATCH on /files/:bucket/:id', done => { |
| 177 | + const { testPatch } = testRouter(options, fileRouter); |
| 178 | + const updates = { metadata: { owner: faker.name.findName() } }; |
| 179 | + const params = { bucket: 'documents', id: file._id }; |
| 180 | + testPatch(params, updates) |
| 181 | + .expect('Content-Type', /json/) |
| 182 | + .expect(200, (error, { body }) => { |
| 183 | + expect(error).to.not.exist; |
| 184 | + expect(body).to.exist; |
| 185 | + expect(body._id).to.exist.and.be.eql(file._id); |
| 186 | + expect(body.filename).to.exist.and.be.eql(file.filename); |
| 187 | + expect(body.contentType).to.exist.and.be.eql(file.contentType); |
| 188 | + expect(body.length).to.exist.and.be.eql(file.length); |
| 189 | + expect(body.chunkSize).to.exist.and.be.eql(file.chunkSize); |
| 190 | + expect(body.uploadDate).to.exist.and.be.eql(file.uploadDate); |
| 191 | + expect(body.md5).to.exist.and.be.eql(file.md5); |
| 192 | + expect(body.metadata).to.exist.and.be.eql(updates.metadata); |
| 193 | + file = body; |
| 194 | + done(error, body); |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + it('should handle HTTP PUT on /files/:bucket/:id', done => { |
| 199 | + const { testPut } = testRouter(options, fileRouter); |
| 200 | + const updates = { metadata: { owner: faker.name.findName() } }; |
| 201 | + const params = { bucket: 'documents', id: file._id }; |
| 202 | + testPut(params, updates) |
| 203 | + .expect('Content-Type', /json/) |
| 204 | + .expect(200, (error, { body }) => { |
| 205 | + expect(error).to.not.exist; |
| 206 | + expect(body).to.exist; |
| 207 | + expect(body._id).to.exist.and.be.eql(file._id); |
| 208 | + expect(body.filename).to.exist.and.be.eql(file.filename); |
| 209 | + expect(body.contentType).to.exist.and.be.eql(file.contentType); |
| 210 | + expect(body.length).to.exist.and.be.eql(file.length); |
| 211 | + expect(body.chunkSize).to.exist.and.be.eql(file.chunkSize); |
| 212 | + expect(body.uploadDate).to.exist.and.be.eql(file.uploadDate); |
| 213 | + expect(body.md5).to.exist.and.be.eql(file.md5); |
| 214 | + expect(body.metadata).to.exist.and.be.eql(updates.metadata); |
| 215 | + file = body; |
| 216 | + done(error, body); |
| 217 | + }); |
| 218 | + }); |
| 219 | + |
| 220 | + it('should handle HTTP DELETE on /files/:bucket/:id', done => { |
| 221 | + const { testDelete } = testRouter(options, fileRouter); |
| 222 | + const params = { bucket: 'documents', id: file._id }; |
| 223 | + testDelete(params) |
| 224 | + .expect('Content-Type', /json/) |
| 225 | + .expect(200, (error, { body }) => { |
| 226 | + expect(error).to.not.exist; |
| 227 | + expect(body).to.exist; |
| 228 | + expect(body._id).to.exist.and.be.eql(file._id); |
| 229 | + expect(body.filename).to.exist.and.be.eql(file.filename); |
| 230 | + expect(body.contentType).to.exist.and.be.eql(file.contentType); |
| 231 | + expect(body.length).to.exist.and.be.eql(file.length); |
| 232 | + expect(body.chunkSize).to.exist.and.be.eql(file.chunkSize); |
| 233 | + expect(body.uploadDate).to.exist.and.be.eql(file.uploadDate); |
| 234 | + expect(body.md5).to.exist.and.be.eql(file.md5); |
| 235 | + done(error, body); |
| 236 | + }); |
| 237 | + }); |
| 238 | + |
| 239 | + after(() => clearHttp()); |
| 240 | + after(done => clearDatabase(done)); |
| 241 | +}); |
0 commit comments