UNPKG

smc-hub

Version:

CoCalc: Backend webserver component

302 lines (288 loc) 8.74 kB
// Generated by CoffeeScript 2.5.1 (function() { //######################################################################## // This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. // License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details //######################################################################## /* TESTING of user queries specifically involving changefeeds - part 3 -- collaborators, ... COPYRIGHT : (c) 2017 SageMath, Inc. LICENSE : AGPLv3 */ var async, changefeed_series, create_accounts, create_projects, db, expect, misc, pgtest, setup, teardown; async = require('async'); expect = require('expect'); pgtest = require('./pgtest'); db = void 0; setup = function(cb) { return pgtest.setup(function(err) { db = pgtest.db; return cb(err); }); }; teardown = pgtest.teardown; ({create_accounts, create_projects, changefeed_series} = pgtest); misc = require('smc-util/misc'); describe('test changefeed of all collaborators of a user -- ', function() { var accounts, projects, string_id; this.timeout(10000); before(setup); after(teardown); accounts = projects = string_id = void 0; it('creates 4 accounts', function(done) { return create_accounts(4, function(err, x) { accounts = x; return done(err); }); }); it('creates 1 project', function(done) { return create_projects(1, accounts[0], function(err, x) { projects = x; return done(err); }); }); it('creates another project', function(done) { return create_projects(1, accounts[3], function(err, x) { projects.push(x[0]); return done(err); }); }); return it('create changefeed of collaborators of account0', function(done) { var changefeed_id; changefeed_id = misc.uuid(); return db.user_query({ account_id: accounts[0], query: { collaborators: [ { account_id: null, first_name: null, last_name: null, last_active: null, profile: null } ] }, changes: changefeed_id, cb: changefeed_series([ function(x, cb) { expect(x.collaborators.length).toEqual(1); // add account1 to project return db.add_user_to_project({ account_id: accounts[1], project_id: projects[0], cb: cb }); }, function(x, cb) { expect(x).toEqual({ action: 'insert', new_val: { account_id: accounts[1], first_name: 'Firstname1', last_name: 'Lastname1' } }); // remove account1 from project return db.remove_collaborator_from_project({ account_id: accounts[1], project_id: projects[0], cb: cb }); }, function(x, cb) { expect(x).toEqual({ action: 'delete', old_val: { account_id: accounts[1] } }); // add account2 to project return db.add_user_to_project({ account_id: accounts[2], project_id: projects[0], cb: cb }); }, function(x, cb) { expect(x).toEqual({ action: 'insert', new_val: { account_id: accounts[2], first_name: 'Firstname2', last_name: 'Lastname2' } }); // add account1 to a different project that account0 isn't on -- doesn't fire changefeed return db.add_user_to_project({ account_id: accounts[1], project_id: projects[1], cb: function() { // now add account0 to that project -- this fires changefeeds return db.add_user_to_project({ account_id: accounts[0], project_id: projects[1], cb: cb }); } }); }, function(x, cb) { var ref; // we get accounts1 and accounts3 added to our collabs -- but don't know order expect(x.action).toEqual('insert'); expect((ref = x.new_val.account_id) === accounts[1] || ref === accounts[3]).toEqual(true); return cb(); }, function(x, cb) { var ref; expect(x.action).toEqual('insert'); expect((ref = x.new_val.account_id) === accounts[1] || ref === accounts[3]).toEqual(true); // remove account0 from this other project again... return db.remove_user_from_project({ account_id: accounts[0], project_id: projects[1], cb: cb }); }, function(x, cb) { // ... which triggers a delete. expect(x.action).toEqual('delete'); return cb(); }, function(x, cb) { // ... and another delete. expect(x.action).toEqual('delete'); return db.user_query_cancel_changefeed({ id: changefeed_id, cb: cb }); }, function(x, cb) { expect(x).toEqual({ action: 'close' }); return cb(); } ], done) }); }); }); describe('test collaborators fields are updated -- ', function() { var accounts, projects, string_id, t0; before(setup); after(teardown); accounts = projects = string_id = void 0; t0 = new Date(); it('creates 2 accounts', function(done) { return create_accounts(2, function(err, x) { accounts = x; return done(err); }); }); it('creates 1 project', function(done) { return create_projects(1, accounts[0], function(err, x) { projects = x; return done(err); }); }); it('adds accounts1 to projects[0]', function(done) { return db.add_user_to_project({ account_id: accounts[1], project_id: projects[0], cb: done }); }); return it('create changefeed of collaborators of account0', function(done) { var changefeed_id; changefeed_id = misc.uuid(); return db.user_query({ account_id: accounts[0], query: { collaborators: [ { account_id: null, first_name: null, last_name: null, last_active: null, profile: null } ] }, changes: changefeed_id, cb: changefeed_series([ function(x, cb) { expect(x.collaborators.length).toEqual(2); // changes first and last names return db._query({ query: "UPDATE accounts", set: { first_name: "X1", last_name: "Y1" }, where: { account_id: accounts[1] }, cb: cb }); }, function(x, cb) { expect(x).toEqual({ action: 'insert', new_val: { account_id: accounts[1], first_name: 'X1', last_name: 'Y1' } }); // change last_active and profile return db._query({ query: "UPDATE accounts", set: { last_active: t0, profile: { foo: 'bar' } }, where: { account_id: accounts[1] }, cb: cb }); }, function(x, cb) { expect(x.action).toEqual('update'); expect(x.new_val.last_active).toEqual(t0); expect(x.new_val.profile).toEqual({ foo: 'bar' }); return db.user_query_cancel_changefeed({ id: changefeed_id, cb: cb }); }, function(x, cb) { expect(x).toEqual({ action: 'close' }); return cb(); } ], done) }); }); }); }).call(this); //# sourceMappingURL=postgres-user-queries-changefeeds-3.js.map