Skip to content

Commit f6e14a7

Browse files
committed
Add test for the new action
1 parent 61ed43e commit f6e14a7

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

ckanext/activityinfo/tests/test_download_endpoints.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from ckan.plugins import toolkit
55
from ckantoolkit.tests import factories as ckan_factories
66
from ckanext.activityinfo.tests import factories
7+
from ckanext.activityinfo.jobs.download import download_activityinfo_resource
78

89

910
@pytest.fixture
@@ -189,3 +190,77 @@ def test_act_info_get_job_status_missing_job_id(self, setup_data):
189190
)
190191

191192
assert 'job_id' in str(exc_info.value)
193+
194+
195+
@pytest.mark.usefixtures("clean_db")
196+
class TestUpdateResourceFileAction:
197+
198+
def test_update_resource_file_enqueues_job(self, setup_data):
199+
"""Test that act_info_update_resource_file enqueues a background job"""
200+
user_name = setup_data.activityinfo_user['name']
201+
resource = factories.ActivityInfoResource()
202+
203+
with mock.patch('ckanext.activityinfo.actions.activity_info.toolkit.enqueue_job') as mock_enqueue:
204+
mock_enqueue.return_value = mock.Mock(id='rq_job_123')
205+
206+
result = toolkit.get_action('act_info_update_resource_file')(
207+
context={'user': user_name},
208+
data_dict={'resource_id': resource['id']}
209+
)
210+
211+
assert result['job_id'] == 'rq_job_123'
212+
assert result['resource_id'] == resource['id']
213+
mock_enqueue.assert_called_once_with(
214+
download_activityinfo_resource,
215+
[resource['id'], user_name],
216+
title=f"Download ActivityInfo for resource {resource['id']}",
217+
rq_kwargs={'timeout': 600}
218+
)
219+
220+
def test_update_resource_file_missing_resource_id(self, setup_data):
221+
"""Test that act_info_update_resource_file raises error when resource_id is missing"""
222+
user_name = setup_data.activityinfo_user['name']
223+
224+
with pytest.raises(toolkit.ValidationError) as exc_info:
225+
toolkit.get_action('act_info_update_resource_file')(
226+
context={'user': user_name},
227+
data_dict={}
228+
)
229+
230+
assert 'resource_id' in str(exc_info.value)
231+
232+
def test_update_resource_file_uses_context_user(self, setup_data):
233+
"""Test that the action falls back to context user when no user in data_dict"""
234+
user_name = setup_data.activityinfo_user['name']
235+
resource = factories.ActivityInfoResource()
236+
237+
with mock.patch('ckanext.activityinfo.actions.activity_info.toolkit.enqueue_job') as mock_enqueue:
238+
mock_enqueue.return_value = mock.Mock(id='rq_job_456')
239+
240+
toolkit.get_action('act_info_update_resource_file')(
241+
context={'user': user_name},
242+
data_dict={'resource_id': resource['id']}
243+
)
244+
245+
call_args = mock_enqueue.call_args[0]
246+
assert call_args[1] == [resource['id'], user_name]
247+
248+
def test_update_resource_file_unauthorized_anonymous(self, setup_data):
249+
"""Test that anonymous users cannot update resource files"""
250+
resource = factories.ActivityInfoResource()
251+
252+
with pytest.raises(toolkit.NotAuthorized):
253+
toolkit.get_action('act_info_update_resource_file')(
254+
context={'user': ''},
255+
data_dict={'resource_id': resource['id']}
256+
)
257+
258+
def test_update_resource_file_unauthorized_no_api_key(self, setup_data):
259+
"""Test that users without an ActivityInfo API key cannot update"""
260+
resource = factories.ActivityInfoResource()
261+
262+
with pytest.raises(toolkit.NotAuthorized):
263+
toolkit.get_action('act_info_update_resource_file')(
264+
context={'user': setup_data.regular_user['name']},
265+
data_dict={'resource_id': resource['id']}
266+
)

0 commit comments

Comments
 (0)