From 7d57e87bf036ae83cd7063661dca6cf45114ed04 Mon Sep 17 00:00:00 2001 From: mprahl Date: Oct 21 2019 15:34:00 +0000 Subject: Prevent tests that test authentication from affecting tests after it --- diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..e6127c6 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2019 Red Hat, Inc. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import flask +import pytest + + +@pytest.fixture(autouse=True) +def clear_flask_g(): + """ + Clear the Flask global variables after each test. + + Many of the tests end up modifying flask.g such as for testing or mocking authentication. + If it isn't cleared, it would end up leaking into other tests which don't expect it. + """ + for attr in ('group', 'user'): + if hasattr(flask.g, attr): + delattr(flask.g, attr) diff --git a/tests/test_views.py b/tests/test_views.py index ee04236..88bc5a6 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -435,32 +435,6 @@ class TestViews(helpers.ModelsTestCase): self.assertEqual(data['error'], 'Bad Request') self.assertTrue(data['message'].startswith('Unsupported action requested.')) - def test_patch_event_cancel(self): - event = models.Event.create(db.session, "2017-00000000-0000-0000-0000-000000000003", - "RHSA-2018-103", events.TestingEvent) - models.ArtifactBuild.create(db.session, event, "mksh", "module", build_id=1237, - state=ArtifactBuildState.PLANNED.value) - models.ArtifactBuild.create(db.session, event, "bash", "module", build_id=1238, - state=ArtifactBuildState.PLANNED.value) - models.ArtifactBuild.create(db.session, event, "dash", "module", build_id=1239, - state=ArtifactBuildState.BUILD.value) - models.ArtifactBuild.create(db.session, event, "tcsh", "module", build_id=1240, - state=ArtifactBuildState.DONE.value) - db.session.commit() - - resp = self.client.patch( - '/api/1/events/{}'.format(event.id), - data=json.dumps({'action': 'cancel'})) - data = json.loads(resp.get_data(as_text=True)) - - self.assertEqual(data['id'], event.id) - self.assertEqual(len(data['builds']), 4) - self.assertEqual(data['state_name'], 'CANCELED') - self.assertTrue(data['state_reason'].startswith( - 'Event id {} requested for canceling by user '.format(event.id))) - self.assertEqual(len([b for b in data['builds'] if b['state_name'] == 'CANCELED']), 3) - self.assertEqual(len([b for b in data['builds'] if b['state_name'] == 'DONE']), 1) - def test_query_event_types(self): resp = self.client.get('/api/1/event-types/') event_types = json.loads(resp.get_data(as_text=True))['items'] @@ -605,7 +579,7 @@ class TestViewsMultipleFilterValues(helpers.ModelsTestCase): self.assertEqual(len(evs), 2) -class TestManualTriggerRebuild(helpers.ModelsTestCase): +class TestManualTriggerRebuild(ViewBaseTest): def setUp(self): super(TestManualTriggerRebuild, self).setUp() self.client = app.test_client() @@ -620,9 +594,13 @@ class TestManualTriggerRebuild(helpers.ModelsTestCase): 123, 'name', 'REL_PREP', ['rpm']) with patch('freshmaker.models.datetime') as datetime_patch: datetime_patch.utcnow.return_value = datetime.datetime(2017, 8, 21, 13, 42, 20) - resp = self.client.post('/api/1/builds/', - data=json.dumps({'errata_id': 1}), - content_type='application/json') + + with self.test_request_context(user='root'): + resp = self.client.post( + '/api/1/builds/', + data=json.dumps({'errata_id': 1}), + content_type='application/json', + ) data = json.loads(resp.get_data(as_text=True)) # Other fields are predictible. @@ -641,7 +619,7 @@ class TestManualTriggerRebuild(helpers.ModelsTestCase): u'time_done': None, u'url': u'/api/1/events/1', u'dry_run': False, - u'requester': 'tester1', + u'requester': 'root', u'requested_rebuilds': [], u'requester_metadata': {}}) publish.assert_called_once_with( @@ -657,9 +635,9 @@ class TestManualTriggerRebuild(helpers.ModelsTestCase): from_advisory_id.return_value = ErrataAdvisory( 123, 'name', 'REL_PREP', ['rpm']) - resp = self.client.post('/api/1/builds/', - data=json.dumps({'errata_id': 1, 'dry_run': True}), - content_type='application/json') + payload = {'errata_id': 1, 'dry_run': True} + with self.test_request_context(user='root'): + resp = self.client.post('/api/1/builds/', json=payload, content_type='application/json') data = json.loads(resp.get_data(as_text=True)) # Other fields are predictible. @@ -677,10 +655,12 @@ class TestManualTriggerRebuild(helpers.ModelsTestCase): from_advisory_id.return_value = ErrataAdvisory( 123, 'name', 'REL_PREP', ['rpm']) - resp = self.client.post( - '/api/1/builds/', data=json.dumps({ - 'errata_id': 1, 'container_images': ["foo-1-1", "bar-1-1"]}), - content_type='application/json') + payload = { + 'errata_id': 1, + 'container_images': ['foo-1-1', 'bar-1-1'], + } + with self.test_request_context(user='root'): + resp = self.client.post('/api/1/builds/', json=payload, content_type='application/json') data = json.loads(resp.get_data(as_text=True)) # Other fields are predictible. @@ -699,10 +679,12 @@ class TestManualTriggerRebuild(helpers.ModelsTestCase): from_advisory_id.return_value = ErrataAdvisory( 123, 'name', 'REL_PREP', ['rpm']) - resp = self.client.post( - '/api/1/builds/', data=json.dumps({ - 'errata_id': 1, 'metadata': {"foo": ["bar"]}}), - content_type='application/json') + payload = { + 'errata_id': 1, + 'metadata': {'foo': ['bar']}, + } + with self.test_request_context(user='root'): + resp = self.client.post('/api/1/builds/', json=payload, content_type='application/json') data = json.loads(resp.get_data(as_text=True)) # Other fields are predictible. @@ -727,12 +709,15 @@ class TestManualTriggerRebuild(helpers.ModelsTestCase): from_advisory_id.return_value = ErrataAdvisory( 123, 'name', 'REL_PREP', ['rpm']) - resp = self.client.post( - '/api/1/builds/', data=json.dumps({ - 'errata_id': 1, 'container_images': ["foo-1-1"], - 'freshmaker_event_id': 1}), - content_type='application/json') + payload = { + 'errata_id': 1, + 'container_images': ['foo-1-1'], + 'freshmaker_event_id': 1, + } + with self.test_request_context(user='root'): + resp = self.client.post('/api/1/builds/', json=payload, content_type='application/json') data = json.loads(resp.get_data(as_text=True)) + # Other fields are predictible. self.assertEqual(data['requested_rebuilds'], ["foo-1-1"]) assert add_dependency.call_count == 1 @@ -743,6 +728,33 @@ class TestManualTriggerRebuild(helpers.ModelsTestCase): 'container_images': ["foo-1-1"], 'freshmaker_event_id': 1}) +class TestPatchAPI(ViewBaseTest): + def test_patch_event_cancel(self): + event = models.Event.create(db.session, "2017-00000000-0000-0000-0000-000000000003", + "RHSA-2018-103", events.TestingEvent) + models.ArtifactBuild.create(db.session, event, "mksh", "module", build_id=1237, + state=ArtifactBuildState.PLANNED.value) + models.ArtifactBuild.create(db.session, event, "bash", "module", build_id=1238, + state=ArtifactBuildState.PLANNED.value) + models.ArtifactBuild.create(db.session, event, "dash", "module", build_id=1239, + state=ArtifactBuildState.BUILD.value) + models.ArtifactBuild.create(db.session, event, "tcsh", "module", build_id=1240, + state=ArtifactBuildState.DONE.value) + db.session.commit() + + with self.test_request_context(user='root'): + resp = self.client.patch(f'/api/1/events/{event.id}', json={'action': 'cancel'}) + data = json.loads(resp.get_data(as_text=True)) + + self.assertEqual(data['id'], event.id) + self.assertEqual(len(data['builds']), 4) + self.assertEqual(data['state_name'], 'CANCELED') + self.assertTrue(data['state_reason'].startswith( + 'Event id {} requested for canceling by user '.format(event.id))) + self.assertEqual(len([b for b in data['builds'] if b['state_name'] == 'CANCELED']), 3) + self.assertEqual(len([b for b in data['builds'] if b['state_name'] == 'DONE']), 1) + + class TestOpenIDCLogin(ViewBaseTest): """Test that OpenIDC login"""