it('should RR use the first client when all server down', function* () { mm(this.store.availables, '0', false); mm(this.store.availables, '1', false); this.store.index = 0; yield this.store.get(this.name); this.store.index = 1; yield this.store.get(this.name); });
it('should sync public scoped package', function* () { mm(config, 'syncMode', 'all'); mm(config, 'registryHost', ''); // mm(config, 'sourceNpmRegistry', 'https://registry.npmjs.org'); let envelope; mm(config, 'globalHook', function* (e) { envelope = e; }); var worker = new SyncModuleWorker({ name: '@sindresorhus/df', username: '******', noDep: true, }); worker.start(); var end = thunkify.event(worker, 'end'); yield end(); assert(envelope); assert(envelope.event === 'package:sync'); assert(envelope.name === '@sindresorhus/df'); assert(envelope.payload.changedVersions.length > 0); // sync again var worker = new SyncModuleWorker({ name: '@sindresorhus/df', username: '******', }); worker.start(); var end = thunkify.event(worker, 'end'); yield end(); assert(envelope); assert(envelope.event === 'package:sync'); assert(envelope.name === '@sindresorhus/df'); assert(envelope.payload.changedVersions.length === 0); var tgzUrl; function checkResult() { return function (done) { request(app) .get('/@sindresorhus/df') .expect(function (res) { var latest = res.body.versions[res.body['dist-tags']['latest']]; tgzUrl = latest.dist.tarball; }) .expect(200, done); }; } yield checkResult(); const p = urlparse(tgzUrl); var r = yield request(app).get(p.path); r.status.should.equal(200); r.headers['content-type'].should.equal('application/octet-stream'); });
before(function (done) { mm(config, 'enablePrivate', false); mm(config, 'forcePublishWithScope', true); var pkg = utils.getPackage('@cnpm/public-remove-all-module', '0.0.1', utils.otherUser); request(app) .put('/@cnpm/public-remove-all-module') .set('content-type', 'application/json') .set('authorization', utils.otherUserAuth) .send(pkg) .expect(201, done); });
it('should be yellow on warn console log color', () => { mm(chalk, 'supportsColor', true); mm(chalk, 'enabled', true); const ret = utils.consoleFormatter({ date: '2016-02-26 16:35:40,511', level: 'WARN', pid: '50864', message: 'warn', }); /* eslint-disable-next-line no-control-regex */ ret.should.match(/^\u001b\[33m/); });
it('should RR use the first client when all server down', function () { mm(this.store.availables, '0', false); mm(this.store.availables, '1', false); this.store.index = 0; let url = this.store.signatureUrl(this.name); url.should.match(/ali-sdk\/oss\/get-meta\.js/); this.store.index = 1; url = this.store.signatureUrl(this.name); url.should.match(/ali-sdk\/oss\/get-meta\.js/); });
it('should get() 404 when adapt package is not private package', function (done) { var getByTag = Module.getByTag; mm(Module, 'getByTag', function* (name, tag) { var pkg = yield getByTag.call(Module, name, tag); pkg && delete pkg.package._publish_on_cnpm; return pkg; }); mm(config, 'adaptScope', true); request(app) .get('/@cnpm/test-default-scope-package/latest') .expect(404, done); });
it('should 404 when package not exists', () => { mm(config, 'syncModel', 'all'); mm(config, 'enableAbbreviatedMetadata', true); return request(app.listen()) .get('/@cnpmtest/not-exists-package') .set('Accept', 'application/vnd.npm.install-v1+json') .expect(404) .expect({ error: 'not_found', reason: 'document not found' }); });
it('should show normal color', () => { mm(chalk, 'supportsColor', true); mm(chalk, 'enabled', true); const ret = utils.consoleFormatter({ date: '2016-02-26 16:35:40,511', level: 'INFO', pid: '50864', message: '[master] POST log (10ms)', }); ret.should.containEql('\u001b[34m[master'); ret.should.containEql('(\u001b[32m10ms\u001b[39m)'); ret.should.containEql('\u001b[36mPOST \u001b[39m'); });
before(function (done) { mm(config, 'enablePrivate', false); mm(config, 'forcePublishWithScope', false); request(app) .put('/publictestmodule/-rev/1') .send({ maintainers: [{ name: 'cnpmjstest101', email: '*****@*****.**' }] }) .set('authorization', utils.otherUserAuth) .expect({"ok":true,"id":"publictestmodule","rev":"1"}, done); });
it('should MS always get from clients[0] ok', function* () { mm(this.store, 'schedule', 'masterSlave'); mm(this.store.clients[1], 'get', 'mock error'); function onerror() { throw new Error('should not emit error event'); } this.store.on('error', onerror); let res = yield this.store.get(this.name); res.res.status.should.equal(200); res = yield this.store.get(this.name); res.res.status.should.equal(200); this.store.removeListener('error', onerror); });
it('should 422 add user without email', function (done) { mm(userService, 'get', function* () { return null; }); mm(userService, 'add', function* () { return {rev: '1-123'}; }); request(app) .put('/-/user/org.couchdb.user:name') .send({ name: 'name', password: '******' }) .expect(422, done); });
before(function (done) { mm(config, 'enablePrivate', false); mm(config, 'forcePublishWithScope', true); var pkg = utils.getPackage('@cnpm/public-test-delete-download-module', '0.1.9', utils.otherUser); request(app) .put('/' + pkg.name) .set('content-type', 'application/json') .set('authorization', utils.otherUserAuth) .send(pkg) .expect(201, function (err, res) { should.not.exist(err); withScopeRev = res.body.rev; done(); }); });
client.get(table, get, function (err) { should.not.exists(err); var counter = require('../lib/connection').Call_Counter; for (var k in client.servers) { var server = client.servers[k]; // if (k !== 'dw48.kgb.sqa.cm4:36020') { // continue; // } mm(server, 'in', utils.createNotServingRegionExceptionBuffer(counter)); } rows.forEach(function (row) { var get = new Get(row); get.addColumn('f', 'history'); get.addColumn('f', 'qualifier2'); client.get(table, get, function (err, result) { should.exists(err); err.name.should.equal('org.apache.hadoop.hbase.NotServingRegionException'); err.message.should.include('at org.apache.hadoop.hbase.regionserver.HRegionServer.getRegion(HRegionServer.java:3518)'); should.not.exists(result); mm.restore(); done(); }); }); });
it('should send message to all mention users', function (done) { done = pedding(done, 2); var atUserIds = [String(normalUser._id), String(normalUser2._id)]; var ep = new eventproxy(); ep.after('user_id', atUserIds.length, function (user_ids) { user_ids.sort().should.eql(atUserIds.sort()); done(); }); mm(message, 'sendAtMessage', function (atUserId, authorId, topicId, replyId, callback) { // String(atUserId).should.equal(String(atUserIds[count++])); ep.emit('user_id', String(atUserId)); callback(); }); var text = '@' + normalUser.loginname + ' @' + normalUser2.loginname + ' @notexitstuser 你们好'; at.sendMessageToMentionUsers(text, testTopic._id, normalUser._id, function (err) { should.not.exist(err); done(); }); });
it('should 403 when user not exists', function (done) { mm(config, 'customUserService', true); request(app) .put('/@cnpm/testmodule-update-1/-rev/1') .send({ maintainers: [{ name: 'cnpmjstest101', email: '*****@*****.**' }, { name: 'fengmk2', email: '*****@*****.**' }, { name: 'not-exists', email: '*****@*****.**' }, { name: 'not-exists2', email: '*****@*****.**' }] }) .set('authorization', utils.otherUserAuth) .expect({ error: 'invalid user name', reason: 'User: `not-exists, not-exists2` not exists' }) .expect(403, done); });
it('should return custom opensearch host', function (done) { mm(config.opensearch, 'host', 'foo.com'); request(app) .get('/opensearch.xml') .set('host', 'localhost:6002') .expect(/http:\/\/foo\.com\/browse\/keyword\//, done); });
it('should get 302 when do not support scope', function (done) { mm(config, 'scopes', []); request(app) .get('/@invalid/test') .expect('Location', 'https://registry.npmjs.com/@invalid/test') .expect(302, done); });
function (done) { mm(config, 'enablePrivate', false); mm(config, 'forcePublishWithScope', false); request(app) .put('/' + pkg.name) .set('authorization', baseauthOther) .send(pkg) .expect(400, function (err, res) { should.not.exist(err); res.body.should.eql({ error: 'version_error', reason: 'version undefined not found' }); done(); }); });
it('should 403 when user is not maintainer', function (done) { mm(config, 'enablePrivate', false); var pkg = utils.getPackage('@cnpmtest/testpublishmodule-not-maintainer', '0.0.1'); request(app) .put('/' + pkg.name) .set('authorization', utils.adminAuth) .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 403 request(app) .put('/' + pkg.name) .set('authorization', utils.otherUserAuth) .send(pkg) .expect(403, function (err, res) { should.not.exist(err); res.body.should.eql({ error: 'forbidden user', reason: 'cnpmjstest101 not authorized to modify @cnpmtest/testpublishmodule-not-maintainer, please contact maintainers: cnpmjstest10' }); done(); }); }); });
it('should 201 when user.add ok', function (done) { mm(userService, 'get', function* () { return null; }); mm(userService, 'add', function* () { return {rev: '1-123'}; }); request(app) .put('/-/user/org.couchdb.user:name') .send({ name: 'name', password: '******', email: 'email' }) .expect(201, done); });
it('should 201 login success', function (done) { mm(userService, 'auth', function* () { return { "login": "******", "email": "*****@*****.**", "name": "Yuan Feng", "html_url": "http://fengmk2.github.com", "avatar_url": "https://avatars3.githubusercontent.com/u/156269?s=460", "im_url": "", "site_admin": false, "scopes": ["@org1", "@cnpmtest"] }; }); request(app) .put('/-/user/org.couchdb.user:cnpmjstest11111') .send({ name: 'cnpmjstest11111', password: '******', email: '*****@*****.**' }) .expect(201, function (err, res) { should.not.exist(err); res.body.should.have.keys('ok', 'id', 'rev'); res.body.id.should.equal('org.couchdb.user:cnpmjstest11111'); res.body.rev.should.match(/\d+\-cnpmjstest11111/); res.body.ok.should.equal(true); done(); }); });
it('should admin remove module without scope ok', function (done) { mm(config, 'forcePublishWithScope', true); request(app) .del('/public-remove-all-module-admin/-rev/1') .set('authorization', utils.adminAuth) .expect(200, done); });
before(function () { mm(require('../../lib/utils'), 'execFile', function (command, callback) { process.nextTick(function () { callback(new Error('mock error')); }); }); });
it('should response no private packages', function (done) { mm(config, 'scopes', []); request(app) .get('/privates') .expect(/Can not found private package/) .expect(200, done); });
it('should throw error when status === 404', function* () { const request = urllib.request; mm(urllib, 'request', function* (url, options) { if (url.endsWith('.tgz')) { mm.restore(); } const result = yield request.call(urllib, url, options); if (url.endsWith('.tgz')) { result.status = 404; } return result; }); try { yield install({ root: tmp, pkgs: [ { name: 'pedding' }, ], production: true, }); throw new Error('should not run this'); } catch (err) { assert(/response 404 status/.test(err.message), err.message); } });
client.get('bulkwriter_test', get, function (err, result) { should.not.exist(err); var kvs = result.raw(); // if (kvs.length > 0) { // for (var i = 0; i < kvs.length; i++) { // var kv = kvs[i]; // console.log('kv: %s: %s', kv.getFamily().toString() + ':' + kv.getQualifier().toString(), kv.getValue().toString()); // } // } var location = client.getCachedLocation('bulkwriter_test', get.getRow()); var get2 = new hbase.Get('fdbf2da2cc85e1c79f953a3d8f482edf'); get2.addColumn('cf', 'w02'); console.log(location.toString()); mm(client, 'locateRegion', function (tableName, row, useCache, callback) { callback(null, location); mm.restore(); }); client.get('bulkwriter_test', get2, function (err, result) { should.not.exist(err); should.exist(result); var kvs = result.raw(); // if (kvs.length > 0) { // for (var i = 0; i < kvs.length; i++) { // var kv = kvs[i]; // console.log('kv: %s: %s', kv.getFamily().toString() + ':' + kv.getQualifier().toString(), kv.getValue().toString()); // } // } done(); }); });
it('should throw error when status === 206', function* () { const request = urllib.request; mm(urllib, 'request', function* (url, options) { if (url.endsWith('.tgz')) { mm.restore(); } const result = yield request.call(urllib, url, options); if (url.endsWith('.tgz')) { result.status = 206; } return result; }); try { yield install({ root: tmp, pkgs: [ { name: 'pedding' }, ], cacheDir: '', }); throw new Error('should not run this'); } catch (err) { assert(/status: 206 error, should be 200/.test(err.message), err.message); } });
it('should publish with scope, addPackageAndDist()', function (done) { mm(config, 'forcePublishWithScope', false); var pkg = utils.getPackage('@cnpm/publicpublishmodule', '0.0.2', utils.otherUser); request(app) .put('/' + pkg.name) .set('authorization', utils.otherUserAuth) .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 403 request(app) .put('/' + pkg.name) .set('authorization', utils.otherUserAuth) .send(pkg) .expect(403, function (err, res) { should.not.exist(err); res.body.should.eql({ error: 'forbidden', reason: 'cannot modify pre-existing version: 0.0.2' }); done(); }); }); });
before(function (done) { mm(config, 'privatePackages', ['testmodule-new-1', 'testmodule-new-2']); var pkg = utils.getPackage('testmodule-new-1', '0.0.1', utils.admin); pkg.versions['0.0.1'].dependencies = { 'bytetest-1': '~0.0.1', mocha: '~1.0.0' }; request(app.listen()) .put('/' + pkg.name) .set('authorization', utils.adminAuth) .send(pkg) .expect(201, function (err) { should.not.exist(err); var pkg = utils.getPackage('testmodule-new-2', '1.0.0', utils.admin); pkg.versions['1.0.0'].dependencies = { 'bytetest-1': '~0.0.1', mocha: '~1.0.0' }; request(app.listen()) .put('/' + pkg.name) .set('authorization', utils.adminAuth) .send(pkg) .expect(201, done); }); });
it('should fail when user remove module without scope', function (done) { mm(config, 'forcePublishWithScope', true); request(app) .del('/public-remove-all-module/-rev/1') .set('authorization', utils.otherUserAuth) .expect(403, done); });