channel: Simplify password change tracking.

Move the password tracking variables from setup to channel module.
Track password_change requests in channel.
Directly use xhr object to store `password_changes` information.
Tests modified to accomodote this change by converting strings to
objects.
This commit is contained in:
Aman Agrawal
2021-10-05 06:31:45 +05:30
committed by Tim Abbott
parent 8433ce90dc
commit cc8cb5432e
4 changed files with 27 additions and 42 deletions

View File

@@ -19,15 +19,6 @@ const xhr_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: () => {
@@ -44,7 +35,7 @@ set_global("window", {
const reload_state = zrequire("reload_state");
const channel = zrequire("channel");
const default_stub_xhr = "default-stub-xhr";
const default_stub_xhr = {"default-stub-xhr": 0};
const $ = mock_jquery({});
@@ -176,7 +167,7 @@ test("normal_post", () => {
let orig_success_called;
let orig_error_called;
const stub_xhr = "stub-xhr-normal-post";
const stub_xhr = {"stub-xhr-normal-post": 0};
test_with_mock_ajax({
xhr: stub_xhr,
@@ -273,12 +264,12 @@ test("authentication_error_401_password_change_in_progress", () => {
// password_change_in_progress = true
check_ajax_options(options) {
page_params.is_spectator = true;
setup.password_change_in_progress = true;
channel.set_password_change_in_progress(true);
options.simulate_error();
assert.ok(!login_to_access_shown);
setup.password_change_in_progress = false;
channel.set_password_change_in_progress(false);
page_params.is_spectator = false;
login_to_access_shown = false;
},
@@ -372,7 +363,7 @@ test("retry", () => {
test("too_many_pending", () => {
channel.clear_for_tests();
$.ajax = () => {
const xhr = "stub";
const xhr = {stub: 0};
return xhr;
};

View File

@@ -3,9 +3,19 @@ import $ from "jquery";
import * as blueslip from "./blueslip";
import {page_params} from "./page_params";
import * as reload_state from "./reload_state";
import * as setup from "./setup";
import * as spectators from "./spectators";
let password_change_in_progress = false;
export let password_changes = 0;
export function set_password_change_in_progress(value) {
password_change_in_progress = value;
if (!value) {
password_changes += 1;
}
}
export const xhr_password_changes = new WeakMap();
const pending_requests = [];
export function clear_for_tests() {
@@ -56,10 +66,7 @@ function call(args, idempotent) {
}
if (xhr.status === 401) {
if (
setup.password_change_in_progress ||
setup.xhr_password_changes.get(xhr) !== setup.password_changes
) {
if (password_change_in_progress || xhr.password_changes !== password_changes) {
// The backend for handling password change API requests
// will replace the user's session; this results in a
// brief race where any API request will fail with a 401
@@ -135,6 +142,13 @@ function call(args, idempotent) {
const jqXHR = $.ajax(args);
add_pending_request(jqXHR);
// Remember the number of completed password changes when the
// request was initiated. This allows us to detect race
// situations where a password change occurred before we got a
// response that failed due to the ongoing password change.
jqXHR.password_changes = password_changes;
return jqXHR;
}

View File

@@ -21,7 +21,6 @@ import * as pill_typeahead from "./pill_typeahead";
import * as settings_bots from "./settings_bots";
import * as settings_data from "./settings_data";
import * as settings_ui from "./settings_ui";
import * as setup from "./setup";
import * as ui_report from "./ui_report";
import * as user_pill from "./user_pill";
import * as user_profile from "./user_profile";
@@ -443,14 +442,14 @@ export function set_up() {
}
}
setup.set_password_change_in_progress(true);
channel.set_password_change_in_progress(true);
const opts = {
success_continuation() {
setup.set_password_change_in_progress(false);
channel.set_password_change_in_progress(false);
overlays.close_modal("#change_password_modal");
},
error_continuation() {
setup.set_password_change_in_progress(false);
channel.set_password_change_in_progress(false);
},
error_msg_element: change_password_error,
failure_msg_html: null,

View File

@@ -6,17 +6,6 @@ import {page_params} from "./page_params";
import * as util from "./util";
// Miscellaneous early setup.
export let password_change_in_progress = false;
export let password_changes = 0;
export const xhr_password_changes = new WeakMap();
export function set_password_change_in_progress(value) {
password_change_in_progress = value;
if (!value) {
password_changes += 1;
}
}
$(() => {
if (util.is_mobile()) {
// Disable the tutorial; it's ugly on mobile.
@@ -47,14 +36,6 @@ $(() => {
return this.outerWidth(...args) || 0;
};
// Remember the number of completed password changes when the
// request was initiated. This allows us to detect race
// situations where a password change occurred before we got a
// response that failed due to the ongoing password change.
$(document).ajaxSend((event, xhr) => {
xhr_password_changes.set(xhr, password_changes);
});
$.fn.expectOne = function () {
if (blueslip && this.length !== 1) {
blueslip.error("Expected one element in jQuery set, " + this.length + " found");