diff --git a/frontend_tests/node_tests/stream_data.js b/frontend_tests/node_tests/stream_data.js index cc78df039b..398bbb2840 100644 --- a/frontend_tests/node_tests/stream_data.js +++ b/frontend_tests/node_tests/stream_data.js @@ -5,6 +5,7 @@ add_dependencies({ }); set_global('blueslip', {}); +set_global('page_params', {is_admin: false}); var stream_data = require('js/stream_data.js'); @@ -134,3 +135,55 @@ var stream_data = require('js/stream_data.js'); assert.equal(stream_data.user_is_subscribed('Rome', email), undefined); }()); + +(function test_admin_options() { + function make_sub() { + return { + subscribed: false, + color: 'blue', + name: 'stream_to_admin', + stream_id: 1, + in_home_view: false, + invite_only: false + }; + } + + // 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); +}()); diff --git a/static/js/stream_data.js b/static/js/stream_data.js index ad7fafbc3a..f5f3b0d81a 100644 --- a/static/js/stream_data.js +++ b/static/js/stream_data.js @@ -209,6 +209,15 @@ exports.receives_audible_notifications = function (stream_name) { return sub.audible_notifications; }; +exports.add_admin_options = function (sub) { + return _.extend(sub, { + 'is_admin': page_params.is_admin, + 'can_make_public': page_params.is_admin && sub.invite_only && sub.subscribed, + 'can_make_private': page_params.is_admin && !sub.invite_only + }); +}; + + return exports; }()); diff --git a/static/js/subs.js b/static/js/subs.js index 9c89462b75..0dc1212bcd 100644 --- a/static/js/subs.js +++ b/static/js/subs.js @@ -2,14 +2,6 @@ var subs = (function () { var exports = {}; -function add_admin_options(sub) { - return _.extend(sub, { - 'is_admin': page_params.is_admin, - 'can_make_public': page_params.is_admin && sub.invite_only && stream_data.is_subscribed(sub.name), - 'can_make_private': page_params.is_admin && !sub.invite_only - }); -} - function get_color() { var used_colors = stream_data.get_colors(); var color = stream_color.pick_color(used_colors); @@ -255,7 +247,7 @@ function add_email_hint(row, email_address_hint_content) { } function add_sub_to_table(sub) { - sub = add_admin_options(sub); + sub = stream_data.add_admin_options(sub); var html = templates.render('subscription', sub); $('#create_or_filter_stream_row').after(html); settings_for_sub(sub).collapse('show'); @@ -493,7 +485,7 @@ exports.setup_page = function () { // Add in admin options. var sub_rows = []; _.each(all_subs, function (sub) { - sub = add_admin_options(sub); + sub = stream_data.add_admin_options(sub); sub_rows.push(sub); }); @@ -1125,7 +1117,7 @@ $(function () { function redraw_privacy_related_stuff(sub_row, sub) { var html; - sub = add_admin_options(sub); + sub = stream_data.add_admin_options(sub); html = templates.render('subscription_setting_icon', sub); sub_row.find('.subscription-setting-icon').expectOne().html(html);