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-30 14:08:46 +01:00
|
|
|
const fs = require("fs");
|
|
|
|
|
2020-07-24 06:02:07 +02:00
|
|
|
const {JSDOM} = require("jsdom");
|
|
|
|
|
2021-06-16 15:58:34 +02:00
|
|
|
const {set_global, 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");
|
2021-03-25 22:35:45 +01:00
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const noop = () => {};
|
2019-01-30 14:08:46 +01:00
|
|
|
const template = fs.readFileSync("templates/corporate/upgrade.html", "utf-8");
|
2020-07-16 22:40:18 +02:00
|
|
|
const dom = new JSDOM(template, {pretendToBeVisual: true});
|
2019-01-30 14:08:46 +01:00
|
|
|
const document = dom.window.document;
|
2021-10-14 00:23:21 +02:00
|
|
|
const location = set_global("location", {});
|
2019-01-30 14:08:46 +01:00
|
|
|
|
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
|
|
|
const helpers = zrequire("../js/billing/helpers");
|
2021-03-13 15:49:01 +01:00
|
|
|
zrequire("../js/billing/upgrade");
|
2019-01-30 14:08:46 +01:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
run_test("initialize", ({override_rewire}) => {
|
2021-04-03 19:07:13 +02:00
|
|
|
page_params.annual_price = 8000;
|
|
|
|
page_params.monthly_price = 800;
|
|
|
|
page_params.seat_count = 8;
|
|
|
|
page_params.percent_off = 20;
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(helpers, "set_tab", (page_name) => {
|
2019-01-30 14:08:46 +01:00
|
|
|
assert.equal(page_name, "upgrade");
|
2021-02-28 01:28:31 +01:00
|
|
|
});
|
2019-01-30 14:08:46 +01:00
|
|
|
|
2020-07-31 16:55:55 +02:00
|
|
|
let create_ajax_request_form_call_count = 0;
|
2020-12-30 19:00:49 +01:00
|
|
|
helpers.__Rewire__(
|
|
|
|
"create_ajax_request",
|
2021-08-29 15:33:29 +02:00
|
|
|
(url, form_name, ignored_inputs, type, success_callback) => {
|
2020-12-30 19:00:49 +01:00
|
|
|
create_ajax_request_form_call_count += 1;
|
|
|
|
switch (form_name) {
|
|
|
|
case "autopay":
|
|
|
|
assert.equal(url, "/json/billing/upgrade");
|
2020-09-03 15:34:20 +02:00
|
|
|
assert.deepEqual(ignored_inputs, []);
|
|
|
|
assert.equal(type, "POST");
|
2021-10-14 00:23:21 +02:00
|
|
|
location.replace = (new_location) => {
|
2021-08-29 15:33:29 +02:00
|
|
|
assert.equal(new_location, "https://stripe_session_url");
|
2020-09-03 15:34:20 +02:00
|
|
|
};
|
2021-08-29 15:33:29 +02:00
|
|
|
// mock redirectToCheckout and verify its called
|
|
|
|
success_callback({stripe_session_url: "https://stripe_session_url"});
|
2020-12-30 19:00:49 +01:00
|
|
|
break;
|
|
|
|
case "invoice":
|
|
|
|
assert.equal(url, "/json/billing/upgrade");
|
2020-09-03 15:34:20 +02:00
|
|
|
assert.deepEqual(ignored_inputs, []);
|
|
|
|
assert.equal(type, "POST");
|
2021-10-14 00:23:21 +02:00
|
|
|
location.replace = (new_location) => {
|
2020-09-03 15:34:20 +02:00
|
|
|
assert.equal(new_location, "/billing");
|
|
|
|
};
|
|
|
|
success_callback();
|
2020-12-30 19:00:49 +01:00
|
|
|
break;
|
|
|
|
case "sponsorship":
|
|
|
|
assert.equal(url, "/json/billing/sponsorship");
|
2020-09-03 15:34:20 +02:00
|
|
|
assert.deepEqual(ignored_inputs, []);
|
|
|
|
assert.equal(type, "POST");
|
2021-10-14 00:23:21 +02:00
|
|
|
location.replace = (new_location) => {
|
2020-09-03 15:34:20 +02:00
|
|
|
assert.equal(new_location, "/");
|
|
|
|
};
|
|
|
|
success_callback();
|
2020-12-30 19:00:49 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Unhandled case");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2019-01-30 14:08:46 +01:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(helpers, "show_license_section", (section) => {
|
2019-01-30 14:08:46 +01:00
|
|
|
assert.equal(section, "automatic");
|
2021-02-28 01:28:31 +01:00
|
|
|
});
|
2019-01-30 14:08:46 +01:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(helpers, "update_charged_amount", (prices, schedule) => {
|
2019-01-30 14:08:46 +01:00
|
|
|
assert.equal(prices.annual, 6400);
|
|
|
|
assert.equal(prices.monthly, 640);
|
2020-04-17 11:17:28 +02:00
|
|
|
assert.equal(schedule, "monthly");
|
2021-02-28 01:28:31 +01:00
|
|
|
});
|
2019-01-30 14:08:46 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
$("input[type=radio][name=license_management]:checked").val = () =>
|
|
|
|
document.querySelector("input[type=radio][name=license_management]:checked").value;
|
2019-01-30 14:08:46 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
$("input[type=radio][name=schedule]:checked").val = () =>
|
|
|
|
document.querySelector("input[type=radio][name=schedule]:checked").value;
|
2019-01-30 14:08:46 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#autopay-form").data = (key) =>
|
|
|
|
document.querySelector("#autopay-form").getAttribute("data-" + key);
|
2019-01-30 14:08:46 +01:00
|
|
|
|
2021-08-29 15:33:29 +02:00
|
|
|
const initialize_function = $.get_initialize_function();
|
|
|
|
initialize_function();
|
2019-01-30 14:08:46 +01:00
|
|
|
|
|
|
|
const e = {
|
|
|
|
preventDefault: noop,
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const add_card_click_handler = $("#add-card-button").get_on_handler("click");
|
|
|
|
const invoice_click_handler = $("#invoice-button").get_on_handler("click");
|
|
|
|
const request_sponsorship_click_handler = $("#sponsorship-button").get_on_handler("click");
|
2019-01-30 14:08:46 +01:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(helpers, "is_valid_input", () => true);
|
2019-01-30 14:08:46 +01:00
|
|
|
add_card_click_handler(e);
|
2020-07-31 16:55:55 +02:00
|
|
|
assert.equal(create_ajax_request_form_call_count, 1);
|
2019-01-30 14:08:46 +01:00
|
|
|
invoice_click_handler(e);
|
2020-07-31 16:55:55 +02:00
|
|
|
assert.equal(create_ajax_request_form_call_count, 2);
|
|
|
|
request_sponsorship_click_handler(e);
|
|
|
|
assert.equal(create_ajax_request_form_call_count, 3);
|
2019-01-30 14:08:46 +01:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(helpers, "is_valid_input", () => false);
|
2019-01-30 14:08:46 +01:00
|
|
|
add_card_click_handler(e);
|
|
|
|
invoice_click_handler(e);
|
2020-06-09 12:24:32 +02:00
|
|
|
request_sponsorship_click_handler(e);
|
2020-07-31 16:55:55 +02:00
|
|
|
assert.equal(create_ajax_request_form_call_count, 3);
|
2020-06-09 12:24:32 +02:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(helpers, "show_license_section", (section) => {
|
2019-01-30 14:08:46 +01:00
|
|
|
assert.equal(section, "manual");
|
2021-02-28 01:28:31 +01:00
|
|
|
});
|
2020-07-15 00:34:28 +02:00
|
|
|
const license_change_handler = $("input[type=radio][name=license_management]").get_on_handler(
|
|
|
|
"change",
|
|
|
|
);
|
2019-01-30 14:08:46 +01:00
|
|
|
license_change_handler.call({value: "manual"});
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(helpers, "update_charged_amount", (prices, schedule) => {
|
2019-01-30 14:08:46 +01:00
|
|
|
assert.equal(prices.annual, 6400);
|
|
|
|
assert.equal(prices.monthly, 640);
|
|
|
|
assert.equal(schedule, "monthly");
|
2021-02-28 01:28:31 +01:00
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
const schedule_change_handler = $("input[type=radio][name=schedule]").get_on_handler("change");
|
2019-01-30 14:08:46 +01:00
|
|
|
schedule_change_handler.call({value: "monthly"});
|
|
|
|
|
|
|
|
assert.equal($("#autopay_annual_price").text(), "64");
|
|
|
|
assert.equal($("#autopay_annual_price_per_month").text(), "5.34");
|
|
|
|
assert.equal($("#autopay_monthly_price").text(), "6.40");
|
|
|
|
assert.equal($("#invoice_annual_price").text(), "64");
|
|
|
|
assert.equal($("#invoice_annual_price_per_month").text(), "5.34");
|
2020-06-09 12:24:32 +02:00
|
|
|
|
2021-07-19 03:36:52 +02:00
|
|
|
helpers.update_discount_details("opensource");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#sponsorship-discount-details").text(),
|
2021-07-19 03:36:52 +02:00
|
|
|
"Zulip Cloud Standard is free for open-source projects.",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-07-19 03:36:52 +02:00
|
|
|
helpers.update_discount_details("research");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#sponsorship-discount-details").text(),
|
2021-07-19 03:36:52 +02:00
|
|
|
"Zulip Cloud Standard is free for academic research.",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-07-19 03:36:52 +02:00
|
|
|
helpers.update_discount_details("event");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#sponsorship-discount-details").text(),
|
2021-07-19 20:14:45 +02:00
|
|
|
"Zulip Cloud Standard is free for academic conferences and most non-profit events.",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-07-19 03:36:52 +02:00
|
|
|
helpers.update_discount_details("education");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#sponsorship-discount-details").text(),
|
2021-07-19 03:36:52 +02:00
|
|
|
"Zulip Cloud Standard is discounted 85% for education.",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-07-19 03:36:52 +02:00
|
|
|
helpers.update_discount_details("nonprofit");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#sponsorship-discount-details").text(),
|
2021-07-19 20:14:45 +02:00
|
|
|
"Zulip Cloud Standard is discounted 85%+ for registered non-profits.",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-07-19 03:36:52 +02:00
|
|
|
helpers.update_discount_details("other");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#sponsorship-discount-details").text(),
|
2021-07-19 20:14:45 +02:00
|
|
|
"Your organization may be eligible for a discount on Zulip Cloud Standard. Organizations whose members are not employees are generally eligible.",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2019-01-30 14:08:46 +01:00
|
|
|
});
|
|
|
|
|
2019-02-27 13:53:43 +01:00
|
|
|
run_test("autopay_form_fields", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
document.querySelector("#autopay-form [name=seat_count]").value,
|
|
|
|
"{{ seat_count }}",
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
document.querySelector("#autopay-form [name=signed_seat_count]").value,
|
|
|
|
"{{ signed_seat_count }}",
|
|
|
|
);
|
2019-02-27 13:53:43 +01:00
|
|
|
assert.equal(document.querySelector("#autopay-form [name=salt]").value, "{{ salt }}");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
document.querySelector("#autopay-form [name=billing_modality]").value,
|
|
|
|
"charge_automatically",
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
document.querySelector("#autopay-form #automatic_license_count").value,
|
|
|
|
"{{ seat_count }}",
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
document.querySelector("#autopay-form #manual_license_count").min,
|
|
|
|
"{{ seat_count }}",
|
|
|
|
);
|
|
|
|
|
|
|
|
const license_options = document.querySelectorAll(
|
|
|
|
"#autopay-form input[type=radio][name=license_management]",
|
|
|
|
);
|
2019-01-30 14:08:46 +01:00
|
|
|
assert.equal(license_options.length, 2);
|
|
|
|
assert.equal(license_options[0].value, "automatic");
|
|
|
|
assert.equal(license_options[1].value, "manual");
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const schedule_options = document.querySelectorAll(
|
|
|
|
"#autopay-form input[type=radio][name=schedule]",
|
|
|
|
);
|
2019-02-27 13:53:43 +01:00
|
|
|
assert.equal(schedule_options.length, 2);
|
2020-04-17 11:17:28 +02:00
|
|
|
assert.equal(schedule_options[0].value, "monthly");
|
|
|
|
assert.equal(schedule_options[1].value, "annual");
|
2019-01-30 14:08:46 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(document.querySelector("#autopay-error"));
|
|
|
|
assert.ok(document.querySelector("#autopay-loading"));
|
|
|
|
assert.ok(document.querySelector("#autopay"));
|
|
|
|
assert.ok(document.querySelector("#autopay-success"));
|
|
|
|
assert.ok(document.querySelector("#autopay_loading_indicator"));
|
2019-02-27 13:53:43 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(document.querySelector("input[name=csrfmiddlewaretoken]"));
|
2020-05-22 15:42:46 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(document.querySelector("#free-trial-alert-message"));
|
2019-02-27 13:53:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
run_test("invoice_form_fields", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
document.querySelector("#invoice-form [name=signed_seat_count]").value,
|
|
|
|
"{{ signed_seat_count }}",
|
|
|
|
);
|
2019-02-27 13:53:43 +01:00
|
|
|
assert.equal(document.querySelector("#invoice-form [name=salt]").value, "{{ salt }}");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
document.querySelector("#invoice-form [name=billing_modality]").value,
|
|
|
|
"send_invoice",
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
document.querySelector("#invoice-form [name=licenses]").min,
|
|
|
|
"{{ min_invoiced_licenses }}",
|
|
|
|
);
|
|
|
|
|
|
|
|
const schedule_options = document.querySelectorAll(
|
|
|
|
"#invoice-form input[type=radio][name=schedule]",
|
|
|
|
);
|
2019-02-27 13:53:43 +01:00
|
|
|
assert.equal(schedule_options.length, 1);
|
|
|
|
assert.equal(schedule_options[0].value, "annual");
|
2019-01-30 14:08:46 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(document.querySelector("#invoice-error"));
|
|
|
|
assert.ok(document.querySelector("#invoice-loading"));
|
|
|
|
assert.ok(document.querySelector("#invoice"));
|
|
|
|
assert.ok(document.querySelector("#invoice-success"));
|
|
|
|
assert.ok(document.querySelector("#invoice_loading_indicator"));
|
2019-01-30 14:08:46 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(document.querySelector("input[name=csrfmiddlewaretoken]"));
|
2020-05-22 15:42:46 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(document.querySelector("#free-trial-alert-message"));
|
2019-01-30 14:08:46 +01:00
|
|
|
});
|