Skip to content

Commit 40a94c7

Browse files
authored
Merge pull request #14547 from canonical/wd-16440-restrict-edit-billing
Wd-16440 Restrict channel users from editing billing information
2 parents 3a689e8 + 491d609 commit 40a94c7

File tree

7 files changed

+413
-199
lines changed

7 files changed

+413
-199
lines changed

static/js/src/advantage/distributor/components/DistributorBuyButton/DistributorBuyButton.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
FormContext,
1313
defaultValues,
1414
} from "advantage/distributor/utils/FormContext";
15-
import { distributorProduct } from "advantage/subscribe/checkout/utils/test/Mocks";
15+
import { DistributorProduct } from "advantage/subscribe/checkout/utils/test/Mocks";
1616
import { ChannelOfferFactory } from "advantage/offers/tests/factories/channelOffers";
1717
import { UserSubscriptionMarketplace } from "advantage/api/enum";
1818

@@ -27,7 +27,7 @@ const mockSubscription: SubscriptionItem = {
2727
const mockContextValue = {
2828
...defaultValues,
2929
subscriptionList: [mockSubscription] as SubscriptionItem[],
30-
products: [distributorProduct] as ChannelProduct[],
30+
products: [DistributorProduct] as ChannelProduct[],
3131
offer: ChannelOfferFactory.build({ id: "offer-id-1" }),
3232
};
3333

static/js/src/advantage/distributor/components/DistributorShopForm/AddSubscriptions/SubscriptionCard.tsx/SubscriptionCard.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
FormContext,
1212
defaultValues,
1313
} from "advantage/distributor/utils/FormContext";
14-
import { distributorProduct } from "advantage/subscribe/checkout/utils/test/Mocks";
14+
import { DistributorProduct } from "advantage/subscribe/checkout/utils/test/Mocks";
1515

1616
const mockSubscription: SubscriptionItem = {
1717
id: "mocked-id-1",
@@ -25,7 +25,7 @@ const mockContextValue = {
2525
...defaultValues,
2626
subscriptionList: [mockSubscription] as SubscriptionItem[],
2727
setSubscriptionList: jest.fn(),
28-
products: [distributorProduct] as ChannelProduct[],
28+
products: [DistributorProduct] as ChannelProduct[],
2929
duration: 1,
3030
};
3131

static/js/src/advantage/subscribe/checkout/components/Taxes/Taxes.test.tsx

+129-48
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
22
import { Formik } from "formik";
33
import { Elements } from "@stripe/react-stripe-js";
44
import { loadStripe } from "@stripe/stripe-js";
5-
import { fireEvent, render, screen } from "@testing-library/react";
6-
import { UAProduct } from "../../utils/test/Mocks";
5+
import { render, screen } from "@testing-library/react";
6+
import { DistributorProduct, UAProduct } from "../../utils/test/Mocks";
77
import Taxes from "./Taxes";
8+
import userEvent from "@testing-library/user-event";
9+
import { UserSubscriptionMarketplace } from "advantage/api/enum";
810

911
describe("TaxesTests", () => {
1012
let queryClient: QueryClient;
@@ -14,6 +16,10 @@ describe("TaxesTests", () => {
1416
queryClient = new QueryClient();
1517
});
1618

19+
afterEach(() => {
20+
jest.clearAllMocks();
21+
});
22+
1723
it("renders country select correctly", () => {
1824
const products = [
1925
{
@@ -53,12 +59,11 @@ describe("TaxesTests", () => {
5359
);
5460

5561
expect(screen.getByTestId("select-country")).toBeInTheDocument();
56-
fireEvent.change(screen.getByTestId("select-country"), {
57-
target: { value: "JP" },
58-
});
62+
userEvent.selectOptions(screen.getByTestId("select-country"), "JP");
63+
expect(screen.queryByText("VAT number:")).not.toBeInTheDocument();
5964
});
6065

61-
it("When VAT country is selected, VAT Number input displays", () => {
66+
it("When VAT country is selected, VAT Number input displays", async () => {
6267
const products = [
6368
{
6469
product: UAProduct,
@@ -76,14 +81,11 @@ describe("TaxesTests", () => {
7681
);
7782

7883
expect(screen.getByTestId("select-country")).toBeInTheDocument();
79-
fireEvent.change(screen.getByTestId("select-country"), {
80-
target: { value: "FR" },
81-
});
82-
83-
expect(screen.getByText("VAT number:")).toBeInTheDocument();
84+
await userEvent.selectOptions(screen.getByTestId("select-country"), "FR");
85+
expect(screen.queryByText("VAT number:")).toBeInTheDocument();
8486
});
8587

86-
it("When USA is selected, State select displays", () => {
88+
it("When USA is selected, State select displays", async () => {
8789
const products = [
8890
{
8991
product: UAProduct,
@@ -99,20 +101,17 @@ describe("TaxesTests", () => {
99101
</Formik>
100102
</QueryClientProvider>,
101103
);
102-
fireEvent.change(getByTestId("select-country"), {
103-
target: { value: "US" },
104-
});
104+
await userEvent.selectOptions(getByTestId("select-country"), "US");
105105
expect(screen.getByText("State:")).toBeInTheDocument();
106-
fireEvent.change(getByTestId("select-state"), {
107-
target: { value: "Texas" },
108-
});
106+
await userEvent.selectOptions(getByTestId("select-state"), "Texas");
107+
expect(screen.queryByText("VAT number:")).not.toBeInTheDocument();
109108
});
110109

111110
it("sets status right if country is stored", () => {
112111
global.window = Object.create(window);
113112
Object.defineProperty(window, "accountId", { value: "ABCDEF" });
114113

115-
const intialValues = {
114+
const initialValues = {
116115
country: "GB",
117116
};
118117
const products = [
@@ -123,7 +122,7 @@ describe("TaxesTests", () => {
123122
];
124123
render(
125124
<QueryClientProvider client={queryClient}>
126-
<Formik initialValues={intialValues} onSubmit={jest.fn()}>
125+
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
127126
<Elements stripe={stripePromise}>
128127
<Taxes products={products} setError={jest.fn()} />
129128
</Elements>
@@ -154,11 +153,11 @@ describe("TaxesTests", () => {
154153
expect(screen.getByRole("button", { name: "Save" })).toBeInTheDocument();
155154
});
156155

157-
it("cancel button resets tax step values", () => {
156+
it("cancel button resets tax step values", async () => {
158157
global.window = Object.create(window);
159158
Object.defineProperty(window, "accountId", { value: "ABCDEF" });
160159

161-
const intialValues = {
160+
const initialValues = {
162161
country: "GB",
163162
VATNumber: "GB123123123",
164163
};
@@ -171,45 +170,127 @@ describe("TaxesTests", () => {
171170
];
172171
render(
173172
<QueryClientProvider client={queryClient}>
174-
<Formik initialValues={intialValues} onSubmit={jest.fn()}>
173+
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
175174
<Elements stripe={stripePromise}>
176175
<Taxes products={products} setError={jest.fn()} />
177176
</Elements>
178177
</Formik>
179178
</QueryClientProvider>,
180179
);
181-
182-
fireEvent.click(screen.getByRole("button", { name: "Edit" }));
183-
fireEvent.change(screen.getByTestId("select-country"), {
184-
target: { value: "FR" },
185-
});
186-
fireEvent.change(screen.getByTestId("field-vat-number"), {
187-
target: { value: "FR123123123" },
188-
});
189-
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
180+
await userEvent.click(screen.getByRole("button", { name: "Edit" }));
181+
await userEvent.selectOptions(screen.getByTestId("select-country"), "FR");
182+
await userEvent.type(screen.getByTestId("field-vat-number"), "FR123123123");
183+
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
190184
expect(screen.getByTestId("country")).toHaveTextContent("United Kingdom");
191185
expect(screen.getByTestId("vat-number")).toHaveTextContent("GB123123123");
192186

193-
fireEvent.click(screen.getByRole("button", { name: "Edit" }));
194-
fireEvent.change(screen.getByTestId("select-country"), {
195-
target: { value: "US" },
196-
});
197-
fireEvent.change(screen.getByTestId("select-state"), {
198-
target: { value: "AL" },
199-
});
200-
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
187+
await userEvent.click(screen.getByRole("button", { name: "Edit" }));
188+
await userEvent.selectOptions(screen.getByTestId("select-country"), "US");
189+
await userEvent.selectOptions(screen.getByTestId("select-state"), "AL");
190+
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
201191
expect(screen.getByTestId("country")).toHaveTextContent("United Kingdom");
202192
expect(screen.getByTestId("vat-number")).toHaveTextContent("GB123123123");
203193

204-
fireEvent.click(screen.getByRole("button", { name: "Edit" }));
205-
fireEvent.change(screen.getByTestId("select-country"), {
206-
target: { value: "CA" },
207-
});
208-
fireEvent.change(screen.getByTestId("select-ca-province"), {
209-
target: { value: "AL" },
210-
});
211-
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
194+
await userEvent.click(screen.getByRole("button", { name: "Edit" }));
195+
await userEvent.selectOptions(screen.getByTestId("select-country"), "CA");
196+
await userEvent.selectOptions(
197+
screen.getByTestId("select-ca-province"),
198+
"AB",
199+
);
200+
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
212201
expect(screen.getByTestId("country")).toHaveTextContent("United Kingdom");
213202
expect(screen.getByTestId("vat-number")).toHaveTextContent("GB123123123");
214203
});
204+
205+
it("Edit should be available for pro users", async () => {
206+
global.window = Object.create(window);
207+
Object.defineProperty(window, "accountId", { value: "ABCDEF" });
208+
209+
const initialValues = {
210+
country: "GB",
211+
VATNumber: "GB123123123",
212+
marketPlace: "canonical-ua",
213+
};
214+
215+
const products = [
216+
{
217+
product: UAProduct,
218+
quantity: 1,
219+
},
220+
];
221+
render(
222+
<QueryClientProvider client={queryClient}>
223+
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
224+
<Elements stripe={stripePromise}>
225+
<Taxes products={products} setError={jest.fn()} />
226+
</Elements>
227+
</Formik>
228+
</QueryClientProvider>,
229+
);
230+
231+
expect(screen.queryByRole("button", { name: "Edit" })).toBeInTheDocument();
232+
await userEvent.click(screen.getByRole("button", { name: "Edit" }));
233+
234+
const countryField = await screen.findByTestId("select-country");
235+
expect(countryField).toBeInTheDocument();
236+
expect(screen.getByTestId("field-vat-number")).toBeInTheDocument();
237+
expect(screen.getByRole("button", { name: "Save" })).toBeInTheDocument();
238+
});
239+
240+
it("Edit button should not be displayed for channel users", () => {
241+
global.window = Object.create(window);
242+
Object.defineProperty(window, "accountId", { value: "ABCDEF" });
243+
const initialValues = {
244+
country: "GB",
245+
VATNumber: "GB123123123",
246+
marketPlace: UserSubscriptionMarketplace.CanonicalProChannel,
247+
};
248+
const products = [
249+
{
250+
product: DistributorProduct,
251+
quantity: 1,
252+
},
253+
];
254+
render(
255+
<QueryClientProvider client={queryClient}>
256+
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
257+
<Elements stripe={stripePromise}>
258+
<Taxes products={products} setError={jest.fn()} />
259+
</Elements>
260+
</Formik>
261+
</QueryClientProvider>,
262+
);
263+
264+
expect(screen.queryByTestId("tax-edit-button")).toBeInTheDocument();
265+
});
266+
267+
it("New channel users should be able to add their tax info", async () => {
268+
global.window = Object.create(window);
269+
Object.defineProperty(window, "accountId", { value: "ABCDEF" });
270+
271+
const initialValues = {
272+
country: "",
273+
VATNumber: "",
274+
marketPlace: UserSubscriptionMarketplace.CanonicalProChannel,
275+
};
276+
277+
const products = [
278+
{
279+
product: DistributorProduct,
280+
quantity: 1,
281+
},
282+
];
283+
render(
284+
<QueryClientProvider client={queryClient}>
285+
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
286+
<Elements stripe={stripePromise}>
287+
<Taxes products={products} setError={jest.fn()} />
288+
</Elements>
289+
</Formik>
290+
</QueryClientProvider>,
291+
);
292+
293+
expect(screen.queryByTestId("tax-edit-button")).not.toBeInTheDocument();
294+
expect(screen.getByRole("button", { name: "Save" })).toBeInTheDocument();
295+
});
215296
});

0 commit comments

Comments
 (0)