|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +/* |
| 4 | + This mostly tests the peer_data module, but it |
| 5 | + also tests some stream_data functions that are |
| 6 | + glorified wrappers for peer_data functions. |
| 7 | +*/ |
| 8 | + |
| 9 | +const {strict: assert} = require("assert"); |
| 10 | + |
| 11 | +const {set_global, with_field, zrequire} = require("../zjsunit/namespace"); |
| 12 | +const {run_test} = require("../zjsunit/test"); |
| 13 | + |
| 14 | +const peer_data = zrequire("peer_data"); |
| 15 | +const people = zrequire("people"); |
| 16 | +zrequire("hash_util"); |
| 17 | +zrequire("stream_data"); |
| 18 | + |
| 19 | +set_global("page_params", { |
| 20 | + is_admin: false, |
| 21 | + realm_users: [], |
| 22 | + is_guest: false, |
| 23 | +}); |
| 24 | + |
| 25 | +const me = { |
| 26 | + |
| 27 | + full_name: "Current User", |
| 28 | + user_id: 100, |
| 29 | +}; |
| 30 | + |
| 31 | +// set up user data |
| 32 | +people.add_active_user(me); |
| 33 | +people.initialize_current_user(me.user_id); |
| 34 | + |
| 35 | +function contains_sub(subs, sub) { |
| 36 | + return subs.some((s) => s.name === sub.name); |
| 37 | +} |
| 38 | + |
| 39 | +run_test("unsubscribe", () => { |
| 40 | + stream_data.clear_subscriptions(); |
| 41 | + |
| 42 | + let sub = {name: "devel", subscribed: false, stream_id: 1}; |
| 43 | + |
| 44 | + // set up our subscription |
| 45 | + stream_data.add_sub(sub); |
| 46 | + sub.subscribed = true; |
| 47 | + peer_data.set_subscribers(sub.stream_id, [me.user_id]); |
| 48 | + |
| 49 | + // ensure our setup is accurate |
| 50 | + assert(stream_data.is_subscribed("devel")); |
| 51 | + |
| 52 | + // DO THE UNSUBSCRIBE HERE |
| 53 | + stream_data.unsubscribe_myself(sub); |
| 54 | + assert(!sub.subscribed); |
| 55 | + assert(!stream_data.is_subscribed("devel")); |
| 56 | + assert(!contains_sub(stream_data.subscribed_subs(), sub)); |
| 57 | + assert(contains_sub(stream_data.unsubscribed_subs(), sub)); |
| 58 | + |
| 59 | + // make sure subsequent calls work |
| 60 | + sub = stream_data.get_sub("devel"); |
| 61 | + assert(!sub.subscribed); |
| 62 | +}); |
| 63 | + |
| 64 | +run_test("subscribers", () => { |
| 65 | + stream_data.clear_subscriptions(); |
| 66 | + let sub = {name: "Rome", subscribed: true, stream_id: 1001}; |
| 67 | + |
| 68 | + stream_data.add_sub(sub); |
| 69 | + |
| 70 | + const fred = { |
| 71 | + |
| 72 | + full_name: "Fred", |
| 73 | + user_id: 101, |
| 74 | + }; |
| 75 | + const not_fred = { |
| 76 | + |
| 77 | + full_name: "Not Fred", |
| 78 | + user_id: 102, |
| 79 | + }; |
| 80 | + const george = { |
| 81 | + |
| 82 | + full_name: "George", |
| 83 | + user_id: 103, |
| 84 | + }; |
| 85 | + people.add_active_user(fred); |
| 86 | + people.add_active_user(not_fred); |
| 87 | + people.add_active_user(george); |
| 88 | + |
| 89 | + function potential_subscriber_ids() { |
| 90 | + const users = peer_data.potential_subscribers(sub.stream_id); |
| 91 | + return users.map((u) => u.user_id).sort(); |
| 92 | + } |
| 93 | + |
| 94 | + assert.deepEqual(potential_subscriber_ids(), [ |
| 95 | + me.user_id, |
| 96 | + fred.user_id, |
| 97 | + not_fred.user_id, |
| 98 | + george.user_id, |
| 99 | + ]); |
| 100 | + |
| 101 | + peer_data.set_subscribers(sub.stream_id, [me.user_id, fred.user_id, george.user_id]); |
| 102 | + stream_data.update_calculated_fields(sub); |
| 103 | + assert(stream_data.is_user_subscribed(sub.stream_id, me.user_id)); |
| 104 | + assert(stream_data.is_user_subscribed(sub.stream_id, fred.user_id)); |
| 105 | + assert(stream_data.is_user_subscribed(sub.stream_id, george.user_id)); |
| 106 | + assert(!stream_data.is_user_subscribed(sub.stream_id, not_fred.user_id)); |
| 107 | + |
| 108 | + assert.deepEqual(potential_subscriber_ids(), [not_fred.user_id]); |
| 109 | + |
| 110 | + peer_data.set_subscribers(sub.stream_id, []); |
| 111 | + |
| 112 | + const brutus = { |
| 113 | + |
| 114 | + full_name: "Brutus", |
| 115 | + user_id: 104, |
| 116 | + }; |
| 117 | + people.add_active_user(brutus); |
| 118 | + assert(!stream_data.is_user_subscribed(sub.stream_id, brutus.user_id)); |
| 119 | + |
| 120 | + // add |
| 121 | + let ok = peer_data.add_subscriber(sub.stream_id, brutus.user_id); |
| 122 | + assert(ok); |
| 123 | + assert(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id)); |
| 124 | + sub = stream_data.get_sub("Rome"); |
| 125 | + assert.equal(peer_data.get_subscriber_count(sub.stream_id), 1); |
| 126 | + const sub_email = "Rome:[email protected]:9991"; |
| 127 | + stream_data.update_stream_email_address(sub, sub_email); |
| 128 | + assert.equal(sub.email_address, sub_email); |
| 129 | + |
| 130 | + // verify that adding an already-added subscriber is a noop |
| 131 | + peer_data.add_subscriber(sub.stream_id, brutus.user_id); |
| 132 | + assert(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id)); |
| 133 | + sub = stream_data.get_sub("Rome"); |
| 134 | + assert.equal(peer_data.get_subscriber_count(sub.stream_id), 1); |
| 135 | + |
| 136 | + // remove |
| 137 | + ok = peer_data.remove_subscriber(sub.stream_id, brutus.user_id); |
| 138 | + assert(ok); |
| 139 | + assert(!stream_data.is_user_subscribed(sub.stream_id, brutus.user_id)); |
| 140 | + sub = stream_data.get_sub("Rome"); |
| 141 | + assert.equal(peer_data.get_subscriber_count(sub.stream_id), 0); |
| 142 | + |
| 143 | + // verify that checking subscription with undefined user id |
| 144 | + |
| 145 | + blueslip.expect("warn", "Undefined user_id passed to function is_user_subscribed"); |
| 146 | + assert.equal(stream_data.is_user_subscribed(sub.stream_id, undefined), undefined); |
| 147 | + |
| 148 | + // Verify noop for bad stream when removing subscriber |
| 149 | + const bad_stream_id = 999999; |
| 150 | + blueslip.expect( |
| 151 | + "warn", |
| 152 | + "We got a remove_subscriber call for an untracked stream " + bad_stream_id, |
| 153 | + ); |
| 154 | + ok = peer_data.remove_subscriber(bad_stream_id, brutus.user_id); |
| 155 | + assert(!ok); |
| 156 | + |
| 157 | + // verify that removing an already-removed subscriber is a noop |
| 158 | + blueslip.expect("warn", "We tried to remove invalid subscriber: 104"); |
| 159 | + ok = peer_data.remove_subscriber(sub.stream_id, brutus.user_id); |
| 160 | + assert(!ok); |
| 161 | + assert(!stream_data.is_user_subscribed(sub.stream_id, brutus.user_id)); |
| 162 | + sub = stream_data.get_sub("Rome"); |
| 163 | + assert.equal(peer_data.get_subscriber_count(sub.stream_id), 0); |
| 164 | + |
| 165 | + // Verify defensive code in set_subscribers, where the second parameter |
| 166 | + // can be undefined. |
| 167 | + stream_data.add_sub(sub); |
| 168 | + peer_data.add_subscriber(sub.stream_id, brutus.user_id); |
| 169 | + sub.subscribed = true; |
| 170 | + assert(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id)); |
| 171 | + |
| 172 | + // Verify that we noop and don't crash when unsubscribed. |
| 173 | + sub.subscribed = false; |
| 174 | + stream_data.update_calculated_fields(sub); |
| 175 | + ok = peer_data.add_subscriber(sub.stream_id, brutus.user_id); |
| 176 | + assert(ok); |
| 177 | + assert.equal(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id), true); |
| 178 | + peer_data.remove_subscriber(sub.stream_id, brutus.user_id); |
| 179 | + assert.equal(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id), false); |
| 180 | + peer_data.add_subscriber(sub.stream_id, brutus.user_id); |
| 181 | + assert.equal(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id), true); |
| 182 | + |
| 183 | + blueslip.expect( |
| 184 | + "warn", |
| 185 | + "We got a is_user_subscribed call for a non-existent or inaccessible stream.", |
| 186 | + 2, |
| 187 | + ); |
| 188 | + sub.invite_only = true; |
| 189 | + stream_data.update_calculated_fields(sub); |
| 190 | + assert.equal(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id), undefined); |
| 191 | + peer_data.remove_subscriber(sub.stream_id, brutus.user_id); |
| 192 | + assert.equal(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id), undefined); |
| 193 | + |
| 194 | + // Verify that we don't crash and return false for a bad stream. |
| 195 | + blueslip.expect("warn", "We got an add_subscriber call for an untracked stream: 9999999"); |
| 196 | + ok = peer_data.add_subscriber(9999999, brutus.user_id); |
| 197 | + assert(!ok); |
| 198 | + |
| 199 | + // Verify that we don't crash and return false for a bad user id. |
| 200 | + blueslip.expect("error", "Unknown user_id in get_by_user_id: 9999999"); |
| 201 | + blueslip.expect("error", "We tried to add invalid subscriber: 9999999"); |
| 202 | + ok = peer_data.add_subscriber(sub.stream_id, 9999999); |
| 203 | + assert(!ok); |
| 204 | +}); |
| 205 | + |
| 206 | +run_test("get_subscriber_count", () => { |
| 207 | + const india = { |
| 208 | + stream_id: 102, |
| 209 | + name: "India", |
| 210 | + subscribed: true, |
| 211 | + }; |
| 212 | + stream_data.clear_subscriptions(); |
| 213 | + |
| 214 | + blueslip.expect("warn", "We got a get_subscriber_count call for an untracked stream: 102"); |
| 215 | + assert.equal(peer_data.get_subscriber_count(india.stream_id), undefined); |
| 216 | + |
| 217 | + stream_data.add_sub(india); |
| 218 | + assert.equal(peer_data.get_subscriber_count(india.stream_id), 0); |
| 219 | + |
| 220 | + const fred = { |
| 221 | + |
| 222 | + full_name: "Fred", |
| 223 | + user_id: 101, |
| 224 | + }; |
| 225 | + people.add_active_user(fred); |
| 226 | + peer_data.add_subscriber(india.stream_id, 102); |
| 227 | + assert.equal(peer_data.get_subscriber_count(india.stream_id), 1); |
| 228 | + const george = { |
| 229 | + |
| 230 | + full_name: "George", |
| 231 | + user_id: 103, |
| 232 | + }; |
| 233 | + people.add_active_user(george); |
| 234 | + peer_data.add_subscriber(india.stream_id, 103); |
| 235 | + assert.equal(peer_data.get_subscriber_count(india.stream_id), 2); |
| 236 | + |
| 237 | + peer_data.remove_subscriber(india.stream_id, 103); |
| 238 | + assert.deepStrictEqual(peer_data.get_subscriber_count(india.stream_id), 1); |
| 239 | +}); |
| 240 | + |
| 241 | +run_test("is_subscriber_subset", () => { |
| 242 | + function make_sub(stream_id, user_ids) { |
| 243 | + const sub = {stream_id}; |
| 244 | + peer_data.set_subscribers(sub.stream_id, user_ids); |
| 245 | + return sub; |
| 246 | + } |
| 247 | + |
| 248 | + const sub_a = make_sub(301, [1, 2]); |
| 249 | + const sub_b = make_sub(302, [2, 3]); |
| 250 | + const sub_c = make_sub(303, [1, 2, 3]); |
| 251 | + |
| 252 | + // The bogus case should not come up in normal |
| 253 | + // use. |
| 254 | + // We simply punt on any calculation if |
| 255 | + // a stream has no subscriber info (like |
| 256 | + // maybe Zephyr?). |
| 257 | + const bogus = {}; // no subscribers |
| 258 | + |
| 259 | + const matrix = [ |
| 260 | + [sub_a, sub_a, true], |
| 261 | + [sub_a, sub_b, false], |
| 262 | + [sub_a, sub_c, true], |
| 263 | + [sub_b, sub_a, false], |
| 264 | + [sub_b, sub_b, true], |
| 265 | + [sub_b, sub_c, true], |
| 266 | + [sub_c, sub_a, false], |
| 267 | + [sub_c, sub_b, false], |
| 268 | + [sub_c, sub_c, true], |
| 269 | + [bogus, bogus, false], |
| 270 | + ]; |
| 271 | + |
| 272 | + for (const row of matrix) { |
| 273 | + assert.equal(peer_data.is_subscriber_subset(row[0].stream_id, row[1].stream_id), row[2]); |
| 274 | + } |
| 275 | +}); |
| 276 | + |
| 277 | +run_test("warn if subscribers are missing", () => { |
| 278 | + // This should only happen in this contrived test situation. |
| 279 | + stream_data.clear_subscriptions(); |
| 280 | + const sub = { |
| 281 | + name: "test", |
| 282 | + stream_id: 3, |
| 283 | + can_access_subscribers: true, |
| 284 | + }; |
| 285 | + |
| 286 | + with_field( |
| 287 | + stream_data, |
| 288 | + "get_sub_by_id", |
| 289 | + () => sub, |
| 290 | + () => { |
| 291 | + blueslip.expect("warn", "We called is_user_subscribed for an untracked stream: 3"); |
| 292 | + stream_data.is_user_subscribed(sub.stream_id, me.user_id); |
| 293 | + |
| 294 | + blueslip.expect("warn", "We called get_subscribers for an untracked stream: 3"); |
| 295 | + assert.deepEqual(peer_data.get_subscribers(sub.stream_id), []); |
| 296 | + }, |
| 297 | + ); |
| 298 | +}); |
0 commit comments