Skip to content

Commit

Permalink
Merge 01a0b2d into 8320347
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaclarke committed Sep 1, 2021
2 parents 8320347 + 01a0b2d commit 55d92f2
Show file tree
Hide file tree
Showing 14 changed files with 223 additions and 233 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ repository, and the results are hosted on the

### Format code (before committing)
(conbench) $ cd ~/workspace/conbench/
(conbench) $ git status
modified: foo.py
(conbench) $ black foo.py
(conbench) $ black .
reformatted foo.py
(conbench) $ git add foo.py

Expand Down
10 changes: 10 additions & 0 deletions conbench/app/_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import os

import flask as f
import flask.views
import flask_login


def as_bool(x):
return x.lower() in ["yes", "y", "1", "on", "true"]


class AppEndpoint(flask.views.MethodView):
def public_data_off(self):
public = as_bool(os.getenv("BENCHMARKS_DATA_PUBLIC", "yes"))
return not flask_login.current_user.is_authenticated and not public

def redirect(self, endpoint, **kwargs):
return f.redirect(f.url_for(endpoint, **kwargs))

Expand Down
3 changes: 3 additions & 0 deletions conbench/app/batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def page(self, by_group):
)

def get(self, batch_id):
if self.public_data_off():
return self.redirect("app.login")

benchmarks, response = self._get_benchmarks(batch_id)
if response.status_code != 200:
self.flash("Error getting benchmarks.")
Expand Down
6 changes: 6 additions & 0 deletions conbench/app/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def page(self, benchmark, run, form):
)

def get(self, benchmark_id):
if self.public_data_off():
return self.redirect("app.login")

benchmark, run = self._get_benchmark_and_run(benchmark_id)
return self.page(benchmark, run, DeleteForm())

Expand Down Expand Up @@ -187,6 +190,9 @@ def page(self, benchmarks):
)

def get(self):
if self.public_data_off():
return self.redirect("app.login")

benchmarks, response = self._get_benchmarks()
if response.status_code != 200:
self.flash("Error getting benchmarks.")
Expand Down
3 changes: 3 additions & 0 deletions conbench/app/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def _get_plot(self, baseline, contender):
return plot

def get(self, compare_ids):
if self.public_data_off():
return self.redirect("app.login")

threshold = f.request.args.get("threshold")
params = {"compare_ids": compare_ids}
if threshold is not None:
Expand Down
3 changes: 3 additions & 0 deletions conbench/app/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def page(self, runs):
)

def get(self):
if self.public_data_off():
return self.redirect("app.login")

runs = self.get_display_runs()
return self.page(runs)

Expand Down
5 changes: 4 additions & 1 deletion conbench/app/runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@ def page(self, benchmarks, baseline_run, contender_run):
)

def get(self, run_id):
if self.public_data_off():
return self.redirect("app.login")

contender_run, baseline_run = self.get_display_run(run_id), None
if contender_run:
baseline_url = contender_run["links"].get("baseline")
if baseline_url:
baseline_run = self.get_display_baseline_run(baseline_url)

benchmarks, response = self._get_benchmarks(run_id)
contexts = self.get_contexts(benchmarks)
if response.status_code != 200:
self.flash("Error getting benchmarks.")
return self.redirect("app.index")

contexts = self.get_contexts(benchmarks)
for benchmark in benchmarks:
augment(benchmark, contexts)

Expand Down
115 changes: 115 additions & 0 deletions conbench/tests/app/_asserts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re

from ...tests.api import _fixtures
from ...tests.helpers import _create_fixture_user, create_random_user


Expand Down Expand Up @@ -67,3 +68,117 @@ def assert_page(self, r, title):
self.assert_200_ok(r)
title = "{} - ".format(title).encode()
assert title in r.data, r.data

def create_benchmark(self, client):
self.authenticate(client)
response = client.post("/api/benchmarks/", json=_fixtures.VALID_PAYLOAD)
benchmark_id = response.json["id"]
self.logout(client)
return benchmark_id


class Enforcer(AppEndpointTest):
def test_authenticated(self, client):
raise NotImplementedError()

def test_unauthenticated(self, client):
raise NotImplementedError()

def test_public_data_off(self, client, monkeypatch):
raise NotImplementedError()


class ListEnforcer(Enforcer):
def _assert_view(self, client, new_id):
response = client.get(self.url)
self.assert_page(response, self.title)
assert f"{new_id}".encode() in response.data

def test_authenticated(self, client, monkeypatch):
new_id = self._create(client)

monkeypatch.setenv("BENCHMARKS_DATA_PUBLIC", "on")
self.authenticate(client)
self._assert_view(client, new_id)

def test_unauthenticated(self, client, monkeypatch):
new_id = self._create(client)

monkeypatch.setenv("BENCHMARKS_DATA_PUBLIC", "on")
self.logout(client)
self._assert_view(client, new_id)

def test_public_data_off(self, client, monkeypatch):
new_id = self._create(client)

monkeypatch.setenv("BENCHMARKS_DATA_PUBLIC", "off")
self.logout(client)
response = client.get(self.url, follow_redirects=True)
assert new_id.encode() not in response.data
assert b"Sign In - Conbench" in response.data, response.data


class GetEnforcer(Enforcer):
def _assert_view(self, client, new_id):
response = client.get(self.url.format(new_id))
self.assert_page(response, self.title)
assert f'{new_id.split("...")[0]}'.encode() in response.data

def test_authenticated(self, client, monkeypatch):
new_id = self._create(client)

monkeypatch.setenv("BENCHMARKS_DATA_PUBLIC", "on")
self.authenticate(client)
self._assert_view(client, new_id)

def test_unauthenticated(self, client, monkeypatch):
new_id = self._create(client)

monkeypatch.setenv("BENCHMARKS_DATA_PUBLIC", "on")
self.logout(client)
self._assert_view(client, new_id)

def test_public_data_off(self, client, monkeypatch):
new_id = self._create(client)
entity_url = self.url.format(new_id)

monkeypatch.setenv("BENCHMARKS_DATA_PUBLIC", "off")
self.logout(client)
response = client.get(entity_url, follow_redirects=True)
assert new_id.encode() not in response.data
assert b"Sign In - Conbench" in response.data, response.data

def test_unknown(self, client):
self.authenticate(client)
unknown_url = self.url.format("unknown")
response = client.get(unknown_url, follow_redirects=True)
if getattr(self, "redirect_on_unknown", True):
assert b"Home - Conbench" in response.data, response.data
else:
title = "{} - Conbench".format(self.title).encode()
assert title in response.data, response.data


class CreateEnforcer(Enforcer):
def test_authenticated(self, client):
raise NotImplementedError()

def test_unauthenticated(self, client):
raise NotImplementedError()

def test_no_csrf_token(self, client):
raise NotImplementedError()


class DeleteEnforcer(Enforcer):
def test_authenticated(self, client):
raise NotImplementedError()

def test_unauthenticated(self, client):
raise NotImplementedError()

def test_no_csrf_token(self, client):
raise NotImplementedError()

def test_public_data_off(self, client, monkeypatch):
pass
48 changes: 6 additions & 42 deletions conbench/tests/app/test_batches.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,12 @@
import copy

from ...tests.api import _fixtures
from ...tests.app import _asserts


def create_benchmark(client):
# also create a benchmark with a different name & batch_id
other = copy.deepcopy(_fixtures.VALID_PAYLOAD)
other["batch_id"] = other["batch_id"] + "-other"
other["tags"]["name"] = other["tags"]["name"] + "-other"
other["timestamp"] = "2019-11-25T21:02:42.706806+00:00"
client.post("/api/benchmarks/", json=other)

# create a benchmark
data = copy.deepcopy(_fixtures.VALID_PAYLOAD)
response = client.post("/api/benchmarks/", json=data)
new_id = response.json["id"]
batch_id = response.json["batch_id"]
return new_id, batch_id
class TestBatch(_asserts.GetEnforcer):
url = "/batches/{}/"
title = "Batch"
redirect_on_unknown = False


class TestBatch(_asserts.AppEndpointTest):
def _create(self, client):
self.authenticate(client)
new_id, batch_id = create_benchmark(client)
self.logout(client)
return new_id, batch_id

def _assert_view(self, client, batch_id):
response = client.get(f"/batches/{batch_id}/")
self.assert_page(response, "Batch")
assert f"{batch_id}</li>".encode() in response.data

def test_batch_get_authenticated(self, client):
_, batch_id = self._create(client)
self.authenticate(client)
self._assert_view(client, batch_id)

def test_batch_get_unauthenticated(self, client):
_, batch_id = self._create(client)
self.logout(client)
self._assert_view(client, batch_id)

def test_batch_get_unknown(self, client):
self.authenticate(client)
response = client.get("/batches/unknown/", follow_redirects=True)
self.assert_page(response, "Batch")
self.create_benchmark(client)
return _fixtures.VALID_PAYLOAD["batch_id"]

0 comments on commit 55d92f2

Please sign in to comment.