Skip to content

Commit

Permalink
chore: implement PR review suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: SeSo <[email protected]>
  • Loading branch information
Sepehr-Sobhani committed Nov 21, 2024
1 parent dc06cec commit 9c23643
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 53 deletions.
14 changes: 12 additions & 2 deletions bc_obps/registration/tests/endpoints/v2/test_user_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ def test_list_user_operators_v2_with_filter(self):
assert response.status_code == 200
response_items_1 = response.json().get('items')
assert response_items_1[0].get('user__first_name') == "Jane"
# Test with a type filter that doesn't exist
response = TestUtils.mock_get_with_auth_role(
self, "cas_admin", self.url + "?user__first_name=John&user__last_name=Smith"
)
Expand Down Expand Up @@ -390,7 +389,7 @@ def test_list_user_operators_v2_returns_valid_data(self):
assert response.status_code == 200
response_data = response.json()
assert len(response_data) == 2
# assert one of the approved admin user operators is in the response
# assert one of the approved admin user operators (as a sample) is in the response and has proper data
approved_admin_user_operator_to_check = approved_admin_user_operators[0]
approved_admin_user_operator_in_response = next(
(
Expand Down Expand Up @@ -426,3 +425,14 @@ def test_list_user_operators_v2_returns_valid_data(self):
approved_admin_user_operator_in_response["operator__legal_name"]
== approved_admin_user_operator_to_check.operator.legal_name
)
# make sure keys are what we expect based on the schema (using sorted to ensure order doesn't matter)
assert set(approved_admin_user_operator_in_response.keys()) == {
"id",
"user_friendly_id",
"status",
"user__first_name",
"user__last_name",
"user__email",
"user__bceid_business_name",
"operator__legal_name",
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from itertools import cycle

import pytest
from model_bakery import baker
from registration.models import Operator
Expand All @@ -23,58 +25,75 @@ def test_get_admin_user_operator_requests_for_irc_users():

# Prepare user operators for various roles and statuses
# Declined operator with user operators (should be excluded in final result)
for _ in range(5):
user_operators_with_declined_operator.append(
baker.make_recipe(
'utils.user_operator',
role=UserOperator.Roles.ADMIN,
status=UserOperator.Statuses.PENDING,
operator=declined_operator,
)
user_operators_with_declined_operator.extend(
baker.make_recipe(
'utils.user_operator',
user=cycle(baker.make_recipe('utils.industry_operator_user', _quantity=5)),
role=UserOperator.Roles.ADMIN,
status=UserOperator.Statuses.PENDING,
operator=declined_operator,
_quantity=5,
)
)

# Approved admin user operators (should be included in final result)
approved_admin_user_operators.append(
baker.make_recipe(
'utils.user_operator', role=UserOperator.Roles.ADMIN, status=UserOperator.Statuses.APPROVED
)
# Approved admin user operators (should be included in final result)
approved_admin_user_operators.extend(
baker.make_recipe(
'utils.user_operator',
user=cycle(baker.make_recipe('utils.industry_operator_user', _quantity=5)),
role=UserOperator.Roles.ADMIN,
status=UserOperator.Statuses.APPROVED,
_quantity=5,
)
)

# Pending admin user operators for approved operator (should be included in final result)
pending_admin_user_operators_for_approved_operator.append(
baker.make_recipe(
'utils.user_operator',
operator=approved_operator,
role=UserOperator.Roles.ADMIN,
status=UserOperator.Statuses.PENDING,
)
# Pending(status) admin user operators for approved operator (should be included in final result)
pending_admin_user_operators_for_approved_operator.extend(
baker.make_recipe(
'utils.user_operator',
user=cycle(baker.make_recipe('utils.industry_operator_user', _quantity=5)),
operator=approved_operator,
role=UserOperator.Roles.ADMIN,
status=UserOperator.Statuses.PENDING,
_quantity=5,
)
)

# Declined admin user operators (should be included in final result)
declined_admin_user_operators.append(
baker.make_recipe(
'utils.user_operator', role=UserOperator.Roles.ADMIN, status=UserOperator.Statuses.DECLINED
)
# Declined admin user operators (should be included in final result)
declined_admin_user_operators.extend(
baker.make_recipe(
'utils.user_operator',
user=cycle(baker.make_recipe('utils.industry_operator_user', _quantity=5)),
role=UserOperator.Roles.ADMIN,
status=UserOperator.Statuses.DECLINED,
_quantity=5,
)
)

# Declined pending user operators (should be included in final result only if no approved admin exists)
declined_pending_user_operators.append(
baker.make_recipe(
'utils.user_operator', role=UserOperator.Roles.PENDING, status=UserOperator.Statuses.DECLINED
)
# Declined pending (role) user operators (should be included in final result only if no approved admin exists)
declined_pending_user_operators.extend(
baker.make_recipe(
'utils.user_operator',
user=cycle(baker.make_recipe('utils.industry_operator_user', _quantity=5)),
role=UserOperator.Roles.PENDING,
status=UserOperator.Statuses.DECLINED,
_quantity=5,
)
)

# Pending user operators for the approved operator, with a PENDING status (should be excluded due to approved admin user)
pending_user_operators_with_pending_status.append(
baker.make_recipe(
'utils.user_operator',
role=UserOperator.Roles.PENDING,
status=UserOperator.Statuses.PENDING,
operator=approved_operator,
)
# Pending (role/status) user operators for the approved operator(should be excluded due to approved admin user)
pending_user_operators_with_pending_status.extend(
baker.make_recipe(
'utils.user_operator',
user=cycle(baker.make_recipe('utils.industry_operator_user', _quantity=5)),
role=UserOperator.Roles.PENDING,
status=UserOperator.Statuses.PENDING,
operator=approved_operator,
_quantity=5,
)
)

# Add approved admin user for the approved operator (to prevent showing pending user operators for this operator)
# Add approved admin user for the approved operator (to prevent showing pending(status) user operators for this operator)
approved_user_operator_for_approved_operator = baker.make_recipe(
'utils.user_operator',
role=UserOperator.Roles.ADMIN,
Expand Down
38 changes: 27 additions & 11 deletions bc_obps/service/tests/test_user_operator_service_v2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from itertools import cycle

import pytest
from model_bakery import baker

Expand Down Expand Up @@ -44,7 +46,7 @@ def test_save_operator():
assert Operator.objects.first().status == Operator.Statuses.APPROVED

@staticmethod
def test_list_user_operators_v2():
def test_list_user_operators_v2_industry_users_are_not_authorized():
filters_1 = UserOperatorFilterSchema(
user_friendly_id="1",
status="pending",
Expand All @@ -62,17 +64,31 @@ def test_list_user_operators_v2():
user_guid=industry_user.user_guid, filters=filters_1, sort_field="created_at", sort_order="asc"
)

@staticmethod
def test_list_user_operators_v2():

# add some user operators
for _ in range(5):
baker.make_recipe(
'utils.user_operator', role=UserOperator.Roles.ADMIN, status=UserOperator.Statuses.APPROVED
)
baker.make_recipe(
'utils.user_operator', role=UserOperator.Roles.ADMIN, status=UserOperator.Statuses.DECLINED
)
baker.make_recipe(
'utils.user_operator', role=UserOperator.Roles.ADMIN, status=UserOperator.Statuses.PENDING
)
baker.make_recipe(
'utils.user_operator',
user=cycle(baker.make_recipe('utils.industry_operator_user', _quantity=5)),
role=UserOperator.Roles.ADMIN,
status=UserOperator.Statuses.APPROVED,
_quantity=5,
)
baker.make_recipe(
'utils.user_operator',
user=cycle(baker.make_recipe('utils.industry_operator_user', _quantity=5)),
role=UserOperator.Roles.ADMIN,
status=UserOperator.Statuses.DECLINED,
_quantity=5,
)
baker.make_recipe(
'utils.user_operator',
user=cycle(baker.make_recipe('utils.industry_operator_user', _quantity=5)),
role=UserOperator.Roles.ADMIN,
status=UserOperator.Statuses.PENDING,
_quantity=5,
)

assert UserOperator.objects.count() == 15

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from "@bciers/testConfig/mocks";
import UserOperatorsPage from "@/administration/app/components/userOperators/UserOperatorsPage";
import { expect } from "vitest";
import { UserOperatorStatus } from "@bciers/utils/src/enums";

useSearchParams.mockReturnValue({
get: vi.fn(),
Expand All @@ -17,6 +18,32 @@ vi.mock(
}),
);

const mockResponse = {
rows: [
{
id: 1,
user_friendly_id: "1",
status: UserOperatorStatus.APPROVED,
user__first_name: "John",
user__last_name: "Doe",
user__email: "[email protected]",
user__bceid_business_name: "John Doe Inc.",
operator__legal_name: "FakeOperator 1",
},
{
id: 2,
user_friendly_id: "2",
status: UserOperatorStatus.PENDING,
user__first_name: "Jane",
user__last_name: "Smith",
user__email: "[email protected]",
user__bceid_business_name: "Jane Smith Inc.",
operator__legal_name: "FakeOperator 2",
},
],
row_count: 2,
};

describe("User Operators (External Access Requests) Page", () => {
beforeEach(() => {
vi.clearAllMocks();
Expand All @@ -37,6 +64,13 @@ describe("User Operators (External Access Requests) Page", () => {
expect(screen.queryByRole("grid")).toBeInTheDocument();
expect(screen.getByText(/No records found/i)).toBeVisible();
});
it("renders UserOperatorsPage that correctly handle a non-empty data array", async () => {
getUserOperatorsPageData.mockReturnValueOnce(mockResponse);
render(await UserOperatorsPage({ searchParams: {} }));
expect(screen.queryByRole("grid")).toBeInTheDocument();
const allRows = screen.getAllByRole("row");
expect(allRows).toHaveLength(4); // 2 rows + 1 header + 1 filter row
});
it("renders the appropriate error component when getUserOperatorsPageData fails", async () => {
getUserOperatorsPageData.mockReturnValueOnce(undefined);
expect(async () =>
Expand Down

0 comments on commit 9c23643

Please sign in to comment.