From 4ced84e84a13ed9b3fcd23af87c88e3d271d9314 Mon Sep 17 00:00:00 2001 From: Willem Date: Sun, 3 Nov 2024 22:11:59 +0100 Subject: [PATCH] Fix tests (#920) * fix: fix tests for mod_sample * fix: fix regression tests tests * fix: fix test tests tests --- tests/test_regression/test_controllers.py | 17 +++++++++--- tests/test_sample/test_controllers.py | 34 +++++++++++++++++------ tests/test_test/test_controllers.py | 14 ++++++++-- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/tests/test_regression/test_controllers.py b/tests/test_regression/test_controllers.py index 4269e54b..83eb4039 100644 --- a/tests/test_regression/test_controllers.py +++ b/tests/test_regression/test_controllers.py @@ -12,6 +12,7 @@ from mod_sample.models import Sample from mod_test.models import Test, TestResultFile from tests.base import BaseTestCase +from tests.test_auth.test_controllers import MockUser class TestControllers(BaseTestCase): @@ -48,10 +49,12 @@ def test_regression_test_status_toggle(self): self.assertEqual('True', response.json['active']) @mock.patch('mod_regression.controllers.RegressionTestOutput') - def test_download_result_file_not_found(self, mock_regression_output): + @mock.patch('mod_auth.controllers.g') + def test_download_result_file_not_found(self, mock_g, mock_regression_output): """Test that non-existent result file gives 404.""" from mod_regression.controllers import test_result_file mock_regression_output.query.filter.return_value.first.return_value = None + mock_g.user = MockUser(id=1, role="None") with self.assertRaises(NotFound): test_result_file(1) @@ -59,10 +62,12 @@ def test_download_result_file_not_found(self, mock_regression_output): mock_regression_output.query.filter.assert_called_once_with(mock_regression_output.id == 1) @mock.patch('mod_regression.controllers.RegressionTestOutputFiles') - def test_download_result_file_not_found_variant(self, mock_regression_output_file): + @mock.patch('mod_auth.controllers.g') + def test_download_result_file_not_found_variant(self, mock_g, mock_regression_output_file): """Test that non-existent result file gives 404.""" from mod_regression.controllers import multiple_test_result_file mock_regression_output_file.query.filter.return_value.first.return_value = None + mock_g.user = MockUser(id=1, role="None") with self.assertRaises(NotFound): multiple_test_result_file(1) @@ -71,10 +76,12 @@ def test_download_result_file_not_found_variant(self, mock_regression_output_fil @mock.patch('mod_regression.controllers.serve_file_download') @mock.patch('mod_regression.controllers.RegressionTestOutput') - def test_download_result_file(self, mock_regression_output, mock_serve): + @mock.patch('mod_auth.controllers.g') + def test_download_result_file(self, mock_g, mock_regression_output, mock_serve): """Test that correct result file triggers serve download.""" from mod_regression.controllers import test_result_file + mock_g.user = MockUser(id=1, role="None") response = test_result_file(1) mock_regression_output.query.filter.assert_called_once_with(mock_regression_output.id == 1) @@ -82,10 +89,12 @@ def test_download_result_file(self, mock_regression_output, mock_serve): @mock.patch('mod_regression.controllers.serve_file_download') @mock.patch('mod_regression.controllers.RegressionTestOutputFiles') - def test_download_result_file_variant(self, mock_regression_output_file, mock_serve): + @mock.patch('mod_auth.controllers.g') + def test_download_result_file_variant(self, mock_g, mock_regression_output_file, mock_serve): """Test that correct result file triggers serve download for variants.""" from mod_regression.controllers import multiple_test_result_file + mock_g.user = MockUser(id=1, role="None") response = multiple_test_result_file(1) mock_regression_output_file.query.filter.assert_called_once_with(mock_regression_output_file.id == 1) diff --git a/tests/test_sample/test_controllers.py b/tests/test_sample/test_controllers.py index 1699958d..70e86d29 100644 --- a/tests/test_sample/test_controllers.py +++ b/tests/test_sample/test_controllers.py @@ -8,6 +8,7 @@ from mod_sample.media_info_parser import InvalidMediaInfoError from mod_sample.models import Sample, Tag from tests.base import BaseTestCase +from tests.test_auth.test_controllers import MockUser def raise_media_exception(): @@ -75,10 +76,12 @@ def test_sample_id(self): @mock.patch('mod_sample.controllers.serve_file_download') @mock.patch('mod_sample.controllers.Sample') - def test_download_sample(self, mock_sample, mock_serve_download): + @mock.patch('mod_auth.controllers.g') + def test_download_sample(self, mock_g, mock_sample, mock_serve_download): """Test function download_sample.""" from mod_sample.controllers import download_sample + mock_g.user = MockUser(id=1, role="None") response = download_sample(1) self.assertEqual(response, mock_serve_download()) @@ -86,12 +89,14 @@ def test_download_sample(self, mock_sample, mock_serve_download): @mock.patch('mod_sample.controllers.serve_file_download') @mock.patch('mod_sample.controllers.Sample') - def test_download_sample_raise_exception(self, mock_sample, mock_serve_download): + @mock.patch('mod_auth.controllers.g') + def test_download_sample_raise_exception(self, mock_g, mock_sample, mock_serve_download): """Test function download_sample to raise SampleNotFoundException.""" from mod_sample.controllers import (SampleNotFoundException, download_sample) mock_sample.query.filter.return_value.first.return_value = None + mock_g.user = MockUser(id=1, role="None") with self.assertRaises(SampleNotFoundException): download_sample(1) @@ -102,10 +107,12 @@ def test_download_sample_raise_exception(self, mock_sample, mock_serve_download) @mock.patch('mod_sample.controllers.serve_file_download') @mock.patch('mod_sample.controllers.Sample') @mock.patch('mod_sample.controllers.os') - def test_download_sample_media_info(self, mock_os, mock_sample, mock_serve_download): + @mock.patch('mod_auth.controllers.g') + def test_download_sample_media_info(self, mock_g, mock_os, mock_sample, mock_serve_download): """Test function download_sample_media_info.""" from mod_sample.controllers import download_sample_media_info + mock_g.user = MockUser(id=1, role="None") response = download_sample_media_info(1) self.assertEqual(response, mock_serve_download()) @@ -115,12 +122,14 @@ def test_download_sample_media_info(self, mock_os, mock_sample, mock_serve_downl @mock.patch('mod_sample.controllers.serve_file_download') @mock.patch('mod_sample.controllers.Sample') @mock.patch('mod_sample.controllers.os') - def test_download_sample_media_info_path_wrong(self, mock_os, mock_sample, mock_serve_download): + @mock.patch('mod_auth.controllers.g') + def test_download_sample_media_info_path_wrong(self, mock_g, mock_os, mock_sample, mock_serve_download): """Test function download_sample_media_info with wrong path for media info.""" from mod_sample.controllers import (SampleNotFoundException, download_sample_media_info) mock_os.path.isfile.return_value = False + mock_g.user = MockUser(id=1, role="None") with self.assertRaises(SampleNotFoundException): download_sample_media_info(1) @@ -131,12 +140,14 @@ def test_download_sample_media_info_path_wrong(self, mock_os, mock_sample, mock_ @mock.patch('mod_sample.controllers.serve_file_download') @mock.patch('mod_sample.controllers.Sample') @mock.patch('mod_sample.controllers.os') - def test_download_sample_media_info_sample_not_found(self, mock_os, mock_sample, mock_serve_download): + @mock.patch('mod_auth.controllers.g') + def test_download_sample_media_info_sample_not_found(self, mock_g, mock_os, mock_sample, mock_serve_download): """Test function download_sample_media_info to raise SampleNotFoundException.""" from mod_sample.controllers import (SampleNotFoundException, download_sample_media_info) mock_sample.query.filter.return_value.first.return_value = None + mock_g.user = MockUser(id=1, role="None") with self.assertRaises(SampleNotFoundException): download_sample_media_info(1) @@ -147,10 +158,13 @@ def test_download_sample_media_info_sample_not_found(self, mock_os, mock_sample, @mock.patch('mod_sample.controllers.serve_file_download') @mock.patch('mod_sample.controllers.Sample') @mock.patch('mod_sample.controllers.ExtraFile') - def test_download_sample_additional(self, mock_extra, mock_sample, mock_serve_download): + @mock.patch('mod_auth.controllers.g') + def test_download_sample_additional(self, mock_g, mock_extra, mock_sample, mock_serve_download): """Test function download_sample_additional.""" from mod_sample.controllers import download_sample_additional + mock_g.user = MockUser(id=1, role="None") + response = download_sample_additional(1, 1) self.assertEqual(response, mock_serve_download()) @@ -160,12 +174,14 @@ def test_download_sample_additional(self, mock_extra, mock_sample, mock_serve_do @mock.patch('mod_sample.controllers.serve_file_download') @mock.patch('mod_sample.controllers.Sample') @mock.patch('mod_sample.controllers.ExtraFile') - def test_download_sample_additional_sample_not_found(self, mock_extra, mock_sample, mock_serve_download): + @mock.patch('mod_auth.controllers.g') + def test_download_sample_additional_sample_not_found(self, mock_g, mock_extra, mock_sample, mock_serve_download): """Test function download_sample_additional to raise SampleNotFoundException.""" from mod_sample.controllers import (SampleNotFoundException, download_sample_additional) mock_sample.query.filter.return_value.first.return_value = None + mock_g.user = MockUser(id=1, role="None") with self.assertRaises(SampleNotFoundException): download_sample_additional(1, 1) @@ -176,12 +192,14 @@ def test_download_sample_additional_sample_not_found(self, mock_extra, mock_samp @mock.patch('mod_sample.controllers.serve_file_download') @mock.patch('mod_sample.controllers.Sample') @mock.patch('mod_sample.controllers.ExtraFile') - def test_download_sample_additional_extrafile_not_found(self, mock_extra, mock_sample, mock_serve_download): + @mock.patch('mod_auth.controllers.g') + def test_download_sample_additional_extrafile_not_found(self, mock_g, mock_extra, mock_sample, mock_serve_download): """Test function download_sample_additional to raise SampleNotFoundException when extra file not found.""" from mod_sample.controllers import (SampleNotFoundException, download_sample_additional) mock_extra.query.filter.return_value.first.return_value = None + mock_g.user = MockUser(id=1, role="None") with self.assertRaises(SampleNotFoundException): download_sample_additional(1, 1) diff --git a/tests/test_test/test_controllers.py b/tests/test_test/test_controllers.py index 2666820d..cbf0fc9c 100644 --- a/tests/test_test/test_controllers.py +++ b/tests/test_test/test_controllers.py @@ -7,6 +7,7 @@ from mod_test.models import (Test, TestPlatform, TestProgress, TestResult, TestResultFile, TestStatus) from tests.base import BaseTestCase +from tests.test_auth.test_controllers import MockUser class TestControllers(BaseTestCase): @@ -209,12 +210,14 @@ def test_generate_diff_download(self, mock_response, mock_request, mock_test_res self.assertTrue(response, mock_response()) @mock.patch('mod_test.controllers.Test') - def test_download_build_log_file_test_not_found(self, mock_test): + @mock.patch('mod_auth.controllers.g') + def test_download_build_log_file_test_not_found(self, mock_g, mock_test): """Try to download build log for invalid test.""" from mod_test.controllers import (TestNotFoundException, download_build_log_file) mock_test.query.filter.return_value.first.return_value = None + mock_g.user = MockUser(id=1, role="None") with self.assertRaises(TestNotFoundException): download_build_log_file(1) @@ -223,12 +226,14 @@ def test_download_build_log_file_test_not_found(self, mock_test): @mock.patch('mod_test.controllers.os') @mock.patch('mod_test.controllers.Test') - def test_download_build_log_file_log_not_file(self, mock_test, mock_os): + @mock.patch('mod_auth.controllers.g') + def test_download_build_log_file_log_not_file(self, mock_g, mock_test, mock_os): """Try to download build log for invalid file path.""" from mod_test.controllers import (TestNotFoundException, download_build_log_file) mock_os.path.isfile.side_effect = TestNotFoundException('msg') + mock_g.user = MockUser(id=1, role="None") with self.assertRaises(TestNotFoundException): download_build_log_file('1') @@ -239,11 +244,14 @@ def test_download_build_log_file_log_not_file(self, mock_test, mock_os): @mock.patch('mod_test.controllers.os') @mock.patch('mod_test.controllers.Test') @mock.patch('mod_test.controllers.serve_file_download') - def test_download_build_log_file(self, mock_serve, mock_test, mock_os): + @mock.patch('mod_auth.controllers.g') + def test_download_build_log_file(self, mock_g, mock_serve, mock_test, mock_os): """Try to download build log.""" from mod_test.controllers import (TestNotFoundException, download_build_log_file) + mock_g.user = MockUser(id=1, role="None") + response = download_build_log_file('1') self.assertEqual(response, mock_serve())