Fakultas Ilmu Komputer UI

Commit 52aeffdc authored by Irwanto's avatar Irwanto
Browse files

Merge branch 'irwanto/fix-logs' into 'staging'

Fixed logs and is_positive condition

See merge request !56
parents 0633b5e5 07ac6a8b
Pipeline #49473 passed with stages
in 6 minutes and 6 seconds
......@@ -35,6 +35,7 @@ class AccountViewTest(APITestCase):
cls.faker = Faker()
def setUp(self):
self.noAuthClient = APIClient()
self.client = APIClient(HTTP_AUTHORIZATION=HEADER_PREFIX + self.token_1.key)
def test_string_representation(self):
......@@ -138,6 +139,30 @@ class AccountViewTest(APITestCase):
f'"action_type":"{ACTIVITY_TYPE_CREATE}"', response_string
)
def test_create_new_officer_success_without_auth_token(self):
url = self.BASE_URL
_account_id = self.faker.email()
officer_prev_count = Account.objects.filter(is_admin=False).count()
data = {
"name": self.faker.name(),
"username": _account_id,
"password": "justpass",
"email": _account_id,
"phone_number": "+999999999999",
"area": self.faker.city(),
"is_admin": False,
"is_verified": False,
"is_active": False,
}
response = self.noAuthClient.post(path=url, data=data, format="json",)
officer_current_count = Account.objects.filter(is_admin=False).count()
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(officer_current_count, officer_prev_count + 1)
def test_create_existing_user_fails(self):
url = self.BASE_URL
data = {
......
from django.core.mail import send_mail
from django.contrib.auth.models import User
from django.contrib.auth.models import AnonymousUser, User
from django.shortcuts import get_object_or_404
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import status, viewsets
......@@ -73,7 +73,13 @@ class AccountViewSet(viewsets.ModelViewSet):
)
user = User.objects.create_user(username=username, password=password)
Account.objects.create(user=user, **serializer.validated_data)
if isinstance(self.request.user, AnonymousUser):
Account.objects.create(user=user, **serializer.validated_data)
else:
Account.objects.create(
user=user,
author=self.request.user.account,
**serializer.validated_data)
return Response(
serializer.data, status=status.HTTP_201_CREATED,
......
......@@ -93,7 +93,7 @@ class InvestigationCaseSerializer(serializers.ModelSerializer):
outcome = self.validated_data.get('outcome', None)
is_positive = None
if outcome is not None:
is_positive = True if "+" in outcome else False
is_positive = False if "-" in outcome else True
super(InvestigationCaseSerializer, self).save(author=account, is_positive=is_positive)
......
......@@ -13,7 +13,7 @@ class CustomAuthTest(APITestCase):
user = User.objects.create(username="jonathan")
user.set_password("justpass")
user.save()
cls.user = user
cls.user = user
def _test_success(self, data):
response = self.client.post(path=self.URL, data=data, format="json")
......
......@@ -19,4 +19,4 @@ class CustomAuthToken(ObtainAuthToken):
user = serializer.validated_data["user"]
token, _ = Token.objects.get_or_create(user=user)
return Response(data={ "token": token.key }, status=status.HTTP_200_OK)
return Response(data={"token": token.key}, status=status.HTTP_200_OK)
......@@ -10,15 +10,16 @@ from apps.cases.tests.factories.cases import InvestigationCaseFactory
from apps.exportables.renderers import INVESTIGATION_CASE_RENDERER_FIELDS
def init_data():
InvestigationCaseFactory(
case_subject=CaseSubjectFactory(
district="Beji",
age=19,
is_male=True,
),
is_positive=True,
)
case_subject=CaseSubjectFactory(
district="Beji",
age=19,
is_male=True,
),
is_positive=True,
)
InvestigationCaseFactory(
case_subject=CaseSubjectFactory(
district="Cilodong",
......
......@@ -39,8 +39,4 @@ class ActivityLogViewTest(APITestCase):
def test_list_only_current_author_logs_success(self):
self.client = APIClient(HTTP_AUTHORIZATION=HEADER_PREFIX + self.officer_token.key)
self.assertIn('"count":2', self.get_logs_response())
def test_admin_account_get_all_logs_success(self):
self.client = APIClient(HTTP_AUTHORIZATION=HEADER_PREFIX + self.admin_token.key)
self.assertIn('"count":4', self.get_logs_response())
self.assertIn('"count":1', self.get_logs_response())
......@@ -21,16 +21,11 @@ class LogAPIView(ListAPIView):
def _get_logs_queryset(self, queryset, request):
account = request.user.account
if account.is_admin:
return queryset
return queryset.filter(author=account)
def get_queryset(self, request):
account = request.user.account
if account.is_admin:
account_logs = AccountHistory.objects.all()
else:
account_logs = AccountHistory.objects.filter(object_id=account.id)
account_logs = AccountHistory.objects.filter(author=account.id)
case_subject_logs = (
self._get_logs_queryset(CaseSubjectHistory.objects.all(), request))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment