-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added test_user_register and test_users_list to the test_user_service.py
- Loading branch information
Showing
1 changed file
with
172 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,8 @@ | |
from user_app.views import UserViewSet, RegisterViewSet, FriendsViewSet, validate_token | ||
from user_app.user_session_views import UserLoginView, UserLogoutView | ||
from rest_framework import status | ||
from user_app.models import UserProfileModel | ||
from user_app.models import UserProfileModel, ConfirmEmail | ||
from user_app.serializers import UserSerializer | ||
import os | ||
from dotenv import load_dotenv | ||
|
||
|
@@ -27,13 +28,26 @@ def user_data(): | |
'password': 'Test@123' | ||
} | ||
|
||
def admin_data(): | ||
return { | ||
'id': 1, | ||
'username': 'adminuser', | ||
'email': '[email protected]', | ||
'password': 'Admin@123' | ||
} | ||
|
||
@pytest.fixture | ||
def user(db): | ||
return UserProfileModel.objects.create_user(username='testuser', email='[email protected]', password='Test@123') | ||
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') | ||
|
||
@pytest.fixture | ||
def admin_user(db): | ||
return UserProfileModel.objects.create_superuser(username='admin', email='[email protected]', password='Admin@123') | ||
admin_email = ConfirmEmail.objects.create(user_email = '[email protected]', verify_status = True) | ||
user_obj = UserSerializer(data = {"username":'adminuser', "email":admin_email.pk, "password":'Admin@123'}) | ||
user_obj.is_valid(raise_exception=True) | ||
user_obj.save() | ||
return UserProfileModel.objects.get(username='adminuser') | ||
|
||
@pytest.fixture | ||
def user_token(user): | ||
|
@@ -52,20 +66,28 @@ def mock_validate_token(): | |
|
||
@pytest.mark.django_db | ||
def test_user_register(api_client, user_data): | ||
email_obj = ConfirmEmail.objects.create(user_email = '[email protected]', verify_status = True) | ||
url = reverse('user-register') | ||
user_data["email"] = email_obj.pk | ||
response = api_client.post(url, user_data, format='json') | ||
assert response.status_code == 201 | ||
assert response.data['id'] == 1 | ||
assert response.data['username'] == 'testuser' | ||
assert response.data['email'] == '[email protected]' | ||
|
||
@pytest.mark.django_db | ||
def test_users_list(api_client, admin_user, admin_token, user_data): | ||
def test_users_list(api_client, admin_user, admin_token): | ||
# Mock the RabbitMQ interactions | ||
user1 = UserProfileModel.objects.create_user(username='testuser1',email='[email protected]',password='Test@123') | ||
user2 = UserProfileModel.objects.create_user(username='testuser2',email='[email protected]',password='Test@123') | ||
email1_obj = ConfirmEmail.objects.create(user_email = '[email protected]', verify_status = True) | ||
user1 = UserSerializer(data = {'username':'testuser1','email' : email1_obj.pk,'password':'Test@123'}) | ||
user1.is_valid(raise_exception=True) | ||
user1.save() | ||
email2_obj = ConfirmEmail.objects.create(user_email = '[email protected]', verify_status = True) | ||
user2 = UserSerializer(data = {'username':'testuser2','email' : email2_obj.pk,'password':'Test@123'}) | ||
user2.is_valid(raise_exception=True) | ||
user2.save() | ||
|
||
# Authenticate the request | ||
# Authenticate the request | ||
token = admin_token | ||
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {token}') | ||
|
||
|
@@ -77,146 +99,146 @@ def test_users_list(api_client, admin_user, admin_token, user_data): | |
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":"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 |