From 611b87ad94feaca192c09b18c05b03a4b92720d4 Mon Sep 17 00:00:00 2001 From: Sean Hammond Date: Tue, 20 Nov 2012 14:50:59 +0100 Subject: [PATCH] Fix a crash in dashboard_activity_list auth Fix the dashboard_activity_list auth function to not crash when no user is logged in, and add some tests for this case. --- ckan/logic/auth/get.py | 2 +- ckan/tests/functional/api/test_dashboard.py | 50 ++++++++++++--------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/ckan/logic/auth/get.py b/ckan/logic/auth/get.py index cc3c20f88fe..51174f89b26 100644 --- a/ckan/logic/auth/get.py +++ b/ckan/logic/auth/get.py @@ -192,7 +192,7 @@ def get_site_user(context, data_dict): def dashboard_activity_list(context, data_dict): - if 'user' in context: + if context.get('user'): return {'success': True} else: return {'success': False, diff --git a/ckan/tests/functional/api/test_dashboard.py b/ckan/tests/functional/api/test_dashboard.py index 087acfb9c48..1d7c291c200 100644 --- a/ckan/tests/functional/api/test_dashboard.py +++ b/ckan/tests/functional/api/test_dashboard.py @@ -46,27 +46,32 @@ def setup_class(cls): def teardown_class(cls): ckan.model.repo.rebuild_db() + def post(self, action, params=None, apikey=None, status=200): + '''Post to the CKAN API and return the result.''' + if params is None: + params = {} + params = json.dumps(params) + response = self.app.post('/api/action/{0}'.format(action), + params=params, + extra_environ={'Authorization': str(apikey)}, + status=status) + if status in (200,): + assert response.json['success'] is True + return response.json['result'] + else: + assert response.json['success'] is False + return response.json['error'] + def dashboard_new_activities_count(self, user): '''Return the given user's new activities count from the CKAN API.''' - params = json.dumps({}) - response = self.app.post('/api/action/dashboard_new_activities_count', - params=params, - extra_environ={'Authorization': str(user['apikey'])}) - assert response.json['success'] is True - new_activities_count = response.json['result'] - return new_activities_count + return self.post('dashboard_new_activities_count', + apikey=user['apikey']) def dashboard_activity_list(self, user): '''Return the given user's dashboard activity list from the CKAN API. ''' - params = json.dumps({}) - response = self.app.post('/api/action/dashboard_activity_list', - params=params, - extra_environ={'Authorization': str(user['apikey'])}) - assert response.json['success'] is True - activity_list = response.json['result'] - return activity_list + return self.post('dashboard_activity_list', apikey=user['apikey']) def dashboard_new_activities(self, user): '''Return the activities from the user's dashboard activity stream @@ -75,12 +80,17 @@ def dashboard_new_activities(self, user): return [activity for activity in activity_list if activity['is_new']] def dashboard_mark_all_new_activities_as_old(self, user): - params = json.dumps({}) - response = self.app.post( - '/api/action/dashboard_mark_all_new_activities_as_old', - params=params, - extra_environ={'Authorization': str(user['apikey'])}) - assert response.json['success'] is True + self.post('dashboard_mark_all_new_activities_as_old', + apikey=user['apikey']) + + def test_00_dashboard_activity_list_not_logged_in(self): + self.post('dashboard_activity_list', status=403) + + def test_00_dashboard_new_activities_count_not_logged_in(self): + self.post('dashboard_new_activities_count', status=403) + + def test_00_dashboard_mark_new_activities_not_logged_in(self): + self.post('dashboard_mark_all_new_activities_as_old', status=403) def test_01_new_activities_count_for_new_user(self): '''Test that a newly registered user's new activities count is 0.'''