-
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 units to user-service and added volumes to user-service an…
…d frontend
- Loading branch information
Showing
9 changed files
with
205 additions
and
6 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
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
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 |
---|---|---|
|
@@ -21,4 +21,5 @@ gunicorn==20.1.0; python_version <= '3.12' | |
django-environ | ||
daphne | ||
pytest | ||
pytest-django | ||
pytest-django | ||
pytest-mock |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[pytest] | ||
DJANGO_SETTINGS_MODULE = user_service.settings | ||
python_files = tests.py test_*.py *_tests.py |
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
Empty file.
103 changes: 103 additions & 0 deletions
103
Backend/user_service/user_service/user_service/test/conftest.py
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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# conftest.py | ||
import os | ||
import django | ||
from django.conf import settings | ||
import pytest | ||
from datetime import timedelta | ||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'user_service.settings') | ||
|
||
if not settings.configured: | ||
settings.configure( | ||
DEBUG=True, | ||
DATABASES={ | ||
"default": { | ||
"ENGINE": "django.db.backends.postgresql", | ||
"NAME": "postgres", | ||
"USER": "root", | ||
"PASSWORD": "root", | ||
"PORT": "5432", | ||
"HOST": "localhost", | ||
"ATOMIC_REQUESTS": True, | ||
"TEST": { | ||
"NAME": "mytestdatabase", | ||
"ATOMIC_REQUESTS": True, | ||
}, | ||
}, | ||
}, | ||
INSTALLED_APPS=[ | ||
'django.contrib.admin', | ||
'django.contrib.auth', | ||
'django.contrib.contenttypes', | ||
'django.contrib.sessions', | ||
'django.contrib.messages', | ||
'django.contrib.staticfiles', | ||
'rest_framework', | ||
'corsheaders', | ||
'user_app', | ||
], | ||
MIDDLEWARE=[ | ||
'django.middleware.security.SecurityMiddleware', | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.middleware.common.CommonMiddleware', | ||
'django.middleware.csrf.CsrfViewMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'django.contrib.messages.middleware.MessageMiddleware', | ||
'django.middleware.clickjacking.XFrameOptionsMiddleware', | ||
], | ||
ROOT_URLCONF='user_service.urls', | ||
TEMPLATES=[ | ||
{ | ||
'BACKEND': 'django.template.backends.django.DjangoTemplates', | ||
'DIRS': [], | ||
'APP_DIRS': True, | ||
'OPTIONS': { | ||
'context_processors': [ | ||
'django.template.context_processors.debug', | ||
'django.template.context_processors.request', | ||
'django.contrib.auth.context_processors.auth', | ||
'django.contrib.messages.context_processors.messages', | ||
], | ||
}, | ||
}, | ||
], | ||
WSGI_APPLICATION='user_service.wsgi.application', | ||
SECRET_KEY='django-insecure-woftd2en2**zr(b%#*2vit2v%s@(k54gb^c(ots0abo7(wsmo%', | ||
ALLOWED_HOSTS=['localhost', '127.0.0.1', '[::1]', 'user-session', 'user-session:8004'], | ||
RABBITMQ_HOST='localhost', | ||
RABBITMQ_USER='user', | ||
RABBITMQ_PASS='pass', | ||
RABBITMQ_PORT='5672', | ||
REST_FRAMEWORK={ | ||
'DEFAULT_AUTHENTICATION_CLASSES': ( | ||
'rest_framework_simplejwt.authentication.JWTAuthentication', | ||
), | ||
}, | ||
SIMPLE_JWT={ | ||
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60), | ||
'REFRESH_TOKEN_LIFETIME': timedelta(days=1), | ||
'ROTATE_REFRESH_TOKENS': False, | ||
'BLACKLIST_AFTER_ROTATION': True, | ||
'AUTH_HEADER_TYPES': ('Bearer',), | ||
'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',), | ||
'TOKEN_OBTAIN_SERIALIZER': 'user_auth.serializers.CustomTokenObtainPairSerializer', | ||
}, | ||
LANGUAGE_CODE='en-us', | ||
TIME_ZONE='UTC', | ||
USE_I18N=True, | ||
USE_L10N=True, | ||
USE_TZ=True, | ||
STATIC_URL='/static/', | ||
DEFAULT_AUTO_FIELD='django.db.models.BigAutoField', | ||
CORS_ORIGIN_ALLOW_ALL=True, | ||
) | ||
|
||
django.setup() | ||
|
||
@pytest.fixture(scope='session', autouse=True) | ||
def django_db_modify_db_settings(): | ||
settings.DATABASES['default'] = settings.DATABASES['default']['TEST'] | ||
|
||
@pytest.fixture(scope='session') | ||
def db_access(db): | ||
pass |
79 changes: 79 additions & 0 deletions
79
Backend/user_service/user_service/user_service/test/tests.py
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import pytest | ||
import json | ||
from django.urls import reverse | ||
from unittest.mock import patch, MagicMock | ||
from rest_framework_simplejwt.tokens import RefreshToken | ||
from user_app.views import UserViewSet, RegisterViewSet | ||
from rest_framework import status | ||
from user_app.models import User | ||
|
||
@pytest.fixture | ||
def api_client(): | ||
from rest_framework.test import APIClient | ||
return APIClient() | ||
|
||
@pytest.fixture | ||
def user_data(): | ||
return { | ||
'id': 1, | ||
'username': 'testuser', | ||
'email': '[email protected]', | ||
'password': 'Test@123' | ||
} | ||
|
||
@pytest.mark.django_db | ||
def test_user_register(api_client, user_data): | ||
url = reverse('register-user') | ||
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.fixture | ||
def user(db): | ||
return User.objects.create_user(username='testuser', email='[email protected]', password='Test@123') | ||
|
||
@pytest.fixture | ||
def admin_user(db): | ||
return User.objects.create_superuser(username='admin', email='[email protected]', password='Admin@123') | ||
|
||
@pytest.fixture | ||
def user_token(user): | ||
refresh = RefreshToken.for_user(user) | ||
return str(refresh.access_token) | ||
|
||
@pytest.fixture | ||
def admin_token(admin_user): | ||
refresh = RefreshToken.for_user(admin_user) | ||
return str(refresh.access_token) | ||
|
||
@pytest.mark.django_db | ||
def test_users_list(api_client, admin_user, admin_token, user_data): | ||
# Mock the RabbitMQ interactions | ||
user1 = User.objects.create_user(username='testuser1',email='[email protected]',password='Test@123') | ||
user2 = User.objects.create_user(username='testuser2',email='[email protected]',password='Test@123') | ||
with patch('user_app.views.publish_message') as mock_publish, patch('user_app.views.consume_message') as mock_consume: | ||
# Set up the mock for consume_message to simulate a valid token response | ||
def mock_consume_response(queue_name, callback): | ||
response_data = json.dumps({"is_valid": True}) | ||
ch_mock = MagicMock() | ||
method = None | ||
properties = None | ||
body = response_data.encode('utf-8') | ||
callback(ch_mock, method, properties, body) | ||
|
||
mock_consume.side_effect = mock_consume_response | ||
|
||
# Authenticate the request | ||
token = admin_token | ||
api_client.credentials(HTTP_AUTHORIZATION=f'Bearer {token}') | ||
|
||
# Call the API endpoint | ||
url = reverse('users-list') # Ensure this matches your URL configuration | ||
response = api_client.get(url) | ||
|
||
# Assert the response status and content | ||
print("response data=", response.data) | ||
assert response.status_code == status.HTTP_200_OK |
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