Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

add put /:name/:tag, close #332 #333

Merged
merged 2 commits into from
Apr 18, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions controllers/registry/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
var debug = require('debug')('cnpmjs.org:controllers:registry:module');
var path = require('path');
var fs = require('fs');
var util = require('util');
var crypto = require('crypto');
var utility = require('utility');
var coRead = require('co-read');
Expand Down Expand Up @@ -938,3 +939,47 @@ exports.listAllModuleNames = function *() {
return m.name;
});
};

exports.updateTag = function *() {
var version = this.request.body;
var tag = this.params.tag;
var name =this.params.name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space


if (!version) {
this.status = 400;
this.body = {
error: 'version_missed',
reason: 'version not found'
};
return;
}

if (!semver.valid(version)) {
this.status = 403;
var reason = util.format('setting tag %s to invalid version: %s: %s/%s',
tag, version, name, tag);
this.body = {
error: 'forbidden',
reason: reason
};
return;
}

var mod = yield Module.get(name, version);
if (!mod) {
this.status = 403;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be 404

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npm return 403..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...... got it

var reason = util.format('setting tag %s to unknown version: %s: %s/%s',
tag, version, name, tag);
this.body = {
error: 'forbidden',
reason: reason
};
return;
}

yield Module.addTag(name, tag, version);
this.status = 201;
this.body = {
ok: true
};
};
2 changes: 2 additions & 0 deletions routes/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ function routes(app) {
app.put('/:name/sync', sync.sync);
app.get('/:name/sync/log/:id', sync.getSyncLog);

app.put('/:name/:tag', mod.updateTag);

// need limit by ip
app.get('/:name/download/:filename', limit, mod.download);

Expand Down
1 change: 0 additions & 1 deletion sync/sync_all.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var Npm = require('../proxy/npm');
var Total = require('../proxy/total');
var SyncModuleWorker = require('../proxy/sync_module_worker');
var Module = require('../proxy/module');
var co = require('co');
var thunkify = require('thunkify-wrap');

function subtract(subtracter, minuend) {
Expand Down
44 changes: 43 additions & 1 deletion test/controllers/registry/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,49 @@ describe('controllers/registry/module.test.js', function () {
});
});

describe('DELETE /:name/-rev/:rev', function (done) {
describe('PUT /:name/:tag', function () {
it('should create new tag ok', function (done) {
request(app)
.put('/testputmodule/newtag')
.set('content-type', 'application/json')
.send('"0.1.9"')
.expect(201, done);
});

it('should override exist tag ok', function (done) {
request(app)
.put('/testputmodule/newtag')
.set('content-type', 'application/json')
.send('"0.1.9"')
.expect(201, done);
});

it('should tag invalid version 403', function (done) {
request(app)
.put('/testputmodule/newtag')
.set('content-type', 'application/json')
.send('"hello"')
.expect(403)
.expect({
error: 'forbidden',
reason: 'setting tag newtag to invalid version: hello: testputmodule/newtag'
}, done);
});

it('should tag not eixst version 403', function (done) {
request(app)
.put('/testputmodule/newtag')
.set('content-type', 'application/json')
.send('"5.0.0"')
.expect(403)
.expect({
error: 'forbidden',
reason: 'setting tag newtag to unknown version: 5.0.0: testputmodule/newtag'
}, done);
});
});

describe('DELETE /:name/-rev/:rev', function () {
var baseauth = 'Basic ' + new Buffer('cnpmjstest10:cnpmjstest10').toString('base64');
var baseauthOther = 'Basic ' + new Buffer('cnpmjstest101:cnpmjstest101').toString('base64');
var lastRev;
Expand Down