Skip to content

Commit 621f751

Browse files
fix: add extra restrictions to non-cappasity uploads (#42)
* fix: add extra restrictions to non-cappasity uploads * Add tests for cappasity-upload-pre hook * delay before sync in tests
1 parent b07b32c commit 621f751

4 files changed

Lines changed: 185 additions & 12 deletions

File tree

src/custom/cappasity-upload-pre.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const Promise = require('bluebird');
33
const assert = require('assert');
44
const isCappasityUpload = require('../utils/isCappasityUpload');
55

6-
module.exports = function extractMetadata({ files, meta, temp }) {
6+
module.exports = function extractMetadata({ files, meta, temp, unlisted, access }) {
77
let sourceSHA;
88

99
return Promise
@@ -27,19 +27,31 @@ module.exports = function extractMetadata({ files, meta, temp }) {
2727
}
2828
});
2929

30-
if (isCappasityUpload(Object.keys(fileTypes))) {
31-
// assert constraints
32-
if (differentFileTypes === 1) {
30+
const cappasityUpload = isCappasityUpload(Object.keys(fileTypes));
31+
32+
// assert constraints
33+
if (differentFileTypes === 1) {
34+
if (cappasityUpload) {
3335
assert.equal(fileTypes['c-preview'], 1, 'must contain exactly one preview');
34-
} else {
35-
// must always be true if it's not a simple preview upload
36-
assert.equal(fileTypes['c-bin'], 1, 'must contain exactly one binary upload');
37-
meta.sourceSHA = sourceSHA;
3836
}
39-
}
4037

41-
if (meta.export && !temp) {
42-
throw new Errors.HttpStatusError(412, 'temp must be set to true');
38+
if (!unlisted) {
39+
throw new Errors.HttpStatusError(412, 'following upload must be unlisted');
40+
}
41+
42+
if (!access.setPublic) {
43+
throw new Errors.HttpStatusError(412, 'following upload must be public');
44+
}
45+
} else if (cappasityUpload) {
46+
// must always be true if it's not a simple preview upload
47+
assert.equal(fileTypes['c-bin'], 1, 'must contain exactly one binary upload');
48+
meta.sourceSHA = sourceSHA;
49+
50+
if (meta.export && !temp) {
51+
throw new Errors.HttpStatusError(412, 'temp must be set to true');
52+
}
53+
} else {
54+
throw new Errors.HttpStatusError(400, 'should be either a cappasity model or a single image');
4355
}
4456
});
4557
};

test/docker.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ COMPOSE=$(which docker-compose)
1010
MOCHA=$BIN/_mocha
1111
COVER="$BIN/isparta cover"
1212
NODE=$BIN/babel-node
13-
TESTS=${TESTS:-test/suites/*.js}
13+
TESTS=${TESTS:-test/suites/**/*.js test/suites/*.js}
1414
COMPOSE_VER=${COMPOSE_VER:-1.7.1}
1515
COMPOSE="docker-compose -f $DC"
1616

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
const assert = require('assert');
2+
const hook = require('../../../src/custom/cappasity-upload-pre');
3+
const reject = require('lodash/reject');
4+
const { inspectPromise } = require('../../helpers/utils');
5+
6+
describe('cappasity-upload-pre hook test suite', function suite() {
7+
describe('validate model files', function modelSuite() {
8+
const data = {
9+
files: [{
10+
type: 'c-bin',
11+
}, {
12+
type: 'c-texture',
13+
}, {
14+
type: 'c-archive',
15+
}],
16+
meta: {},
17+
};
18+
19+
it('should pass', function test() {
20+
return hook(data)
21+
.reflect()
22+
.then(inspectPromise());
23+
});
24+
25+
it('should fail due to no c-bin files passed', function test() {
26+
const payload = {
27+
...data,
28+
files: reject(data.files, { type: 'c-bin' }),
29+
};
30+
31+
return hook(payload)
32+
.reflect()
33+
.then(inspectPromise(false));
34+
});
35+
36+
it('should fail due to no temp flag passed along with export flag', function test() {
37+
const payload = {
38+
...data,
39+
meta: {
40+
export: true,
41+
},
42+
};
43+
44+
return hook(payload)
45+
.reflect()
46+
.then(inspectPromise(false));
47+
});
48+
49+
it('should fail when several c-bin files are passed', function test() {
50+
const payload = {
51+
...data,
52+
files: [{
53+
type: 'c-bin',
54+
}, {
55+
type: 'c-bin',
56+
}],
57+
};
58+
59+
return hook(payload)
60+
.reflect()
61+
.then(inspectPromise(false));
62+
});
63+
64+
it('should pass when both flags `export` and `temp` are passed', function test() {
65+
const payload = {
66+
...data,
67+
export: true,
68+
temp: true,
69+
};
70+
71+
return hook(payload)
72+
.reflect()
73+
.then(inspectPromise());
74+
});
75+
});
76+
77+
describe('validate non-model files', function nonModelSuite() {
78+
const data = {
79+
files: [{
80+
type: 'c-preview',
81+
}],
82+
access: {
83+
setPublic: true,
84+
},
85+
unlisted: true,
86+
};
87+
88+
it('should pass for cappasity preview', function test() {
89+
return hook(data)
90+
.reflect()
91+
.then(inspectPromise());
92+
});
93+
94+
it('should pass for background image', function test() {
95+
const payload = {
96+
...data,
97+
files: [{
98+
type: 'background',
99+
}],
100+
};
101+
102+
return hook(payload)
103+
.reflect()
104+
.then(inspectPromise());
105+
});
106+
107+
it('should fail when passed a single non-preview, non-background file', function test() {
108+
const payload = {
109+
...data,
110+
files: [{
111+
type: 'c-bin',
112+
}],
113+
};
114+
115+
return hook(payload)
116+
.reflect()
117+
.then(inspectPromise(false));
118+
});
119+
120+
it('should fail when file is private', function test() {
121+
const payload = {
122+
...data,
123+
access: {
124+
setPublic: false,
125+
},
126+
};
127+
128+
return hook(payload)
129+
.reflect()
130+
.then(inspectPromise(false));
131+
});
132+
133+
it('should fail when file is listed', function test() {
134+
const payload = {
135+
...data,
136+
unlisted: false,
137+
};
138+
139+
return hook(payload)
140+
.reflect()
141+
.then(inspectPromise(false));
142+
});
143+
144+
it('should fail when several non-model files are passed', function test() {
145+
const payload = {
146+
...data,
147+
files: [{
148+
type: 'background',
149+
}, {
150+
type: 'c-preview',
151+
}],
152+
};
153+
154+
return hook(payload)
155+
.reflect()
156+
.then(inspectPromise(false));
157+
});
158+
});
159+
});

test/suites/sync.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ describe('finish upload suite', function suite() {
2525
// tear-down
2626
after('stop service', stopService);
2727

28+
it('...wait while upload completes', () => Promise.delay(10000));
29+
2830
it('runs sync service', function test() {
2931
return this.send({}, 15000);
3032
});

0 commit comments

Comments
 (0)