2016-10-30 15:47:20 +01:00
|
|
|
set_global('page_params', {
|
|
|
|
is_admin: false,
|
2017-04-24 21:59:07 +02:00
|
|
|
realm_users: [],
|
2016-10-30 15:47:20 +01:00
|
|
|
});
|
|
|
|
|
2017-06-20 04:34:04 +02:00
|
|
|
set_global('$', function () {
|
|
|
|
});
|
|
|
|
|
2013-09-16 16:56:46 +02:00
|
|
|
set_global('blueslip', {});
|
|
|
|
|
2017-08-09 18:47:44 +02:00
|
|
|
zrequire('util');
|
|
|
|
zrequire('hash_util');
|
|
|
|
zrequire('narrow');
|
|
|
|
zrequire('topic_data');
|
|
|
|
zrequire('people');
|
|
|
|
zrequire('stream_color');
|
|
|
|
zrequire('stream_data');
|
2017-12-08 17:14:56 +01:00
|
|
|
zrequire('marked', 'third/marked/lib/marked');
|
2013-08-19 18:46:21 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('basics', () => {
|
2013-08-19 18:46:21 +02:00
|
|
|
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,
|
2018-05-14 12:06:56 +02:00
|
|
|
is_announcement_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'));
|
2018-05-14 12:06:56 +02:00
|
|
|
assert(stream_data.get_announcement_only('social'));
|
|
|
|
assert(!stream_data.get_announcement_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
|
|
|
|
2017-05-13 20:54:53 +02:00
|
|
|
assert(stream_data.in_home_view(social.stream_id));
|
|
|
|
assert(!stream_data.in_home_view(denmark.stream_id));
|
2017-07-15 19:12:31 +02:00
|
|
|
|
|
|
|
assert.equal(stream_data.maybe_get_stream_name(), undefined);
|
|
|
|
assert.equal(stream_data.maybe_get_stream_name(social.stream_id), 'social');
|
|
|
|
assert.equal(stream_data.maybe_get_stream_name(42), undefined);
|
2017-12-03 20:40:01 +01:00
|
|
|
|
|
|
|
stream_data.set_realm_default_streams([denmark]);
|
|
|
|
assert(stream_data.get_default_status('Denmark'));
|
|
|
|
assert(!stream_data.get_default_status('social'));
|
|
|
|
assert(!stream_data.get_default_status('UNKNOWN'));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-23 05:29:41 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('renames', () => {
|
2014-02-05 20:35:16 +01:00
|
|
|
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
|
|
|
|
2017-03-05 00:35:45 +01:00
|
|
|
stream_data.rename_sub(sub, 'Sweden');
|
2016-10-30 17:33:23 +01:00
|
|
|
sub = stream_data.get_sub_by_id(id);
|
|
|
|
assert.equal(sub.color, 'red');
|
|
|
|
assert.equal(sub.name, 'Sweden');
|
2017-05-11 21:49:38 +02:00
|
|
|
|
|
|
|
sub = stream_data.get_sub('Denmark');
|
|
|
|
assert.equal(sub, undefined);
|
|
|
|
|
|
|
|
sub = stream_data.get_sub_by_name('Denmark');
|
|
|
|
assert.equal(sub.name, 'Sweden');
|
|
|
|
|
|
|
|
var actual_id = stream_data.get_stream_id('Denmark');
|
|
|
|
assert.equal(actual_id, 42);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2014-02-05 20:35:16 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('unsubscribe', () => {
|
2016-11-09 16:26:35 +01:00
|
|
|
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);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-11-09 16:26:35 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('subscribers', () => {
|
2013-09-12 16:55:56 +02:00
|
|
|
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]);
|
2018-03-19 05:52:37 +01:00
|
|
|
stream_data.update_calculated_fields(sub);
|
2018-04-06 05:22:07 +02:00
|
|
|
assert(stream_data.is_user_subscribed('Rome', fred.user_id));
|
|
|
|
assert(stream_data.is_user_subscribed('Rome', george.user_id));
|
|
|
|
assert(!stream_data.is_user_subscribed('Rome', not_fred.user_id));
|
2013-09-16 22:26:53 +02:00
|
|
|
|
|
|
|
stream_data.set_subscribers(sub, []);
|
|
|
|
|
2016-10-30 15:47:20 +01:00
|
|
|
var brutus = {
|
2018-04-06 05:22:07 +02:00
|
|
|
email: 'brutus@zulip.com',
|
2016-10-30 15:47:20 +01:00
|
|
|
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);
|
2018-04-06 05:22:07 +02:00
|
|
|
assert(!stream_data.is_user_subscribed('Rome', brutus.user_id));
|
2013-09-12 16:55:56 +02:00
|
|
|
|
|
|
|
// add
|
2017-03-03 23:14:06 +01:00
|
|
|
var ok = stream_data.add_subscriber('Rome', brutus.user_id);
|
|
|
|
assert(ok);
|
2018-04-06 05:22:07 +02:00
|
|
|
assert(stream_data.is_user_subscribed('Rome', brutus.user_id));
|
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);
|
2018-04-05 19:58:27 +02:00
|
|
|
var sub_email = "Rome:214125235@zulipdev.com:9991";
|
|
|
|
stream_data.update_stream_email_address(sub, sub_email);
|
|
|
|
assert.equal(sub.email_address, sub_email);
|
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);
|
2018-04-06 05:22:07 +02:00
|
|
|
assert(stream_data.is_user_subscribed('Rome', brutus.user_id));
|
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
|
2017-03-03 23:14:06 +01:00
|
|
|
ok = stream_data.remove_subscriber('Rome', brutus.user_id);
|
|
|
|
assert(ok);
|
2018-04-06 05:22:07 +02:00
|
|
|
assert(!stream_data.is_user_subscribed('Rome', brutus.user_id));
|
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
|
|
|
|
2018-03-25 14:53:01 +02:00
|
|
|
// verify that deactivating user should unsubscribe user from all streams
|
|
|
|
assert(stream_data.add_subscriber('Rome', george.user_id));
|
|
|
|
set_global('subs', { rerender_subscriptions_settings: function () {} });
|
|
|
|
stream_data.remove_deactivated_user_from_all_streams(george.user_id);
|
|
|
|
assert(!stream_data.is_user_subscribed('Rome', george.user_id));
|
|
|
|
|
2018-04-06 05:22:07 +02:00
|
|
|
// verify that checking subscription with undefined user id
|
2017-12-03 18:46:30 +01:00
|
|
|
global.blueslip.warn = function (msg) {
|
2018-04-06 05:22:07 +02:00
|
|
|
assert.equal(msg, "Undefined user_id passed to function is_user_subscribed");
|
2017-12-03 18:46:30 +01:00
|
|
|
};
|
2018-04-06 05:22:07 +02:00
|
|
|
assert.equal(stream_data.is_user_subscribed('Rome', undefined), undefined);
|
2017-12-03 18:46:30 +01:00
|
|
|
|
2017-12-03 19:50:50 +01:00
|
|
|
// Verify noop for bad stream when removing subscriber
|
|
|
|
var bad_stream = 'UNKNOWN';
|
|
|
|
global.blueslip.warn = function (msg) {
|
|
|
|
assert.equal(msg, "We got a remove_subscriber call for a non-existent stream " + bad_stream);
|
|
|
|
};
|
|
|
|
ok = stream_data.remove_subscriber(bad_stream, brutus.user_id);
|
|
|
|
assert(!ok);
|
|
|
|
|
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
|
2017-03-03 23:14:06 +01:00
|
|
|
ok = stream_data.remove_subscriber('Rome', brutus.user_id);
|
|
|
|
assert(!ok);
|
2018-04-06 05:22:07 +02:00
|
|
|
assert(!stream_data.is_user_subscribed('Rome', brutus.user_id));
|
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;
|
2018-04-06 05:22:07 +02:00
|
|
|
assert(stream_data.is_user_subscribed('Rome', brutus.user_id));
|
2013-10-15 19:08:35 +02:00
|
|
|
|
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;
|
2018-03-19 05:52:37 +01:00
|
|
|
stream_data.update_calculated_fields(sub);
|
2017-03-03 23:14:06 +01:00
|
|
|
ok = stream_data.add_subscriber('Rome', brutus.user_id);
|
|
|
|
assert(ok);
|
2018-04-06 05:22:07 +02:00
|
|
|
assert.equal(stream_data.is_user_subscribed('Rome', brutus.user_id), true);
|
2018-03-19 05:52:37 +01:00
|
|
|
stream_data.remove_subscriber('Rome', brutus.user_id);
|
2018-04-06 05:22:07 +02:00
|
|
|
assert.equal(stream_data.is_user_subscribed('Rome', brutus.user_id), false);
|
2018-03-19 05:52:37 +01:00
|
|
|
stream_data.add_subscriber('Rome', brutus.user_id);
|
2018-04-06 05:22:07 +02:00
|
|
|
assert.equal(stream_data.is_user_subscribed('Rome', brutus.user_id), true);
|
2018-03-19 05:52:37 +01:00
|
|
|
|
|
|
|
sub.invite_only = true;
|
|
|
|
stream_data.update_calculated_fields(sub);
|
2018-04-06 05:22:07 +02:00
|
|
|
assert.equal(stream_data.is_user_subscribed('Rome', brutus.user_id), undefined);
|
2016-11-09 16:05:30 +01:00
|
|
|
stream_data.remove_subscriber('Rome', brutus.user_id);
|
2018-04-06 05:22:07 +02:00
|
|
|
assert.equal(stream_data.is_user_subscribed('Rome', brutus.user_id), undefined);
|
2013-09-12 16:55:56 +02:00
|
|
|
|
2017-03-03 23:14:06 +01:00
|
|
|
// Verify that we don't crash and return false for a bad stream.
|
|
|
|
ok = stream_data.add_subscriber('UNKNOWN', brutus.user_id);
|
|
|
|
assert(!ok);
|
|
|
|
|
|
|
|
// Verify that we don't crash and return false for a bad user id.
|
|
|
|
global.blueslip.error = function () {};
|
|
|
|
ok = stream_data.add_subscriber('Rome', 9999999);
|
|
|
|
assert(!ok);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-10-17 16:38:15 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('is_active', () => {
|
2017-04-28 15:59:30 +02:00
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
|
|
|
|
var sub = {name: 'pets', subscribed: false, stream_id: 1};
|
|
|
|
stream_data.add_sub('pets', sub);
|
|
|
|
|
|
|
|
assert(!stream_data.is_active(sub));
|
|
|
|
|
|
|
|
stream_data.subscribe_myself(sub);
|
|
|
|
assert(stream_data.is_active(sub));
|
|
|
|
|
|
|
|
stream_data.unsubscribe_myself(sub);
|
|
|
|
assert(!stream_data.is_active(sub));
|
|
|
|
|
2017-05-14 15:39:02 +02:00
|
|
|
sub = {name: 'lunch', subscribed: false, stream_id: 222};
|
2017-04-28 15:59:30 +02:00
|
|
|
stream_data.add_sub('lunch', sub);
|
|
|
|
|
|
|
|
assert(!stream_data.is_active(sub));
|
|
|
|
|
2017-07-26 14:05:25 +02:00
|
|
|
var opts = {
|
2017-05-14 15:39:02 +02:00
|
|
|
stream_id: 222,
|
2017-07-26 14:05:25 +02:00
|
|
|
message_id: 108,
|
|
|
|
topic_name: 'topic2',
|
2017-04-28 15:59:30 +02:00
|
|
|
};
|
2017-07-26 14:05:25 +02:00
|
|
|
topic_data.add_message(opts);
|
2017-04-28 15:59:30 +02:00
|
|
|
|
|
|
|
assert(stream_data.is_active(sub));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-04-28 15:59:30 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('admin_options', () => {
|
2016-10-17 16:38:15 +02:00
|
|
|
function make_sub() {
|
2017-04-28 17:55:22 +02:00
|
|
|
var sub = {
|
2016-10-17 16:38:15 +02:00
|
|
|
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
|
|
|
};
|
2017-04-28 17:55:22 +02:00
|
|
|
stream_data.add_sub(sub.name, sub);
|
|
|
|
return sub;
|
2016-10-17 16:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// non-admins can't do anything
|
|
|
|
global.page_params.is_admin = false;
|
|
|
|
var sub = make_sub();
|
2017-04-28 17:55:22 +02:00
|
|
|
stream_data.update_calculated_fields(sub);
|
2016-10-17 16:38:15 +02:00
|
|
|
assert(!sub.is_admin);
|
2018-05-22 01:14:18 +02:00
|
|
|
assert(!sub.can_change_stream_permissions);
|
2016-10-17 16:38:15 +02:00
|
|
|
|
|
|
|
// 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();
|
2017-04-28 17:55:22 +02:00
|
|
|
stream_data.update_calculated_fields(sub);
|
2016-10-17 16:38:15 +02:00
|
|
|
assert(sub.is_admin);
|
2018-05-22 01:14:18 +02:00
|
|
|
assert(sub.can_change_stream_permissions);
|
2016-10-17 16:38:15 +02:00
|
|
|
|
|
|
|
// admins can only make private streams become public
|
|
|
|
// if they are subscribed
|
|
|
|
sub = make_sub();
|
|
|
|
sub.invite_only = true;
|
|
|
|
sub.subscribed = false;
|
2017-04-28 17:55:22 +02:00
|
|
|
stream_data.update_calculated_fields(sub);
|
2016-10-17 16:38:15 +02:00
|
|
|
assert(sub.is_admin);
|
2018-05-22 01:14:18 +02:00
|
|
|
assert(!sub.can_change_stream_permissions);
|
2016-10-17 16:38:15 +02:00
|
|
|
|
|
|
|
sub = make_sub();
|
|
|
|
sub.invite_only = true;
|
|
|
|
sub.subscribed = true;
|
2017-04-28 17:55:22 +02:00
|
|
|
stream_data.update_calculated_fields(sub);
|
2016-10-17 16:38:15 +02:00
|
|
|
assert(sub.is_admin);
|
2018-05-22 01:14:18 +02:00
|
|
|
assert(sub.can_change_stream_permissions);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-10-17 17:48:56 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('stream_settings', () => {
|
2016-10-17 17:48:56 +02:00
|
|
|
var cinnamon = {
|
|
|
|
stream_id: 1,
|
|
|
|
name: 'c',
|
|
|
|
color: 'cinnamon',
|
2016-12-03 23:17:57 +01:00
|
|
|
subscribed: true,
|
2017-08-22 20:51:41 +02:00
|
|
|
invite_only: false,
|
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,
|
2017-08-22 20:51:41 +02:00
|
|
|
invite_only: 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,
|
2017-08-22 20:51:41 +02:00
|
|
|
invite_only: 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-08-22 20:51:41 +02:00
|
|
|
sub_rows = stream_data.get_streams_for_admin();
|
|
|
|
assert.equal(sub_rows[0].name, 'a');
|
|
|
|
assert.equal(sub_rows[1].name, 'b');
|
|
|
|
assert.equal(sub_rows[2].name, 'c');
|
|
|
|
assert.equal(sub_rows[0].invite_only, true);
|
|
|
|
assert.equal(sub_rows[1].invite_only, false);
|
|
|
|
assert.equal(sub_rows[2].invite_only, false);
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-02-16 03:47:08 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('get_non_default_stream_names', () => {
|
2017-08-22 20:00:09 +02:00
|
|
|
var announce = {
|
|
|
|
stream_id: 101,
|
|
|
|
name: 'announce',
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
var public_stream = {
|
|
|
|
stream_id: 102,
|
|
|
|
name: 'public',
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
var private_stream = {
|
|
|
|
stream_id: 103,
|
|
|
|
name: 'private',
|
|
|
|
subscribed: true,
|
|
|
|
invite_only: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
stream_data.set_realm_default_streams([announce]);
|
|
|
|
stream_data.add_sub('announce', announce);
|
|
|
|
stream_data.add_sub('public_stream', public_stream);
|
|
|
|
stream_data.add_sub('private_stream', private_stream);
|
|
|
|
|
|
|
|
var names = stream_data.get_non_default_stream_names();
|
2018-03-04 08:25:59 +01:00
|
|
|
assert.deepEqual(names, ['public', 'private']);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-08-22 20:00:09 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('delete_sub', () => {
|
2017-02-16 03:47:08 +01:00
|
|
|
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));
|
2017-12-14 16:23:19 +01:00
|
|
|
|
|
|
|
var warned = false;
|
|
|
|
blueslip.warn = function (msg) {
|
|
|
|
warned = true;
|
|
|
|
assert.equal(msg, 'Failed to delete stream does_not_exist');
|
|
|
|
};
|
|
|
|
stream_data.delete_sub('does_not_exist');
|
|
|
|
assert(warned);
|
|
|
|
blueslip.warn = function () {};
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-29 15:35:34 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('get_subscriber_count', () => {
|
2017-06-29 15:35:34 +02:00
|
|
|
var india = {
|
|
|
|
stream_id: 102,
|
|
|
|
name: 'India',
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
assert.equal(stream_data.get_subscriber_count('India'), undefined);
|
|
|
|
stream_data.add_sub('India', india);
|
|
|
|
assert.equal(stream_data.get_subscriber_count('India'), 0);
|
|
|
|
|
|
|
|
var fred = {
|
|
|
|
email: 'fred@zulip.com',
|
|
|
|
full_name: 'Fred',
|
|
|
|
user_id: 101,
|
|
|
|
};
|
|
|
|
people.add(fred);
|
|
|
|
stream_data.add_subscriber('India', 102);
|
|
|
|
assert.equal(stream_data.get_subscriber_count('India'), 1);
|
|
|
|
var george = {
|
|
|
|
email: 'george@zulip.com',
|
|
|
|
full_name: 'George',
|
|
|
|
user_id: 103,
|
|
|
|
};
|
|
|
|
people.add(george);
|
|
|
|
stream_data.add_subscriber('India', 103);
|
|
|
|
assert.equal(stream_data.get_subscriber_count('India'), 2);
|
2017-12-08 16:16:59 +01:00
|
|
|
|
|
|
|
var sub = stream_data.get_sub_by_name('India');
|
|
|
|
delete sub.subscribers;
|
|
|
|
assert.deepStrictEqual(stream_data.get_subscriber_count('India'), 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-03 05:30:58 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('notifications', () => {
|
2017-12-03 05:30:58 +01:00
|
|
|
var india = {
|
|
|
|
stream_id: 102,
|
|
|
|
name: 'India',
|
|
|
|
subscribed: true,
|
|
|
|
desktop_notifications: true,
|
2017-12-03 05:33:29 +01:00
|
|
|
audible_notifications: true,
|
2017-12-03 05:30:58 +01:00
|
|
|
};
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
stream_data.add_sub('India', india);
|
|
|
|
assert(stream_data.receives_desktop_notifications('India'));
|
|
|
|
assert(!stream_data.receives_desktop_notifications('Indiana'));
|
2017-12-03 05:33:29 +01:00
|
|
|
|
|
|
|
assert(stream_data.receives_audible_notifications('India'));
|
|
|
|
assert(!stream_data.receives_audible_notifications('Indiana'));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-05 18:18:25 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('in_home_view', () => {
|
2018-05-07 03:30:13 +02:00
|
|
|
var tony = {
|
|
|
|
stream_id: 999,
|
|
|
|
name: 'tony',
|
|
|
|
subscribed: true,
|
|
|
|
in_home_view: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
var jazy = {
|
|
|
|
stream_id: 500,
|
|
|
|
name: 'jazy',
|
|
|
|
subscribed: false,
|
|
|
|
in_home_view: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub('tony', tony);
|
|
|
|
stream_data.add_sub('jazy', jazy);
|
|
|
|
assert(stream_data.name_in_home_view('tony'));
|
|
|
|
assert(!stream_data.name_in_home_view('jazy'));
|
|
|
|
assert(!stream_data.name_in_home_view('EEXISTS'));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-05 18:46:01 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('notifications_in_home_view', () => {
|
2017-12-05 18:46:01 +01:00
|
|
|
page_params.notifications_stream = 'tony';
|
|
|
|
assert(stream_data.notifications_in_home_view());
|
|
|
|
|
|
|
|
page_params.notifications_stream = 'jazy';
|
|
|
|
assert(!stream_data.notifications_in_home_view());
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-07 02:32:47 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('remove_default_stream', () => {
|
2017-12-07 02:32:47 +01:00
|
|
|
var remove_me = {
|
|
|
|
stream_id: 674,
|
|
|
|
name: 'remove_me',
|
|
|
|
subscribed: false,
|
|
|
|
in_home_view: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub('remove_me', remove_me);
|
|
|
|
stream_data.set_realm_default_streams([remove_me]);
|
|
|
|
stream_data.remove_default_stream(remove_me.stream_id);
|
|
|
|
assert(!stream_data.get_default_status('remove_me'));
|
|
|
|
assert.equal(page_params.realm_default_streams.length, 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-08 17:14:56 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('render_stream_description', () => {
|
2017-12-08 17:14:56 +01:00
|
|
|
var desc = {
|
|
|
|
name: 'no_desc',
|
|
|
|
stream_id: 1002,
|
|
|
|
description: '<p>rendered desc</p>',
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub('desc', desc);
|
|
|
|
var sub = stream_data.get_sub_by_name('desc');
|
|
|
|
stream_data.render_stream_description(sub);
|
|
|
|
assert.deepStrictEqual(sub.rendered_description, "rendered desc");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-08 18:17:38 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('canonicalized_name', () => {
|
2017-12-08 18:17:38 +01:00
|
|
|
assert.deepStrictEqual(
|
|
|
|
stream_data.canonicalized_name('Stream_Bar'),
|
|
|
|
"stream_bar"
|
|
|
|
);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-05 09:22:13 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('create_sub', () => {
|
2017-12-05 09:22:13 +01:00
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
var india = {
|
|
|
|
stream_id: 102,
|
|
|
|
name: 'India',
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
var canada = {
|
|
|
|
name: 'Canada',
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
var antarctica = {
|
|
|
|
stream_id: 103,
|
|
|
|
name: 'Antarctica',
|
|
|
|
subscribed: true,
|
|
|
|
color: '#76ce90',
|
|
|
|
};
|
|
|
|
|
|
|
|
global.stream_color.pick_color = function () {
|
|
|
|
return '#bd86e5';
|
|
|
|
};
|
|
|
|
|
|
|
|
var india_sub = stream_data.create_sub_from_server_data('India', india);
|
|
|
|
assert(india_sub);
|
|
|
|
assert.equal(india_sub.color, '#bd86e5');
|
|
|
|
var new_sub = stream_data.create_sub_from_server_data('India', india); // make sure sub doesn't get created twice
|
|
|
|
assert.equal(india_sub, new_sub);
|
|
|
|
|
|
|
|
var called = false;
|
|
|
|
global.blueslip.fatal = function (msg) {
|
|
|
|
assert.equal(msg, 'We cannot create a sub without a stream_id');
|
|
|
|
called = true;
|
|
|
|
};
|
|
|
|
var ok = stream_data.create_sub_from_server_data('Canada', canada);
|
|
|
|
assert.equal(ok, undefined);
|
|
|
|
assert(called);
|
|
|
|
|
|
|
|
var antarctica_sub = stream_data.create_sub_from_server_data('Antarctica', antarctica);
|
|
|
|
assert(antarctica_sub);
|
|
|
|
assert.equal(antarctica_sub.color, '#76ce90');
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-05 09:22:13 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('initialize_from_page_params', () => {
|
2017-12-14 18:00:30 +01:00
|
|
|
function initialize() {
|
|
|
|
page_params.subscriptions = [{
|
|
|
|
name: 'subscriptions',
|
|
|
|
}];
|
|
|
|
|
|
|
|
page_params.unsubscribed = [{
|
|
|
|
name: 'unsubscribed',
|
|
|
|
}];
|
|
|
|
|
|
|
|
page_params.never_subscribed = [{
|
|
|
|
name: 'never_subscribed',
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
initialize();
|
|
|
|
page_params.realm_notifications_stream_id = -1;
|
|
|
|
stream_data.initialize_from_page_params();
|
|
|
|
|
|
|
|
assert(!page_params.subscriptions);
|
|
|
|
assert(!page_params.unsubscribed);
|
|
|
|
assert(!page_params.never_subscribed);
|
|
|
|
assert.equal(page_params.notifications_stream, "");
|
|
|
|
|
2018-05-03 17:28:38 +02:00
|
|
|
// Simulate a private stream the user isn't subscribed to
|
|
|
|
initialize();
|
|
|
|
page_params.realm_notifications_stream_id = 89;
|
|
|
|
stream_data.initialize_from_page_params();
|
|
|
|
assert.equal(page_params.notifications_stream, "");
|
|
|
|
|
|
|
|
// Now actually subscribe the user to the stream
|
2017-12-14 18:00:30 +01:00
|
|
|
initialize();
|
|
|
|
var foo = {
|
|
|
|
name: 'foo',
|
|
|
|
stream_id: 89,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub('foo', foo);
|
|
|
|
stream_data.initialize_from_page_params();
|
|
|
|
|
|
|
|
assert.equal(page_params.notifications_stream, "foo");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-22 14:59:10 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('get_newbie_stream', () => {
|
2017-12-22 14:59:10 +01:00
|
|
|
var newbie = {
|
|
|
|
name: 'newbie',
|
|
|
|
stream_id: 234,
|
|
|
|
subscribed: true,
|
|
|
|
in_home_view: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
var new_members = {
|
|
|
|
subscribed: true,
|
|
|
|
name: 'new members',
|
|
|
|
stream_id: 531,
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.equal(stream_data.get_newbie_stream(), undefined);
|
|
|
|
|
|
|
|
stream_data.add_sub('newbie', newbie);
|
|
|
|
page_params.notifications_stream = 'newbie';
|
|
|
|
assert.equal(stream_data.get_newbie_stream(), 'newbie');
|
|
|
|
|
|
|
|
newbie.in_home_view = false;
|
|
|
|
assert.equal(stream_data.get_newbie_stream(), undefined);
|
|
|
|
|
|
|
|
stream_data.add_sub('new members', new_members);
|
|
|
|
assert.equal(stream_data.get_newbie_stream(), 'new members');
|
|
|
|
|
|
|
|
new_members.subscribed = false;
|
|
|
|
assert.equal(stream_data.get_newbie_stream(), undefined);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-14 17:04:36 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('invite_streams', () => {
|
2017-12-14 17:04:36 +01:00
|
|
|
// add default stream
|
|
|
|
var orie = {
|
|
|
|
stream_id: 320,
|
|
|
|
name: 'Orie',
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
// clear all the data form stream_data, and people
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
people.init();
|
|
|
|
|
|
|
|
stream_data.add_sub('Orie', orie);
|
|
|
|
stream_data.set_realm_default_streams([orie]);
|
|
|
|
|
|
|
|
var expected_list = ['Orie'];
|
|
|
|
assert.deepEqual(stream_data.invite_streams(), expected_list);
|
|
|
|
|
|
|
|
var inviter = {
|
|
|
|
stream_id: 25,
|
|
|
|
name: 'Inviter',
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
stream_data.add_sub('Inviter', inviter);
|
|
|
|
|
|
|
|
expected_list.push('Inviter');
|
|
|
|
assert.deepEqual(stream_data.invite_streams(), expected_list);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|