Skip to content

Commit

Permalink
Add type hints for all test fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
jace committed Jan 8, 2024
1 parent 02cf833 commit e95e970
Show file tree
Hide file tree
Showing 35 changed files with 498 additions and 212 deletions.
16 changes: 13 additions & 3 deletions tests/e2e/basic/live_server_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@

import re

from playwright.sync_api import expect
from flask import Flask
from playwright.sync_api import Page, expect

from funnel import models

def test_open_homepage(live_server, page, db_session, org_uu):
from ...conftest import LiveServerProtocol, scoped_session


def test_open_homepage(
live_server: LiveServerProtocol,
page: Page,
db_session: scoped_session,
org_uu: models.Organization,
):
page.goto(live_server.url)
expect(page).to_have_title(re.compile("Test Hasgeek"))
expect(page.get_by_text("Past sessions")).to_be_visible()


def test_transport_mock_sms(live_server, page, app):
def test_transport_mock_sms(live_server: LiveServerProtocol, page: Page, app: Flask):
page.goto(app.url_for('login'))
textbox = page.locator('#username')
textbox.fill("8123456789")
Expand Down
38 changes: 32 additions & 6 deletions tests/integration/views/account_add_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from funnel import models

from ...conftest import LoginFixtureProtocol, TestClient

PATCH_EMAIL_VALIDATOR = (
'funnel.models.email_address.EmailAddress.is_valid_email_address'
)
Expand Down Expand Up @@ -41,7 +43,10 @@ def get_wrong_otp(reference: str) -> str:


def test_add_email_wrong_otp(
client, csrf_token, login, user_rincewind: models.User
client: TestClient,
csrf_token: str,
login: LoginFixtureProtocol,
user_rincewind: models.User,
) -> None:
"""Add a new email address with an OTP and confirm an incorrect OTP is rejected."""
login.as_(user_rincewind)
Expand All @@ -65,7 +70,12 @@ def test_add_email_wrong_otp(
assert 'OTP is incorrect' in rv2.data.decode()


def test_add_email(client, csrf_token, login, user_rincewind: models.User) -> None:
def test_add_email(
client: TestClient,
csrf_token: str,
login: LoginFixtureProtocol,
user_rincewind: models.User,
) -> None:
"""Add a new email address with an OTP."""
login.as_(user_rincewind)
assert user_rincewind.emails == []
Expand All @@ -89,7 +99,11 @@ def test_add_email(client, csrf_token, login, user_rincewind: models.User) -> No


def test_merge_with_email_otp(
client, csrf_token, login, useremail_rincewind, user_mort
client: TestClient,
csrf_token: str,
login: LoginFixtureProtocol,
useremail_rincewind: models.AccountEmail,
user_mort: models.User,
) -> None:
"""Providing a valid OTP for another user's email address causes a merge prompt."""
login.as_(user_mort)
Expand All @@ -115,7 +129,10 @@ def test_merge_with_email_otp(


def test_add_phone_wrong_otp(
client, csrf_token, login, user_rincewind: models.User
client: TestClient,
csrf_token: str,
login: LoginFixtureProtocol,
user_rincewind: models.User,
) -> None:
"""Add a new phone number with an OTP and confirm an incorrect OTP is rejected."""
login.as_(user_rincewind)
Expand All @@ -138,7 +155,12 @@ def test_add_phone_wrong_otp(
assert 'OTP is incorrect' in rv2.data.decode()


def test_add_phone(client, csrf_token, login, user_rincewind: models.User) -> None:
def test_add_phone(
client: TestClient,
csrf_token: str,
login: LoginFixtureProtocol,
user_rincewind: models.User,
) -> None:
"""Add a new phone number with an OTP."""
login.as_(user_rincewind)
assert user_rincewind.phones == []
Expand All @@ -161,7 +183,11 @@ def test_add_phone(client, csrf_token, login, user_rincewind: models.User) -> No


def test_merge_with_phone_otp(
client, csrf_token, login, userphone_rincewind, user_mort
client: TestClient,
csrf_token: str,
login: LoginFixtureProtocol,
userphone_rincewind: models.AccountPhone,
user_mort: models.User,
) -> None:
"""Providing a valid OTP for another user's phone number causes a merge prompt."""
login.as_(user_mort)
Expand Down
74 changes: 38 additions & 36 deletions tests/integration/views/comment_moderation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@

from funnel import models

from ...conftest import LoginFixtureProtocol, TestClient, scoped_session


@pytest.mark.usefixtures('app_context')
def test_comment_report_same(
client,
db_session,
login,
new_user,
new_user2,
new_user_admin,
new_user_owner,
new_project,
csrf_token,
client: TestClient,
db_session: scoped_session,
login: LoginFixtureProtocol,
new_user: models.User,
new_user2: models.User,
new_user_admin: models.User,
new_user_owner: models.User,
new_project: models.Project,
csrf_token: str,
) -> None:
# Let's give new_user site_editor role
sm = models.SiteMembership(
Expand Down Expand Up @@ -86,15 +88,15 @@ def test_comment_report_same(

@pytest.mark.usefixtures('app_context')
def test_comment_report_opposing(
client,
db_session,
login,
new_user,
new_user2,
new_user_admin,
new_user_owner,
new_project,
csrf_token,
client: TestClient,
db_session: scoped_session,
login: LoginFixtureProtocol,
new_user: models.User,
new_user2: models.User,
new_user_admin: models.User,
new_user_owner: models.User,
new_project: models.Project,
csrf_token: str,
) -> None:
# Let's give new_user site_editor role
sm = models.SiteMembership(
Expand Down Expand Up @@ -162,15 +164,15 @@ def test_comment_report_opposing(

@pytest.mark.usefixtures('app_context')
def test_comment_report_majority_spam(
client,
db_session,
login,
new_user,
new_user2,
new_user_admin,
new_user_owner,
new_project,
csrf_token,
client: TestClient,
db_session: scoped_session,
login: LoginFixtureProtocol,
new_user: models.User,
new_user2: models.User,
new_user_admin: models.User,
new_user_owner: models.User,
new_project: models.Project,
csrf_token: str,
) -> None:
# Let's give new_user site_editor role
sm = models.SiteMembership(
Expand Down Expand Up @@ -251,15 +253,15 @@ def test_comment_report_majority_spam(

@pytest.mark.usefixtures('app_context')
def test_comment_report_majority_ok(
client,
db_session,
login,
new_user,
new_user2,
new_user_admin,
new_user_owner,
new_project,
csrf_token,
client: TestClient,
db_session: scoped_session,
login: LoginFixtureProtocol,
new_user: models.User,
new_user2: models.User,
new_user_admin: models.User,
new_user_owner: models.User,
new_project: models.Project,
csrf_token: str,
) -> None:
# Let's give new_user site_editor role
sm = models.SiteMembership(
Expand Down
7 changes: 3 additions & 4 deletions tests/integration/views/login_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
from werkzeug.datastructures import MultiDict

from coaster.utils import utcnow
from tests.conftest import LoginFixtureProtocol

from funnel import models
from funnel import forms, models
from funnel.auth import current_auth
from funnel.loginproviders import GitHubProvider
from funnel.registry import (
Expand All @@ -27,7 +26,7 @@
from funnel.transports import TransportConnectionError, TransportRecipientError
from funnel.views.otp import OtpSession

from ...conftest import TestClient, scoped_session
from ...conftest import LoginFixtureProtocol, TestClient, scoped_session

pytestmark = pytest.mark.filterwarnings(
"ignore:Object of type <AccountPhone> not in session",
Expand Down Expand Up @@ -719,7 +718,7 @@ def test_phone_otp_not_sent(


@pytest.mark.usefixtures('user_rincewind_email', 'user_rincewind_with_weak_password')
def test_weak_password_exception(forms, client: TestClient, csrf_token: str) -> None:
def test_weak_password_exception(client: TestClient, csrf_token: str) -> None:
"""If login form blocks weak password, login view will force user to reset it."""
with client:
with patch(
Expand Down
12 changes: 11 additions & 1 deletion tests/integration/views/membership_views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

import pytest

from funnel import models

from ...conftest import LoginFixtureProtocol, TestClient


@pytest.mark.usefixtures('app_context')
def test_create_new_member(client, login, new_user_owner, new_project) -> None:
def test_create_new_member(
client: TestClient,
login: LoginFixtureProtocol,
new_user_owner: models.User,
new_project: models.Project,
) -> None:
login.as_(new_user_owner)
# GET request should return a form
resp = client.get(new_project.url_for('new_member'))
assert resp.status_code == 200
assert resp.json is not None
assert 'form' in resp.json
assert new_project.url_for('new_member') in resp.json.get('form')

Expand Down
9 changes: 8 additions & 1 deletion tests/integration/views/project_views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

from funnel import forms, models

from ...conftest import LoginFixtureProtocol, TestClient


@pytest.mark.usefixtures('app_context')
def test_new_label_get(client, login, new_user, new_project) -> None:
def test_new_label_get(
client: TestClient,
login: LoginFixtureProtocol,
new_user: models.User,
new_project: models.Project,
) -> None:
login.as_(new_user)
resp = client.get(new_project.url_for('new_label'))
label_form = forms.LabelForm(parent=new_project, model=models.Label)
Expand Down
26 changes: 13 additions & 13 deletions tests/unit/forms/proposal_forms_test.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""Tests for Proposal forms."""

from funnel import models
from flask import Flask

from funnel import forms, models


def test_proposal_label_admin_form(
forms,
app,
new_main_label,
new_main_label_unrestricted,
new_label,
new_proposal,
app: Flask,
new_main_label: models.Label,
new_main_label_unrestricted: models.Label,
new_label: models.Label,
new_proposal: models.Proposal,
) -> None:
assert new_main_label.restricted
assert not new_main_label_unrestricted.restricted
Expand Down Expand Up @@ -39,12 +40,11 @@ def test_proposal_label_admin_form(


def test_proposal_label_form(
forms,
app,
new_main_label,
new_main_label_unrestricted,
new_label,
new_proposal,
app: Flask,
new_main_label: models.Label,
new_main_label_unrestricted: models.Label,
new_label: models.Label,
new_proposal: models.Proposal,
) -> None:
assert new_main_label.restricted
assert not new_main_label_unrestricted.restricted
Expand Down
12 changes: 7 additions & 5 deletions tests/unit/forms/rsvp_json_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import json

from flask import Flask

from funnel import forms

valid_schema = {
Expand Down Expand Up @@ -33,7 +35,7 @@
}


def test_valid_boxoffice_form(app) -> None:
def test_valid_boxoffice_form(app: Flask) -> None:
"""Valid schema is accepted by the schema form validator."""
with app.test_request_context(
method='POST',
Expand All @@ -43,7 +45,7 @@ def test_valid_boxoffice_form(app) -> None:
assert form.validate() is True


def test_invalid_boxoffice_form(app) -> None:
def test_invalid_boxoffice_form(app: Flask) -> None:
"""Invalid schema is rejected by the schema form validator."""
with app.test_request_context(
method='POST',
Expand All @@ -56,7 +58,7 @@ def test_invalid_boxoffice_form(app) -> None:
assert form.errors == {'register_form_schema': ['Invalid JSON']}


def test_valid_json_register_form(app) -> None:
def test_valid_json_register_form(app: Flask) -> None:
with app.test_request_context(
method='POST',
data={'form': '{"field_name":"Vetinari","has_checked":"on"}'},
Expand All @@ -65,7 +67,7 @@ def test_valid_json_register_form(app) -> None:
assert form.validate() is True


def test_invalid_json_register_form(app) -> None:
def test_invalid_json_register_form(app: Flask) -> None:
with app.test_request_context(
method='POST',
data={'form': 'This is an invalid json'},
Expand All @@ -76,7 +78,7 @@ def test_invalid_json_register_form(app) -> None:
assert form.form.errors == ['Invalid JSON']


def test_excess_json_register_form(app) -> None:
def test_excess_json_register_form(app: Flask) -> None:
with app.test_request_context(
method='POST',
data={'form': json.dumps(rsvp_excess_json)},
Expand Down
Loading

0 comments on commit e95e970

Please sign in to comment.