Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dchhabda committed Nov 5, 2024
1 parent c96ef38 commit 05a4fe3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
17 changes: 17 additions & 0 deletions pybossa/repositories/task_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,24 @@ def update(self, element):
except IntegrityError as e:
raise DBIntegrityError(e)

def _delete(self, element):
table = element.__class__
inst = self.db.session.query(table).filter(table.id==element.id).first()
self.db.session.delete(inst)

def delete_taskrun(self, element):
self._delete(element)
self.db.session.commit()
cached_projects.clean_project(element.project_id)

def delete(self, element):
# task repo is shared between task and taskun
# call taskrun specific delete for taskrun deletes
self._validate_can_be('deleted', element)
if element.__tablename__ == "task_run":
self.delete_taskrun(element)
return

tstart = time.perf_counter()
self._validate_can_be('deleted', element)
tend = time.perf_counter()
Expand Down
33 changes: 22 additions & 11 deletions test/test_api/test_task_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,20 +950,23 @@ def test_task_delete(self):
root_task = TaskFactory.create(project=project)

## anonymous
res = self.app.delete('/api/task/%s' % task.id)
with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}):
res = self.app.delete('/api/task/%s' % task.id)
error_msg = 'Anonymous should not be allowed to delete'
print(res.status)
assert_equal(res.status, '401 UNAUTHORIZED', error_msg)

### real user but not allowed as not owner!
url = '/api/task/%s?api_key=%s' % (task.id, non_owner.api_key)
res = self.app.delete(url)
with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}):
res = self.app.delete(url)
error_msg = 'Should not be able to update tasks of others'
assert_equal(res.status, '403 FORBIDDEN', error_msg)

#### real user
# DELETE with not allowed args
res = self.app.delete(url + "&foo=bar")
with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}):
res = self.app.delete(url + "&foo=bar")
err = json.loads(res.data)
assert res.status_code == 415, err
assert err['status'] == 'failed', err
Expand All @@ -973,13 +976,15 @@ def test_task_delete(self):

# DELETE returns 204
url = '/api/task/%s?api_key=%s' % (task.id, user.api_key)
res = self.app.delete(url)
with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}):
res = self.app.delete(url)
assert_equal(res.status, '204 NO CONTENT', res.data)
assert res.data == b'', res.data # res.data is bytes type

#### root user
url = '/api/task/%s?api_key=%s' % (root_task.id, admin.api_key)
res = self.app.delete(url)
with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}):
res = self.app.delete(url)
assert_equal(res.status, '204 NO CONTENT', res.data)

tasks = task_repo.filter_tasks_by(project_id=project.id)
Expand All @@ -992,10 +997,12 @@ def test_delete_task_cascade(self):
task = TaskFactory.create()
task_runs = TaskRunFactory.create_batch(3, task=task)
url = '/api/task/%s?api_key=%s' % (task.id, task.project.owner.api_key)
res = self.app.delete(url)
task_id = task.id
with patch.dict(self.flask_app.config, {'SESSION_REPLICATION_ROLE_DISABLED': True}):
res = self.app.delete(url)

assert_equal(res.status, '204 NO CONTENT', res.data)
task_runs = task_repo.filter_task_runs_by(task_id=task.id)
task_runs = task_repo.filter_task_runs_by(task_id=task_id)
assert len(task_runs) == 0, "There should not be any task run for task"

@with_context
Expand Down Expand Up @@ -1034,7 +1041,8 @@ def test_delete_task_when_result_associated_admin(self):

url = '/api/task/%s?api_key=%s' % (result.task_id,
admin.api_key)
res = self.app.delete(url)
with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}):
res = self.app.delete(url)
assert_equal(res.status, '204 NO CONTENT', res.status)

@with_context
Expand All @@ -1051,7 +1059,8 @@ def test_delete_task_when_result_associated_variation(self):

url = '/api/task/%s?api_key=%s' % (result.task_id,
admin.api_key)
res = self.app.delete(url)
with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}):
res = self.app.delete(url)
assert_equal(res.status, '204 NO CONTENT', res.status)

@with_context
Expand All @@ -1068,7 +1077,8 @@ def test_counter_table(self):
assert len(items) == 10

task_id = task.id
task_repo.delete(task)
with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}):
task_repo.delete(task)
items = db.session.query(Counter).filter_by(project_id=project.id).all()
assert len(items) == 9
items = db.session.query(Counter).filter_by(task_id=task_id).all()
Expand Down Expand Up @@ -1103,7 +1113,8 @@ def test_counter_table_api(self):
items = db.session.query(Counter).filter_by(project_id=project.id).all()
assert len(items) == 10, len(items)

res = self.app.delete('/api/task/%s?api_key=%s' % (created_task['id'],
with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}):
res = self.app.delete('/api/task/%s?api_key=%s' % (created_task['id'],
project.owner.api_key))
items = db.session.query(Counter).filter_by(project_id=project.id).all()
assert len(items) == 9
Expand Down
12 changes: 8 additions & 4 deletions test/test_repository/test_task_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,11 @@ def test_delete_task(self):
"""Test delete removes the Task instance"""

task = TaskFactory.create()
task_id = task.id

self.task_repo.delete(task)
deleted = self.task_repo.get_task(task.id)
with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}):
self.task_repo.delete(task)
deleted = self.task_repo.get_task(task_id)

assert deleted is None, deleted

Expand All @@ -734,9 +736,11 @@ def test_delete_task_deletes_dependent_taskruns(self):

task = TaskFactory.create()
taskrun = TaskRunFactory.create(task=task)
taskrun_id = taskrun.id

self.task_repo.delete(task)
deleted = self.task_repo.get_task_run(taskrun.id)
with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}):
self.task_repo.delete(task)
deleted = self.task_repo.get_task_run(taskrun_id)

assert deleted is None, deleted

Expand Down

0 comments on commit 05a4fe3

Please sign in to comment.