Skip to content

Elastic search assets and users 165965388 #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 67 additions & 3 deletions api/tests/test_asset_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,21 @@ def test_authenticated_user_get_single_asset_via_asset_code(
"{}?asset_code={}".format(self.asset_urls, self.asset.asset_code),
HTTP_AUTHORIZATION="Token {}".format(self.token_user),
)
self.assertIn(self.asset.asset_code, response.data["results"][0]["asset_code"])
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn(self.asset.asset_code, response.data["results"][0]["asset_code"])

@patch("api.authentication.auth.verify_id_token")
def test_authenticated_securityuser_get_single_asset_via_asset_code(
def test_securityuser_filtering_asset_via_asset_code(
self, mock_verify_id_token
):
mock_verify_id_token.return_value = {"email": self.security_user.email}
response = client.get(
"{}?asset_code={}".format(self.asset_urls, self.asset.asset_code),
f"{self.asset_urls}?asset_code={self.asset.asset_code}",
HTTP_AUTHORIZATION="Token {}".format(self.token_checked_by),
)
filtered_assets = Asset.objects.filter(asset_code=self.asset.asset_code)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(filtered_assets), len(response.data["results"]))
self.assertIn(self.asset.asset_code, response.data["results"][0]["asset_code"])

@patch("api.authentication.auth.verify_id_token")
Expand Down Expand Up @@ -470,3 +472,65 @@ def test_fetching_assets_assigned_to_a_random_user_by_non_admin(
str(response.data["detail"]),
"Operation not permitted. You are not authorised.",
)

@patch("api.authentication.auth.verify_id_token")
def test_search_fnx_on_asset_endpoint(self, mock_verify_id_token):
mock_verify_id_token.return_value = {"email": self.admin_user.email}
self.admin_user.is_securityuser = True
self.admin_user.save()
self.admin_user.refresh_from_db()

asset_one = Asset.objects.create(
asset_code="ANDELA001",
serial_number="SN_ART_001",
active=True,
model_number=self.assetmodel,
purchase_date="2018-07-10",
asset_location=self.centre,
department=self.department,
)

asset_two = Asset.objects.create(
asset_code="ANDELA002",
serial_number="RANDOM_SERIAL_NUMBER",
model_number=self.assetmodel,
purchase_date="2018-07-10",
asset_location=self.centre,
department=self.department,
)

asset_three = Asset.objects.create(
asset_code="NCOOO",
serial_number="SN_ART_002",
model_number=self.assetmodel,
purchase_date="2018-07-10",
asset_location=self.centre,
department=self.department,
)

response = client.get(
"{}?search={}".format(self.asset_urls, "ANDELA"),
HTTP_AUTHORIZATION="Token {}".format(self.token_admin),
)
# return only assets with ANDELA match which is just asset_one and asset_two
self.assertEqual(response.data["count"], 2)
self.assertEqual(
response.data.get("results")[1].get("uuid"), str(asset_one.uuid)
)
self.assertEqual(
response.data.get("results")[0].get("uuid"), str(asset_two.uuid)
)

response = client.get(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more appropriate if the tests are independent to show different functionalities rather than grouping them together. Different tests cases could should show different fields the search query string reaches.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test objects created could be done in a helper function/method and called when need be.

"{}?search={}".format(self.asset_urls, "SN_ART"),
HTTP_AUTHORIZATION="Token {}".format(self.token_admin),
)
# return only assets with SN_ART match which is just asset_one and asset_three
self.assertEqual(response.data["count"], 2)
self.assertEqual(
response.data.get("results")[1].get("uuid"), str(asset_one.uuid)
)
self.assertEqual(
response.data.get("results")[0].get("uuid"), str(asset_three.uuid)
)

5 changes: 4 additions & 1 deletion api/views/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
from django.db.models import Q
from django.db.utils import IntegrityError
from django.http import FileResponse
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import serializers, status
from rest_framework.exceptions import PermissionDenied
from rest_framework.filters import OrderingFilter
from rest_framework.filters import OrderingFilter, SearchFilter
from rest_framework.generics import get_object_or_404
from rest_framework.parsers import MultiPartParser
from rest_framework.permissions import IsAdminUser, IsAuthenticated
Expand Down Expand Up @@ -127,6 +128,8 @@ class AssetViewSet(ModelViewSet):
filterset_class = AssetFilter
authentication_classes = (FirebaseTokenAuthentication,)
http_method_names = ["get"]
filter_backends = (SearchFilter, DjangoFilterBackend)
search_fields = ['asset_code', 'serial_number']

def get_queryset(self):
user = self.request.user
Expand Down
Binary file added files/admin_exported_assets.xlsx
Binary file not shown.
1 change: 1 addition & 0 deletions media/[email protected]_file.mp4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file_content
1 change: 1 addition & 0 deletions media/[email protected]_file_3Mqeic5.mp4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file_content