Skip to content

Commit

Permalink
Merge pull request RedHatInsights#931 from petracihalova/create_custo…
Browse files Browse the repository at this point in the history
…mer_method_removal

refactor: removed not used method _create_customer()
  • Loading branch information
petracihalova authored Oct 30, 2023
2 parents a3cbfd5 + e21d7d9 commit fad0387
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 58 deletions.
6 changes: 3 additions & 3 deletions tests/api/cross_access/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def setUp(self):
self.account = self.customer_data["account_id"]
self.org_id = self.customer_data["org_id"]
self.associate_non_admin_request_context = self._create_request_context(
self.customer_data, self.user_data, create_customer=False, is_org_admin=False, is_internal=True
self.customer_data, self.user_data, is_org_admin=False, is_internal=True
)
self.associate_non_admin_request = self.associate_non_admin_request_context["request"]

Expand All @@ -57,13 +57,13 @@ def setUp(self):
self.not_anemic_customer_data["account_id"] = self.not_anemic_account
self.not_anemic_customer_data["tenant_name"] = f"acct{self.not_anemic_account}"
self.associate_not_anemic_request_context = self._create_request_context(
self.not_anemic_customer_data, self.user_data, create_customer=False, is_org_admin=False, is_internal=True
self.not_anemic_customer_data, self.user_data, is_org_admin=False, is_internal=True
)
self.associate_not_anemic_request = self.associate_not_anemic_request_context["request"]
self.not_anemic_headers = self.associate_not_anemic_request_context["request"].META

self.associate_admin_request_context = self._create_request_context(
self.customer_data, self.user_data, create_customer=False, is_org_admin=True, is_internal=True
self.customer_data, self.user_data, is_org_admin=True, is_internal=True
)
self.associate_admin_request = self.associate_admin_request_context["request"]

Expand Down
22 changes: 0 additions & 22 deletions tests/identity_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,11 @@ def _create_user_data(cls):
user_data = {"username": cls.fake.user_name(), "email": cls.fake.email()}
return user_data

@classmethod
def _create_customer(cls, account, create_tenant=False, org_id=None):
"""Create a customer.
Args:
account (str): The account identifier
Returns:
(Customer) The created customer
"""
tenant_name = create_tenant_name(account)
tenant = None
if create_tenant:
tenant = Tenant(tenant_name=tenant_name, account_id=account, org_id=org_id)
tenant.save()
return tenant

@classmethod
def _create_request_context(
cls,
customer_data,
user_data,
create_customer=True,
create_tenant=False,
is_org_admin=True,
is_internal=False,
cross_account=False,
Expand All @@ -103,8 +83,6 @@ def _create_request_context(
customer = customer_data
account = customer.get("account_id")
org_id = customer.get("org_id", None)
if create_customer:
cls.customer = cls._create_customer(account, create_tenant=create_tenant, org_id=org_id)

identity = cls._build_identity(user_data, account, org_id, is_org_admin, is_internal)
if cross_account:
Expand Down
20 changes: 5 additions & 15 deletions tests/internal/integration/test_integration_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def setUp(self):
super().setUp()
self.client = APIClient()
self.customer = self.customer_data
self.internal_request_context = self._create_request_context(
self.customer, self.user_data, create_customer=False, is_internal=True, create_tenant=False
)
self.internal_request_context = self._create_request_context(self.customer, self.user_data, is_internal=True)

self.request = self.internal_request_context["request"]
user = User()
Expand Down Expand Up @@ -129,9 +127,7 @@ def test_groups_valid_account(self, mock_request):

def test_groups_invalid_account(self):
"""Test that a /tenant/<id>/groups/?username= request from an external account fails."""
external_request_context = self._create_request_context(
self.customer, self.user_data, create_customer=False, is_internal=False, create_tenant=False
)
external_request_context = self._create_request_context(self.customer, self.user_data, is_internal=False)
request = external_request_context["request"]
response = self.client.get(
f"/_private/api/v1/integrations/tenant/{self.tenant.org_id}/groups/?username=user_a",
Expand Down Expand Up @@ -198,9 +194,7 @@ def test_groups_for_principal_valid_account(self):

def test_groups_for_principal_invalid_account(self):
"""Test that a /tenant/<id>/principal/<username>groups/ request from an external account fails."""
external_request_context = self._create_request_context(
self.customer, self.user_data, create_customer=False, is_internal=False, create_tenant=False
)
external_request_context = self._create_request_context(self.customer, self.user_data, is_internal=False)
request = external_request_context["request"]
response = self.client.get(
f"/_private/api/v1/integrations/tenant/{self.tenant.org_id}/principal/user_a/groups/",
Expand Down Expand Up @@ -274,9 +268,7 @@ def test_roles_from_group_invalid_uuid(self):
def test_roles_from_group_invalid_account(self):
"""Test that a /tenant/<id>/groups/<uuid>/roles/ request from an external account fails."""
group_all_uuid = Group.objects.get(name="Group All").uuid
external_request_context = self._create_request_context(
self.customer, self.user_data, create_customer=False, is_internal=False, create_tenant=False
)
external_request_context = self._create_request_context(self.customer, self.user_data, is_internal=False)
request = external_request_context["request"]
response = self.client.get(
f"/_private/api/v1/integrations/tenant/{self.tenant.org_id}/groups/{group_all_uuid}/roles/",
Expand Down Expand Up @@ -341,9 +333,7 @@ def test_roles_for_group_principal_valid(self):
def test_roles_for_group_principal_invalid_account(self):
"""Test that a valid request to /tenant/<id>/principal/user_admin/groups/<uuid>/roles/ from an external account fails."""
group_all_uuid = Group.objects.get(name="Group All").uuid
external_request_context = self._create_request_context(
self.customer, self.user_data, create_customer=False, is_internal=False, create_tenant=False
)
external_request_context = self._create_request_context(self.customer, self.user_data, is_internal=False)
request = external_request_context["request"]
response = self.client.get(
f"/_private/api/v1/integrations/tenant/{self.tenant.org_id}/principal/user_admin/groups/{group_all_uuid}/roles/",
Expand Down
4 changes: 1 addition & 3 deletions tests/internal/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ def setUp(self):
super().setUp()
self.client = APIClient()
self.customer = self.customer_data
self.internal_request_context = self._create_request_context(
self.customer, self.user_data, create_customer=False, is_internal=True, create_tenant=False
)
self.internal_request_context = self._create_request_context(self.customer, self.user_data, is_internal=True)

self.request = self.internal_request_context["request"]
user = User()
Expand Down
2 changes: 1 addition & 1 deletion tests/management/role/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ def test_list_role_with_username_forbidden_to_nonadmin(self):
"""Test that non admin can not read a list of roles for username."""
# Setup non admin request
non_admin_request_context = self._create_request_context(
self.customer_data, self.user_data, create_customer=False, is_org_admin=False
self.customer_data, self.user_data, is_org_admin=False
)
non_admin_request = non_admin_request_context["request"]

Expand Down
24 changes: 10 additions & 14 deletions tests/rbac/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def setUp(self):
self.customer = self._create_customer_data()
self.tenant_name = create_tenant_name(self.customer["account_id"])
self.org_id = self.customer["org_id"]
self.request_context = self._create_request_context(self.customer, self.user_data, create_customer=False)
self.request_context = self._create_request_context(self.customer, self.user_data)
self.request = self.request_context["request"]
self.request.path = "/api/v1/providers/"
user = User()
Expand All @@ -117,7 +117,7 @@ def test_get_tenant_with_user(self):

def test_get_tenant_with_no_user(self):
"""Test that a 401 is returned."""
request_context = self._create_request_context(self.customer, None, create_customer=False)
request_context = self._create_request_context(self.customer, None)
mock_request = request_context["request"]
mock_request.path = "/api/v1/providers/"
middleware = IdentityHeaderMiddleware(get_response=IdentityHeaderMiddleware.process_request)
Expand All @@ -129,7 +129,7 @@ def test_get_tenant_with_org_id(self):
user_data = self._create_user_data()
customer = self._create_customer_data()
customer["org_id"] = "45321"
request_context = self._create_request_context(customer, user_data, create_customer=True)
request_context = self._create_request_context(customer, user_data)
request = request_context["request"]
request.path = "/api/v1/providers/"
request.META["QUERY_STRING"] = ""
Expand All @@ -154,7 +154,7 @@ def setUp(self):
self.customer = self._create_customer_data()
self.tenant_name = create_tenant_name(self.customer["account_id"])
self.org_id = self.customer["org_id"]
self.request_context = self._create_request_context(self.customer, self.user_data, create_customer=False)
self.request_context = self._create_request_context(self.customer, self.user_data)
self.request = self.request_context["request"]
self.request.path = "/api/v1/providers/"
self.request.META["QUERY_STRING"] = ""
Expand All @@ -171,7 +171,7 @@ def test_process_cross_account_request(self):
middleware = IdentityHeaderMiddleware(get_response=IdentityHeaderMiddleware.process_request)
# User without redhat email will fail.
request_context = self._create_request_context(
self.customer, self.user_data, create_customer=False, cross_account=True, is_internal=True
self.customer, self.user_data, cross_account=True, is_internal=True
)
mock_request = request_context["request"]
mock_request.path = "/api/v1/providers/"
Expand All @@ -181,9 +181,7 @@ def test_process_cross_account_request(self):

# User with is_internal equal to False will fail.
self.user_data["email"] = "[email protected]"
request_context = self._create_request_context(
self.customer, self.user_data, create_customer=False, cross_account=True
)
request_context = self._create_request_context(self.customer, self.user_data, cross_account=True)
mock_request = request_context["request"]
mock_request.path = "/api/v1/providers/"

Expand All @@ -193,7 +191,7 @@ def test_process_cross_account_request(self):
# Success pass if user is internal and with redhat email
self.user_data["email"] = "[email protected]"
request_context = self._create_request_context(
self.customer, self.user_data, create_customer=False, cross_account=True, is_internal=True
self.customer, self.user_data, cross_account=True, is_internal=True
)
mock_request = request_context["request"]
mock_request.path = "/api/v1/providers/"
Expand Down Expand Up @@ -227,7 +225,7 @@ def test_process_no_customer(self):
customer = self._create_customer_data()
account_id = customer["account_id"]
del customer["account_id"]
request_context = self._create_request_context(customer, self.user_data, create_customer=False)
request_context = self._create_request_context(customer, self.user_data)
mock_request = request_context["request"]
mock_request.path = "/api/v1/providers/"
middleware = IdentityHeaderMiddleware(get_response=IdentityHeaderMiddleware.process_request)
Expand Down Expand Up @@ -260,7 +258,7 @@ def test_tenant_process_without_org_id(self):

user_data = self._create_user_data()
customer = self._create_customer_data()
request_context = self._create_request_context(customer, user_data, create_customer=False)
request_context = self._create_request_context(customer, user_data)
request = request_context["request"]
request.path = "/api/v1/providers/"
request.META["QUERY_STRING"] = ""
Expand Down Expand Up @@ -356,9 +354,7 @@ def setUp(self):
super().setUp()
self.user_data = self._create_user_data()
self.customer = self._create_customer_data()
self.internal_request_context = self._create_request_context(
self.customer, self.user_data, create_customer=False, is_internal=True
)
self.internal_request_context = self._create_request_context(self.customer, self.user_data, is_internal=True)

def test_internal_user_can_access_private_api(self):
request = self.internal_request_context["request"]
Expand Down

0 comments on commit fad0387

Please sign in to comment.