Skip to content

Commit

Permalink
CUEOBSERVE #64 Test case fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
PraveenCuebook committed Aug 12, 2021
1 parent 4486a90 commit 5da4f31
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
1 change: 0 additions & 1 deletion api/anomaly/tests/test_anomalyDefinition_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from users.models import CustomUser
from django.contrib.auth import login, authenticate

auth_required= True if settings.AUTHENTICATION_REQUIRED == "True" else False

@pytest.fixture()
def setup_user(db):
Expand Down
1 change: 0 additions & 1 deletion api/anomaly/tests/test_anomaly_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from anomaly.models import AnomalyDefinition, Anomaly
from users.models import CustomUser
from django.conf import settings
auth_required= True if settings.AUTHENTICATION_REQUIRED == "True" else False

@pytest.fixture()
def setup_user(db):
Expand Down
16 changes: 14 additions & 2 deletions api/anomaly/tests/test_rootCauseAnalysis_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@
from anomaly.models import RCAAnomaly
from ops.tasks import rootCauseAnalysisJob
from ops.tasks.rootCauseAnalysis import _anomalyDetectionForValue
from users.models import CustomUser

@pytest.fixture()
def setup_user(db):
"""sets up a user to be used for login"""
user = CustomUser.objects.create_superuser("admin@domain.com", "admin")
user.status = "Active"
user.is_active = True
user.name = "Sachin"
user.save()

@pytest.mark.django_db(transaction=True)
def test_rootCauseAnalysis(client, mocker):
def test_rootCauseAnalysis(setup_user, client, mocker):
"""
"""
client.login(email="admin@domain.com", password="admin")
anomalyData = {
'anomalyLatest': {'highOrLow': 'low',
'value': 2.0,
Expand Down Expand Up @@ -225,8 +236,9 @@ def dummyParallelizeAnomalyDetection(anomalyId: int, dimension: str, dimValsData


@pytest.mark.django_db(transaction=True)
def test_calculateRCA(client, mocker):
def test_calculateRCA(setup_user, client, mocker):

client.login(email="admin@domain.com", password="admin")
mockRCAJob = mocker.patch(
"ops.tasks.rootCauseAnalysis.rootCauseAnalysisJob.delay",
new=mock.MagicMock(
Expand Down
16 changes: 8 additions & 8 deletions api/app/middlewares.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from django.utils.deprecation import MiddlewareMixin
from django.contrib.auth.decorators import login_required
import os
from django.conf import settings
from django.shortcuts import redirect
from django.http import HttpResponseForbidden
from django.utils.timezone import now
from django.contrib.auth import logout
from django.shortcuts import redirect
from django.conf import settings
import os
from django.utils.timezone import now
from django.utils.deprecation import MiddlewareMixin
from django.contrib.auth.decorators import login_required

class DisableCsrfCheck(MiddlewareMixin):
"""
Expand Down Expand Up @@ -42,8 +42,8 @@ def process_view(self, request, view_func, view_args, view_kwargs): # pylint: d
return
except:
pass
auth_required= True if settings.AUTHENTICATION_REQUIRED == "True" else False
if(not auth_required):
authenticationRequired= True if settings.AUTHENTICATION_REQUIRED == "True" else False
if(not authenticationRequired):
return

if getattr(view_func, "login_exempt", False):
Expand Down
4 changes: 2 additions & 2 deletions api/users/tests/test_user_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from django.contrib.auth.models import Group
# from commons.utils.token_generator import id_generator
from django.conf import settings
auth_required= True if settings.AUTHENTICATION_REQUIRED == "True" else False
authenticationRequired= True if settings.AUTHENTICATION_REQUIRED == "True" else False

@pytest.fixture()
def setup_user(db):
Expand All @@ -28,7 +28,7 @@ def test_authenticate_get(setup_user, client):
path = reverse("login")
client.login(email="admin@domain.com", password="admin")
response = client.get(path)
assert response.json()["success"] == (True and auth_required)
assert response.json()["success"] == (True and authenticationRequired)

def test_authenticate_post(setup_user, client):
""" tests authenticate_post """
Expand Down
10 changes: 5 additions & 5 deletions api/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def authenticate(self, request):
class Account(APIView):
"""Account authentication"""

auth_required= True if settings.AUTHENTICATION_REQUIRED == "True" else False
authenticationRequired= True if settings.AUTHENTICATION_REQUIRED == "True" else False
authentication_classes = (UnsafeSessionAuthentication,)
@staticmethod
def parse_user(user):
Expand All @@ -50,16 +50,16 @@ def parse_user(user):
def get(self, request):
"""Checks existing session, etc"""
print("request", request)
if self.auth_required:
if self.authenticationRequired:
if request.user.is_authenticated :
user = Account.parse_user(request.user)
Users.objects.filter(pk=request.user.pk)
return Response({"data": user, "success": True, "isAuthenticationRequired": self.auth_required})
return Response({"data": user, "success": True, "isAuthenticationRequired": self.authenticationRequired})
else:
# the login is a GET request, so just show the user the login form.
return Response({"message": "Please log in", "success": False, "isAuthenticationRequired": self.auth_required }, status=401)
return Response({"message": "Please log in", "success": False, "isAuthenticationRequired": self.authenticationRequired }, status=401)
else:
return Response({"message": "Authentication not required", "success": False, "isAuthenticationRequired": self.auth_required})
return Response({"message": "Authentication not required", "success": False, "isAuthenticationRequired": self.authenticationRequired})



Expand Down

0 comments on commit 5da4f31

Please sign in to comment.