smc-hub
Version:
CoCalc: Backend webserver component
453 lines (440 loc) • 15.5 kB
JavaScript
// 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 API functions relating to users and user accounts
COPYRIGHT : (c) 2017 SageMath, Inc.
LICENSE : AGPLv3
*/
var api, async, email, expect, misc, reset, setup, teardown, winston;
api = require('./apitest');
({setup, teardown, reset, winston} = api);
misc = require('smc-util/misc');
email = require('../../email');
async = require('async');
expect = require('expect');
describe('testing calls relating to creating user accounts -- ', function() {
var account_id2, account_id3, base_url, project_id, project_id2;
this.timeout(15000);
before(setup);
after(teardown);
beforeEach(reset);
it("gets names for empty list of users", function(done) {
return api.call({
event: 'get_usernames',
body: {
account_ids: []
},
cb: function(err, resp) {
expect(err).toEqual(null);
expect(resp != null ? resp.event : void 0).toBe('usernames');
expect(resp != null ? resp.usernames : void 0).toEqual({});
return done(err);
}
});
});
it("gets names for api test account", function(done) {
return api.call({
event: 'get_usernames',
body: {
account_ids: [api.account_id]
},
cb: function(err, resp) {
expect(resp != null ? resp.event : void 0).toBe('usernames');
expect(resp != null ? resp.usernames : void 0).toEqual({
[`${api.account_id}`]: {
first_name: 'Sage',
last_name: 'CoCalc'
}
});
return done(err);
}
});
});
account_id2 = void 0;
it("uses api call to create a second account", function(done) {
return api.call({
event: 'create_account',
body: {
first_name: "Sage2",
last_name: "CoCalc2",
email_address: "cocalc+2@sagemath.com",
password: "1234qwerty",
agreed_to_terms: true
},
cb: function(err, resp) {
expect(resp != null ? resp.event : void 0).toBe('account_created');
expect(misc.is_valid_uuid_string(resp != null ? resp.account_id : void 0)).toBe(true);
account_id2 = resp != null ? resp.account_id : void 0;
return done(err);
}
});
});
it("tries to create the same account again", function(done) {
return api.call({
event: 'create_account',
body: {
first_name: "Sage2",
last_name: "CoCalc2",
email_address: "cocalc+2@sagemath.com",
password: "1234qwerty",
agreed_to_terms: true
},
cb: function(err, resp) {
var opts0;
expect(resp != null ? resp.event : void 0).toBe('account_creation_failed');
expect(resp != null ? resp.reason : void 0).toEqual({
"email_address": "This e-mail address is already taken."
});
console.log('EMAIL', email.send_email);
opts0 = email.send_email.args[0][0];
expect(opts0.subject.indexOf('Welcome to CoCalc') === 0).toBe(true);
return done(err);
}
});
});
project_id = void 0;
it("creates test project", function(done) {
return api.call({
event: 'create_project',
body: {
title: 'COLLABTEST',
description: 'Testing collaboration ops'
},
cb: function(err, resp) {
expect(resp != null ? resp.event : void 0).toBe('project_created');
project_id = resp.project_id;
return done(err);
}
});
});
it("invites collaborator to project", function(done) {
return api.call({
event: 'invite_collaborator',
body: {
account_id: account_id2,
project_id: project_id
},
cb: function(err, resp) {
expect(resp != null ? resp.event : void 0).toBe('success');
return done(err);
}
});
});
project_id2 = void 0;
account_id3 = void 0;
it("invites a collaborator to a project via an email message", function(done) {
// create new account and then invite
return async.series([
function(cb) {
return api.call({
event: 'create_project',
body: {
title: 'COLLABTEST2',
description: 'Testing collaboration ops'
},
cb: function(err,
resp) {
expect(resp != null ? resp.event : void 0).toBe('project_created');
project_id2 = resp.project_id;
expect(misc.is_valid_uuid_string(project_id2)).toBe(true);
//winston.info("project_id2: #{project_id2}")
return cb(err);
}
});
},
function(cb) {
return api.call({
event: 'create_account',
body: {
first_name: "Sage3",
last_name: "CoCalc3",
email_address: "cocalc+3@sagemath.com",
password: "1234qwerty",
agreed_to_terms: true
},
cb: function(err,
resp) {
expect(resp != null ? resp.event : void 0).toBe('account_created');
expect(misc.is_valid_uuid_string(resp != null ? resp.account_id : void 0)).toBe(true);
account_id3 = resp != null ? resp.account_id : void 0;
expect(misc.is_valid_uuid_string(account_id3)).toBe(true);
console.log(`account created: ${account_id3}`);
return cb(err);
}
});
},
function(cb) {
return api.call({
event: 'invite_collaborator',
body: {
account_id: account_id3,
project_id: project_id2,
title: 'PROJECT_TITLE',
link2proj: 'https://link.to.project/',
replyto: 'cocalc+3@sagemath.com',
replyto_name: 'Sage3 CoCalc3',
email: 'BODY_OF_EMAIL_1',
subject: 'SUBJECT_OF_EMAIL_1'
},
cb: function(err,
resp) {
winston.info(`invites a collaborator to a project with an email message: ${misc.to_json(resp)}`);
// maybe actual email is sent async, hence we wait a bit...
return setTimeout((function() {
return cb(err);
}),
100);
}
});
},
// there shouldn't be a second email (during a week or so) upon inviting again
function(cb) {
return api.call({
event: 'invite_collaborator',
body: {
account_id: account_id3,
project_id: project_id2,
title: 'PROJECT_TITLE',
link2proj: 'https://link.to.project/',
replyto: 'cocalc+3@sagemath.com',
replyto_name: 'Sage3 CoCalc3',
email: 'BODY_OF_EMAIL_2',
subject: 'SUBJECT_OF_EMAIL_2'
},
cb: function(err,
resp) {
// maybe actual email is sent async, hence we wait a bit...
return setTimeout((function() {
return cb(err);
}),
100);
}
});
}
], function(err) {
var opts0, opts1;
expect(err != null).toBe(false);
opts0 = email.send_email.args[0][0];
//console.log(misc.to_json(opts0))
expect(opts0.subject.indexOf('Welcome to') >= 0).toBe(true);
opts1 = email.send_email.args[1][0];
//console.log(misc.to_json(opts1))
expect(opts1.subject).toBe('SUBJECT_OF_EMAIL_1');
expect(opts1.from).toBe('invites@sagemath.com');
expect(opts1.to).toBe('cocalc+3@sagemath.com');
expect(opts1.replyto_name).toBe('Sage3 CoCalc3');
expect(opts1.body.indexOf('BODY_OF_EMAIL_1') === 0).toBe(true);
// no second email
winston.info(`email.send_email.args: ${misc.to_json(email.send_email.args)}`);
//console.log("email3: #{misc.to_json(opts2)}")
expect(email.send_email.args.length).toBe(2); // only two, because the second invite is not going through in client/mesg_invite_collaborator
return done(err);
});
});
it("lists project collaborators", function(done) {
return api.call({
event: 'query',
body: {
query: {
projects: {
project_id: project_id,
users: null
}
}
},
cb: function(err, resp) {
var ref, ref1;
expect(resp != null ? resp.event : void 0).toBe('query');
expect(resp != null ? (ref = resp.query) != null ? (ref1 = ref.projects) != null ? ref1.users[account_id2] : void 0 : void 0 : void 0).toEqual({
group: 'collaborator'
});
return done(err);
}
});
});
base_url = 'https://cocalc.com';
it("invites non-cloud collaborators", function(done) {
// TODO: this test cannot check contents of the email message sent,
// because api.last_email isn't set until after this test runs.
// See TODO in smc-hub/client.coffee around L1216, where cb() is called before
// email is sent.
return api.call({
event: 'invite_noncloud_collaborators',
body: {
project_id: project_id,
to: 'someone@m.local',
email: 'Plese sign up and join this project.',
title: 'Team Project',
link2proj: `${base_url}/projects/${project_id}`
},
cb: function(err, resp) {
var check;
check = function(err, resp) {
var opts0;
opts0 = email.send_email.args[0][0];
expect(resp != null ? resp.event : void 0).toBe('invite_noncloud_collaborators_resp');
expect(opts0 != null ? opts0.subject : void 0).toBe('CoCalc Invitation');
return done();
};
// sending email is probably async, wait a bit
return setTimeout((function() {
return check(err, resp);
}), 100);
}
});
});
it("removes collaborator", function(done) {
return api.call({
event: 'remove_collaborator',
body: {
account_id: account_id2,
project_id: project_id
},
cb: function(err, resp) {
expect(resp != null ? resp.event : void 0).toBe('success');
return done(err);
}
});
});
return it("deletes the second account", function(done) {
return api.call({
event: 'delete_account',
body: {
account_id: account_id2
},
cb: function(err, resp) {
expect(resp != null ? resp.event : void 0).toBe('account_deleted');
return done(err);
}
});
});
});
describe('testing invalid input to creating user accounts -- ', function() {
this.timeout(15000);
before(setup);
after(teardown);
beforeEach(reset);
it("leaves off the first name", function(done) {
return api.call({
event: 'create_account',
body: {
last_name: "CoCalc3",
email_address: "cocalc+3@sagemath.com",
password: "god",
agreed_to_terms: true
},
cb: function(err, resp) {
expect(misc.startswith(err, 'invalid parameters')).toBe(true);
return done();
}
});
});
it("leaves first name blank", function(done) {
return api.call({
event: 'create_account',
body: {
first_name: "",
last_name: "xxxx",
email_address: "cocalc+3@sagemath.com",
password: "xyz123",
agreed_to_terms: true
},
cb: function(err, resp) {
if (resp != null) {
delete resp.id;
}
expect(resp).toEqual({
event: 'account_creation_failed',
reason: {
first_name: 'Enter your first name.'
}
});
return done(err);
}
});
});
return it("leaves last name blank", function(done) {
return api.call({
event: 'create_account',
body: {
first_name: "C",
last_name: "",
email_address: "cocalc+3@sagemath.com",
password: "xyz123",
agreed_to_terms: true
},
cb: function(err, resp) {
if (resp != null) {
delete resp.id;
}
expect(resp).toEqual({
event: 'account_creation_failed',
reason: {
last_name: 'Enter your last name.'
}
});
return done(err);
}
});
});
});
describe('testing user_search -- ', function() {
this.timeout(15000);
before(setup);
after(teardown);
beforeEach(reset);
it("searches by email", function(done) {
return api.call({
event: 'user_search',
body: {
query: 'cocalc@sagemath.com'
},
cb: function(err, resp) {
var ref, ref1, ref2, ref3;
expect(resp != null ? resp.event : void 0).toBe('user_search_results');
expect(resp != null ? (ref = resp.results) != null ? ref.length : void 0 : void 0).toBe(1);
expect(resp != null ? (ref1 = resp.results) != null ? ref1[0].first_name : void 0 : void 0).toBe('Sage');
expect(resp != null ? (ref2 = resp.results) != null ? ref2[0].last_name : void 0 : void 0).toBe('CoCalc');
expect(resp != null ? (ref3 = resp.results) != null ? ref3[0].email_address : void 0 : void 0).toBe('cocalc@sagemath.com');
return done(err);
}
});
});
it("searches by first and last name prefixes", function(done) {
return api.call({
event: 'user_search',
body: {
query: 'coc sag'
},
cb: function(err, resp) {
var ref, ref1, ref2, ref3;
expect(resp != null ? resp.event : void 0).toBe('user_search_results');
expect(resp != null ? (ref = resp.results) != null ? ref.length : void 0 : void 0).toBe(1);
expect(resp != null ? (ref1 = resp.results) != null ? ref1[0].first_name : void 0 : void 0).toBe('Sage');
expect(resp != null ? (ref2 = resp.results) != null ? ref2[0].last_name : void 0 : void 0).toBe('CoCalc');
expect(resp != null ? (ref3 = resp.results) != null ? ref3[0] : void 0 : void 0).toExcludeKey('email_address');
return done(err);
}
});
});
return it("searches by email and first and last name prefixes", function(done) {
return api.call({
event: 'user_search',
body: {
query: 'coc sag,cocalc@sagemath.com'
},
cb: function(err, resp) {
var ref;
expect(resp != null ? resp.event : void 0).toBe('user_search_results');
expect(resp != null ? (ref = resp.results) != null ? ref.length : void 0 : void 0).toBe(2);
return done(err);
}
});
});
});
}).call(this);
//# sourceMappingURL=account.js.map