2014-02-20 19:50:14 +01:00
|
|
|
add_dependencies({
|
2016-12-03 23:17:57 +01:00
|
|
|
util: 'js/util.js',
|
2014-02-20 19:50:14 +01:00
|
|
|
});
|
|
|
|
|
2016-07-30 20:01:15 +02:00
|
|
|
global.stub_out_jquery();
|
2016-07-30 19:39:36 +02:00
|
|
|
|
2014-02-01 16:14:40 +01:00
|
|
|
var people = require("js/people.js");
|
|
|
|
|
|
|
|
set_global('page_params', {
|
2016-06-08 05:54:07 +02:00
|
|
|
people_list: [],
|
2016-12-03 23:17:57 +01:00
|
|
|
email: 'hamlet@example.com',
|
2014-02-01 16:14:40 +01:00
|
|
|
});
|
|
|
|
set_global('activity', {
|
2016-12-03 23:17:57 +01:00
|
|
|
set_user_statuses: function () {},
|
2014-02-01 16:14:40 +01:00
|
|
|
});
|
2014-02-20 21:03:39 +01:00
|
|
|
set_global('admin', {
|
2016-12-03 23:17:57 +01:00
|
|
|
show_or_hide_menu_item: function () {},
|
2014-02-20 21:03:39 +01:00
|
|
|
});
|
2014-02-01 16:14:40 +01:00
|
|
|
|
2016-10-18 19:21:38 +02:00
|
|
|
var _ = global._;
|
|
|
|
|
2014-02-01 16:14:40 +01:00
|
|
|
(function test_basics() {
|
2014-03-12 19:59:17 +01:00
|
|
|
var orig_person = {
|
|
|
|
email: 'orig@example.com',
|
2016-10-31 15:56:57 +01:00
|
|
|
user_id: 31,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Original',
|
2014-03-12 19:59:17 +01:00
|
|
|
};
|
|
|
|
people.add(orig_person);
|
|
|
|
|
2016-11-01 19:36:50 +01:00
|
|
|
var persons = people.get_all_persons();
|
|
|
|
assert.equal(_.size(persons), 1);
|
|
|
|
assert.equal(persons[0].full_name, 'Original');
|
|
|
|
|
2016-11-01 19:45:53 +01:00
|
|
|
var realm_persons = people.get_realm_persons();
|
|
|
|
assert.equal(_.size(realm_persons), 0);
|
|
|
|
|
|
|
|
|
2014-02-01 16:14:40 +01:00
|
|
|
var full_name = 'Isaac Newton';
|
|
|
|
var email = 'isaac@example.com';
|
|
|
|
var isaac = {
|
|
|
|
email: email,
|
2016-10-31 15:56:57 +01:00
|
|
|
user_id: 32,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: full_name,
|
2014-02-01 16:14:40 +01:00
|
|
|
};
|
|
|
|
people.add(isaac);
|
|
|
|
|
|
|
|
var person = people.get_by_name(full_name);
|
2016-10-30 15:22:24 +01:00
|
|
|
assert.equal(people.get_user_id(email), 32);
|
2014-02-01 16:14:40 +01:00
|
|
|
assert.equal(person.email, email);
|
|
|
|
person = people.get_by_email(email);
|
|
|
|
assert.equal(person.full_name, full_name);
|
|
|
|
person = people.realm_get(email);
|
|
|
|
assert(!person);
|
|
|
|
people.add_in_realm(isaac);
|
|
|
|
person = people.realm_get(email);
|
|
|
|
assert.equal(person.email, email);
|
|
|
|
|
2016-11-01 19:45:53 +01:00
|
|
|
realm_persons = people.get_realm_persons();
|
|
|
|
assert.equal(_.size(realm_persons), 1);
|
|
|
|
assert.equal(realm_persons[0].full_name, 'Isaac Newton');
|
|
|
|
|
2014-02-01 16:14:40 +01:00
|
|
|
people.update({email: email, is_admin: true});
|
|
|
|
person = people.get_by_email(email);
|
|
|
|
assert.equal(person.full_name, full_name);
|
|
|
|
assert.equal(person.is_admin, true);
|
|
|
|
|
|
|
|
people.update({email: email, full_name: 'Sir Isaac'});
|
|
|
|
person = people.get_by_email(email);
|
|
|
|
assert.equal(person.full_name, 'Sir Isaac');
|
|
|
|
assert.equal(person.is_admin, true);
|
|
|
|
|
2014-02-20 21:03:39 +01:00
|
|
|
global.page_params.email = email;
|
|
|
|
|
|
|
|
people.update({email: email, is_admin: false});
|
|
|
|
assert(!global.page_params.is_admin);
|
|
|
|
|
|
|
|
people.update({email: email, full_name: 'The Godfather of Calculus'});
|
|
|
|
assert.equal(global.page_params.fullname, 'The Godfather of Calculus');
|
|
|
|
|
2016-12-15 22:44:42 +01:00
|
|
|
// Now deactivate isaac
|
|
|
|
people.deactivate(isaac);
|
|
|
|
person = people.realm_get(email);
|
2014-02-01 16:14:40 +01:00
|
|
|
assert(!person);
|
2014-03-12 19:59:17 +01:00
|
|
|
|
2016-12-15 22:44:42 +01:00
|
|
|
// We can still get their info for non-realm needs.
|
|
|
|
person = people.get_by_email(email);
|
|
|
|
assert.equal(person.email, email);
|
|
|
|
|
2014-03-12 19:59:17 +01:00
|
|
|
// The original person should still be there
|
|
|
|
person = people.get_by_email('orig@example.com');
|
|
|
|
assert.equal(person.full_name, 'Original');
|
2014-02-01 16:14:40 +01:00
|
|
|
}());
|
2014-02-20 19:50:14 +01:00
|
|
|
|
2016-10-31 15:56:57 +01:00
|
|
|
(function test_get_person_from_user_id() {
|
|
|
|
var person = {
|
|
|
|
email: 'mary@example.com',
|
|
|
|
user_id: 42,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Mary',
|
2016-10-31 15:56:57 +01:00
|
|
|
};
|
|
|
|
people.add(person);
|
|
|
|
person = people.get_by_email('mary@example.com');
|
|
|
|
assert.equal(person.full_name, 'Mary');
|
|
|
|
person = people.get_person_from_user_id(42);
|
|
|
|
assert.equal(person.email, 'mary@example.com');
|
|
|
|
|
|
|
|
// The semantics for update() are going to eventually
|
|
|
|
// change to use user_id as a key, but now we use email
|
|
|
|
// as a key and change attributes. With the current
|
|
|
|
// behavior, we don't have to make update() do anything
|
|
|
|
// new.
|
|
|
|
person = {
|
|
|
|
email: 'mary@example.com',
|
|
|
|
user_id: 42,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Mary New',
|
2016-10-31 15:56:57 +01:00
|
|
|
};
|
|
|
|
people.update(person);
|
|
|
|
person = people.get_person_from_user_id(42);
|
|
|
|
assert.equal(person.full_name, 'Mary New');
|
|
|
|
|
2016-12-15 22:44:42 +01:00
|
|
|
// deactivate() should eventually just take a user_id, but
|
|
|
|
// now it takes a full person object. Note that deactivate()
|
|
|
|
// won't actually make the user disappear completely.
|
|
|
|
people.deactivate(person);
|
|
|
|
person = people.realm_get('mary@example.com');
|
2016-10-31 15:56:57 +01:00
|
|
|
assert.equal(person, undefined);
|
|
|
|
person = people.get_person_from_user_id(42);
|
2016-12-15 22:44:42 +01:00
|
|
|
assert.equal(person.user_id, 42);
|
2016-10-31 15:56:57 +01:00
|
|
|
}());
|
|
|
|
|
2014-02-20 19:50:14 +01:00
|
|
|
(function test_get_rest_of_realm() {
|
|
|
|
var myself = {
|
|
|
|
email: 'myself@example.com',
|
2016-10-31 15:56:57 +01:00
|
|
|
user_id: 201,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Yours Truly',
|
2014-02-20 19:50:14 +01:00
|
|
|
};
|
|
|
|
global.page_params.email = myself.email;
|
|
|
|
var alice1 = {
|
|
|
|
email: 'alice1@example.com',
|
2016-10-31 15:56:57 +01:00
|
|
|
user_id: 202,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Alice',
|
2014-02-20 19:50:14 +01:00
|
|
|
};
|
|
|
|
var alice2 = {
|
|
|
|
email: 'alice2@example.com',
|
2016-10-31 15:56:57 +01:00
|
|
|
user_id: 203,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Alice',
|
2014-02-20 19:50:14 +01:00
|
|
|
};
|
|
|
|
var bob = {
|
|
|
|
email: 'bob@example.com',
|
2016-10-31 15:56:57 +01:00
|
|
|
user_id: 204,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Bob van Roberts',
|
2014-02-20 19:50:14 +01:00
|
|
|
};
|
|
|
|
people.add_in_realm(myself);
|
|
|
|
people.add_in_realm(alice1);
|
|
|
|
people.add_in_realm(bob);
|
|
|
|
people.add_in_realm(alice2);
|
|
|
|
var others = people.get_rest_of_realm();
|
|
|
|
var expected = [
|
|
|
|
{ email: 'alice1@example.com', full_name: 'Alice' },
|
|
|
|
{ email: 'alice2@example.com', full_name: 'Alice' },
|
2016-12-03 23:17:57 +01:00
|
|
|
{ email: 'bob@example.com', full_name: 'Bob van Roberts' },
|
2014-02-20 19:50:14 +01:00
|
|
|
];
|
|
|
|
assert.deepEqual(others, expected);
|
2016-10-18 19:21:38 +02:00
|
|
|
|
|
|
|
}());
|
|
|
|
|
2016-12-15 22:18:59 +01:00
|
|
|
people.init();
|
|
|
|
|
2016-11-03 21:59:18 +01:00
|
|
|
(function test_recipient_counts() {
|
|
|
|
var email = 'anybody@example.com';
|
|
|
|
assert.equal(people.get_recipient_count({email: email}), 0);
|
|
|
|
people.incr_recipient_count(email);
|
|
|
|
people.incr_recipient_count(email);
|
|
|
|
assert.equal(people.get_recipient_count({email: email}), 2);
|
2016-11-04 14:34:58 +01:00
|
|
|
|
|
|
|
assert.equal(people.get_recipient_count({pm_recipient_count: 5}), 5);
|
2016-11-03 21:59:18 +01:00
|
|
|
}());
|
|
|
|
|
2016-10-18 19:21:38 +02:00
|
|
|
(function test_filtered_users() {
|
|
|
|
var charles = {
|
|
|
|
email: 'charles@example.com',
|
2016-10-31 15:56:57 +01:00
|
|
|
user_id: 301,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Charles Dickens',
|
2016-10-18 19:21:38 +02:00
|
|
|
};
|
|
|
|
var maria = {
|
|
|
|
email: 'athens@example.com',
|
2016-10-31 15:56:57 +01:00
|
|
|
user_id: 302,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Maria Athens',
|
2016-10-18 19:21:38 +02:00
|
|
|
};
|
|
|
|
var ashton = {
|
|
|
|
email: 'ashton@example.com',
|
2016-10-31 15:56:57 +01:00
|
|
|
user_id: 303,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Ashton Smith',
|
2016-10-18 19:21:38 +02:00
|
|
|
};
|
2016-11-14 21:37:36 +01:00
|
|
|
var linus = {
|
|
|
|
email: 'ltorvalds@example.com',
|
|
|
|
user_id: 304,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Linus Torvalds',
|
2016-11-14 21:37:36 +01:00
|
|
|
};
|
2016-10-18 19:21:38 +02:00
|
|
|
|
|
|
|
people.add_in_realm(charles);
|
|
|
|
people.add_in_realm(maria);
|
|
|
|
people.add_in_realm(ashton);
|
2016-11-14 21:37:36 +01:00
|
|
|
people.add_in_realm(linus);
|
|
|
|
|
2016-10-18 19:21:38 +02:00
|
|
|
var search_term = 'a';
|
|
|
|
var users = people.get_rest_of_realm();
|
2016-11-14 21:26:12 +01:00
|
|
|
var filtered_people = people.filter_people_by_search_terms(users, [search_term]);
|
2016-10-18 19:21:38 +02:00
|
|
|
assert.equal(filtered_people["ashton@example.com"], true);
|
|
|
|
assert.equal(filtered_people["athens@example.com"], true);
|
|
|
|
assert.equal(_.keys(filtered_people).length, 2);
|
|
|
|
assert(!_.has(filtered_people, 'charles@example.com'));
|
|
|
|
|
2016-11-14 21:26:12 +01:00
|
|
|
filtered_people = people.filter_people_by_search_terms(users, []);
|
2016-10-18 19:21:38 +02:00
|
|
|
assert(_.isEmpty(filtered_people));
|
|
|
|
|
2016-11-14 21:37:36 +01:00
|
|
|
filtered_people = people.filter_people_by_search_terms(users, ['ltorv']);
|
|
|
|
assert.equal(_.keys(filtered_people).length, 1);
|
|
|
|
assert(_.has(filtered_people, 'ltorvalds@example.com'));
|
|
|
|
|
2016-11-14 22:34:46 +01:00
|
|
|
filtered_people = people.filter_people_by_search_terms(users, ['ch di', 'maria']);
|
|
|
|
assert.equal(_.keys(filtered_people).length, 2);
|
|
|
|
assert(_.has(filtered_people, 'charles@example.com'));
|
|
|
|
assert(_.has(filtered_people, 'athens@example.com'));
|
|
|
|
|
2014-02-20 19:50:14 +01:00
|
|
|
}());
|
2016-11-15 22:55:37 +01:00
|
|
|
|
2016-12-15 22:18:59 +01:00
|
|
|
people.init();
|
|
|
|
|
2016-11-15 22:55:37 +01:00
|
|
|
(function test_multi_user_methods() {
|
2016-12-15 22:18:59 +01:00
|
|
|
var emp401 = {
|
2016-11-15 22:55:37 +01:00
|
|
|
email: 'emp401@example.com',
|
|
|
|
user_id: 401,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'whatever 401',
|
2016-11-15 22:55:37 +01:00
|
|
|
};
|
|
|
|
var emp402 = {
|
2016-11-19 00:33:32 +01:00
|
|
|
email: 'EMP402@example.com',
|
2016-11-15 22:55:37 +01:00
|
|
|
user_id: 402,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'whatever 402',
|
2016-11-15 22:55:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
people.add_in_realm(emp401);
|
|
|
|
people.add_in_realm(emp402);
|
|
|
|
|
2016-11-19 00:33:32 +01:00
|
|
|
var emails_string = people.user_ids_string_to_emails_string('402,401');
|
2016-11-15 22:55:37 +01:00
|
|
|
assert.equal(emails_string, 'emp401@example.com,emp402@example.com');
|
|
|
|
|
2016-11-19 00:33:32 +01:00
|
|
|
emails_string = 'emp402@example.com,EMP401@EXAMPLE.COM';
|
2016-11-15 22:55:37 +01:00
|
|
|
var user_ids_string = people.emails_strings_to_user_ids_string(emails_string);
|
|
|
|
assert.equal(user_ids_string, '401,402');
|
|
|
|
}());
|