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

Commit

Permalink
Merge pull request #463 from cnpm/by-user-api
Browse files Browse the repository at this point in the history
list user all packages api. fixed #462
  • Loading branch information
dead-horse committed Oct 7, 2014
2 parents 5d0d742 + 188dab4 commit 6e9ceaf
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 2 deletions.
1 change: 0 additions & 1 deletion config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ var config = {
fengmk2: 'fengmk2@gmail.com',
admin: 'admin@cnpmjs.org',
dead_horse: 'dead_horse@qq.com',
cnpmjstest10: 'cnpmjstest10@cnpmjs.org',
},

// email notification for errors
Expand Down
42 changes: 42 additions & 0 deletions controllers/registry/user_package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**!
* cnpmjs.org - controllers/registry/user_package.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/

'use strict';

/**
* Module dependencies.
*/

var ModuleMaintainer = require('../../proxy/module_maintainer');

exports.list = function* () {
var users = this.params.user.split('|');
if (users.length > 20) {
this.status = 400;
this.body = {
error: 'bad_request',
reason: 'reach max user names limit, must <= 20 user names'
};
return;
}


var rows = yield* ModuleMaintainer.listByUsers(users);
var data = {};
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
if (data[row.user]) {
data[row.user].push(row.name);
} else {
data[row.user] = [row.name];
}
}
this.body = data;
};
10 changes: 10 additions & 0 deletions proxy/module_maintainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ exports.update = function* (name, maintainers) {
remove: removeUsers
};
};

exports.listByUsers = function* (users) {
var sql = 'SELECT name, user FROM module_maintainer WHERE user = ?;';
var args = users;
if (users.length > 1) {
sql = 'SELECT name, user FROM module_maintainer WHERE user in (?);';
args = [users];
}
return yield mysql.query(sql, args);
};
4 changes: 4 additions & 0 deletions routes/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var total = require('../controllers/total');
var mod = require('../controllers/registry/module');
var user = require('../controllers/registry/user');
var sync = require('../controllers/sync');
var userPackage = require('../controllers/registry/user_package');

function routes(app) {

Expand Down Expand Up @@ -83,6 +84,9 @@ function routes(app) {
app.put('/-/user/org.couchdb.user::name', user.add);
app.get('/-/user/org.couchdb.user::name', user.show);
app.put('/-/user/org.couchdb.user::name/-rev/:rev', login, user.update);

// list all packages of user
app.get('/-/by-user/:user', userPackage.list);
}

module.exports = routes;
151 changes: 151 additions & 0 deletions test/controllers/registry/user_package.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**!
* cnpmjs.org - test/contributors/registry/user_package.test.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/

'use strict';

/**
* Module dependencies.
*/

var should = require('should');
var request = require('supertest');
var pedding = require('pedding');
var app = require('../../../servers/registry');
var utils = require('../../utils');

describe('contributors/registry/user_package.test.js', function () {
before(function (done) {
// TODO: need to unpublish all exists packages
var pkg = utils.getPackage('user_package_list_one_package_1', '0.0.1', utils.otherAdmin2);

request(app.listen())
.put('/' + pkg.name)
.set('authorization', utils.otherAdmin2Auth)
.send(pkg)
.expect(201, function (err) {
should.not.exists(err);
pkg = utils.getPackage('user_package_list_one_package_2', '0.0.2', utils.otherAdmin2);
request(app.listen())
.put('/' + pkg.name)
.set('authorization', utils.otherAdmin2Auth)
.send(pkg)
.expect(201, function (err) {
// other admin publish
should.not.exists(err);
pkg = utils.getPackage('user_package_list_one_package_3', '1.0.2', utils.otherAdmin3);
request(app.listen())
.put('/' + pkg.name)
.set('authorization', utils.otherAdmin3Auth)
.send(pkg)
.expect(201, done);
});
});
});

describe('listOne()', function () {
it('should return one user\'s all package names', function (done) {
request(app.listen())
.get('/-/by-user/' + utils.otherAdmin2)
.expect(200)
.expect({
cnpmjstestAdmin2: [
'user_package_list_one_package_1',
'user_package_list_one_package_2'
]
}, done);
});

it('should return {} when user not exists', function (done) {
request(app.listen())
.get('/-/by-user/user-not-exists')
.expect(200)
.expect({}, done);
});
});

describe('listMulti()', function () {
it('should return two exists user\'s all package names', function (done) {
done = pedding(2, done);

request(app.listen())
.get('/-/by-user/' + encodeURIComponent(utils.otherAdmin2 + '|' + utils.otherAdmin3))
.expect(200)
.expect({
cnpmjstestAdmin2: [
'user_package_list_one_package_1',
'user_package_list_one_package_2'
],
cnpmjstestAdmin3: [
'user_package_list_one_package_3'
]
}, done);

request(app.listen())
.get('/-/by-user/' + utils.otherAdmin2 + '|' + utils.otherAdmin3)
.expect(200)
.expect({
cnpmjstestAdmin2: [
'user_package_list_one_package_1',
'user_package_list_one_package_2'
],
cnpmjstestAdmin3: [
'user_package_list_one_package_3'
]
}, done);
});

it('should return one exists user\'s all package names', function (done) {
done = pedding(2, done);

request(app.listen())
.get('/-/by-user/' + encodeURIComponent(utils.otherAdmin2 + '|user-not-exists'))
.expect(200)
.expect({
cnpmjstestAdmin2: [
'user_package_list_one_package_1',
'user_package_list_one_package_2'
]
}, done);

request(app.listen())
.get('/-/by-user/' + utils.otherAdmin2 + '|user-not-exists')
.expect(200)
.expect({
cnpmjstestAdmin2: [
'user_package_list_one_package_1',
'user_package_list_one_package_2'
]
}, done);
});

it('should return {} when users not exists', function (done) {
request(app.listen())
.get('/-/by-user/user-not-exists1|user-not-exists2')
.expect(200)
.expect({}, done);
});

it('should return 200 when users length equal limit count', function (done) {
request(app.listen())
.get('/-/by-user/n1|n2|n3|n4|n5|n6|n7|n8|n9|n10|n11|n12|n13|n14|n15|n16|n17|n18|n19|n20')
.expect(200, done);
});

it('should return 400 when users reach limit count', function (done) {
request(app.listen())
.get('/-/by-user/n1|n2|n3|n4|n5|n6|n7|n8|n9|n10|n11|n12|n13|n14|n15|n16|n17|n18|n19|n20|n21')
.expect(400)
.expect({
error: 'bad_request',
reason: 'reach max user names limit, must <= 20 user names'
}, done);
});
});
});
4 changes: 3 additions & 1 deletion test/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ var User = require('../proxy/user');
var usernames = [
'cnpmjstest101',
'cnpmjstest102',
'cnpmjstest10'
'cnpmjstest10', // admin
'cnpmjstestAdmin2', // other admin
'cnpmjstestAdmin3', // other admin
];

usernames.forEach(function (name) {
Expand Down
10 changes: 10 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@

var path = require('path');
var fs = require('fs');
var config = require('../config');

var fixtures = path.join(__dirname, 'fixtures');

var admin = exports.admin = 'cnpmjstest10';
exports.adminAuth = 'Basic ' + new Buffer(admin + ':' + admin).toString('base64');
config.admins[admin] = admin + '@cnpmjs.org';

var otherAdmin2 = exports.otherAdmin2 = 'cnpmjstestAdmin2';
exports.otherAdmin2Auth = 'Basic ' + new Buffer(otherAdmin2 + ':' + otherAdmin2).toString('base64');
config.admins[otherAdmin2] = otherAdmin2 + '@cnpmjs.org';

var otherAdmin3 = exports.otherAdmin3 = 'cnpmjstestAdmin3';
exports.otherAdmin3Auth = 'Basic ' + new Buffer(otherAdmin3 + ':' + otherAdmin3).toString('base64');
config.admins[otherAdmin3] = otherAdmin3 + '@cnpmjs.org';

var otherUser = exports.otherUser = 'cnpmjstest101';
exports.otherUserAuth = 'Basic ' + new Buffer(otherUser + ':' + otherUser).toString('base64');
Expand Down

0 comments on commit 6e9ceaf

Please sign in to comment.