Skip to content

Commit

Permalink
Merge pull request #1733 from Scifabric/issue-1732
Browse files Browse the repository at this point in the history
Issue 1732
  • Loading branch information
teleyinex committed Nov 6, 2017
2 parents 2f92a12 + 9072a44 commit 02563b5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pybossa/api/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from pybossa.model import DomainObject, announcement
from pybossa.model.task import Task
from pybossa.cache.projects import clean_project
from pybossa.cache.users import delete_user_summary_id

repos = {'Task': {'repo': task_repo, 'filter': 'filter_tasks_by',
'get': 'get_task', 'save': 'save', 'update': 'update',
Expand Down Expand Up @@ -74,7 +75,8 @@
'get': 'get', 'update': 'update',
'save': 'save', 'delete': 'delete'}}

caching = {'Project': {'refresh': clean_project}}
caching = {'Project': {'refresh': clean_project},
'User': {'refresh': delete_user_summary_id}}

error = ErrorStatus()

Expand Down
4 changes: 4 additions & 0 deletions pybossa/cache/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ def get_users_page(page, per_page=24):
accounts.append(tmp)
return accounts

def delete_user_summary_id(oid):
"""Delete from cache the user summary."""
user = db.session.query(User).get(oid)
delete_memoized(get_user_summary, user.name)

def delete_user_summary(name):
"""Delete from cache the user summary."""
Expand Down
1 change: 1 addition & 0 deletions pybossa/view/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ def update_profile(name):
else:
return abort(415)
if succeed:
cached_users.delete_user_summary(user.name)
return redirect_content_type(url_for('.update_profile',
name=user.name),
status=SUCCESS)
Expand Down
16 changes: 14 additions & 2 deletions test/test_api/test_user_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from pybossa.api.user import UserAPI
from test_api import TestAPI
from pybossa.core import db
from mock import patch, MagicMock

from factories import UserFactory

Expand Down Expand Up @@ -119,9 +120,12 @@ def test_user_not_allowed_actions_anon(self):
assert res.status_code == 405, res.status_code

@with_context
def test_user_not_allowed_actions_user(self):
@patch('pybossa.api.api_base.caching')
def test_user_not_allowed_actions_user(self, caching_mock):
"""Test POST, PUT and DELETE for USER actions are not allowed for user
in the API"""
clean_user_mock = MagicMock()
caching_mock.get.return_value = dict(refresh=clean_user_mock)
admin = UserFactory.create()
auth = UserFactory.create()
user = UserFactory.create()
Expand All @@ -148,11 +152,17 @@ def test_user_not_allowed_actions_user(self):
data = json.loads(res.data)
assert res.status_code == 200, res.data
assert data['name'] == 'new', data
clean_user_mock.assert_called_with(data['id'])

@with_context
def test_user_not_allowed_actions_admin (self):
@patch('pybossa.api.api_base.caching')
def test_user_not_allowed_actions_admin(self, caching_mock):
"""Test POST, PUT and DELETE for ADMIN actions are not allowed for user
in the API"""

clean_user_mock = MagicMock()
caching_mock.get.return_value = dict(refresh=clean_user_mock)

admin = UserFactory.create()
auth = UserFactory.create()
user = UserFactory.create()
Expand All @@ -173,6 +183,7 @@ def test_user_not_allowed_actions_admin (self):
assert res.status_code == 200, res.data
assert data['name'] == 'new', data
assert data['info']['foo'] == 'bar', data
clean_user_mock.assert_called_with(data['id'])

res = self.app.delete(url + '?apikey=%s' % auth.api_key)
assert res.status_code == 405, res.status_code
Expand All @@ -184,6 +195,7 @@ def test_user_not_allowed_actions_admin (self):
data = json.loads(res.data)
assert res.status_code == 200, res.data
assert data['name'] == 'newadmin', data
clean_user_mock.assert_called_with(data['id'])


@with_context
Expand Down

0 comments on commit 02563b5

Please sign in to comment.