-
-
Notifications
You must be signed in to change notification settings - Fork 218
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 Type 'has users' util function check #34820
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d86a557
add tests for util function
AddisonDunn e4c66aa
add util function
AddisonDunn 11f6ff0
add backend validation code for loc type 'has users' check
AddisonDunn 71048bf
add FF
AddisonDunn 0c30734
use count for more efficient ES query
AddisonDunn 96060a6
use view's existing way of telling whether location type is newly cre…
AddisonDunn 372f5c8
specify domain when grabbing loc type
AddisonDunn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from corehq.apps.domain.shortcuts import create_domain | ||
from corehq.apps.commtrack.tests.util import TEST_LOCATION_TYPE, make_loc | ||
from corehq.apps.users.models import CommCareUser, WebUser | ||
from corehq.apps.locations.util import does_location_type_have_users | ||
|
||
from django.test import TestCase | ||
from corehq.apps.users.dbaccessors import delete_all_users | ||
from corehq.apps.es.client import manager | ||
from corehq.apps.es.tests.utils import es_test | ||
from corehq.apps.es.users import user_adapter | ||
from corehq.util.es.testing import sync_users_to_es | ||
from corehq.apps.locations.models import LocationType | ||
|
||
|
||
@es_test(requires=[user_adapter]) | ||
class UtilTest(TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
super(UtilTest, cls).setUpClass() | ||
cls.domain = create_domain('locations-test') | ||
cls.loc = make_loc('loc', type='outlet', domain=cls.domain.name) | ||
cls.loc_type = LocationType.objects.get(name=TEST_LOCATION_TYPE) | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
cls.domain.delete() | ||
super(UtilTest, cls).tearDownClass() | ||
|
||
def tearDown(self): | ||
delete_all_users() | ||
|
||
@sync_users_to_es() | ||
def test_loc_type_has_users(self): | ||
delete_all_users() | ||
user1 = WebUser.create(self.domain.name, '[email protected]', '123', None, None) | ||
user2 = CommCareUser.create(self.domain.name, 'mobile-user', '123', None, None) | ||
user1.set_location(self.domain.name, self.loc) | ||
user2.set_location(self.loc) | ||
manager.index_refresh(user_adapter.index_name) | ||
self.assertTrue(does_location_type_have_users(self.loc_type)) | ||
|
||
def _loc_type_does_not_have_user(self): | ||
self.assertFalse(does_location_type_have_users(self.loc_type)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just in case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, just good practice, since the ID is untrusted input. A malicious actor could make a request with IDs from another domain (and since they're incrementing integer IDs, they're guessable). Wouldn't give them anything useful in this case, but still good to guard against. I've toyed with the idea making a domain filter required by default for all SQL models unless specifically exempted, but haven't really gone anywhere with that yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see, interesting.