Skip to content

Commit

Permalink
Refactor users and users_profile unit and endtoend test
Browse files Browse the repository at this point in the history
  • Loading branch information
riadelimemmedov committed Mar 26, 2024
1 parent 88992b1 commit 0b96cc0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 70 deletions.
19 changes: 12 additions & 7 deletions backend/config/tests/factories.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
21 changes: 5 additions & 16 deletions backend/config/tests/user_profile/test_user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
66 changes: 29 additions & 37 deletions backend/config/tests/users/test_endpoint_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"]
Expand All @@ -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.
Expand All @@ -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"],
Expand All @@ -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.
Expand All @@ -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}
Expand All @@ -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.
Expand All @@ -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"
)
Expand All @@ -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.
Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions backend/config/tests/users/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0b96cc0

Please sign in to comment.