diff --git a/OpenBench/tests_pipeline_views.py b/OpenBench/tests_pipeline_views.py new file mode 100644 index 0000000..6c951fd --- /dev/null +++ b/OpenBench/tests_pipeline_views.py @@ -0,0 +1,495 @@ +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# # +# OpenBench is a chess engine testing framework authored by Andrew Grant. # +# # +# # +# OpenBench is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# OpenBench is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + + +# Run with: python3 manage.py test OpenBench +# +# The Pipeline pages, with a real ( test ) database and the real URL routing. As +# in tests_pipelines.py, only the call out to Github is replaced. + +import re + +from django.contrib.auth.models import User +from django.test import Client + +import OpenBench.pipelines as pipelines +import OpenBench.tests_pipelines as base + +from OpenBench.models import LogEvent, Pipeline, PipelineRun, Profile, StepRun, Test + +def make_user(name, enabled=True, approver=False): + user = User.objects.create_user(name, '%s@example.com' % (name), 'pw') + Profile.objects.create(user=user, enabled=enabled, approver=approver) + return user + +class PagesTestCase(base.PipelineTestCase): + + def setUp(self): + + super().setUp() + + self.profile.approver = False # Alice is the author, and nothing more than that + self.profile.save() + + self.bob = make_user('bob') # Somebody else + self.carol = make_user('carol', approver=True) # An approver + self.dave = make_user('dave', enabled=False) # A disabled account + + def client_for(self, user=None, **kwargs): + client = Client(**kwargs) + if user is not None: client.force_login(user) + return client + + def events(self): + return list(LogEvent.objects.order_by('id').values_list('author', 'summary')) + + def reload(self, model, pk): + return model.objects.get(id=pk) + +class PermissionTests(PagesTestCase): + + """ Who may change what. Every row of every table here is one person on one route """ + + ACTIONS = ('start', 'toggle', 'delete') + + def effect(self, action, pipeline): + + """ Whether the action visibly did its job """ + + pipeline = self.reload(Pipeline, pipeline.id) + return { + 'start' : PipelineRun.objects.filter(pipeline=pipeline).exists(), + 'toggle' : not pipeline.enabled, + 'delete' : pipeline.deleted, + }[action] + + def test_who_may_do_what_to_a_pipeline(self): + + allowed = { 'alice' : True, 'carol' : True, 'bob' : False, 'dave' : False, None : False } + + for action in self.ACTIONS: + for name, may in allowed.items(): + + pipeline = self.pipeline() + user = User.objects.get(username=name) if name else None + response = self.client_for(user).post('/pipelines/%d/%s/' % (pipeline.id, action)) + label = '%s tries to %s' % (name, action) + + self.assertEqual(response.status_code, 302, label) + self.assertEqual(self.effect(action, pipeline), may, label) + + def test_where_people_who_may_not_are_sent(self): + + pipeline = self.pipeline() + + self.assertEqual(self.client_for().post('/pipelines/%d/start/' % pipeline.id)['Location'], '/login/') + self.assertEqual(self.client_for(self.dave).post('/pipelines/%d/start/' % pipeline.id)['Location'], '/index/') + self.assertEqual(self.client_for(self.bob).post('/pipelines/%d/start/' % pipeline.id)['Location'], '/pipelines/') + + def test_who_may_cancel_a_run(self): + + for name, may in { 'alice' : True, 'carol' : True, 'bob' : False, 'dave' : False, None : False }.items(): + + run = self.start() + user = User.objects.get(username=name) if name else None + self.client_for(user).post('/pipelines/run/%d/cancel/' % run.id) + + self.assertEqual(self.status(run) == 'CANCELLED', may, name) + + def test_a_refusal_is_written_down_and_an_anonymous_one_is_not(self): + + pipeline = self.pipeline() + self.client_for(self.bob).post('/pipelines/%d/delete/' % pipeline.id) + self.client_for().post('/pipelines/%d/delete/' % pipeline.id) + + self.assertEqual(self.events(), [('bob', 'PIPELINE #%d DELETE REFUSED' % pipeline.id)]) + + def test_a_change_that_goes_through_is_written_down_under_who_made_it(self): + + pipeline = self.pipeline() + self.client_for(self.carol).post('/pipelines/%d/toggle/' % pipeline.id) + self.assertIn(('carol', 'PIPELINE #%d PAUSED' % pipeline.id), self.events()) + + def test_an_approver_starts_it_in_the_name_of_the_author(self): + + pipeline = self.pipeline() + self.client_for(self.carol).post('/pipelines/%d/start/' % pipeline.id) + + run = PipelineRun.objects.get(pipeline=pipeline) + self.assertEqual(run.starter, 'carol') # Who pressed the button + self.assertEqual(self.creator.calls[0].user.username, 'alice') # Whose test it is + self.assertIn(('carol', 'PIPELINE #%d RUN #%d STARTED' % (pipeline.id, run.id)), self.events()) + + def test_nothing_changes_on_a_get(self): + + pipeline = self.pipeline(); run = self.start() + client = self.client_for(self.alice()) + + for url in ('/pipelines/%d/start/', '/pipelines/%d/toggle/', '/pipelines/%d/delete/'): + self.assertEqual(client.get(url % pipeline.id).status_code, 405, url) + + self.assertEqual(client.get('/pipelines/run/%d/cancel/' % run.id).status_code, 405) + + pipeline = self.reload(Pipeline, pipeline.id) + self.assertTrue(pipeline.enabled and not pipeline.deleted) + self.assertEqual(self.status(run), 'RUNNING') + + def test_a_post_needs_the_csrf_token(self): + + pipeline = self.pipeline() + response = self.client_for(self.alice(), enforce_csrf_checks=True).post('/pipelines/%d/toggle/' % pipeline.id) + + self.assertEqual(response.status_code, 403) + self.assertTrue(self.reload(Pipeline, pipeline.id).enabled) + + def test_the_rule_is_closed_by_default(self): + + import OpenBench.pipeline_views as views + pipeline = self.pipeline() + + self.assertFalse(views.can_manage(None, pipeline)) + self.assertFalse(views.can_manage(Profile.objects.get(user=self.dave), pipeline)) # Disabled + self.assertFalse(views.can_manage(Profile.objects.get(user=self.bob), pipeline)) # Somebody else + + disabled_approver = make_user('erin', enabled=False, approver=True) + self.assertFalse(views.can_manage(Profile.objects.get(user=disabled_approver), pipeline)) + + def alice(self): + return self.user + +class ActionTests(PagesTestCase): + + def test_starting_begins_the_first_step_and_opens_the_run(self): + + pipeline = self.pipeline() + response = self.client_for(self.user).post('/pipelines/%d/start/' % pipeline.id) + + run = PipelineRun.objects.get(pipeline=pipeline) + self.assertEqual(response['Location'], '/pipelines/run/%d/' % run.id) + self.assertEqual(StepRun.objects.get(run=run).status, 'RUNNING') + + def test_a_double_click_does_not_start_everything_twice(self): + + pipeline, client = self.pipeline(), self.client_for(self.user) + client.post('/pipelines/%d/start/' % pipeline.id) + again = client.post('/pipelines/%d/start/' % pipeline.id) + + self.assertEqual(PipelineRun.objects.filter(pipeline=pipeline).count(), 1) + self.assertEqual(len(self.creator.calls), 1) + self.assertIn('/pipelines/run/', again['Location']) + + def test_a_paused_pipeline_does_not_start(self): + + pipeline = self.pipeline(enabled=False) + self.client_for(self.user).post('/pipelines/%d/start/' % pipeline.id) + + self.assertEqual(PipelineRun.objects.count(), 0) + self.assertEqual(self.creator.calls, []) + + def test_toggling_pauses_and_resumes(self): + + pipeline, client = self.pipeline(), self.client_for(self.user) + + client.post('/pipelines/%d/toggle/' % pipeline.id); self.assertFalse(self.reload(Pipeline, pipeline.id).enabled) + client.post('/pipelines/%d/toggle/' % pipeline.id); self.assertTrue(self.reload(Pipeline, pipeline.id).enabled) + + def test_deleting_cancels_the_runs_that_are_going(self): + + run = self.start() + self.client_for(self.user).post('/pipelines/%d/delete/' % run.pipeline_id) + + self.assertTrue(self.reload(Pipeline, run.pipeline_id).deleted) + self.assertEqual(self.status(run), 'CANCELLED') + + def test_cancelling_a_run_that_is_over_says_so(self): + + run = self.start() + client = self.client_for(self.user) + + client.post('/pipelines/run/%d/cancel/' % run.id) + client.post('/pipelines/run/%d/cancel/' % run.id) + + self.assertEqual(len(PipelineRun.objects.get(id=run.id).notes), 1) # Cancelled once + + def test_an_unknown_id_is_not_a_crash(self): + + client = self.client_for(self.user) + for url in ('/pipelines/9999/', '/pipelines/9999/start/', '/pipelines/run/9999/', '/pipelines/run/9999/cancel/'): + response = client.post(url) if url.endswith(('start/', 'cancel/')) else client.get(url) + self.assertEqual((url, response.status_code), (url, 302)) + self.assertEqual(response['Location'], '/pipelines/') + + def test_a_first_step_that_could_not_be_created_is_reported(self): + + self.creator.errors = ['dev branch was not found'] + pipeline = self.pipeline() + response = self.client_for(self.user).post('/pipelines/%d/start/' % pipeline.id) + + self.assertIn('/pipelines/run/', response['Location']) + self.assertEqual(PipelineRun.objects.get(pipeline=pipeline).status, 'ERROR') + +class PageTests(PagesTestCase): + + def get(self, url, user=None): + return self.client_for(user or self.user).get(url) + + def test_pages_ask_anonymous_visitors_to_log_in(self): + + run = self.start() + for url in ('/pipelines/', '/pipelines/%d/' % run.pipeline_id, '/pipelines/run/%d/' % run.id): + response = self.client_for().get(url) + self.assertEqual((url, response.status_code, response['Location']), (url, 302, '/login/')) + + def test_a_disabled_account_sees_nothing(self): + + run = self.start() + for url in ('/pipelines/', '/pipelines/%d/' % run.pipeline_id, '/pipelines/run/%d/' % run.id): + self.assertEqual(self.get(url, self.dave).status_code, 302, url) + + def test_the_list_shows_pipelines_and_how_they_are_doing(self): + + run = self.start() + self.pipeline(enabled=False) + gone = self.pipeline(); Pipeline.objects.filter(id=gone.id).update(deleted=True, name='Gone for good') + + page = self.get('/pipelines/').content.decode() + + self.assertIn('Running', page); self.assertIn('Paused', page) + self.assertIn('/pipelines/%d/' % run.pipeline_id, page) + self.assertNotIn('Gone for good', page) + + def test_the_empty_list_says_what_to_do(self): + self.assertIn('No pipelines yet', self.get('/pipelines/').content.decode()) + + def test_names_cannot_inject_markup_anywhere(self): + + evil = '' + pipeline = self.pipeline(graph=base.graph(steps={ + 'stc' : base.step(evil), 'ltc' : base.step('LTC'), 'retry' : base.step('Retry') })) + Pipeline.objects.filter(id=pipeline.id).update(name=evil) + + run, _ = __import__('OpenBench.pipelines', fromlist=['x']).start_pipeline(self.reload(Pipeline, pipeline.id)) + StepRun.objects.filter(run=run).update(status='ERROR', message=evil) + + for url in ('/pipelines/', '/pipelines/%d/' % pipeline.id, '/pipelines/run/%d/' % run.id): + page = self.get(url).content.decode() + self.assertNotIn(evil, page, url) + self.assertNotIn('' + run = self.start() + Pipeline.objects.filter(id=run.pipeline_id).update(name=evil) + url = '/test/%d/' % self.steps(run)['stc'].test_id + + page = self.client_for(self.user).get(url).content.decode() + self.assertNotIn('