2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
2019-01-08 18:06:06 +01:00
|
|
|
const fs = require("fs");
|
|
|
|
|
2020-07-24 06:02:07 +02:00
|
|
|
const {JSDOM} = require("jsdom");
|
|
|
|
|
2021-03-11 05:43:45 +01:00
|
|
|
const {mock_cjs, mock_esm, set_global, with_field, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-02-21 15:38:51 +01:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2019-01-08 18:06:06 +01:00
|
|
|
const template = fs.readFileSync("templates/corporate/billing.html", "utf-8");
|
2020-07-16 22:40:18 +02:00
|
|
|
const dom = new JSDOM(template, {pretendToBeVisual: true});
|
2019-01-08 18:06:06 +01:00
|
|
|
const document = dom.window.document;
|
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const StripeCheckout = set_global("StripeCheckout", {
|
2021-02-23 14:10:56 +01:00
|
|
|
configure: () => {},
|
2019-01-08 18:06:06 +01:00
|
|
|
});
|
2021-03-07 13:57:14 +01:00
|
|
|
|
2021-03-11 05:43:45 +01:00
|
|
|
mock_cjs("jquery", $);
|
2021-03-10 06:10:32 +01:00
|
|
|
const helpers = mock_esm("../../static/js/billing/helpers", {
|
2021-03-07 13:57:14 +01:00
|
|
|
set_tab: () => {},
|
|
|
|
});
|
2019-01-08 18:06:06 +01:00
|
|
|
|
2020-12-23 17:08:27 +01:00
|
|
|
const billing = zrequire("billing/billing");
|
2021-03-13 15:49:01 +01:00
|
|
|
|
2021-02-23 14:10:56 +01:00
|
|
|
run_test("initialize", (override) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let token_func;
|
2020-04-24 17:38:13 +02:00
|
|
|
|
|
|
|
let set_tab_called = false;
|
2021-02-23 14:10:56 +01:00
|
|
|
override(helpers, "set_tab", (page_name) => {
|
2019-01-08 18:06:06 +01:00
|
|
|
assert.equal(page_name, "billing");
|
2020-04-24 17:38:13 +02:00
|
|
|
set_tab_called = true;
|
2021-02-23 14:10:56 +01:00
|
|
|
});
|
2019-01-08 18:06:06 +01:00
|
|
|
|
2020-04-24 17:38:13 +02:00
|
|
|
let create_ajax_request_called = false;
|
2020-12-30 19:00:49 +01:00
|
|
|
function card_change_ajax(url, form_name, stripe_token, ignored_inputs, redirect_to, method) {
|
2019-01-08 18:06:06 +01:00
|
|
|
assert.equal(url, "/json/billing/sources/change");
|
|
|
|
assert.equal(form_name, "cardchange");
|
|
|
|
assert.equal(stripe_token, "stripe_token");
|
2020-12-30 19:00:49 +01:00
|
|
|
assert.deepEqual(ignored_inputs, undefined);
|
2020-12-10 18:15:09 +01:00
|
|
|
assert.equal(redirect_to, undefined);
|
|
|
|
assert.equal(method, undefined);
|
2020-04-24 17:38:13 +02:00
|
|
|
create_ajax_request_called = true;
|
2021-02-23 14:10:56 +01:00
|
|
|
}
|
2019-01-08 18:06:06 +01:00
|
|
|
|
2020-04-24 17:38:13 +02:00
|
|
|
let open_func_called = false;
|
2019-01-08 18:06:06 +01:00
|
|
|
const open_func = (config_opts) => {
|
|
|
|
assert.equal(config_opts.name, "Zulip");
|
|
|
|
assert.equal(config_opts.zipCode, true);
|
|
|
|
assert.equal(config_opts.billingAddress, true);
|
|
|
|
assert.equal(config_opts.panelLabel, "Update card");
|
|
|
|
assert.equal(config_opts.label, "Update card");
|
|
|
|
assert.equal(config_opts.allowRememberMe, false);
|
|
|
|
assert.equal(config_opts.email, "{{stripe_email}}");
|
|
|
|
|
|
|
|
token_func("stripe_token");
|
2020-04-24 17:38:13 +02:00
|
|
|
open_func_called = true;
|
2019-01-08 18:06:06 +01:00
|
|
|
};
|
|
|
|
|
2020-04-24 17:38:13 +02:00
|
|
|
let stripe_checkout_configure_called = false;
|
2021-02-23 14:10:56 +01:00
|
|
|
override(StripeCheckout, "configure", (config_opts) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(config_opts.image, "/static/images/logo/zulip-icon-128x128.png");
|
|
|
|
assert.equal(config_opts.locale, "auto");
|
|
|
|
assert.equal(config_opts.key, "{{publishable_key}}");
|
2019-01-08 18:06:06 +01:00
|
|
|
token_func = config_opts.token;
|
2020-04-24 17:38:13 +02:00
|
|
|
stripe_checkout_configure_called = true;
|
2019-01-08 18:06:06 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
open: open_func,
|
|
|
|
};
|
2021-02-23 14:10:56 +01:00
|
|
|
});
|
2019-01-08 18:06:06 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#payment-method").data = (key) =>
|
|
|
|
document.querySelector("#payment-method").getAttribute("data-" + key);
|
2019-01-08 18:06:06 +01:00
|
|
|
|
2021-03-13 15:49:01 +01:00
|
|
|
$.get_initialize_function()();
|
2019-01-08 18:06:06 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(set_tab_called);
|
|
|
|
assert.ok(stripe_checkout_configure_called);
|
2019-01-08 18:06:06 +01:00
|
|
|
const e = {
|
2021-02-23 14:10:56 +01:00
|
|
|
preventDefault: () => {},
|
2019-01-08 18:06:06 +01:00
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
const update_card_click_handler = $("#update-card-button").get_on_handler("click");
|
2021-02-23 14:10:56 +01:00
|
|
|
with_field(helpers, "create_ajax_request", card_change_ajax, () => {
|
|
|
|
update_card_click_handler(e);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(create_ajax_request_called);
|
|
|
|
assert.ok(open_func_called);
|
2021-02-23 14:10:56 +01:00
|
|
|
});
|
2020-04-24 17:38:13 +02:00
|
|
|
|
|
|
|
create_ajax_request_called = false;
|
2020-12-30 19:00:49 +01:00
|
|
|
function plan_change_ajax(url, form_name, stripe_token, ignored_inputs, redirect_to, method) {
|
2020-12-10 18:15:09 +01:00
|
|
|
assert.equal(url, "/json/billing/plan");
|
2020-04-24 17:38:13 +02:00
|
|
|
assert.equal(form_name, "planchange");
|
|
|
|
assert.equal(stripe_token, undefined);
|
2020-12-30 19:00:49 +01:00
|
|
|
assert.deepEqual(ignored_inputs, undefined);
|
2020-12-10 18:15:09 +01:00
|
|
|
assert.equal(redirect_to, undefined);
|
|
|
|
assert.equal(method, "PATCH");
|
2020-04-24 17:38:13 +02:00
|
|
|
create_ajax_request_called = true;
|
2021-02-23 14:10:56 +01:00
|
|
|
}
|
2020-04-24 17:38:13 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const change_plan_status_click_handler = $("#change-plan-status").get_on_handler("click");
|
2021-02-23 14:10:56 +01:00
|
|
|
|
|
|
|
with_field(helpers, "create_ajax_request", plan_change_ajax, () => {
|
|
|
|
change_plan_status_click_handler(e);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(create_ajax_request_called);
|
2020-12-23 17:08:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
create_ajax_request_called = false;
|
|
|
|
function license_change_ajax(
|
|
|
|
url,
|
|
|
|
form_name,
|
|
|
|
stripe_token,
|
|
|
|
ignored_inputs,
|
|
|
|
redirect_to,
|
|
|
|
method,
|
|
|
|
) {
|
|
|
|
assert.equal(url, "/json/billing/plan");
|
|
|
|
assert.equal(form_name, "licensechange");
|
|
|
|
assert.equal(stripe_token, undefined);
|
|
|
|
assert.deepEqual(ignored_inputs, ["licenses_at_next_renewal"]);
|
|
|
|
assert.equal(redirect_to, undefined);
|
|
|
|
assert.equal(method, "PATCH");
|
|
|
|
create_ajax_request_called = true;
|
|
|
|
}
|
|
|
|
with_field(helpers, "create_ajax_request", license_change_ajax, () => {
|
|
|
|
billing.create_update_license_request();
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(create_ajax_request_called);
|
2020-12-23 17:08:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
let create_update_license_request_called = false;
|
|
|
|
override(billing, "create_update_license_request", () => {
|
|
|
|
create_update_license_request_called = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
const confirm_license_update_click_handler = $("#confirm-license-update-button").get_on_handler(
|
|
|
|
"click",
|
|
|
|
);
|
|
|
|
confirm_license_update_click_handler(e);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(create_update_license_request_called);
|
2020-12-23 17:08:27 +01:00
|
|
|
|
|
|
|
let confirm_license_modal_shown = false;
|
|
|
|
override(helpers, "is_valid_input", () => true);
|
|
|
|
$("#confirm-licenses-modal").modal = (action) => {
|
2021-06-03 16:42:03 +02:00
|
|
|
assert.equal(action, "show");
|
2020-12-23 17:08:27 +01:00
|
|
|
confirm_license_modal_shown = true;
|
|
|
|
};
|
|
|
|
$("#licensechange-input-section").data = (key) => {
|
2021-06-03 16:42:03 +02:00
|
|
|
assert.equal(key, "licenses");
|
2020-12-23 17:08:27 +01:00
|
|
|
return 20;
|
|
|
|
};
|
|
|
|
$("#new_licenses_input").val = () => 15;
|
|
|
|
create_update_license_request_called = false;
|
|
|
|
const update_licenses_button_click_handler =
|
|
|
|
$("#update-licenses-button").get_on_handler("click");
|
|
|
|
update_licenses_button_click_handler(e);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(create_update_license_request_called);
|
|
|
|
assert.ok(!confirm_license_modal_shown);
|
2020-12-23 17:08:27 +01:00
|
|
|
|
|
|
|
$("#new_licenses_input").val = () => 25;
|
|
|
|
create_update_license_request_called = false;
|
|
|
|
update_licenses_button_click_handler(e);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!create_update_license_request_called);
|
|
|
|
assert.ok(confirm_license_modal_shown);
|
2020-12-23 17:08:27 +01:00
|
|
|
|
|
|
|
override(helpers, "is_valid_input", () => false);
|
|
|
|
let prevent_default_called = false;
|
|
|
|
const event = {
|
|
|
|
prevent_default: () => {
|
|
|
|
prevent_default_called = true;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
update_licenses_button_click_handler(event);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!prevent_default_called);
|
2020-12-23 17:08:27 +01:00
|
|
|
|
|
|
|
const update_next_renewal_licenses_button_click_handler = $(
|
|
|
|
"#update-licenses-at-next-renewal-button",
|
|
|
|
).get_on_handler("click");
|
|
|
|
create_ajax_request_called = false;
|
|
|
|
function licenses_at_next_renewal_change_ajax(
|
|
|
|
url,
|
|
|
|
form_name,
|
|
|
|
stripe_token,
|
|
|
|
ignored_inputs,
|
|
|
|
redirect_to,
|
|
|
|
method,
|
|
|
|
) {
|
|
|
|
assert.equal(url, "/json/billing/plan");
|
|
|
|
assert.equal(form_name, "licensechange");
|
|
|
|
assert.equal(stripe_token, undefined);
|
|
|
|
assert.deepEqual(ignored_inputs, ["licenses"]);
|
|
|
|
assert.equal(redirect_to, undefined);
|
|
|
|
assert.equal(method, "PATCH");
|
|
|
|
create_ajax_request_called = true;
|
|
|
|
}
|
|
|
|
with_field(helpers, "create_ajax_request", licenses_at_next_renewal_change_ajax, () => {
|
|
|
|
update_next_renewal_licenses_button_click_handler(e);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(create_ajax_request_called);
|
2021-02-23 14:10:56 +01:00
|
|
|
});
|
2019-01-08 18:06:06 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
run_test("billing_template", () => {
|
|
|
|
// Elements necessary for create_ajax_request
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(document.querySelector("#cardchange-error"));
|
|
|
|
assert.ok(document.querySelector("#cardchange-loading"));
|
|
|
|
assert.ok(document.querySelector("#cardchange_loading_indicator"));
|
|
|
|
assert.ok(document.querySelector("#cardchange-success"));
|
|
|
|
|
|
|
|
assert.ok(document.querySelector("#licensechange-error"));
|
|
|
|
assert.ok(document.querySelector("#licensechange-loading"));
|
|
|
|
assert.ok(document.querySelector("#licensechange_loading_indicator"));
|
|
|
|
assert.ok(document.querySelector("#licensechange-success"));
|
|
|
|
|
|
|
|
assert.ok(document.querySelector("#planchange-error"));
|
|
|
|
assert.ok(document.querySelector("#planchange-loading"));
|
|
|
|
assert.ok(document.querySelector("#planchange_loading_indicator"));
|
|
|
|
assert.ok(document.querySelector("#planchange-success"));
|
|
|
|
|
|
|
|
assert.ok(document.querySelector("input[name=csrfmiddlewaretoken]"));
|
2019-01-08 18:06:06 +01:00
|
|
|
});
|