Fakultas Ilmu Komputer UI

Commit dd92ebe2 authored by Jonathan Christopher Jakub's avatar Jonathan Christopher Jakub
Browse files

[RED] Change and fix tests for accounts and logs

parent 3dbaec9a
Pipeline #41269 failed with stages
in 1 minute and 28 seconds
......@@ -51,33 +51,6 @@ class AccountViewTest(APITestCase):
self.assertIn('"user_1"', response_string)
self.assertIn('"user_2"', response_string)
def test_list_all_accounts_paginate_failed(self):
url = self.BASE_URL + "?page=100"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"detail":"Invalid page."', response_string)
def test_list_all_accounts_filter_success(self):
url = self.BASE_URL + "?username=" + self.user_1.username
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":1', response_string)
def test_list_all_accounts_filter_failed(self):
url = self.BASE_URL + "?username=1234567890"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":0', response_string)
def test_retrieve_account_success(self):
url = self.BASE_URL + str(self.officer.id) + "/"
response = self.client.get(url)
......@@ -140,33 +113,15 @@ class AccountViewTest(APITestCase):
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(officer_current_count, officer_prev_count + 1)
# Have account creation log for the new officer
new_officer_id = Account.objects.filter(email=_account_id)[0].id
response = self.client.get("/logs/")
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"object_id":"{}"'.format(new_officer_id), response_string)
self.assertIn(
'"action_type":"{}"'.format(ACTIVITY_TYPE_CREATE), response_string
)
def test_username_lowercased(self):
url = self.BASE_URL
data = {
"name": self.faker.name(),
"username": "aBcDeFgH",
"password": "justpass",
"email": self.faker.email(),
"phone_number": self.faker.phone_number(),
"area": self.faker.city(),
"is_admin": False,
}
# # Have account creation log for the new officer
# new_officer_id = Account.objects.filter(email=_account_id)[0].id
# response = self.client.get("/logs/")
# response_string = response.rendered_content.decode("utf-8")
self.client.post(
path=url, data=data, format="json",
)
self.assertTrue(Account.objects.filter(user__username="abcdefgh").exists())
# self.assertIn('"object_id":"{}"'.format(new_officer_id), response_string)
# self.assertIn(
# '"action_type":"{}"'.format(ACTIVITY_TYPE_CREATE), response_string
# )
def test_create_new_account_fails_with_poor_password(self):
url = self.BASE_URL
......@@ -209,12 +164,12 @@ class AccountViewTest(APITestCase):
str(response.content, encoding="utf8"), expected_returned_data
)
# Have account update log
response = self.client.get("/logs/")
response_string = response.rendered_content.decode("utf-8")
# # Have account update log
# response = self.client.get("/logs/")
# response_string = response.rendered_content.decode("utf-8")
self.assertIn('"object_id":"{}"'.format(self.officer.id), response_string)
self.assertIn('"action_type":"{}"'.format(ACTIVITY_TYPE_EDIT), response_string)
# self.assertIn('"object_id":"{}"'.format(self.officer.id), response_string)
# self.assertIn('"action_type":"{}"'.format(ACTIVITY_TYPE_EDIT), response_string)
def test_edit_account_fail_without_complete_fields(self):
url = self.BASE_URL + str(self.officer.id) + "/"
......@@ -233,16 +188,16 @@ class AccountViewTest(APITestCase):
response = self.client.delete(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
# Have account deletion log
response = self.client.get("/logs/")
response_string = response.rendered_content.decode("utf-8")
# # Have account deletion log
# response = self.client.get("/logs/")
# response_string = response.rendered_content.decode("utf-8")
self.assertIn('"object_id":"{}"'.format(self.officer.id), response_string)
self.assertIn(
'"action_type":"{}"'.format(ACTIVITY_TYPE_DELETE), response_string
)
# self.assertIn('"object_id":"{}"'.format(self.officer.id), response_string)
# self.assertIn(
# '"action_type":"{}"'.format(ACTIVITY_TYPE_DELETE), response_string
# )
def test_retrieve_current_profile_success(self):
url = self.PROFILE_URL
......
import factory
import uuid
from django.contrib.auth.models import User
from faker import Faker
from apps.logs.models import Log
from apps.accounts.tests.factories.accounts import AccountFactory
faker = Faker()
class LogFactory(factory.DjangoModelFactory):
class Meta:
model = Log
author = factory.SubFactory(AccountFactory)
action_type = "Create"
......@@ -6,8 +6,6 @@ from rest_framework.authtoken.models import Token
from rest_framework.test import APITestCase, APIClient
from apps.accounts.tests.factories.accounts import AccountFactory, UserFactory
from apps.logs.tests.factories.logs import LogFactory
from apps.logs.models import Log
from apps.cases.models import CaseSubject
from apps.cases.tests.factories.case_subjects import CaseSubjectFactory
from apps.cases.tests.factories.cases import InvestigationCaseFactory
......@@ -19,97 +17,30 @@ class ActivityLogViewTest(APITestCase):
def setUpTestData(self):
self.maxDiff = None
user = UserFactory(username="user_1", password="justpass")
self.account = AccountFactory(admin=False, user=user)
self.token, _ = Token.objects.get_or_create(user=user)
officer_user = UserFactory(username="user_1", password="justpass")
self.officer_account = AccountFactory(admin=False, user=officer_user)
self.officer_token, _ = Token.objects.get_or_create(user=officer_user)
another_user = UserFactory(username="user_2", password="justpass")
another_account = AccountFactory(admin=False, user=another_user)
admin_user = UserFactory(username="admin_user_1", password="justpass")
admin_user = UserFactory(username="admin_user", password="justpass")
self.admin_account = AccountFactory(admin=True, user=admin_user)
self.admin_token, _ = Token.objects.get_or_create(user=admin_user)
case_subject = CaseSubjectFactory()
self.case = InvestigationCaseFactory(
author=self.account, case_subject_id=case_subject.subject_id,
)
self.log = LogFactory(
author=self.account,
revision_id=self.case.revision_id,
object_id=self.case.case_id,
model_name="InvestigationCase",
)
self.another_log = LogFactory(
author=another_account,
revision_id=self.case.revision_id,
object_id=self.case.case_id,
model_name="InvestigationCase",
)
def setUp(self):
self.client = APIClient(HTTP_AUTHORIZATION=HEADER_PREFIX + self.token.key)
def test_string_representation(self):
log_str = (
f"{self.log.created_at} | [{self.log.action_type}] "
+ f"{self.log.model_name} - {self.log.author}"
author=self.officer_account, case_subject=case_subject,
)
self.assertEqual(log_str, str(self.log))
def test_list_only_current_author_logs_success(self):
url = "/logs/"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = {
"count": 1,
"next": None,
"previous": None,
"results": [
{
"id": str(self.log.id),
"model_name": self.log.model_name,
"revision_id": str(self.log.revision_id),
"object_id": str(self.log.object_id),
"action_type": self.log.action_type,
"created_at": str(response.data["results"][0]["created_at"]),
"latest_version": str(self.log.revision_id),
"author": {
"id": str(self.account.id),
"name": self.account.name,
"username": self.account.user.username,
"email": self.account.email,
"phone_number": self.account.phone_number,
"area": self.account.area,
"is_admin": self.account.is_admin,
"is_verified": self.account.is_verified,
"is_active": self.account.is_active,
},
},
],
}
self.assertJSONEqual(json.dumps(response.data), data)
def test_admin_account_get_all_logs(self):
def get_logs_response(self):
url = "/logs/"
self.client = APIClient(HTTP_AUTHORIZATION=HEADER_PREFIX + self.admin_token.key)
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":2', response_string)
return response_string
def test_list_logs_paginate_failed(self):
url = "/logs/?page=100"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"detail":"Invalid page."', response_string)
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_get_latest_version_success(self):
latest_version = self.log.latest_version
latest_editable_version = self.case
self.assertEqual(latest_version, latest_editable_version)
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())
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