Skip to content

Commit

Permalink
test: add support api integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanttV committed Oct 30, 2024
1 parent d92f662 commit 79ab3ad
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 1 deletion.
Empty file.
149 changes: 149 additions & 0 deletions eox_core/api/support/v1/tests/integration/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
"""Support API request mixin for the Pre Enrollments API."""

from __future__ import annotations

import ddt
import requests
from django.conf import settings as ds
from django.urls import reverse
from rest_framework import status

# TODO: move the FAKE_USER_DATA to a common place
from eox_core.api.v1.tests.integration.data.fake_users import FAKE_USER_DATA
from eox_core.api.v1.tests.integration.test_views import UsersAPIRequestMixin
from eox_core.tests.integration.utils import BaseIntegrationTest, make_request

settings = ds.INTEGRATION_TEST_SETTINGS


class SupportAPIRequestMixin:
"""Mixin class for the Pre Enrollments API request methods."""

DELETE_USER_URL = f"{settings['EOX_CORE_API_BASE']}{reverse('eox-support-api:eox-support-api:edxapp-user')}"
UPDATE_USERNAME_URL = (
f"{settings['EOX_CORE_API_BASE']}{reverse('eox-support-api:eox-support-api:edxapp-replace-username')}"
)
OAUTH_APP_URL = (
f"{settings['EOX_CORE_API_BASE']}{reverse('eox-support-api:eox-support-api:edxapp-oauth-application')}"
)

def delete_user(self, tenant: dict, params: dict | None = None) -> requests.Response:
"""
Delete an edxapp user in a tenant.
Args:
tenant (dict): The tenant data.
params (dict, optional): The query parameters for the request.
Returns:
requests.Response: The response object.
"""
return make_request(tenant, "DELETE", url=self.DELETE_USER_URL, params=params)

# TODO: Check of the default value of data should be None
def update_username(self, tenant: dict, params: dict, data: dict | None = None) -> requests.Response:
"""
Update an edxapp user's username in a tenant.
Args:
tenant (dict): The tenant data.
params (dict): The query parameters for the request.
data (dict, optional): The body data for the request.
Returns:
requests.Response: The response object.
"""
return make_request(tenant, "PATCH", url=self.UPDATE_USERNAME_URL, params=params, data=data)

# TODO: Check of the default value of data should be None
def create_oauth_application(self, tenant: dict, data: dict | None = None) -> requests.Response:
"""
Create an oauth application in a tenant.
Args:
tenant (dict): The tenant data.
data (dict, optional): The body data for the request.
Returns:
requests.Response: The response object.
"""
return make_request(tenant, "POST", url=self.OAUTH_APP_URL, data=data)


@ddt.ddt
class TestEdxAppUserAPIIntegration(SupportAPIRequestMixin, BaseIntegrationTest, UsersAPIRequestMixin):
"""Integration tests for the EdxApp User API."""

@ddt.data("username", "email")
def test_delete_user_in_tenant_success(self, query_param: str) -> None:
"""
Test delete an edxapp user in a tenant.
Open edX definitions tested:
- `get_edxapp_user`
- `delete_edxapp_user`
Expected result:
- The status code is 200.
- The response indicates the user was removed successfully from the tenant.
- The user is not found in the tenant.
"""
data = next(FAKE_USER_DATA)
self.create_user(self.tenant_x, data)

response = self.delete_user(self.tenant_x, {query_param: data[query_param]})
response_data = response.json()
get_response = self.get_user(self.tenant_y, {query_param: data[query_param]})
get_response_data = get_response.json()

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response_data, f"The user {data['username']} <{data['email']}> has been removed")
self.assertEqual(get_response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(
get_response_data["detail"],
f"No user found by {{'{query_param}': '{data[query_param]}'}} on site {self.tenant_y['domain']}.",
)

@ddt.data(
("username", "user-not-found"),
("email", "[email protected]"),
)
@ddt.unpack
def test_delete_user_in_tenant_not_found(self, query_param: str, value: str) -> None:
"""
Test delete an edxapp user in a tenant that does not exist.
Open edX definitions tested:
- `get_edxapp_user`
Expected result:
- The status code is 404.
- The response indicates the user was not found in the tenant.
"""
response = self.delete_user(self.tenant_x, {query_param: value})
response_data = response.json()

self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(
response_data["detail"],
f"No user found by {{'{query_param}': '{value}'}} on site {self.tenant_x['domain']}.",
)

def test_delete_user_missing_required_fields(self) -> None:
"""
Test delete an edxapp user in a tenant without providing the username or email.
Expected result:
- The status code is 400.
- The response indicates the username is required.
"""
response = self.delete_user(self.tenant_x)
response_data = response.json()

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response_data, ["Email or username needed"])


@ddt.ddt
class TestOauthApplicationAPIIntegration(SupportAPIRequestMixin, BaseIntegrationTest, UsersAPIRequestMixin):
"""Integration tests for the Oauth Application API."""
9 changes: 8 additions & 1 deletion eox_core/edxapp_wrapper/backends/users_m_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,19 @@ def delete_edxapp_user(*args, **kwargs):
site = kwargs.get("site")
is_support_user = kwargs.get("is_support_user")

print(f"\n\nUser: {user}, Site: {site}, Is Support User: {is_support_user}\n\n")

user_response = f"The user {user.username} <{user.email}> "
print(f"\n\nUser response: {user_response}\n\n")

signup_sources = user.usersignupsource_set.all()
sources = [signup_source.site for signup_source in signup_sources]

if site and site.name.upper() in (source.upper() for source in sources):
print(f"\n\nSources: {sources}\n\n")
print(f"\n\nSite Name: {site.name.upper()}\n\n")
print(f"\n\nSite Domain: {site.domain}\n\n")

if site and site.domain.upper() in (source.upper() for source in sources):
if len(sources) == 1:
with transaction.atomic():
support_label = "_support" if is_support_user else ""
Expand Down

0 comments on commit 79ab3ad

Please sign in to comment.