|
1 | 1 | const assert = require('assert'); |
2 | 2 | const md5 = require('md5'); |
3 | 3 | const url = require('url'); |
| 4 | +const request = require('request-promise'); |
4 | 5 |
|
5 | 6 | // helpers |
6 | 7 | const { |
@@ -45,6 +46,7 @@ describe('upload suite', function suite() { |
45 | 46 | assert.ok(rsp.uploadId); |
46 | 47 | assert.ok(rsp.startedAt); |
47 | 48 | assert.ok(rsp.files); |
| 49 | + assert.ifError(rsp.public); |
48 | 50 | assert.equal(rsp.status, STATUS_PENDING); |
49 | 51 | assert.equal(rsp.parts, message.files.length); |
50 | 52 |
|
@@ -82,4 +84,66 @@ describe('upload suite', function suite() { |
82 | 84 | return null; |
83 | 85 | }); |
84 | 86 | }); |
| 87 | + |
| 88 | + it('initiates public upload and returns correct response format', function test() { |
| 89 | + const message = modelData.message; |
| 90 | + |
| 91 | + return this |
| 92 | + .send({ |
| 93 | + ...message, |
| 94 | + access: { |
| 95 | + setPublic: true, |
| 96 | + }, |
| 97 | + }, 45000) |
| 98 | + .reflect() |
| 99 | + .then(inspectPromise()) |
| 100 | + .then(rsp => { |
| 101 | + assert.equal(rsp.name, message.meta.name); |
| 102 | + assert.equal(rsp.owner, message.username); |
| 103 | + assert.ok(rsp.uploadId); |
| 104 | + assert.ok(rsp.startedAt); |
| 105 | + assert.ok(rsp.files); |
| 106 | + assert.ok(rsp.public); |
| 107 | + assert.equal(rsp.status, STATUS_PENDING); |
| 108 | + assert.equal(rsp.parts, message.files.length); |
| 109 | + |
| 110 | + // verify that location is present |
| 111 | + rsp.files.forEach(part => { |
| 112 | + assert.ok(part.location); |
| 113 | + |
| 114 | + // verify upoad link |
| 115 | + const location = url.parse(part.location, true); |
| 116 | + assert.equal(location.protocol, 'https:'); |
| 117 | + assert.equal(location.hostname, 'www.googleapis.com'); |
| 118 | + assert.equal(location.pathname, `/upload/storage/v1/b/${bucketName}/o`); |
| 119 | + assert.equal(location.query.name, part.filename); |
| 120 | + assert.equal(location.query.uploadType, 'resumable'); |
| 121 | + assert.ok(location.query.upload_id); |
| 122 | + |
| 123 | + // verify that filename contains multiple parts |
| 124 | + const [ownerHash, uploadId, filename] = part.filename.split('/'); |
| 125 | + assert.equal(md5(owner), ownerHash); |
| 126 | + assert.equal(rsp.uploadId, uploadId); |
| 127 | + assert.ok(filename); |
| 128 | + }); |
| 129 | + |
| 130 | + // save for the next |
| 131 | + this.response = rsp; |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + it('upload is possible based on the returned data: public', function test() { |
| 136 | + return uploadFiles(modelData, this.response) |
| 137 | + .reflect() |
| 138 | + .then(inspectPromise()) |
| 139 | + .map(resp => { |
| 140 | + assert.equal(resp.statusCode, 200); |
| 141 | + return null; |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + it('able to download public files right away', function test() { |
| 146 | + const file = this.response.files[0]; |
| 147 | + return request.get(`https://www.googleapis.com/${bucketName}/${file.filename}`); |
| 148 | + }); |
85 | 149 | }); |
0 commit comments