Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mod_test/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ def restart_test(test_id):
:type test_id: int
"""
test = Test.query.filter(Test.id == test_id).first()
if test is None:
g.log.error(f"test with id: {test_id} not found!")
raise TestNotFoundException(f"Test with id {test_id} does not exist")

test_fork = TestFork.query.filter(TestFork.user_id == g.user.id, TestFork.test_id == test_id).first()
if not g.user.is_admin and test_fork is None:
g.log.warning(f"user with id: {g.user.id} tried to access restricted endpoint")
Expand All @@ -441,6 +445,10 @@ def stop_test(test_id):
:type test_id: int
"""
test = Test.query.filter(Test.id == test_id).first()
if test is None:
g.log.error(f"test with id: {test_id} not found!")
raise TestNotFoundException(f"Test with id {test_id} does not exist")

test_fork = TestFork.query.filter(TestFork.user_id == g.user.id, TestFork.test_id == test_id).first()
if not g.user.is_admin and test_fork is None:
g.log.warning(f"user with id: {g.user.id} tried to access restricted endpoint")
Expand Down
44 changes: 44 additions & 0 deletions tests/test_test/test_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,50 @@ def test_stop_fails_on_no_permission(self):
response = c.get('/test/stop_test/1')
self.assert403(response)

def test_restart_nonexistent_test_as_admin(self):
"""Test restart of a nonexistent test as admin returns 404."""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.admin)
with self.app.test_client() as c:
c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
response = c.get('/test/restart_test/99999')
self.assertEqual(response.status_code, 404)
self.assert_template_used('test/test_not_found.html')

def test_restart_nonexistent_test_as_tester(self):
"""Test restart of a nonexistent test as tester returns 404."""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.tester)
with self.app.test_client() as c:
c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
response = c.get('/test/restart_test/99999')
self.assertEqual(response.status_code, 404)
self.assert_template_used('test/test_not_found.html')

def test_stop_nonexistent_test_as_admin(self):
"""Test stop of a nonexistent test as admin returns 404."""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.admin)
with self.app.test_client() as c:
c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
response = c.get('/test/stop_test/99999')
self.assertEqual(response.status_code, 404)
self.assert_template_used('test/test_not_found.html')

def test_stop_nonexistent_test_as_tester(self):
"""Test stop of a nonexistent test as tester returns 404."""
self.create_user_with_role(
self.user.name, self.user.email, self.user.password, Role.tester)
with self.app.test_client() as c:
c.post(
'/account/login', data=self.create_login_form_data(self.user.email, self.user.password))
response = c.get('/test/stop_test/99999')
self.assertEqual(response.status_code, 404)
self.assert_template_used('test/test_not_found.html')

def test_get_json_data_throw_not_found_error(self):
"""Test if get_json_data throws Error 404."""
response = self.app.test_client().get('/test/get_json_data/99999')
Expand Down