channel: Ask spectator to login for unauthenticated requests.

Get complete coverage for channel.
This commit is contained in:
Aman Agrawal
2021-09-28 10:20:14 +00:00
committed by Tim Abbott
parent 50812ead0f
commit 8433ce90dc
3 changed files with 108 additions and 8 deletions

View File

@@ -4,15 +4,43 @@ const {strict: assert} = require("assert");
const _ = require("lodash");
const {mock_jquery, set_global, zrequire} = require("../zjsunit/namespace");
const {mock_jquery, mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const {page_params} = require("../zjsunit/zpage_params");
set_global("setTimeout", (f, delay) => {
assert.equal(delay, 0);
f();
});
const xhr_401 = {
status: 401,
responseText: '{"msg": "Use cannnot access XYZ"}',
};
const xhr_password_changes = new WeakMap();
xhr_password_changes.set(xhr_401, 0);
const setup = mock_esm("../../static/js/setup", {
password_change_in_progress: false,
password_changes: 0,
xhr_password_changes,
});
let login_to_access_shown = false;
mock_esm("../../static/js/spectators", {
login_to_access: () => {
login_to_access_shown = true;
},
});
set_global("window", {
location: {
replace: () => {},
},
});
const reload_state = zrequire("reload_state");
const channel = zrequire("channel");
@@ -215,6 +243,67 @@ test("patch_with_form_data", () => {
});
});
test("authentication_error_401_is_spectator", () => {
test_with_mock_ajax({
xhr: xhr_401,
run_code() {
channel.post({});
},
// is_spectator = true
check_ajax_options(options) {
page_params.is_spectator = true;
options.simulate_error();
assert.ok(login_to_access_shown);
login_to_access_shown = false;
},
});
});
test("authentication_error_401_password_change_in_progress", () => {
test_with_mock_ajax({
xhr: xhr_401,
run_code() {
channel.post({});
},
// is_spectator = true
// password_change_in_progress = true
check_ajax_options(options) {
page_params.is_spectator = true;
setup.password_change_in_progress = true;
options.simulate_error();
assert.ok(!login_to_access_shown);
setup.password_change_in_progress = false;
page_params.is_spectator = false;
login_to_access_shown = false;
},
});
});
test("authentication_error_401_not_spectator", () => {
test_with_mock_ajax({
xhr: xhr_401,
run_code() {
channel.post({});
},
// is_spectator = false
check_ajax_options(options) {
page_params.is_spectator = false;
options.simulate_error();
assert.ok(!login_to_access_shown);
login_to_access_shown = false;
},
});
});
test("reload_on_403_error", () => {
test_with_mock_ajax({
xhr: {