Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankApiyo committed Jul 26, 2024
1 parent 91d521f commit a098af0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions onadata/apps/api/tests/viewsets/test_abstract_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ def _org_create(self, org_data=None):
data["url"] = f"http://testserver/api/v1/orgs/{data['org']}"
data["user"] = f"http://testserver/api/v1/users/{data['org']}"
data["creator"] = "http://testserver/api/v1/users/bob"
data.pop("email")
self.assertDictContainsSubset(data, response.data)
# pylint: disable=attribute-defined-outside-init
self.company_data = response.data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from builtins import str as text
from unittest.mock import patch

from django.contrib.auth.models import User, timezone
from django.contrib.auth.models import User, timezone, AnonymousUser
from django.core.cache import cache
from django.test.utils import override_settings

Expand Down Expand Up @@ -89,6 +89,7 @@ def test_orgs_list(self):
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertEqual(response.status_code, 200)
del self.company_data["metadata"]
del self.company_data["email"]
self.assertEqual(response.data, [self.company_data])

# inactive organization
Expand All @@ -113,6 +114,7 @@ def test_orgs_list_for_authenticated_user(self):
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertEqual(response.status_code, 200)
del self.company_data["metadata"]
del self.company_data["email"]
self.assertEqual(response.data, [self.company_data])

def test_orgs_list_shared_with_user(self):
Expand Down Expand Up @@ -194,6 +196,8 @@ def test_orgs_get_not_creator(self):
response = view(request, user="denoinc")
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertEqual(response.status_code, 200)
del self.company_data['email']
del self.company_data['metadata']
self.assertEqual(response.data, self.company_data)
self.assertIn("users", list(response.data))
for user in response.data["users"]:
Expand All @@ -207,6 +211,8 @@ def test_orgs_get_anon(self):
response = view(request, user="denoinc")
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertEqual(response.status_code, 200)
del self.company_data["email"]
del self.company_data['metadata']
self.assertEqual(response.data, self.company_data)
self.assertIn("users", list(response.data))
for user in response.data["users"]:
Expand Down Expand Up @@ -237,6 +243,29 @@ def test_orgs_create_without_name(self):
response = self.view(request)
self.assertEqual(response.data, {"name": ["This field is required."]})

def test_org_create_and_fetch_by_admin_user(self):
org_email = "[email protected]"
data = {
"name": "denoinc",
"org": "denoinc",
"city": "Denoville",
"country": "US",
"home_page": "deno.com",
"twitter": "denoinc",
"email": org_email,
"description": "",
"address": "",
"phonenumber": "",
"require_auth": False,
}
request = self.factory.post(
"/", data=json.dumps(data), content_type="application/json"
)
request.user = self.user
response = self.view(request)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.data['email'], org_email)

def test_org_create_with_anonymous_user(self):
data = {
"name": "denoinc",
Expand Down Expand Up @@ -391,6 +420,7 @@ def test_member_sees_orgs_added_to(self):
}
)
del expected_data["metadata"]
del expected_data['email']

request = self.factory.get("/", **self.extra)
response = view(request)
Expand Down Expand Up @@ -422,6 +452,8 @@ def test_role_for_org_non_owner(self):
request = self.factory.get("/", **self.extra)
response = view(request, user="denoinc")
self.assertEqual(response.status_code, 200)
self.assertTrue('email' in response.data)
self.assertEqual(response.data['email'], '[email protected]')
self.assertIn("users", list(response.data))

for user in response.data["users"]:
Expand All @@ -434,6 +466,21 @@ def test_role_for_org_non_owner(self):
)
self.assertEqual(role, expected_role)

# getting profile as a member
request = self.factory.get("/", **self.extra)
request.user = User.objects.get(username="aboy")
response = view(request, user="denoinc")
self.assertEqual(response.status_code, 200)

# get profile as a anonymous user
request = self.factory.get("/", **self.extra)
request.user = AnonymousUser()
request.headers = None
request.META['HTTP_AUTHORIZATION'] = ""
response = view(request, user="denoinc")
self.assertEqual(response.status_code, 200)
self.assertFalse('email' in response.data)

def test_add_members_to_org_with_anonymous_user(self):
self._org_create()
view = OrganizationProfileViewSet.as_view({"post": "members"})
Expand Down
8 changes: 5 additions & 3 deletions onadata/apps/api/viewsets/organization_profile_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ class OrganizationProfileViewSet(
def retrieve(self, request, *args, **kwargs):
"""Get organization from cache or db"""
username = kwargs.get("user")
cached_org = cache.get(f"{ORG_PROFILE_CACHE}{username}{request.user.username}")
cache_key = f"{ORG_PROFILE_CACHE}{username}{request.user.username}"
cached_org = cache.get(cache_key)
if cached_org:
return Response(cached_org)
response = super().retrieve(request, *args, **kwargs)
cache.set(f"{ORG_PROFILE_CACHE}{username}{request.user.username}", response.data)
cache.set(cache_key, response.data)
return response

def create(self, request, *args, **kwargs):
Expand All @@ -91,7 +92,8 @@ def update(self, request, *args, **kwargs):
"""Update org in cache and db"""
username = kwargs.get("user")
response = super().update(request, *args, **kwargs)
cache.set(f"{ORG_PROFILE_CACHE}{username}{request.user.username}", response.data)
cache_key = f"{ORG_PROFILE_CACHE}{username}{request.user.username}"
cache.set(cache_key, response.data)
return response

@action(methods=["DELETE", "GET", "POST", "PUT"], detail=True)
Expand Down

0 comments on commit a098af0

Please sign in to comment.