forked from wger-project/wger
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(): 404 if a trainer tries to login to a different gym
Rather than just returning "permission denied", we can interpret this scenario as a "failure to look up a user with that gym/user ID combo". This should give users the information they need to self-recover, while not leaking any other sensitive details. Fixes: wger-project#585
- Loading branch information
Showing
7 changed files
with
96 additions
and
11 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,7 @@ node_modules | |
|
||
# Virtual envs | ||
venv | ||
venv-wget | ||
|
||
# Dummy file for translations | ||
/wger/i18n.tpl |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from unittest import mock | ||
|
||
from wger.core.tests.base_testcase import WgerTestCase | ||
from wger.core.views.user import trainer_login | ||
|
||
|
||
def _build_mock_request(user): | ||
request = mock.Mock() | ||
request.session = dict() | ||
request.GET = dict() | ||
request.user = user | ||
return request | ||
|
||
|
||
def _build_mock_user(gym_name, is_trainer=False): | ||
user = mock.Mock() | ||
user.userprofile.gym = gym_name | ||
|
||
def request_has_perm(perm): | ||
if perm in ['gym.gym_trainer', 'gym.manage_gym', 'gym.manage_gyms']: | ||
return is_trainer | ||
return True # Don't care about other permissions for these tests | ||
|
||
user.has_perm.side_effect = request_has_perm | ||
return user | ||
|
||
|
||
class TrainerLoginTestCase(WgerTestCase): | ||
|
||
mock_django_login = None | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
cls.mock_django_login = mock.patch('wger.core.views.user.django_login').start() | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
cls.mock_django_login.stop() | ||
|
||
def test_trainer_is_allowed_to_login_to_non_trainer_in_same_gym(self): | ||
request_user = _build_mock_user('same-gym', is_trainer=True) | ||
request = _build_mock_request(request_user) | ||
user_from_db_lookup = _build_mock_user('same-gym', is_trainer=False) | ||
|
||
with mock.patch('wger.core.views.user.get_object_or_404', return_value=user_from_db_lookup): | ||
resp = trainer_login(request, 'primary-key-not-needed-because-get-object-is-mocked') | ||
|
||
self.assertEqual(302, resp.status_code) | ||
|
||
def test_trainer_is_denied_from_login_to_trainer_in_same_gym(self): | ||
request_user = _build_mock_user('same-gym', is_trainer=True) | ||
request = _build_mock_request(request_user) | ||
user_from_db_lookup = _build_mock_user('same-gym', is_trainer=True) | ||
|
||
with mock.patch('wger.core.views.user.get_object_or_404', return_value=user_from_db_lookup): | ||
resp = trainer_login(request, 'primary-key-not-needed-because-of-mock') | ||
|
||
self.assertEqual(403, resp.status_code) | ||
|
||
def test_trainer_is_denied_from_login_to_trainer_at_different_gym(self): | ||
request_user = _build_mock_user('trainer-gym', is_trainer=True) | ||
request = _build_mock_request(request_user) | ||
user_from_db_lookup = _build_mock_user('other-trainer-gym', is_trainer=True) | ||
|
||
with mock.patch('wger.core.views.user.get_object_or_404', return_value=user_from_db_lookup): | ||
resp = trainer_login(request, 'primary-key-not-needed-because-of-mock') | ||
|
||
self.assertEqual(403, resp.status_code) | ||
|
||
def test_trainer_gets_404_when_trying_to_login_to_non_trainer_in_different_gym(self): | ||
request_user = _build_mock_user('trainer-gym', is_trainer=True) | ||
request = _build_mock_request(request_user) | ||
user_from_db_lookup = _build_mock_user('user-gym', is_trainer=False) | ||
|
||
with mock.patch('wger.core.views.user.get_object_or_404', return_value=user_from_db_lookup): | ||
resp = trainer_login(request, 'primary-key-not-needed-because-of-mock') | ||
|
||
self.assertEqual(404, resp.status_code) |
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