Move add_admin_options() to stream_data.js.

This function used to live in subs.js.  It's mostly a code move,
but I simplified the logic to determine whether it's subscribed
not to do a lookup into the same data structure that the sub
already came from.

I also added some tests.
This commit is contained in:
Steve Howell 2016-10-17 07:38:15 -07:00 committed by Tim Abbott
parent cf08f04dbc
commit 965c6047db
3 changed files with 65 additions and 11 deletions

View File

@ -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);
}());

View File

@ -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;
}());

View File

@ -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);