Skip to content
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

Location select widget - filter by whether the location "has users" #34912

Merged
merged 10 commits into from
Aug 2, 2024
51 changes: 50 additions & 1 deletion corehq/apps/locations/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
from corehq.apps.es.users import user_adapter
from corehq.apps.locations.exceptions import LocationConsistencyError
from corehq.apps.locations.models import LocationType
from corehq.apps.locations.tests.util import make_loc
from corehq.apps.locations.views import LocationTypesView
from corehq.apps.users.models import WebUser
from corehq.util.test_utils import flag_enabled
from corehq.apps.users.dbaccessors import delete_all_users
from corehq.apps.users.models import WebUser


OTHER_DETAILS = {
'expand_from': None,
Expand Down Expand Up @@ -152,3 +155,49 @@ def test_invalid_remove_has_users(self, _):
data = {'loc_types': [loc_type1, loc_type2]}
with self.assertRaises(LocationConsistencyError):
self.send_request(data)


class LocationSearchViewTest(TestCase):
@classmethod
def setUpClass(cls):
super(LocationSearchViewTest, cls).setUpClass()
delete_all_users()
cls.domain = "test-domain"
cls.project = create_domain(cls.domain)
cls.username = "request-er"
cls.password = "foobar"
cls.web_user = WebUser.create(cls.domain, cls.username, cls.password, None, None)
cls.web_user.add_domain_membership(cls.domain, is_admin=True)
cls.web_user.set_role(cls.domain, "admin")
cls.web_user.save()
cls.loc_type1 = LocationType(domain=cls.domain, name='type1', code='code1')
cls.loc_type1.save()
cls.loc1 = make_loc(
'loc_1', type=cls.loc_type1, domain=cls.domain
)
cls.loc2 = make_loc(
'loc_2', type=cls.loc_type1, domain=cls.domain
)

def setUp(self):
self.client.login(username=self.username, password=self.password)

@classmethod
def tearDownClass(cls):
cls.project.delete()
delete_all_users()
return super().tearDownClass()

@mock.patch('django_prbac.decorators.has_privilege', return_value=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW there's also a @privilege_enabled decorator you can use for this to target a specific privilege

def send_request(self, url, data, _):
return self.client.get(url, {'json': json.dumps(data)})

def test_search_view_basic(self):
url = reverse('location_search', args=[self.domain])
data = {'q': 'loc'}
response = self.send_request(url, data)
self.assertEqual(response.status_code, 200)
results = json.loads(response.content)['results']
self.assertEqual(results[0]['id'], self.loc1.location_id)
self.assertEqual(results[1]['id'], self.loc2.location_id)