style: Run pre-commit

This commit is contained in:
Joakim Saario 2022-08-21 22:08:29 +02:00
parent b47882b114
commit 9c6c13b78a
No known key found for this signature in database
GPG key ID: D8B76D271B7BD453
235 changed files with 7180 additions and 5628 deletions

View file

@ -1,12 +1,10 @@
import re
from http import HTTPStatus
from io import StringIO
from typing import Tuple
import lxml.etree
import pytest
from fastapi.testclient import TestClient
from aurweb import config, db, filters, time
@ -16,8 +14,8 @@ from aurweb.models.tu_voteinfo import TUVoteInfo
from aurweb.models.user import User
from aurweb.testing.requests import Request
DATETIME_REGEX = r'^[0-9]{4}-[0-9]{2}-[0-9]{2} \(.+\)$'
PARTICIPATION_REGEX = r'^1?[0-9]{2}[%]$' # 0% - 100%
DATETIME_REGEX = r"^[0-9]{4}-[0-9]{2}-[0-9]{2} \(.+\)$"
PARTICIPATION_REGEX = r"^1?[0-9]{2}[%]$" # 0% - 100%
def parse_root(html):
@ -43,11 +41,11 @@ def get_pkglist_directions(table):
def get_a(node):
return node.xpath('./a')[0].text.strip()
return node.xpath("./a")[0].text.strip()
def get_span(node):
return node.xpath('./span')[0].text.strip()
return node.xpath("./span")[0].text.strip()
def assert_current_vote_html(row, expected):
@ -82,39 +80,51 @@ def setup(db_test):
@pytest.fixture
def client():
from aurweb.asgi import app
yield TestClient(app=app)
@pytest.fixture
def tu_user():
tu_type = db.query(AccountType,
AccountType.AccountType == "Trusted User").first()
tu_type = db.query(AccountType, AccountType.AccountType == "Trusted User").first()
with db.begin():
tu_user = db.create(User, Username="test_tu",
Email="test_tu@example.org",
RealName="Test TU", Passwd="testPassword",
AccountType=tu_type)
tu_user = db.create(
User,
Username="test_tu",
Email="test_tu@example.org",
RealName="Test TU",
Passwd="testPassword",
AccountType=tu_type,
)
yield tu_user
@pytest.fixture
def tu_user2():
with db.begin():
tu_user2 = db.create(User, Username="test_tu2",
Email="test_tu2@example.org",
RealName="Test TU 2", Passwd="testPassword",
AccountTypeID=TRUSTED_USER_ID)
tu_user2 = db.create(
User,
Username="test_tu2",
Email="test_tu2@example.org",
RealName="Test TU 2",
Passwd="testPassword",
AccountTypeID=TRUSTED_USER_ID,
)
yield tu_user2
@pytest.fixture
def user():
user_type = db.query(AccountType,
AccountType.AccountType == "User").first()
user_type = db.query(AccountType, AccountType.AccountType == "User").first()
with db.begin():
user = db.create(User, Username="test", Email="test@example.org",
RealName="Test User", Passwd="testPassword",
AccountType=user_type)
user = db.create(
User,
Username="test",
Email="test@example.org",
RealName="Test User",
Passwd="testPassword",
AccountType=user_type,
)
yield user
@ -126,10 +136,15 @@ def proposal(user, tu_user):
end = ts + 1000
with db.begin():
voteinfo = db.create(TUVoteInfo,
Agenda=agenda, Quorum=0.0,
User=user.Username, Submitter=tu_user,
Submitted=start, End=end)
voteinfo = db.create(
TUVoteInfo,
Agenda=agenda,
Quorum=0.0,
User=user.Username,
Submitter=tu_user,
Submitted=start,
End=end,
)
yield (tu_user, user, voteinfo)
@ -153,7 +168,7 @@ def test_tu_index_unauthorized(client: TestClient, user: User):
def test_tu_empty_index(client, tu_user):
""" Check an empty index when we don't create any records. """
"""Check an empty index when we don't create any records."""
# Make a default get request to /tu.
cookies = {"AURSID": tu_user.login(Request(), "testPassword")}
@ -179,18 +194,23 @@ def test_tu_index(client, tu_user):
# Create some test votes: (Agenda, Start, End).
votes = [
("Test agenda 1", ts - 5, ts + 1000), # Still running.
("Test agenda 2", ts - 1000, ts - 5) # Not running anymore.
("Test agenda 2", ts - 1000, ts - 5), # Not running anymore.
]
vote_records = []
with db.begin():
for vote in votes:
agenda, start, end = vote
vote_records.append(
db.create(TUVoteInfo, Agenda=agenda,
User=tu_user.Username,
Submitted=start, End=end,
Quorum=0.0,
Submitter=tu_user))
db.create(
TUVoteInfo,
Agenda=agenda,
User=tu_user.Username,
Submitted=start,
End=end,
Quorum=0.0,
Submitter=tu_user,
)
)
with db.begin():
# Vote on an ended proposal.
@ -202,21 +222,23 @@ def test_tu_index(client, tu_user):
cookies = {"AURSID": tu_user.login(Request(), "testPassword")}
with client as request:
# Pass an invalid cby and pby; let them default to "desc".
response = request.get("/tu", cookies=cookies, params={
"cby": "BAD!",
"pby": "blah"
}, allow_redirects=False)
response = request.get(
"/tu",
cookies=cookies,
params={"cby": "BAD!", "pby": "blah"},
allow_redirects=False,
)
assert response.status_code == int(HTTPStatus.OK)
# Rows we expect to exist in HTML produced by /tu for current votes.
expected_rows = [
(
r'Test agenda 1',
r"Test agenda 1",
DATETIME_REGEX,
DATETIME_REGEX,
tu_user.Username,
r'^(Yes|No)$'
r"^(Yes|No)$",
)
]
@ -239,13 +261,13 @@ def test_tu_index(client, tu_user):
# Rows we expect to exist in HTML produced by /tu for past votes.
expected_rows = [
(
r'Test agenda 2',
r"Test agenda 2",
DATETIME_REGEX,
DATETIME_REGEX,
tu_user.Username,
r'^\d+$',
r'^\d+$',
r'^(Yes|No)$'
r"^\d+$",
r"^\d+$",
r"^(Yes|No)$",
)
]
@ -315,19 +337,27 @@ def test_tu_index_table_paging(client, tu_user):
with db.begin():
for i in range(25):
# Create 25 current votes.
db.create(TUVoteInfo, Agenda=f"Agenda #{i}",
User=tu_user.Username,
Submitted=(ts - 5), End=(ts + 1000),
Quorum=0.0,
Submitter=tu_user)
db.create(
TUVoteInfo,
Agenda=f"Agenda #{i}",
User=tu_user.Username,
Submitted=(ts - 5),
End=(ts + 1000),
Quorum=0.0,
Submitter=tu_user,
)
for i in range(25):
# Create 25 past votes.
db.create(TUVoteInfo, Agenda=f"Agenda #{25 + i}",
User=tu_user.Username,
Submitted=(ts - 1000), End=(ts - 5),
Quorum=0.0,
Submitter=tu_user)
db.create(
TUVoteInfo,
Agenda=f"Agenda #{25 + i}",
User=tu_user.Username,
Submitted=(ts - 1000),
End=(ts - 5),
Quorum=0.0,
Submitter=tu_user,
)
cookies = {"AURSID": tu_user.login(Request(), "testPassword")}
with client as request:
@ -347,7 +377,7 @@ def test_tu_index_table_paging(client, tu_user):
DATETIME_REGEX,
DATETIME_REGEX,
tu_user.Username,
r'^(Yes|No)$'
r"^(Yes|No)$",
]
for i, row in enumerate(rows):
@ -361,9 +391,9 @@ def test_tu_index_table_paging(client, tu_user):
# Now, get the next page of current votes.
offset = 10 # Specify coff=10
with client as request:
response = request.get("/tu", cookies=cookies, params={
"coff": offset
}, allow_redirects=False)
response = request.get(
"/tu", cookies=cookies, params={"coff": offset}, allow_redirects=False
)
assert response.status_code == int(HTTPStatus.OK)
old_rows = rows
@ -390,9 +420,9 @@ def test_tu_index_table_paging(client, tu_user):
offset = 20 # Specify coff=10
with client as request:
response = request.get("/tu", cookies=cookies, params={
"coff": offset
}, allow_redirects=False)
response = request.get(
"/tu", cookies=cookies, params={"coff": offset}, allow_redirects=False
)
assert response.status_code == int(HTTPStatus.OK)
# Do it again, we only have five left.
@ -423,11 +453,15 @@ def test_tu_index_sorting(client, tu_user):
with db.begin():
for i in range(2):
# Create 'Agenda #1' and 'Agenda #2'.
db.create(TUVoteInfo, Agenda=f"Agenda #{i + 1}",
User=tu_user.Username,
Submitted=(ts + 5), End=(ts + 1000),
Quorum=0.0,
Submitter=tu_user)
db.create(
TUVoteInfo,
Agenda=f"Agenda #{i + 1}",
User=tu_user.Username,
Submitted=(ts + 5),
End=(ts + 1000),
Quorum=0.0,
Submitter=tu_user,
)
# Let's order each vote one day after the other.
# This will allow us to test the sorting nature
@ -446,27 +480,27 @@ def test_tu_index_sorting(client, tu_user):
rows = get_table_rows(table)
# The latest Agenda is at the top by default.
expected = [
"Agenda #2",
"Agenda #1"
]
expected = ["Agenda #2", "Agenda #1"]
assert len(rows) == len(expected)
for i, row in enumerate(rows):
assert_current_vote_html(row, [
expected[i],
DATETIME_REGEX,
DATETIME_REGEX,
tu_user.Username,
r'^(Yes|No)$'
])
assert_current_vote_html(
row,
[
expected[i],
DATETIME_REGEX,
DATETIME_REGEX,
tu_user.Username,
r"^(Yes|No)$",
],
)
# Make another request; one that sorts the current votes
# in ascending order instead of the default descending order.
with client as request:
response = request.get("/tu", cookies=cookies, params={
"cby": "asc"
}, allow_redirects=False)
response = request.get(
"/tu", cookies=cookies, params={"cby": "asc"}, allow_redirects=False
)
assert response.status_code == int(HTTPStatus.OK)
# Get lxml handles of the document.
@ -478,30 +512,37 @@ def test_tu_index_sorting(client, tu_user):
rev_expected = list(reversed(expected))
assert len(rows) == len(rev_expected)
for i, row in enumerate(rows):
assert_current_vote_html(row, [
rev_expected[i],
DATETIME_REGEX,
DATETIME_REGEX,
tu_user.Username,
r'^(Yes|No)$'
])
assert_current_vote_html(
row,
[
rev_expected[i],
DATETIME_REGEX,
DATETIME_REGEX,
tu_user.Username,
r"^(Yes|No)$",
],
)
def test_tu_index_last_votes(client: TestClient, tu_user: User, tu_user2: User,
user: User):
def test_tu_index_last_votes(
client: TestClient, tu_user: User, tu_user2: User, user: User
):
ts = time.utcnow()
with db.begin():
# Create a proposal which has ended.
voteinfo = db.create(TUVoteInfo, Agenda="Test agenda",
User=user.Username,
Submitted=(ts - 1000),
End=(ts - 5),
Yes=1,
No=1,
ActiveTUs=1,
Quorum=0.0,
Submitter=tu_user)
voteinfo = db.create(
TUVoteInfo,
Agenda="Test agenda",
User=user.Username,
Submitted=(ts - 1000),
End=(ts - 5),
Yes=1,
No=1,
ActiveTUs=1,
Quorum=0.0,
Submitter=tu_user,
)
# Create a vote on it from tu_user.
db.create(TUVote, VoteInfo=voteinfo, User=tu_user)
@ -536,26 +577,27 @@ def test_tu_proposal_not_found(client, tu_user):
assert response.status_code == int(HTTPStatus.NOT_FOUND)
def test_tu_proposal_unauthorized(client: TestClient, user: User,
proposal: Tuple[User, User, TUVoteInfo]):
def test_tu_proposal_unauthorized(
client: TestClient, user: User, proposal: Tuple[User, User, TUVoteInfo]
):
cookies = {"AURSID": user.login(Request(), "testPassword")}
endpoint = f"/tu/{proposal[2].ID}"
with client as request:
response = request.get(endpoint, cookies=cookies,
allow_redirects=False)
response = request.get(endpoint, cookies=cookies, allow_redirects=False)
assert response.status_code == int(HTTPStatus.SEE_OTHER)
assert response.headers.get("location") == "/tu"
with client as request:
response = request.post(endpoint, cookies=cookies,
data={"decision": False},
allow_redirects=False)
response = request.post(
endpoint, cookies=cookies, data={"decision": False}, allow_redirects=False
)
assert response.status_code == int(HTTPStatus.SEE_OTHER)
assert response.headers.get("location") == "/tu"
def test_tu_running_proposal(client: TestClient,
proposal: Tuple[User, User, TUVoteInfo]):
def test_tu_running_proposal(
client: TestClient, proposal: Tuple[User, User, TUVoteInfo]
):
tu_user, user, voteinfo = proposal
with db.begin():
voteinfo.ActiveTUs = 1
@ -576,8 +618,7 @@ def test_tu_running_proposal(client: TestClient,
assert vote_running.text.strip() == "This vote is still running."
# Verify User field.
username = details.xpath(
'./div[contains(@class, "user")]/strong/a/text()')[0]
username = details.xpath('./div[contains(@class, "user")]/strong/a/text()')[0]
assert username.strip() == user.Username
active = details.xpath('./div[contains(@class, "field")]')[1]
@ -585,10 +626,13 @@ def test_tu_running_proposal(client: TestClient,
assert "Active Trusted Users assigned:" in content
assert "1" in content
submitted = details.xpath(
'./div[contains(@class, "submitted")]/text()')[0]
assert re.match(r'^Submitted: \d{4}-\d{2}-\d{2} \d{2}:\d{2} \(.+\) by$',
submitted.strip()) is not None
submitted = details.xpath('./div[contains(@class, "submitted")]/text()')[0]
assert (
re.match(
r"^Submitted: \d{4}-\d{2}-\d{2} \d{2}:\d{2} \(.+\) by$", submitted.strip()
)
is not None
)
submitter = details.xpath('./div[contains(@class, "submitted")]/a')[0]
assert submitter.text.strip() == tu_user.Username
assert submitter.attrib["href"] == f"/account/{tu_user.Username}"
@ -598,8 +642,10 @@ def test_tu_running_proposal(client: TestClient,
assert end_label.strip() == "End:"
end_datetime = end.xpath("./strong/text()")[0]
assert re.match(r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2} \(.+\)$',
end_datetime.strip()) is not None
assert (
re.match(r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2} \(.+\)$", end_datetime.strip())
is not None
)
# We have not voted yet. Assert that our voting form is shown.
form = root.xpath('//form[contains(@class, "action-form")]')[0]
@ -630,8 +676,7 @@ def test_tu_running_proposal(client: TestClient,
# Make another request now that we've voted.
with client as request:
response = request.get(
"/tu", params={"id": voteinfo.ID}, cookies=cookies)
response = request.get("/tu", params={"id": voteinfo.ID}, cookies=cookies)
assert response.status_code == int(HTTPStatus.OK)
# Parse our new root.
@ -685,12 +730,13 @@ def test_tu_ended_proposal(client, proposal):
def test_tu_proposal_vote_not_found(client, tu_user):
""" Test POST request to a missing vote. """
"""Test POST request to a missing vote."""
cookies = {"AURSID": tu_user.login(Request(), "testPassword")}
with client as request:
data = {"decision": "Yes"}
response = request.post("/tu/1", cookies=cookies,
data=data, allow_redirects=False)
response = request.post(
"/tu/1", cookies=cookies, data=data, allow_redirects=False
)
assert response.status_code == int(HTTPStatus.NOT_FOUND)
@ -703,16 +749,14 @@ def test_tu_proposal_vote(client, proposal):
cookies = {"AURSID": tu_user.login(Request(), "testPassword")}
with client as request:
data = {"decision": "Yes"}
response = request.post(f"/tu/{voteinfo.ID}", cookies=cookies,
data=data)
response = request.post(f"/tu/{voteinfo.ID}", cookies=cookies, data=data)
assert response.status_code == int(HTTPStatus.OK)
# Check that the proposal record got updated.
assert voteinfo.Yes == yes + 1
# Check that the new TUVote exists.
vote = db.query(TUVote, TUVote.VoteInfo == voteinfo,
TUVote.User == tu_user).first()
vote = db.query(TUVote, TUVote.VoteInfo == voteinfo, TUVote.User == tu_user).first()
assert vote is not None
root = parse_root(response.text)
@ -723,7 +767,8 @@ def test_tu_proposal_vote(client, proposal):
def test_tu_proposal_vote_unauthorized(
client: TestClient, proposal: Tuple[User, User, TUVoteInfo]):
client: TestClient, proposal: Tuple[User, User, TUVoteInfo]
):
tu_user, user, voteinfo = proposal
with db.begin():
@ -732,8 +777,9 @@ def test_tu_proposal_vote_unauthorized(
cookies = {"AURSID": tu_user.login(Request(), "testPassword")}
with client as request:
data = {"decision": "Yes"}
response = request.post(f"/tu/{voteinfo.ID}", cookies=cookies,
data=data, allow_redirects=False)
response = request.post(
f"/tu/{voteinfo.ID}", cookies=cookies, data=data, allow_redirects=False
)
assert response.status_code == int(HTTPStatus.UNAUTHORIZED)
root = parse_root(response.text)
@ -742,8 +788,9 @@ def test_tu_proposal_vote_unauthorized(
with client as request:
data = {"decision": "Yes"}
response = request.get(f"/tu/{voteinfo.ID}", cookies=cookies,
data=data, allow_redirects=False)
response = request.get(
f"/tu/{voteinfo.ID}", cookies=cookies, data=data, allow_redirects=False
)
assert response.status_code == int(HTTPStatus.OK)
root = parse_root(response.text)
@ -761,8 +808,9 @@ def test_tu_proposal_vote_cant_self_vote(client, proposal):
cookies = {"AURSID": tu_user.login(Request(), "testPassword")}
with client as request:
data = {"decision": "Yes"}
response = request.post(f"/tu/{voteinfo.ID}", cookies=cookies,
data=data, allow_redirects=False)
response = request.post(
f"/tu/{voteinfo.ID}", cookies=cookies, data=data, allow_redirects=False
)
assert response.status_code == int(HTTPStatus.BAD_REQUEST)
root = parse_root(response.text)
@ -771,8 +819,9 @@ def test_tu_proposal_vote_cant_self_vote(client, proposal):
with client as request:
data = {"decision": "Yes"}
response = request.get(f"/tu/{voteinfo.ID}", cookies=cookies,
data=data, allow_redirects=False)
response = request.get(
f"/tu/{voteinfo.ID}", cookies=cookies, data=data, allow_redirects=False
)
assert response.status_code == int(HTTPStatus.OK)
root = parse_root(response.text)
@ -791,8 +840,9 @@ def test_tu_proposal_vote_already_voted(client, proposal):
cookies = {"AURSID": tu_user.login(Request(), "testPassword")}
with client as request:
data = {"decision": "Yes"}
response = request.post(f"/tu/{voteinfo.ID}", cookies=cookies,
data=data, allow_redirects=False)
response = request.post(
f"/tu/{voteinfo.ID}", cookies=cookies, data=data, allow_redirects=False
)
assert response.status_code == int(HTTPStatus.BAD_REQUEST)
root = parse_root(response.text)
@ -801,8 +851,9 @@ def test_tu_proposal_vote_already_voted(client, proposal):
with client as request:
data = {"decision": "Yes"}
response = request.get(f"/tu/{voteinfo.ID}", cookies=cookies,
data=data, allow_redirects=False)
response = request.get(
f"/tu/{voteinfo.ID}", cookies=cookies, data=data, allow_redirects=False
)
assert response.status_code == int(HTTPStatus.OK)
root = parse_root(response.text)
@ -816,8 +867,7 @@ def test_tu_proposal_vote_invalid_decision(client, proposal):
cookies = {"AURSID": tu_user.login(Request(), "testPassword")}
with client as request:
data = {"decision": "EVIL"}
response = request.post(f"/tu/{voteinfo.ID}", cookies=cookies,
data=data)
response = request.post(f"/tu/{voteinfo.ID}", cookies=cookies, data=data)
assert response.status_code == int(HTTPStatus.BAD_REQUEST)
assert response.text == "Invalid 'decision' value."
@ -829,18 +879,17 @@ def test_tu_addvote(client: TestClient, tu_user: User):
assert response.status_code == int(HTTPStatus.OK)
def test_tu_addvote_unauthorized(client: TestClient, user: User,
proposal: Tuple[User, User, TUVoteInfo]):
def test_tu_addvote_unauthorized(
client: TestClient, user: User, proposal: Tuple[User, User, TUVoteInfo]
):
cookies = {"AURSID": user.login(Request(), "testPassword")}
with client as request:
response = request.get("/addvote", cookies=cookies,
allow_redirects=False)
response = request.get("/addvote", cookies=cookies, allow_redirects=False)
assert response.status_code == int(HTTPStatus.SEE_OTHER)
assert response.headers.get("location") == "/tu"
with client as request:
response = request.post("/addvote", cookies=cookies,
allow_redirects=False)
response = request.post("/addvote", cookies=cookies, allow_redirects=False)
assert response.status_code == int(HTTPStatus.SEE_OTHER)
assert response.headers.get("location") == "/tu"
@ -848,8 +897,7 @@ def test_tu_addvote_unauthorized(client: TestClient, user: User,
def test_tu_addvote_invalid_type(client: TestClient, tu_user: User):
cookies = {"AURSID": tu_user.login(Request(), "testPassword")}
with client as request:
response = request.get("/addvote", params={"type": "faketype"},
cookies=cookies)
response = request.get("/addvote", params={"type": "faketype"}, cookies=cookies)
assert response.status_code == int(HTTPStatus.OK)
root = parse_root(response.text)
@ -860,11 +908,7 @@ def test_tu_addvote_invalid_type(client: TestClient, tu_user: User):
def test_tu_addvote_post(client: TestClient, tu_user: User, user: User):
cookies = {"AURSID": tu_user.login(Request(), "testPassword")}
data = {
"user": user.Username,
"type": "add_tu",
"agenda": "Blah"
}
data = {"user": user.Username, "type": "add_tu", "agenda": "Blah"}
with client as request:
response = request.post("/addvote", cookies=cookies, data=data)
@ -874,15 +918,12 @@ def test_tu_addvote_post(client: TestClient, tu_user: User, user: User):
assert voteinfo is not None
def test_tu_addvote_post_cant_duplicate_username(client: TestClient,
tu_user: User, user: User):
def test_tu_addvote_post_cant_duplicate_username(
client: TestClient, tu_user: User, user: User
):
cookies = {"AURSID": tu_user.login(Request(), "testPassword")}
data = {
"user": user.Username,
"type": "add_tu",
"agenda": "Blah"
}
data = {"user": user.Username, "type": "add_tu", "agenda": "Blah"}
with client as request:
response = request.post("/addvote", cookies=cookies, data=data)
@ -904,8 +945,7 @@ def test_tu_addvote_post_invalid_username(client: TestClient, tu_user: User):
assert response.status_code == int(HTTPStatus.NOT_FOUND)
def test_tu_addvote_post_invalid_type(client: TestClient, tu_user: User,
user: User):
def test_tu_addvote_post_invalid_type(client: TestClient, tu_user: User, user: User):
cookies = {"AURSID": tu_user.login(Request(), "testPassword")}
data = {"user": user.Username}
with client as request:
@ -913,8 +953,7 @@ def test_tu_addvote_post_invalid_type(client: TestClient, tu_user: User,
assert response.status_code == int(HTTPStatus.BAD_REQUEST)
def test_tu_addvote_post_invalid_agenda(client: TestClient,
tu_user: User, user: User):
def test_tu_addvote_post_invalid_agenda(client: TestClient, tu_user: User, user: User):
cookies = {"AURSID": tu_user.login(Request(), "testPassword")}
data = {"user": user.Username, "type": "add_tu"}
with client as request: