Add tests for the pipeline pages: permission matrix, actions, pages, live signature, chip

This commit is contained in:
claude committed 2026-10-11 22:37:12 +02:00
1 parent edfc43a2be
commit ea1fed2627
1 file changed
+495
+495
View File
@@ -0,0 +1,495 @@
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# OpenBench is a chess engine testing framework authored by Andrew Grant. #
# <https://github.com/AndyGrant/OpenBench> <andrew@grantnet.us> #
# #
# 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 <http://www.gnu.org/licenses/>. #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# 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 = '<script>alert(1)</script>'
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('<script>alert', page, url)
def test_a_pipeline_page_shows_the_flow_and_the_steps(self):
pipeline = self.pipeline()
page = self.get('/pipelines/%d/' % pipeline.id).content.decode()
self.assertIn('class="pl-graph"', page)
self.assertIn('If it passes, start LTC', page)
self.assertIn('If it fails, start Retry', page)
self.assertIn('If this ends stopped', page) # An outcome that nothing handles is pointed out
self.assertIn('This pipeline has not run yet', page)
def test_only_those_who_may_see_the_buttons(self):
pipeline = self.pipeline()
mine = self.get('/pipelines/%d/' % pipeline.id).content.decode()
theirs = self.get('/pipelines/%d/' % pipeline.id, self.bob).content.decode()
for action in ('start', 'toggle', 'delete'):
self.assertIn('/pipelines/%d/%s/' % (pipeline.id, action), mine, action)
self.assertNotIn('/pipelines/%d/%s/' % (pipeline.id, action), theirs, action)
def test_a_running_pipeline_offers_the_run_not_a_second_start(self):
run = self.start()
page = self.get('/pipelines/%d/' % run.pipeline_id).content.decode()
self.assertIn('Open the running run', page)
self.assertNotIn('/pipelines/%d/start/' % run.pipeline_id, page)
def test_a_paused_pipeline_cannot_be_started_from_its_page(self):
pipeline = self.pipeline(enabled=False)
page = self.get('/pipelines/%d/' % pipeline.id).content.decode()
self.assertNotIn('/pipelines/%d/start/' % pipeline.id, page)
self.assertIn('Resume', page)
def test_a_damaged_graph_is_reported_and_not_drawn(self):
pipeline = self.pipeline()
Pipeline.objects.filter(id=pipeline.id).update(graph={ 'steps' : { 'x' : 'not a step' } }) # Behind our back
response = self.get('/pipelines/%d/' % pipeline.id)
page = response.content.decode()
self.assertEqual(response.status_code, 200)
self.assertIn('settings are damaged', page)
self.assertNotIn('class="pl-graph"', page)
self.assertNotIn('/pipelines/%d/start/' % pipeline.id, page)
def test_a_deleted_pipeline_is_gone_but_its_runs_can_still_be_read(self):
run = self.start()
self.client_for(self.user).post('/pipelines/%d/delete/' % run.pipeline_id)
self.assertEqual(self.get('/pipelines/%d/' % run.pipeline_id).status_code, 302)
self.assertEqual(self.get('/pipelines/run/%d/' % run.id).status_code, 200)
class RunPageTests(PagesTestCase):
def page(self, run, user=None):
return self.client_for(user or self.user).get('/pipelines/run/%d/' % run.id).content.decode()
def signature(self, page):
return re.search(r'data-signature="([^"]*)"', page).group(1)
def test_a_run_shows_its_path_and_links_to_its_tests(self):
run = self.start()
self.finish(self.steps(run)['stc'], passed=True)
page = self.page(run)
steps = self.steps(run)
self.assertIn('pl-taken', page) # The way that was gone along
self.assertIn('pl-idle', page) # And the one that was not
self.assertIn('After STC passed', page)
self.assertIn('href="/test/%d/"' % steps['stc'].test_id, page)
self.assertIn('href="/test/%d/"' % steps['ltc'].test_id, page)
def test_a_tune_links_to_the_tune_page(self):
tune = base.graph(steps={ 'tune' : base.tune_step('Tune') }, start='tune', edges=[])
self.creator.mode = 'SPSA'
run = self.start(graph=tune)
self.assertIn('href="/tune/%d/"' % self.steps(run)['tune'].test_id, self.page(run))
def test_the_page_tells_the_live_script_when_to_stop(self):
run = self.start()
self.assertIn('data-running="true"', self.page(run))
self.finish(self.steps(run)['stc']) # Stopped by hand, nothing follows
self.assertIn('data-running="false"', self.page(run))
def test_the_signature_changes_when_something_happens_and_not_when_it_does_not(self):
run = self.start()
before = self.signature(self.page(run))
self.assertEqual(before, self.signature(self.page(run))) # Nothing happened: no redraw
self.finish(self.steps(run)['stc'], passed=True)
after = self.signature(self.page(run))
self.assertNotEqual(before, after) # A step ended, and another began
def test_the_cancel_button_is_for_those_who_may_use_it_while_the_run_is_going(self):
run = self.start()
self.assertIn('/pipelines/run/%d/cancel/' % run.id, self.page(run))
self.assertNotIn('/pipelines/run/%d/cancel/' % run.id, self.page(run, self.bob))
self.finish(self.steps(run)['stc'])
self.assertNotIn('/pipelines/run/%d/cancel/' % run.id, self.page(run)) # Over
def test_a_test_that_waits_for_approval_is_said_to_wait(self):
run = self.start() # The stand-in creates tests that nobody has approved
self.assertIn('Waiting for an approver', self.page(run))
Test.objects.filter(id=self.steps(run)['stc'].test_id).update(approved=True)
self.assertNotIn('Waiting for an approver', self.page(run))
def test_a_step_that_could_not_start_says_why(self):
self.creator.errors = ['dev branch was not found']
run, _ = __import__('OpenBench.pipelines', fromlist=['x']).start_pipeline(self.pipeline())
page = self.page(run)
self.assertIn('Could not start', page); self.assertIn('dev branch was not found', page)
def test_the_reasons_for_how_a_run_went_are_listed(self):
run = self.start()
self.finish(self.steps(run)['stc']) # Stopped: nothing handles that
self.assertIn('no edge for this outcome', self.page(run))
class ChipTests(PagesTestCase):
""" What the page of a test says when a pipeline started it """
def plain_test(self):
# A test that nobody's pipeline made, with what its page needs to be drawn
return Test.objects.create(
author='alice', book_name='b', dev=self.engine, dev_repo='r', dev_engine='e', dev_options='Threads=1 Hash=8',
dev_time_control='1+1', base=self.engine, base_repo='r', base_engine='e', base_options='Threads=1 Hash=8',
base_time_control='1+1', test_mode='SPRT', priority=0, throughput=1000)
def test_a_workload_that_no_pipeline_made_has_no_step(self):
other = self.plain_test()
self.assertIsNone(pipelines.step_of(other.id))
def test_the_step_is_named_with_its_pipeline_and_its_run(self):
run = self.start()
step = pipelines.step_of(self.steps(run)['stc'].test_id)
self.assertEqual((step['name'], step['pipeline'], step['run_id'], step['after']), ('STC', 'P', run.id, ''))
def test_a_step_that_followed_another_says_so(self):
run = self.start()
self.finish(self.steps(run)['stc'], passed=True)
step = pipelines.step_of(self.steps(run)['ltc'].test_id)
self.assertEqual((step['name'], step['after']), ('LTC', 'After STC passed'))
def test_the_chip_shows_on_the_page_of_the_test_to_those_logged_in(self):
run = self.start()
url = '/test/%d/' % self.steps(run)['stc'].test_id
page = self.client_for(self.bob).get(url).content.decode() # Anybody enabled may see it, not just managers
self.assertIn('pl-chip', page)
self.assertIn('/pipelines/run/%d/' % run.id, page)
self.assertNotIn('pl-chip', self.client_for().get(url).content.decode()) # Not for visitors
def test_there_is_no_chip_on_a_test_that_no_pipeline_made(self):
other = self.plain_test()
self.assertNotIn('pl-chip', self.client_for(self.user).get('/test/%d/' % other.id).content.decode())
def test_the_chip_cannot_inject_markup(self):
evil = '<script>alert(1)</script>'
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('<script>alert(1)', page)