Skip to content

Commit

Permalink
Merge 9e00d18 into 0651cd3
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdesjardins committed Jul 18, 2016
2 parents 0651cd3 + 9e00d18 commit 3250725
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ deploy({
// the version to publish
version: '1.0.0',

// a Buffer or string containing a .zip (WebExtensions) or .xpi (Add-on SDK)
src: fs.readFileSync('path/to/zipped/extension.zip'),
// a ReadStream containing a .zip (WebExtensions) or .xpi (Add-on SDK)
src: fs.createReadStream('path/to/zipped/extension.zip'),
}).then(function() {
// success!
}, function(err) {
// failure :(
// errors are sanitized, so your tokens will not be leaked
});
```
24 changes: 7 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ module.exports = function deploy(options) {
var fieldError = REQUIRED_FIELDS.reduce(function(err, field) {
if (err) return err;
if (!options[field]) {
return new Error('Missing required field: ' + field);
return 'Missing required field: ' + field;
}
}, null);

if (fieldError) {
return Promise.reject(fieldError);
return Promise.reject(new Error(fieldError));
}

var jwtIssuer = options.issuer;
Expand All @@ -40,28 +40,18 @@ module.exports = function deploy(options) {
// SuperAgent's "promise" support doesn't provide a way to get the status of a failed request
return new Promise(function(resolve, reject) {
request
.put('https://addons.mozilla.org/api/v3/addons/' + extensionId + '/versions/' + extensionVersion)
.put('https://addons.mozilla.org/api/v3/addons/' + extensionId + '/versions/' + extensionVersion + '/')
.set('Authorization', 'JWT ' + token)
.type('multipart/form-data')
.send(srcFile)
.field('upload', srcFile)
.end(function(err, response) {
if (err) {
var msg;
switch (response.status) {
case '400':
msg = '400 Bad Request: ' + response.body.error;
break;
case '401':
msg = '401 Unauthorized: authentication failed';
break;
case '403':
msg = '403 Forbidden: you do not have permission to modify this addon';
break;
case '409':
msg = '409 Conflict: version ' + extensionVersion + ' already exists';
case 401:
msg = '401 Unauthorized: ' + response.body.detail;
break;
default:
msg = 'Deployment failed, status: ' + response.status;
msg = 'Status ' + response.status + ': ' + response.body.error;
break;
}
reject(new Error(msg));
Expand Down
16 changes: 7 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import deploy from '../index.js';
test.beforeEach(t => {
t.context.requests = [];
t.context.mock = superagentMock(superagent, [{
pattern: '^https://addons.mozilla.org/api/v3/addons/(.+)/versions/(.+)$',
pattern: '^https://addons.mozilla.org/api/v3/addons/(.+)/versions/(.+)/$',
fixtures(match, params, headers) {
t.context.requests.push({ match, params, headers });
if (t.context.publishFail) {
throw new Error(t.context.publishFail);
throw { message: t.context.publishFail };
}
return t.context.publishResponse;
},
Expand Down Expand Up @@ -59,7 +59,7 @@ test.serial('failing upload, unknown status', async t => {

await t.throws(
deploy({ issuer: 'q', secret: 'q', id: 'q', version: 'q', src: 'q' }),
'Deployment failed, status: fail_message'
'Status fail_message: undefined'
);
});

Expand All @@ -68,7 +68,7 @@ test.serial('failing upload, 400', async t => {

await t.throws(
deploy({ issuer: 'q', secret: 'q', id: 'q', version: 'q', src: 'q' }),
e => (/^400 Bad Request: /).test(e.message)
'Status 400: undefined'
);
});

Expand All @@ -77,7 +77,7 @@ test.serial('failing upload, 401', async t => {

await t.throws(
deploy({ issuer: 'q', secret: 'q', id: 'q', version: 'q', src: 'q' }),
'401 Unauthorized: authentication failed'
'401 Unauthorized: undefined'
);
});

Expand All @@ -86,7 +86,7 @@ test.serial('failing upload, 403', async t => {

await t.throws(
deploy({ issuer: 'q', secret: 'q', id: 'q', version: 'q', src: 'q' }),
'403 Forbidden: you do not have permission to modify this addon'
'Status 403: undefined'
);
});

Expand All @@ -95,7 +95,7 @@ test.serial('failing upload, 409', async t => {

await t.throws(
deploy({ issuer: 'q', secret: 'q', id: 'q', version: 'myVersion', src: 'q' }),
'409 Conflict: version myVersion already exists'
'Status 409: undefined'
);
});

Expand All @@ -109,8 +109,6 @@ test.serial('full deploy', async t => {
t.is(publishReq.match[1], 'someId');
t.is(publishReq.match[2], 'someVersion');
t.regex(publishReq.headers['Authorization'], /^JWT /);
t.is(publishReq.headers['Content-Type'], 'multipart/form-data');
t.is(publishReq.params, 'someSrc');

// throws if invalid
jwt.verify(publishReq.headers['Authorization'].slice(4), 'someSecret', {
Expand Down

0 comments on commit 3250725

Please sign in to comment.