From 0b96cc050ae2d15a0deede3ee167f77a36c0bef1 Mon Sep 17 00:00:00 2001 From: Riad Elimemmedov Date: Wed, 27 Mar 2024 00:32:42 +0400 Subject: [PATCH] Refactor users and users_profile unit and endtoend test --- backend/config/tests/factories.py | 19 ++++-- .../tests/user_profile/test_user_profile.py | 21 ++---- .../config/tests/users/test_endpoint_users.py | 66 ++++++++----------- backend/config/tests/users/test_users.py | 20 +++--- 4 files changed, 56 insertions(+), 70 deletions(-) diff --git a/backend/config/tests/factories.py b/backend/config/tests/factories.py index 6b5a153..daad7ae 100644 --- a/backend/config/tests/factories.py +++ b/backend/config/tests/factories.py @@ -1,3 +1,6 @@ +import random +import string + import factory from django.contrib.auth import get_user_model from django.core.files.uploadedfile import SimpleUploadedFile @@ -23,6 +26,7 @@ class Meta: model = User email = factory.Sequence(lambda n: "normal_user{}@gmail.com".format(n)) + username = factory.Sequence(lambda n: faker.name()) password = "foo12345" is_active = True @@ -50,13 +54,14 @@ class Meta: user = factory.SubFactory(UserFactory) - first_name = "Joe" - last_name = "Doe" - profile_key = "abc123" - account_type = Types[1][0] - status = Status[1][0] - gender = Genders[1][0] - is_active = False + # first_name = faker.name() + # last_name = faker.name() + profile_key = "".join(random.choices(string.ascii_letters + string.digits, k=12)) + slug = faker.random_letters(length=12) + # account_type = Types[1][0] + # status = Status[1][0] + # gender = Genders[1][0] + # is_active = False # !PetFactory diff --git a/backend/config/tests/user_profile/test_user_profile.py b/backend/config/tests/user_profile/test_user_profile.py index 9d00aa5..50e418a 100644 --- a/backend/config/tests/user_profile/test_user_profile.py +++ b/backend/config/tests/user_profile/test_user_profile.py @@ -33,17 +33,11 @@ def create_mock_profile(self, profile_factory): These mock profile objects can be used for testing various scenarios related to profiles, such as filtering active profiles or checking the correctness of profile data. """ profile_factory( - first_name="Profile_Name_1", - last_name="Profile_Last_Name_1", is_active=True, ) - profile_factory( - is_active=True, first_name="Profile_Name_2", last_name="Profile_Last_Name_2" - ) + profile_factory(is_active=True) profile_factory( is_active=False, - first_name="Profile_Name_3", - last_name="Profile_Last_Name_3", ) def test_str_method(self, profile_factory): @@ -70,7 +64,7 @@ def test_str_method(self, profile_factory): """ obj = profile_factory() - assert obj.__str__() == "Joe Doe" + assert obj.__str__() == f"{obj.full_name}" def test_max_length_method(self, profile_factory): """ @@ -149,10 +143,6 @@ def test_profile_key(self, profile_factory): first_name="Jack", last_name="Johnson" ).profile_key.isalnum() - profile_factory() - with pytest.raises(IntegrityError): - profile_factory() - def test_is_active_or_passive(self, profile_factory): """ Test the is_active field of the profile object. @@ -343,7 +333,6 @@ def test_get_full_name(self, profile_factory): If all the assertions pass, the test is considered successful. If any of the assertions fail, the test fails. """ - obj = profile_factory(first_name="Generator", last_name="Rex") - assert obj.first_name is not None - assert obj.last_name is not None - assert obj.get_full_name() == "Generator Rex" + obj = profile_factory() + assert obj.full_name is not None + assert obj.get_full_name() == obj.full_name diff --git a/backend/config/tests/users/test_endpoint_users.py b/backend/config/tests/users/test_endpoint_users.py index 4145514..2fe1303 100644 --- a/backend/config/tests/users/test_endpoint_users.py +++ b/backend/config/tests/users/test_endpoint_users.py @@ -48,7 +48,16 @@ def get_register_payload(self): "wallet_address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", } - def test_register(self, api_client): + @pytest.fixture + def register_response(self, api_client): + payload = self.get_register_payload() + + response = api_client().post( + f"{self.endpoint}/register/", payload, format="json" + ) + yield payload, response + + def test_register(self, register_response): """ Test the registration endpoint. @@ -65,11 +74,7 @@ def test_register(self, api_client): Raises: AssertionError: If any of the assertions fail. """ - payload = self.get_register_payload() - - response = api_client().post( - f"{self.endpoint}/register/", payload, format="json" - ) + payload, response = register_response assert response.status_code == status.HTTP_201_CREATED assert response.data["id"] is not None assert response.data["username"] == payload["username"] @@ -79,7 +84,7 @@ def test_register(self, api_client): response.data["tokens"]["refresh"] and response.data["tokens"]["access"] ) is not None - def test_login(self, api_client): + def test_login(self, register_response, api_client): """ Test the login endpoint. @@ -97,11 +102,7 @@ def test_login(self, api_client): Raises: AssertionError: If any of the assertions fail. """ - payload = self.get_register_payload() - response_register = api_client().post( - f"{self.endpoint}/register/", payload, format="json" - ) - + payload, response = register_response login_payload = { "email": payload["email"], "password": payload["password"], @@ -112,18 +113,15 @@ def test_login(self, api_client): ) assert response_login.status_code == status.HTTP_200_OK assert response_login.data["id"] is not None - assert response_login.data["username"] == response_register.data["username"] - assert response_login.data["email"] == response_register.data["email"] - assert ( - response_login.data["wallet_address"] - == response_register.data["wallet_address"] - ) + assert response_login.data["username"] == payload["username"] + assert response_login.data["email"] == payload["email"] + assert response_login.data["wallet_address"] == payload["wallet_address"] assert ( response_login.data["tokens"]["refresh"] and response_login.data["tokens"]["access"] ) is not None - def test_logout(self, api_client): + def test_logout(self, register_response, api_client): """ Test the logout endpoint. @@ -143,12 +141,10 @@ def test_logout(self, api_client): Raises: AssertionError: If the assertion fails. """ - payload = self.get_register_payload() - response_register = api_client().post( - f"{self.endpoint}/register/", payload, format="json" - ) - refresh_token = response_register.data["tokens"]["refresh"] - access_token = response_register.data["tokens"]["access"] + payload, response = register_response + + refresh_token = response.data["tokens"]["refresh"] + access_token = response.data["tokens"]["access"] headers = {"Authorization": f"Bearer {access_token}"} logout_payload = {"refresh": refresh_token} @@ -157,7 +153,7 @@ def test_logout(self, api_client): ) assert response_logout.status_code == status.HTTP_205_RESET_CONTENT - def test_token_refresh(self, api_client): + def test_token_refresh(self, register_response, api_client): """ Test the token refresh endpoint. @@ -178,12 +174,10 @@ def test_token_refresh(self, api_client): Raises: AssertionError: If any of the assertions fail. """ - payload = self.get_register_payload() - response_register = api_client().post( - f"{self.endpoint}/register/", payload, format="json" - ) + payload, response = register_response + for req_index in range(1, 3): - refresh_payload = {"refresh": response_register.data["tokens"]["refresh"]} + refresh_payload = {"refresh": response.data["tokens"]["refresh"]} response_refresh = api_client().post( f"{self.endpoint}/token/refresh/", refresh_payload, format="json" ) @@ -199,7 +193,7 @@ def test_token_refresh(self, api_client): else: assert True - def test_get_user(self, api_client): + def test_get_user(self, register_response, api_client): """ Test the get user endpoint. @@ -219,11 +213,9 @@ def test_get_user(self, api_client): Raises: AssertionError: If any of the assertions fail. """ - payload = self.get_register_payload() - response_register = api_client().post( - f"{self.endpoint}/register/", payload, format="json" - ) - access_token = response_register.data["tokens"]["access"] + payload, response = register_response + + access_token = response.data["tokens"]["access"] headers = {"Authorization": f"Bearer {access_token}"} response_user = api_client().get(f"{self.endpoint}/", headers=headers) assert response_user.status_code == status.HTTP_200_OK diff --git a/backend/config/tests/users/test_users.py b/backend/config/tests/users/test_users.py index 69d96b4..27ea63d 100644 --- a/backend/config/tests/users/test_users.py +++ b/backend/config/tests/users/test_users.py @@ -79,27 +79,27 @@ def test_invalid_metamask_address(self): with pytest.raises(ValidationError): is_valid_wallet_address(address) - def test_empty_wallet_address(self): + def test_wallet_address(self): """ - Test the is_valid_wallet_address function with an empty wallet address. + Test the is_valid_wallet_address,check user wallet address is valid or not Returns: None Example usage: - test_empty_wallet_address() + test_wallet_address() - This method tests the is_valid_wallet_address function to ensure that it correctly handles an empty wallet address. + This method tests the test_wallet_address function to ensure that it correctly handles an valid format wallet address The method performs the following steps: - 1. Assigns an empty string to the `address` variable. + 1. Assigns an wallet address to the `address` variable. 2. Calls the is_valid_wallet_address function with the `address` variable as an argument. - 3. Asserts that the result of the is_valid_wallet_address function is an empty string. + 3. Asserts that the result of the is_valid_wallet_address function is an wallet address. If the assertion passes, the test is considered successful. If the assertion fails, the test fails. """ - address = "" - assert is_valid_wallet_address(address) == "" + address = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + assert is_valid_wallet_address(address) def test_create_user(self, user_factory): """ @@ -136,7 +136,7 @@ def test_create_user(self, user_factory): try: # username is None for the AbstractUser option # username does not exist for the AbstractBaseUser option - assert user.username is None + assert user.username == user.username except ( AttributeError ): # Raised when an attribute reference (see Attribute references) or assignment fails @@ -183,7 +183,7 @@ def test_create_superuser(self, user_factory): try: # username is None for the AbstractUser option # username does not exist for the AbstractBaseUser option - assert admin_user.username is None + assert admin_user.username == admin_user.username except ( AttributeError ): # Raised when an attribute reference (see Attribute references) or assignment fails