Skip to content

Commit

Permalink
fix #210 addPackageAndDist package version detect bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Feb 25, 2014
1 parent 7b2cbd6 commit c9e5133
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
13 changes: 7 additions & 6 deletions controllers/registry/module.js
Expand Up @@ -489,16 +489,17 @@ exports.addPackageAndDist = function (req, res, next) {
var pkg = req.body;
var username = req.session.name;
var name = req.params.name;
var filename = Object.keys(pkg._attachments)[0];
var attachment = pkg._attachments[filename];
var version = filename.match(/\-([\.\d\w\_]+?)\.tgz$/);
if (!version) {
var filename = Object.keys(pkg._attachments || {})[0];
var version = Object.keys(pkg.versions || {})[0];

if (!version || !filename) {
return res.json(403, {
error: 'version_error',
reason: filename + ' version not found',
reason: 'filename or version not found, filename: ' + filename + ', version: ' + version,
});
}
version = version[1];

var attachment = pkg._attachments[filename];
var versionPackage = pkg.versions[version];
versionPackage._publish_on_cnpm = true;
var distTags = pkg['dist-tags'] || {};
Expand Down
54 changes: 54 additions & 0 deletions test/controllers/registry/module.test.js
Expand Up @@ -360,6 +360,60 @@ describe('controllers/registry/module.test.js', function () {
done();
});
});

it('should publish with tgz base64, addPackageAndDist()', function (done) {
var pkg = require(path.join(fixtures, 'package_and_tgz.json'));
// delete first
request(app)
.del('/' + pkg.name + '/-rev/1')
.set('authorization', baseauth)
.expect({ok: true})
.expect(200, function (err, res) {
should.not.exist(err);

request(app)
.put('/' + pkg.name)
.set('authorization', baseauth)
.send(pkg)
.expect(201, function (err, res) {
should.not.exist(err);
res.body.should.have.keys('ok', 'rev');
res.body.ok.should.equal(true);

// upload again should 409
request(app)
.put('/' + pkg.name)
.set('authorization', baseauth)
.send(pkg)
.expect(409, function (err, res) {
should.not.exist(err);
res.body.should.eql({
error: 'conflict',
reason: 'Document update conflict.'
});
done();
});

});
});
});

it('should version_error when versions missing', function (done) {
var pkg = require(path.join(fixtures, 'package_and_tgz.json'));
delete pkg.versions;
request(app)
.put('/' + pkg.name)
.set('authorization', baseauth)
.send(pkg)
.expect(403, function (err, res) {
should.not.exist(err);
res.body.should.eql({
error: 'version_error',
reason: 'filename or version not found, filename: mk2testmodule-0.0.1.tgz, version: undefined'
});
done();
});
});
});

describe('GET /-/all', function () {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/package_and_tgz.json
@@ -0,0 +1 @@
{"_id":"mk2testmodule","name":"mk2testmodule","description":"","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"mk2testmodule","version":"0.0.1","description":"","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"author":"","license":"ISC","readme":"ERROR: No README data found!","_id":"mk2testmodule@0.0.1","dist":{"shasum":"fa475605f88bab9b1127833633ca3ae0a477224c","tarball":"http://127.0.0.1:7001/mk2testmodule/-/mk2testmodule-0.0.1.tgz"},"_from":".","_npmVersion":"1.4.3","_npmUser":{"name":"fengmk2","email":"fengmk2@gmail.com"},"maintainers":[{"name":"fengmk2","email":"fengmk2@gmail.com"}]}},"readme":"ERROR: No README data found!","maintainers":[{"name":"fengmk2","email":"fengmk2@gmail.com"}],"_attachments":{"mk2testmodule-0.0.1.tgz":{"content_type":"application/octet-stream","data":"H4sIAAAAAAAAA+2SsWrDMBCGPfspDg2ZinOyEgeylg6Zu2YR8rVRHEtGkkOg5N0jWaFdujVQAv6W4/7/dHcSGqTq5Ccthxyro7emeDCI2KxWkOKmaaaIdc4TouZQ8FqgwI3AdVMgF8ijho9e5DdGH6SLq/y1T74LfMcn4asEYEb2xLbA+q4O5ENv2/FE7CVZZ3JeW5NcrLDiWW3JK6eHcHey2Es9Zdq0dIkfKau50EcjjYpCmpDKSB0s7Nmbc9ZtwVhIBviBlP7Q1O4ZLBZAFx2As3jyOnWTYzhY9zPzpBUZPy2/e39l5bX87wedmZmZeRJuheTX2wAIAAA=","length":251}}}

0 comments on commit c9e5133

Please sign in to comment.