|
| 1 | +const CONFIG = require('config') |
| 2 | +const __ = CONFIG.universalPath |
| 3 | +const _ = __.require('lib', 'utils') |
| 4 | + |
| 5 | +require('should') |
| 6 | + |
| 7 | +const breq = require('bluereq') |
| 8 | +const get = (url) => breq.get(url).then(_.property('body')) |
| 9 | +const post = (url, body) => breq.post(url, body).then(_.property('body')) |
| 10 | +const put = (url, body) => breq.put(url, body).then(_.property('body')) |
| 11 | +const delete_ = (url) => breq.delete(url).then(_.property('body')) |
| 12 | + |
| 13 | +const endpoint = CONFIG.server.url() + '/media' |
| 14 | +const mediaNewURL = require('../fixtures/media-new-url.json') |
| 15 | +const mediaNewIPFS = require('../fixtures/media-new-ipfs.json') |
| 16 | +const mediaNewBLOB = '../fixtures/SDvision_MN00097-intermediate-DMs1p36833449a6size0_0005.jpg' |
| 17 | + |
| 18 | +describe('/media', function () { |
| 19 | + describe('POST url', function () { |
| 20 | + it('should return metadata with journal id, assetUrl', function (done) { |
| 21 | + post(endpoint, mediaNewURL) |
| 22 | + .then(function (body) { |
| 23 | + body.ok.should.equal(true) |
| 24 | + _.isUuid(body.id).should.equal(true) |
| 25 | + _.isNonEmptyString(body.assetUrl) |
| 26 | + done() |
| 27 | + }) |
| 28 | + }) |
| 29 | + it('should return a 400 on invalid doc', function (done) { |
| 30 | + const invalidNewDoc = { |
| 31 | + so: 'much', |
| 32 | + invalid: true |
| 33 | + } |
| 34 | + breq.post(endpoint, invalidNewDoc) |
| 35 | + .then(function (res) { |
| 36 | + res.statusCode.should.equal(400) |
| 37 | + done() |
| 38 | + }) |
| 39 | + }) |
| 40 | + }) |
| 41 | + describe('POST ipfs metadata', function () { |
| 42 | + it('should return metadata with journal id, assetUrl, ipfs hash and mimetype', function (done) { |
| 43 | + post(endpoint, mediaNewIPFS) |
| 44 | + .then(function (body) { |
| 45 | + body.ok.should.equal(true) |
| 46 | + _.isUuid(body.id).should.equal(true) |
| 47 | + body.mimetype.should.equal('image/jpeg') |
| 48 | + body.ipfs.should.equal('QmVXmy59f5QqKDGyhpHbRiGnba5SfHaooDPCWhd8Xtgwz2') |
| 49 | + _.isNonEmptyString(body.assetUrl).should.equal(true) |
| 50 | + done() |
| 51 | + }) |
| 52 | + }) |
| 53 | + it('should return a 400 on invalid doc', function (done) { |
| 54 | + const invalidNewDoc = { |
| 55 | + so: 'much', |
| 56 | + invalid: true |
| 57 | + } |
| 58 | + breq.post(endpoint, invalidNewDoc) |
| 59 | + .then(function (res) { |
| 60 | + res.statusCode.should.equal(400) |
| 61 | + done() |
| 62 | + }) |
| 63 | + }) |
| 64 | + }) |
| 65 | + describe('POST image', function () { |
| 66 | + it('should return metadata with journal id, assetUrl, ipfs hash and mimetype', function (done) { |
| 67 | + post(endpoint, mediaNewBLOB) |
| 68 | + .then(function (body) { |
| 69 | + body.ok.should.equal(true) |
| 70 | + _.isUuid(body.id).should.equal(true) |
| 71 | + body.mimetype.should.equal('image/jpeg') |
| 72 | + body.ipfs.should.equal('QmbLbqp41tVPVjQoVvAXKBehWsZ4k16mRu7m9wv3hLPSuK') |
| 73 | + _.isNonEmptyString(body.assetUrl).should.equal(true) |
| 74 | + done() |
| 75 | + }) |
| 76 | + }) |
| 77 | + it('should return a 400 on invalid doc', function (done) { |
| 78 | + const invalidNewDoc = { |
| 79 | + so: 'much', |
| 80 | + invalid: true |
| 81 | + } |
| 82 | + breq.post(endpoint, invalidNewDoc) |
| 83 | + .then(function (res) { |
| 84 | + res.statusCode.should.equal(400) |
| 85 | + done() |
| 86 | + }) |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + // TODO: update tests below to media case |
| 91 | + |
| 92 | + describe('GET id', function () { |
| 93 | + it('should return the same object via GET that is pushed previously via POST', function (done) { |
| 94 | + post(endpoint, mediaNewURL) |
| 95 | + .then(function (body1) { |
| 96 | + get(`${endpoint}/${body1.id}`) |
| 97 | + .then(function (body2) { |
| 98 | + body2.should.be.an.Object() |
| 99 | + body2.properties.should.be.an.Object() |
| 100 | + _.isUuid(body2._id).should.equal(true) |
| 101 | + body2._id.should.equal(body1.id) |
| 102 | + // copying the id to make comparison simpler |
| 103 | + mediaNewURL._id = body2._id |
| 104 | + const b = JSON.stringify(mediaNewURL) |
| 105 | + const a = JSON.stringify(body2) |
| 106 | + if (a !== b) { |
| 107 | + _.log('posted', a) |
| 108 | + _.log('returned', b) |
| 109 | + } |
| 110 | + a.should.equal(b) |
| 111 | + done() |
| 112 | + }) |
| 113 | + }) |
| 114 | + }) |
| 115 | + }) |
| 116 | + describe('UPDATE id', function () { |
| 117 | + it('should accept a correct object for update', function (done) { |
| 118 | + post(endpoint, mediaNewURL) |
| 119 | + .then(function (body1) { |
| 120 | + mediaNewURL.properties.name += ' - Version 2' |
| 121 | + put(`${endpoint}/${body1.id}`, mediaNewURL) |
| 122 | + .then(function (retVal) { |
| 123 | + retVal.ok.should.equal(true) |
| 124 | + done() |
| 125 | + }) |
| 126 | + }) |
| 127 | + }) |
| 128 | + it('should return a 400 on invalid doc', function (done) { |
| 129 | + const invalidNewDoc = { |
| 130 | + so: 'much', |
| 131 | + invalid: true |
| 132 | + } |
| 133 | + breq.post(endpoint, invalidNewDoc) |
| 134 | + .then(function (res) { |
| 135 | + res.statusCode.should.equal(400) |
| 136 | + done() |
| 137 | + }) |
| 138 | + }) |
| 139 | + it('should return the new version after update', function (done) { |
| 140 | + post(endpoint, mediaNewURL) |
| 141 | + .then(function (body1) { |
| 142 | + mediaNewURL.properties.name += ' - Version 2' |
| 143 | + put(`${endpoint}/${body1.id}`, mediaNewURL) |
| 144 | +// .then(_.Log('retval of update command')) |
| 145 | + .then(function (body2) { |
| 146 | + get(`${endpoint}/${body2.id}`) |
| 147 | +// .then(_.Log('retval of get command')) |
| 148 | + .then(function (body3) { |
| 149 | +// console.log(body3) |
| 150 | + body3.should.be.an.Object() |
| 151 | + body3.properties.should.be.an.Object() |
| 152 | + _.isUuid(body3._id).should.equal(true) |
| 153 | + body3._id.should.equal(body2.id) |
| 154 | + // copying the id to make comparison simpler |
| 155 | + mediaNewURL._id = body3._id |
| 156 | + const b = JSON.stringify(mediaNewURL) |
| 157 | + const a = JSON.stringify(body3) |
| 158 | + a.should.equal(b) |
| 159 | + done() |
| 160 | + }) |
| 161 | + }) |
| 162 | + }) |
| 163 | + }) |
| 164 | + }) |
| 165 | + describe('DELETE id', function () { |
| 166 | + it('should add the deleted flag to supplied UUID', function (done) { |
| 167 | + post(endpoint, mediaNewURL) |
| 168 | + .then(function (body1) { |
| 169 | + delete_(`${endpoint}/${body1.id}`) |
| 170 | + .then(function (deleteAnswer) { |
| 171 | + deleteAnswer.ok.should.equal(true) |
| 172 | + get(`${endpoint}/${deleteAnswer.id}`) |
| 173 | + .then(function (body2) { |
| 174 | + body2._deleted.should.equal(true) |
| 175 | + done() |
| 176 | + }) |
| 177 | + }) |
| 178 | + }) |
| 179 | + }) |
| 180 | + it('should return 208 if the UUID is already deleted', function (done) { |
| 181 | + post(endpoint, mediaNewURL) |
| 182 | + .then(function (body1) { |
| 183 | + delete_(`${endpoint}/${body1.id}`) |
| 184 | + .then(function (deleteAnswer1) { |
| 185 | + breq.delete(`${endpoint}/${deleteAnswer1.id}`) |
| 186 | + .then(function (deleteAnswer2) { |
| 187 | + deleteAnswer2.statusCode.should.equal(208) // "already reported", borrowed from WebDAV |
| 188 | + done() |
| 189 | + }) |
| 190 | + }) |
| 191 | + }) |
| 192 | + }) |
| 193 | + it('should return 404 if the UUID is not there', function (done) { |
| 194 | + breq.delete(`${endpoint}/3f99cbbccb02d48b595c369a00000000`) |
| 195 | + .then(function (deleteAnswer) { |
| 196 | + deleteAnswer.statusCode.should.equal(404) |
| 197 | + done() |
| 198 | + }) |
| 199 | + }) |
| 200 | + }) |
| 201 | +}) |
0 commit comments