Skip to content

Commit

Permalink
fixed all tests for user-service
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooria Toof authored and Pooria Toof committed Aug 22, 2024
1 parent 1070616 commit 0890e1a
Showing 1 changed file with 153 additions and 144 deletions.
297 changes: 153 additions & 144 deletions Backend/user_service/user_service/user_service/test/test_user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
from user_app.serializers import UserSerializer
import os
from dotenv import load_dotenv
from django.conf import settings

headers = {
"X-SERVICE-SECRET": settings.SECRET_KEY
}

load_dotenv()
TOEKNSERVICE = os.environ.get('TOKEN_SERVICE')
Expand Down Expand Up @@ -39,7 +44,10 @@ def admin_data():
@pytest.fixture
def user(db):
email_obj = ConfirmEmail.objects.create(user_email = '[email protected]', verify_status = True)
return UserProfileModel.objects.create(username='testuser', email=email_obj.pk, password='Test@123')
user_serializer = UserSerializer(data = {"username":'testuser', "email":email_obj.pk, "password":'Test@123'})
user_serializer.is_valid(raise_exception=True)
user_serializer.save()
return UserProfileModel.objects.get(username='testuser')

@pytest.fixture
def admin_user(db):
Expand Down Expand Up @@ -99,146 +107,147 @@ def test_users_list(api_client, admin_user, admin_token):
print("response data=", response.data)
assert response.status_code == status.HTTP_200_OK

# @pytest.mark.django_db
# def test_user_login(api_client, admin_user):
# data = {
# "username":"admin",
# "password":"Admin@123"
# }
# url = reverse("user-login")
# response = api_client.post(url, data, format='json')
# print("response_data", response.data)
# assert response.status_code == 200

# @pytest.mark.django_db
# @patch('user_app.user_session_views.requests.post')
# def test_user_logout(mock_post, api_client, admin_user, admin_token):
# # Authenticate the request
# mock_post.return_value.status_code = 200
# mock_post.return_value.json.return_value = {"detail": "User logged out successfully"}
# api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {admin_token}')

# url = reverse('user-logout', kwargs={'pk': admin_user.id})
# response = api_client.post(url)
# assert response.status_code == 200
# assert response.data["detail"] == 'User logged out successfully'
# mock_post.assert_called_once_with(
# f"{TOEKNSERVICE}/auth/token/invalidate-tokens/",
# data={"access": admin_token, 'id':admin_user.id}
# )
# assert UserProfileModel.objects.filter(username=admin_user.username).exists()

# @pytest.mark.django_db
# def test_retrieve_user(api_client, user, user_token):
# # Authenticate the request
# api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')

# url = reverse('user-detail', kwargs={'pk': user.id})
# response = api_client.get(url)

# assert response.status_code == status.HTTP_200_OK
# assert response.data['id'] == user.id
# assert response.data['username'] == user.username
# assert response.data['email'] == user.email

# @pytest.mark.django_db
# def test_update_user(api_client, user, user_token):
# data = {
# "username": "newuser",
# "email": "[email protected]"
# }
# api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')

# url = reverse('user-detail', kwargs={'pk': user.id})
# response = api_client.patch(url, data, format='json')

# assert response.status_code == status.HTTP_202_ACCEPTED
# assert response.data['id'] == user.id
# assert response.data['username'] == 'newuser'
# assert response.data['email'] == '[email protected]'

# def test_destroy_user(api_client, user, user_token):
# api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')

# url = reverse('user-detail', kwargs={'pk': user.id}) # Get the URL for the user object to be deleted
# response = api_client.delete(url) # Call the API endpoint to delete the user object and assert the response status code

# assert response.status_code == status.HTTP_204_NO_CONTENT
# assert not UserProfileModel.objects.filter(username=user.username).exists()

# @pytest.mark.django_db
# def test_valid_data_friend_request_functions(api_client, admin_user, user, user_token, admin_token):

# print("\ntestuser sends a friend request to admin user")
# api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')
# url_request = reverse('send-request', kwargs={'user_pk': user.id})
# response_request = api_client.post(url_request, data={'username': 'admin'}, format='json')
# assert response_request.status_code == status.HTTP_201_CREATED
# assert response_request.data["detail"]=='Friend request sent'

# print("\nAdmin check the friend requests list")
# api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')
# url_request = reverse('friend-request-list', kwargs={'user_pk': admin_user.id})
# response_request = api_client.get(url_request, format='json')
# assert response_request.data[0]["sender_username"] == 'testuser'
# assert response_request.data[0]["receiver_username"] == 'admin'
# assert response_request.data[0]["status"] == 'pending'
# assert response_request.status_code == status.HTTP_200_OK

# print("\nAdmin accept the friend request")
# api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')
# url_request = reverse('accept-request', kwargs={'user_pk': admin_user.id, 'pk':user.id})
# response_request = api_client.put(url_request, format='json')
# assert response_request.data["detail"]=='Request accepted'
# assert response_request.status_code == status.HTTP_202_ACCEPTED

# print("\nAdmin user has testuser in its friends list")
# url_request = reverse('friends-list', kwargs={'user_pk': admin_user.id})
# response_request = api_client.get(url_request, format='json')
# assert response_request.data[0]["username"] == "testuser"

# print("\ntest user has admin in its friends list")
# url_request = reverse('friends-list', kwargs={'user_pk': user.id})
# response_request = api_client.get(url_request, format='json')
# assert response_request.data[0]["username"] == "admin"
# assert response_request.status_code == 200

# print("\ntestuser delete the admin user from its friends list")
# url_request = reverse('remove-friend', kwargs={'user_pk': user.id, 'pk': admin_user.id})
# response_request = api_client.delete(url_request, format='json')
# assert response_request.status_code == status.HTTP_204_NO_CONTENT

# @pytest.mark.django_db
# def test_send_friend_request_invalid_user_id(api_client, admin_user, user, user_token, admin_token):
# api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')

# url_request = reverse('send-request', kwargs={'user_pk': user.id})
# response_request = api_client.post(url_request, data={'username':'invalid_user'}, format='json')
# assert response_request.status_code == 404
# assert response_request.data["error"]=="User does not exist"

# @pytest.mark.django_db
# def test_reject_friend_request(api_client, admin_user, user, user_token, admin_token):

# print("\ntestuser sends a friend request to admin user")
# api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')
# url_request = reverse('send-request', kwargs={'user_pk': user.id})
# response_request = api_client.post(url_request, {'username': 'admin'}, format='json')
# assert response_request.status_code == status.HTTP_201_CREATED
# assert response_request.data["detail"]=='Friend request sent'

# print("\nAdmin check the friend requests list")
# api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')
# url_request = reverse('friend-request-list', kwargs={'user_pk': admin_user.id})
# response_request = api_client.get(url_request, format='json')
# assert response_request.data[0]["sender_username"] == 'testuser'
# assert response_request.data[0]["receiver_username"] == 'admin'
# assert response_request.data[0]["status"] == 'pending'
# assert response_request.status_code == status.HTTP_200_OK

# print("Admin reject the testuser request")
# api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')
# url_request = reverse('reject-request', kwargs={'user_pk': admin_user.id,'pk':user.id})
# response_request = api_client.put(url_request, format='json')
# assert response_request.data["detail"] == "Request rejected"
# assert response_request.status_code == status.HTTP_202_ACCEPTED
@pytest.mark.django_db
def test_user_login(api_client, admin_user):
data = {
"username":"adminuser",
"password":"Admin@123"
}
url = reverse("user-login")
response = api_client.post(url, data, format='json')
print("response_data", response.data)
assert response.status_code == 200

@pytest.mark.django_db
@patch('user_app.user_session_views.requests.post')
def test_user_logout(mock_post, api_client, admin_user, admin_token):
# Authenticate the request
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = {"detail": "User logged out successfully"}
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {admin_token}')

url = reverse('user-logout', kwargs={'pk': admin_user.id})
response = api_client.post(url)
assert response.status_code == 200
assert response.data["detail"] == 'User logged out successfully'
mock_post.assert_called_once_with(
f"{TOEKNSERVICE}/auth/token/invalidate-tokens/",
data={"access": admin_token, 'id':admin_user.id}, headers=headers
)
assert UserProfileModel.objects.filter(username=admin_user.username).exists()

@pytest.mark.django_db
def test_retrieve_user(api_client, user, user_token):
# Authenticate the request
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')

url = reverse('user-detail', kwargs={'pk': user.id})
response = api_client.get(url)

assert response.status_code == status.HTTP_200_OK
assert response.data['id'] == user.id
assert response.data['username'] == user.username
assert response.data['email'] == user.email.user_email

@pytest.mark.django_db
def test_update_user(api_client, user, user_token):
email_obj = ConfirmEmail.objects.create(user_email = "[email protected]", verify_status=True)
data = {
"username": "newuser",
"email": email_obj.pk
}
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')

url = reverse('user-detail', kwargs={'pk': user.id})
response = api_client.patch(url, data, format='json')

assert response.status_code == status.HTTP_202_ACCEPTED
assert response.data['id'] == user.id
assert response.data['username'] == 'newuser'
assert response.data['email'] == '[email protected]'

def test_destroy_user(api_client, user, user_token):
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')

url = reverse('user-detail', kwargs={'pk': user.id}) # Get the URL for the user object to be deleted
response = api_client.delete(url) # Call the API endpoint to delete the user object and assert the response status code

assert response.status_code == status.HTTP_204_NO_CONTENT
assert not UserProfileModel.objects.filter(username=user.username).exists()

@pytest.mark.django_db
def test_valid_data_friend_request_functions(api_client, admin_user, user, user_token, admin_token):

print("\ntestuser sends a friend request to admin user")
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')
url_request = reverse('send-request', kwargs={'user_pk': user.id})
response_request = api_client.post(url_request, data={'username': 'adminuser'}, format='json')
assert response_request.status_code == status.HTTP_201_CREATED
assert response_request.data["detail"]=='Friend request sent'

print("\nAdmin check the friend requests list")
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')
url_request = reverse('friend-request-list', kwargs={'user_pk': admin_user.id})
response_request = api_client.get(url_request, format='json')
assert response_request.data[0]["sender_username"] == 'testuser'
assert response_request.data[0]["receiver_username"] == 'adminuser'
assert response_request.data[0]["status"] == 'pending'
assert response_request.status_code == status.HTTP_200_OK

print("\nAdmin accept the friend request")
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')
url_request = reverse('accept-request', kwargs={'user_pk': admin_user.id, 'pk':user.id})
response_request = api_client.put(url_request, format='json')
assert response_request.data["detail"]=='Request accepted'
assert response_request.status_code == status.HTTP_202_ACCEPTED

print("\nAdmin user has testuser in its friends list")
url_request = reverse('friends-list', kwargs={'user_pk': admin_user.id})
response_request = api_client.get(url_request, format='json')
assert response_request.data[0]["username"] == "testuser"

print("\ntest user has admin in its friends list")
url_request = reverse('friends-list', kwargs={'user_pk': user.id})
response_request = api_client.get(url_request, format='json')
assert response_request.data[0]["username"] == "adminuser"
assert response_request.status_code == 200

print("\ntestuser delete the admin user from its friends list")
url_request = reverse('remove-friend', kwargs={'user_pk': user.id, 'pk': admin_user.id})
response_request = api_client.delete(url_request, format='json')
assert response_request.status_code == status.HTTP_204_NO_CONTENT

@pytest.mark.django_db
def test_send_friend_request_invalid_user_id(api_client, admin_user, user, user_token, admin_token):
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')

url_request = reverse('send-request', kwargs={'user_pk': user.id})
response_request = api_client.post(url_request, data={'username':'invalid_user'}, format='json')
assert response_request.status_code == 404
assert response_request.data["error"]=="User does not exist"

@pytest.mark.django_db
def test_reject_friend_request(api_client, admin_user, user, user_token, admin_token):

print("\ntestuser sends a friend request to admin user")
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')
url_request = reverse('send-request', kwargs={'user_pk': user.id})
response_request = api_client.post(url_request, {'username': 'adminuser'}, format='json')
assert response_request.status_code == status.HTTP_201_CREATED
assert response_request.data["detail"]=='Friend request sent'

print("\nAdmin check the friend requests list")
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')
url_request = reverse('friend-request-list', kwargs={'user_pk': admin_user.id})
response_request = api_client.get(url_request, format='json')
assert response_request.data[0]["sender_username"] == 'testuser'
assert response_request.data[0]["receiver_username"] == 'adminuser'
assert response_request.data[0]["status"] == 'pending'
assert response_request.status_code == status.HTTP_200_OK

print("Admin reject the testuser request")
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {user_token}')
url_request = reverse('reject-request', kwargs={'user_pk': admin_user.id,'pk':user.id})
response_request = api_client.put(url_request, format='json')
assert response_request.data["detail"] == "Request rejected"
assert response_request.status_code == status.HTTP_202_ACCEPTED

0 comments on commit 0890e1a

Please sign in to comment.