2016-10-05 03:38:12 +02:00
|
|
|
global.stub_out_jquery();
|
|
|
|
|
2016-10-30 15:47:20 +01:00
|
|
|
set_global('page_params', {
|
|
|
|
is_admin: false,
|
2016-12-03 23:17:57 +01:00
|
|
|
people_list: [],
|
2016-10-30 15:47:20 +01:00
|
|
|
});
|
|
|
|
|
2013-08-21 20:27:14 +02:00
|
|
|
add_dependencies({
|
2016-10-30 15:47:20 +01:00
|
|
|
people: 'js/people.js',
|
2016-10-17 17:48:56 +02:00
|
|
|
stream_color: 'js/stream_color.js',
|
2016-12-03 23:17:57 +01:00
|
|
|
util: 'js/util.js',
|
2013-08-21 20:27:14 +02:00
|
|
|
});
|
|
|
|
|
2013-09-16 16:56:46 +02:00
|
|
|
set_global('blueslip', {});
|
|
|
|
|
2013-08-19 18:46:21 +02:00
|
|
|
var stream_data = require('js/stream_data.js');
|
2016-10-30 15:47:20 +01:00
|
|
|
var people = global.people;
|
2013-08-19 18:46:21 +02:00
|
|
|
|
|
|
|
(function test_basics() {
|
|
|
|
var denmark = {
|
|
|
|
subscribed: false,
|
|
|
|
color: 'blue',
|
|
|
|
name: 'Denmark',
|
2014-02-05 20:35:16 +01:00
|
|
|
stream_id: 1,
|
2016-12-03 23:17:57 +01:00
|
|
|
in_home_view: false,
|
2013-08-19 18:46:21 +02:00
|
|
|
};
|
|
|
|
var social = {
|
|
|
|
subscribed: true,
|
|
|
|
color: 'red',
|
|
|
|
name: 'social',
|
2014-02-05 20:35:16 +01:00
|
|
|
stream_id: 2,
|
2013-08-19 18:46:21 +02:00
|
|
|
in_home_view: true,
|
2016-12-03 23:17:57 +01:00
|
|
|
invite_only: true,
|
2013-08-19 18:46:21 +02:00
|
|
|
};
|
2014-02-20 17:57:30 +01:00
|
|
|
var test = {
|
|
|
|
subscribed: true,
|
|
|
|
color: 'yellow',
|
|
|
|
name: 'test',
|
|
|
|
stream_id: 3,
|
|
|
|
in_home_view: false,
|
2016-12-03 23:17:57 +01:00
|
|
|
invite_only: false,
|
2014-02-20 17:57:30 +01:00
|
|
|
};
|
2013-08-19 18:46:21 +02:00
|
|
|
stream_data.add_sub('Denmark', denmark);
|
|
|
|
stream_data.add_sub('social', social);
|
2014-02-20 17:57:30 +01:00
|
|
|
assert(stream_data.all_subscribed_streams_are_in_home_view());
|
|
|
|
stream_data.add_sub('test', test);
|
|
|
|
assert(!stream_data.all_subscribed_streams_are_in_home_view());
|
2013-08-19 18:46:21 +02:00
|
|
|
|
|
|
|
assert.equal(stream_data.get_sub('denmark'), denmark);
|
|
|
|
assert.equal(stream_data.get_sub('Social'), social);
|
|
|
|
|
2014-02-20 17:57:30 +01:00
|
|
|
assert.deepEqual(stream_data.home_view_stream_names(), ['social']);
|
|
|
|
assert.deepEqual(stream_data.subscribed_streams(), ['social', 'test']);
|
|
|
|
assert.deepEqual(stream_data.get_colors(), ['red', 'yellow']);
|
2013-08-19 18:46:21 +02:00
|
|
|
|
|
|
|
assert(stream_data.is_subscribed('social'));
|
|
|
|
assert(stream_data.is_subscribed('Social'));
|
|
|
|
assert(!stream_data.is_subscribed('Denmark'));
|
|
|
|
assert(!stream_data.is_subscribed('Rome'));
|
|
|
|
|
|
|
|
assert(stream_data.get_invite_only('social'));
|
2013-08-23 05:34:59 +02:00
|
|
|
assert(!stream_data.get_invite_only('unknown'));
|
2013-08-19 18:46:21 +02:00
|
|
|
assert.equal(stream_data.get_color('social'), 'red');
|
2013-08-23 05:32:38 +02:00
|
|
|
assert.equal(stream_data.get_color('unknown'), global.stream_color.default_color);
|
2013-08-19 19:25:44 +02:00
|
|
|
|
|
|
|
assert.equal(stream_data.get_name('denMARK'), 'Denmark');
|
|
|
|
assert.equal(stream_data.get_name('unknown Stream'), 'unknown Stream');
|
2013-08-23 05:36:49 +02:00
|
|
|
|
|
|
|
assert(stream_data.in_home_view('social'));
|
|
|
|
assert(!stream_data.in_home_view('denmark'));
|
2013-08-19 18:46:21 +02:00
|
|
|
}());
|
2013-08-23 05:29:41 +02:00
|
|
|
|
2014-02-05 20:35:16 +01:00
|
|
|
(function test_get_by_id() {
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
var id = 42;
|
|
|
|
var sub = {
|
|
|
|
name: 'Denmark',
|
|
|
|
subscribed: true,
|
|
|
|
color: 'red',
|
2016-12-03 23:17:57 +01:00
|
|
|
stream_id: id,
|
2014-02-05 20:35:16 +01:00
|
|
|
};
|
|
|
|
stream_data.add_sub('Denmark', sub);
|
|
|
|
sub = stream_data.get_sub('Denmark');
|
|
|
|
assert.equal(sub.color, 'red');
|
|
|
|
sub = stream_data.get_sub_by_id(id);
|
|
|
|
assert.equal(sub.color, 'red');
|
2016-10-30 17:33:23 +01:00
|
|
|
|
|
|
|
stream_data.rename_sub(id, 'Sweden');
|
|
|
|
sub = stream_data.get_sub_by_id(id);
|
|
|
|
assert.equal(sub.color, 'red');
|
|
|
|
assert.equal(sub.name, 'Sweden');
|
2014-02-05 20:35:16 +01:00
|
|
|
}());
|
|
|
|
|
2016-11-09 16:26:35 +01:00
|
|
|
(function test_unsubscribe() {
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
|
|
|
|
var sub = {name: 'devel', subscribed: false, stream_id: 1};
|
|
|
|
var me = {
|
|
|
|
email: 'me@zulip.com',
|
|
|
|
full_name: 'Current User',
|
2016-12-03 23:17:57 +01:00
|
|
|
user_id: 81,
|
2016-11-09 16:26:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// set up user data
|
|
|
|
people.add(me);
|
2017-01-20 23:16:28 +01:00
|
|
|
people.initialize_current_user(me.user_id);
|
2016-11-09 16:26:35 +01:00
|
|
|
|
|
|
|
// set up our subscription
|
|
|
|
stream_data.add_sub('devel', sub);
|
|
|
|
sub.subscribed = true;
|
|
|
|
stream_data.set_subscribers(sub, [me.user_id]);
|
|
|
|
|
|
|
|
// ensure our setup is accurate
|
|
|
|
assert(stream_data.is_subscribed('devel'));
|
|
|
|
|
|
|
|
// DO THE UNSUBSCRIBE HERE
|
|
|
|
stream_data.unsubscribe_myself(sub);
|
|
|
|
assert(!sub.subscribed);
|
|
|
|
assert(!stream_data.is_subscribed('devel'));
|
|
|
|
|
|
|
|
// make sure subsequent calls work
|
|
|
|
sub = stream_data.get_sub('devel');
|
|
|
|
assert(!sub.subscribed);
|
|
|
|
}());
|
|
|
|
|
2013-09-12 16:55:56 +02:00
|
|
|
(function test_subscribers() {
|
|
|
|
stream_data.clear_subscriptions();
|
2014-02-05 20:35:16 +01:00
|
|
|
var sub = {name: 'Rome', subscribed: true, stream_id: 1};
|
2013-09-12 16:55:56 +02:00
|
|
|
|
|
|
|
stream_data.add_sub('Rome', sub);
|
2013-09-16 22:26:53 +02:00
|
|
|
|
2016-10-30 15:47:20 +01:00
|
|
|
var fred = {
|
|
|
|
email: 'fred@zulip.com',
|
|
|
|
full_name: 'Fred',
|
2016-12-03 23:17:57 +01:00
|
|
|
user_id: 101,
|
2016-10-30 15:47:20 +01:00
|
|
|
};
|
|
|
|
var not_fred = {
|
|
|
|
email: 'not_fred@zulip.com',
|
|
|
|
full_name: 'Not Fred',
|
2016-12-03 23:17:57 +01:00
|
|
|
user_id: 102,
|
2016-10-30 15:47:20 +01:00
|
|
|
};
|
|
|
|
var george = {
|
|
|
|
email: 'george@zulip.com',
|
|
|
|
full_name: 'George',
|
2016-12-03 23:17:57 +01:00
|
|
|
user_id: 103,
|
2016-10-30 15:47:20 +01:00
|
|
|
};
|
|
|
|
people.add(fred);
|
|
|
|
people.add(not_fred);
|
|
|
|
people.add(george);
|
|
|
|
|
|
|
|
stream_data.set_subscribers(sub, [fred.user_id, george.user_id]);
|
2013-09-16 23:40:57 +02:00
|
|
|
assert(stream_data.user_is_subscribed('Rome', 'FRED@zulip.com'));
|
2013-09-16 22:26:53 +02:00
|
|
|
assert(stream_data.user_is_subscribed('Rome', 'fred@zulip.com'));
|
|
|
|
assert(stream_data.user_is_subscribed('Rome', 'george@zulip.com'));
|
|
|
|
assert(!stream_data.user_is_subscribed('Rome', 'not_fred@zulip.com'));
|
|
|
|
|
|
|
|
stream_data.set_subscribers(sub, []);
|
|
|
|
|
2013-09-12 16:55:56 +02:00
|
|
|
var email = 'brutus@zulip.com';
|
2016-10-30 15:47:20 +01:00
|
|
|
var brutus = {
|
|
|
|
email: email,
|
|
|
|
full_name: 'Brutus',
|
2016-12-03 23:17:57 +01:00
|
|
|
user_id: 104,
|
2016-10-30 15:47:20 +01:00
|
|
|
};
|
|
|
|
people.add(brutus);
|
2013-09-12 16:55:56 +02:00
|
|
|
assert(!stream_data.user_is_subscribed('Rome', email));
|
|
|
|
|
|
|
|
// add
|
2016-11-08 16:06:50 +01:00
|
|
|
stream_data.add_subscriber('Rome', brutus.user_id);
|
2013-09-12 16:55:56 +02:00
|
|
|
assert(stream_data.user_is_subscribed('Rome', email));
|
2016-03-14 06:38:43 +01:00
|
|
|
sub = stream_data.get_sub('Rome');
|
|
|
|
stream_data.update_subscribers_count(sub);
|
|
|
|
assert.equal(sub.subscriber_count, 1);
|
2013-09-12 16:55:56 +02:00
|
|
|
|
2016-08-23 23:39:06 +02:00
|
|
|
// verify that adding an already-added subscriber is a noop
|
2016-11-08 16:06:50 +01:00
|
|
|
stream_data.add_subscriber('Rome', brutus.user_id);
|
2013-09-12 16:55:56 +02:00
|
|
|
assert(stream_data.user_is_subscribed('Rome', email));
|
2016-03-14 06:38:43 +01:00
|
|
|
sub = stream_data.get_sub('Rome');
|
|
|
|
stream_data.update_subscribers_count(sub);
|
|
|
|
assert.equal(sub.subscriber_count, 1);
|
2013-09-12 16:55:56 +02:00
|
|
|
|
|
|
|
// remove
|
2016-11-09 16:05:30 +01:00
|
|
|
stream_data.remove_subscriber('Rome', brutus.user_id);
|
2013-09-12 16:55:56 +02:00
|
|
|
assert(!stream_data.user_is_subscribed('Rome', email));
|
2016-03-14 06:38:43 +01:00
|
|
|
sub = stream_data.get_sub('Rome');
|
|
|
|
stream_data.update_subscribers_count(sub);
|
|
|
|
assert.equal(sub.subscriber_count, 0);
|
2013-09-12 16:55:56 +02:00
|
|
|
|
2016-11-09 16:05:30 +01:00
|
|
|
// Defensive code will give warnings, which we ignore for the
|
|
|
|
// tests, but the defensive code needs to not actually blow up.
|
|
|
|
global.blueslip.warn = function () {};
|
|
|
|
|
2013-09-12 16:55:56 +02:00
|
|
|
// verify that removing an already-removed subscriber is a noop
|
2016-11-09 16:05:30 +01:00
|
|
|
stream_data.remove_subscriber('Rome', brutus.user_id);
|
2013-09-12 16:55:56 +02:00
|
|
|
assert(!stream_data.user_is_subscribed('Rome', email));
|
2016-03-14 06:38:43 +01:00
|
|
|
sub = stream_data.get_sub('Rome');
|
|
|
|
stream_data.update_subscribers_count(sub);
|
|
|
|
assert.equal(sub.subscriber_count, 0);
|
2013-09-12 16:55:56 +02:00
|
|
|
|
2013-10-15 19:08:35 +02:00
|
|
|
// Verify defensive code in set_subscribers, where the second parameter
|
|
|
|
// can be undefined.
|
|
|
|
stream_data.set_subscribers(sub);
|
|
|
|
stream_data.add_sub('Rome', sub);
|
2016-11-08 16:06:50 +01:00
|
|
|
stream_data.add_subscriber('Rome', brutus.user_id);
|
2013-10-15 19:08:35 +02:00
|
|
|
sub.subscribed = true;
|
|
|
|
assert(stream_data.user_is_subscribed('Rome', email));
|
|
|
|
|
2016-11-08 16:06:50 +01:00
|
|
|
// Verify that we noop and don't crash when unsubscribed.
|
2013-09-12 16:55:56 +02:00
|
|
|
sub.subscribed = false;
|
2016-11-08 16:06:50 +01:00
|
|
|
stream_data.add_subscriber('Rome', brutus.user_id);
|
2013-09-12 16:55:56 +02:00
|
|
|
assert.equal(stream_data.user_is_subscribed('Rome', email), undefined);
|
2016-11-09 16:05:30 +01:00
|
|
|
stream_data.remove_subscriber('Rome', brutus.user_id);
|
2013-09-12 16:55:56 +02:00
|
|
|
assert.equal(stream_data.user_is_subscribed('Rome', email), undefined);
|
|
|
|
|
|
|
|
}());
|
2016-10-17 16:38:15 +02:00
|
|
|
|
2016-10-28 17:31:18 +02:00
|
|
|
(function test_process_message_for_recent_topics() {
|
|
|
|
var message = {
|
|
|
|
stream: 'Rome',
|
|
|
|
timestamp: 101,
|
2016-12-03 23:17:57 +01:00
|
|
|
subject: 'toPic1',
|
2016-10-28 17:31:18 +02:00
|
|
|
};
|
|
|
|
stream_data.process_message_for_recent_topics(message);
|
|
|
|
|
2016-10-28 18:47:57 +02:00
|
|
|
var history = stream_data.get_recent_topics('Rome');
|
2016-10-28 17:31:18 +02:00
|
|
|
assert.deepEqual(history, [
|
|
|
|
{
|
|
|
|
subject: 'toPic1',
|
|
|
|
canon_subject: 'topic1',
|
|
|
|
count: 1,
|
2016-12-03 23:17:57 +01:00
|
|
|
timestamp: 101,
|
|
|
|
},
|
2016-10-28 17:31:18 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
message = {
|
|
|
|
stream: 'Rome',
|
|
|
|
timestamp: 102,
|
2016-12-03 23:17:57 +01:00
|
|
|
subject: 'Topic1',
|
2016-10-28 17:31:18 +02:00
|
|
|
};
|
|
|
|
stream_data.process_message_for_recent_topics(message);
|
2016-10-28 18:47:57 +02:00
|
|
|
history = stream_data.get_recent_topics('Rome');
|
2016-10-28 17:31:18 +02:00
|
|
|
assert.deepEqual(history, [
|
|
|
|
{
|
|
|
|
subject: 'Topic1',
|
|
|
|
canon_subject: 'topic1',
|
|
|
|
count: 2,
|
2016-12-03 23:17:57 +01:00
|
|
|
timestamp: 102,
|
|
|
|
},
|
2016-10-28 17:31:18 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
message = {
|
|
|
|
stream: 'Rome',
|
|
|
|
timestamp: 103,
|
2016-12-03 23:17:57 +01:00
|
|
|
subject: 'topic2',
|
2016-10-28 17:31:18 +02:00
|
|
|
};
|
|
|
|
stream_data.process_message_for_recent_topics(message);
|
2016-10-28 18:47:57 +02:00
|
|
|
history = stream_data.get_recent_topics('Rome');
|
2016-10-28 17:31:18 +02:00
|
|
|
assert.deepEqual(history, [
|
|
|
|
{
|
|
|
|
subject: 'topic2',
|
|
|
|
canon_subject: 'topic2',
|
|
|
|
count: 1,
|
2016-12-03 23:17:57 +01:00
|
|
|
timestamp: 103,
|
2016-10-28 17:31:18 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
subject: 'Topic1',
|
|
|
|
canon_subject: 'topic1',
|
|
|
|
count: 2,
|
2016-12-03 23:17:57 +01:00
|
|
|
timestamp: 102,
|
|
|
|
},
|
2016-10-28 17:31:18 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
stream_data.process_message_for_recent_topics(message, true);
|
2016-10-28 18:47:57 +02:00
|
|
|
history = stream_data.get_recent_topics('Rome');
|
2016-10-28 17:31:18 +02:00
|
|
|
assert.deepEqual(history, [
|
|
|
|
{
|
|
|
|
subject: 'Topic1',
|
|
|
|
canon_subject: 'topic1',
|
|
|
|
count: 2,
|
2016-12-03 23:17:57 +01:00
|
|
|
timestamp: 102,
|
|
|
|
},
|
2016-10-28 17:31:18 +02:00
|
|
|
]);
|
|
|
|
}());
|
|
|
|
|
2016-10-17 16:38:15 +02:00
|
|
|
(function test_admin_options() {
|
|
|
|
function make_sub() {
|
|
|
|
return {
|
|
|
|
subscribed: false,
|
|
|
|
color: 'blue',
|
|
|
|
name: 'stream_to_admin',
|
|
|
|
stream_id: 1,
|
|
|
|
in_home_view: false,
|
2016-12-03 23:17:57 +01:00
|
|
|
invite_only: false,
|
2016-10-17 16:38:15 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// non-admins can't do anything
|
|
|
|
global.page_params.is_admin = false;
|
|
|
|
var sub = make_sub();
|
|
|
|
stream_data.add_admin_options(sub);
|
|
|
|
assert(!sub.is_admin);
|
|
|
|
assert(!sub.can_make_public);
|
|
|
|
assert(!sub.can_make_private);
|
|
|
|
|
|
|
|
// just a sanity check that we leave "normal" fields alone
|
|
|
|
assert.equal(sub.color, 'blue');
|
|
|
|
|
|
|
|
// the remaining cases are for admin users
|
|
|
|
global.page_params.is_admin = true;
|
|
|
|
|
|
|
|
// admins can make public streams become private
|
|
|
|
sub = make_sub();
|
|
|
|
stream_data.add_admin_options(sub);
|
|
|
|
assert(sub.is_admin);
|
|
|
|
assert(!sub.can_make_public);
|
|
|
|
assert(sub.can_make_private);
|
|
|
|
|
|
|
|
// admins can only make private streams become public
|
|
|
|
// if they are subscribed
|
|
|
|
sub = make_sub();
|
|
|
|
sub.invite_only = true;
|
|
|
|
sub.subscribed = false;
|
|
|
|
stream_data.add_admin_options(sub);
|
|
|
|
assert(sub.is_admin);
|
|
|
|
assert(!sub.can_make_public);
|
|
|
|
assert(!sub.can_make_private);
|
|
|
|
|
|
|
|
sub = make_sub();
|
|
|
|
sub.invite_only = true;
|
|
|
|
sub.subscribed = true;
|
|
|
|
stream_data.add_admin_options(sub);
|
|
|
|
assert(sub.is_admin);
|
|
|
|
assert(sub.can_make_public);
|
|
|
|
assert(!sub.can_make_private);
|
|
|
|
}());
|
2016-10-17 17:48:56 +02:00
|
|
|
|
|
|
|
(function test_stream_settings() {
|
|
|
|
var cinnamon = {
|
|
|
|
stream_id: 1,
|
|
|
|
name: 'c',
|
|
|
|
color: 'cinnamon',
|
2016-12-03 23:17:57 +01:00
|
|
|
subscribed: true,
|
2016-10-17 17:48:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var blue = {
|
|
|
|
stream_id: 2,
|
|
|
|
name: 'b',
|
|
|
|
color: 'blue',
|
2016-12-03 23:17:57 +01:00
|
|
|
subscribed: false,
|
2016-10-17 17:48:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var amber = {
|
|
|
|
stream_id: 3,
|
|
|
|
name: 'a',
|
|
|
|
color: 'amber',
|
2016-12-03 23:17:57 +01:00
|
|
|
subscribed: true,
|
2016-10-17 17:48:56 +02:00
|
|
|
};
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
stream_data.add_sub(cinnamon.name, cinnamon);
|
|
|
|
stream_data.add_sub(amber.name, amber);
|
2016-10-25 21:45:19 +02:00
|
|
|
stream_data.add_sub(blue.name, blue);
|
2016-10-17 17:48:56 +02:00
|
|
|
|
2016-10-25 21:45:19 +02:00
|
|
|
var sub_rows = stream_data.get_streams_for_settings_page();
|
2016-11-01 22:32:10 +01:00
|
|
|
assert.equal(sub_rows[0].color, 'blue');
|
|
|
|
assert.equal(sub_rows[1].color, 'amber');
|
|
|
|
assert.equal(sub_rows[2].color, 'cinnamon');
|
2016-10-17 17:48:56 +02:00
|
|
|
|
|
|
|
}());
|
2017-02-16 03:47:08 +01:00
|
|
|
|
|
|
|
(function test_delete_sub() {
|
|
|
|
var canada = {
|
|
|
|
stream_id: 101,
|
|
|
|
name: 'Canada',
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
stream_data.add_sub('Canada', canada);
|
|
|
|
|
|
|
|
assert(stream_data.is_subscribed('Canada'));
|
|
|
|
assert(stream_data.get_sub('Canada').stream_id, canada.stream_id);
|
|
|
|
assert(stream_data.get_sub_by_id(canada.stream_id).name, 'Canada');
|
|
|
|
|
|
|
|
stream_data.delete_sub(canada.stream_id);
|
|
|
|
assert(!stream_data.is_subscribed('Canada'));
|
|
|
|
assert(!stream_data.get_sub('Canada'));
|
|
|
|
assert(!stream_data.get_sub_by_id(canada.stream_id));
|
|
|
|
}());
|