Skip to content

Commit

Permalink
Add tests for AHrefParser
Browse files Browse the repository at this point in the history
  • Loading branch information
hellais committed Mar 15, 2024
1 parent 6f9a7e3 commit ac0ea13
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 26 deletions.
4 changes: 0 additions & 4 deletions ooniapi/services/ooniauth/src/ooniauth/routers/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@

router = APIRouter()

# @router.get("/api/v2/ooniauth/user-session")
# @router.post("/api/v2/ooniauth/user-session", response_model=SessionTokenCreate)
# redirect_to: ## Make this optional


class UserRegister(BaseModel):
email_address: EmailStr = Field(
Expand Down
39 changes: 28 additions & 11 deletions ooniapi/services/ooniauth/tests/test_auth_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@
from ooniauth.main import app
from freezegun import freeze_time

from html.parser import HTMLParser


class AHrefParser(HTMLParser):
def __init__(self):
super().__init__()
self.links = []

def handle_starttag(self, tag, attrs):
if tag == "a":
for attr in attrs:
if attr[0] == "href":
self.links.append(attr[1])


def test_ahref_parser():
link = "https://example.com/somewhere"
html_message = f"""
<html>
<a href="{link}" extra_tag="ignored"></a>
<html>
"""
parser = AHrefParser()
parser.feed(html_message)
parser.close()

assert parser.links[0] == link


def register(client, email_address, mock_ses_client, valid_redirect_to_url):
d = dict(email_address=email_address, redirect_to=valid_redirect_to_url)
Expand All @@ -11,17 +39,6 @@ def register(client, email_address, mock_ses_client, valid_redirect_to_url):
assert r.status_code == 200
assert j == {"msg": "ok"}

from html.parser import HTMLParser

class AHrefParser(HTMLParser):
links = []

def handle_starttag(self, tag, attrs):
if tag == "a":
for attr in attrs:
if attr[0] == "href":
self.links.append(attr[1])

mock_send_email = mock_ses_client.send_email
assert (
mock_send_email.call_args.kwargs["Destination"]["ToAddresses"][0]
Expand Down
41 changes: 30 additions & 11 deletions ooniapi/services/ooniauth/tests/test_auth_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,43 @@
from freezegun import freeze_time


from html.parser import HTMLParser


# TODO(art): deduplicate this in a utility lib
class AHrefParser(HTMLParser):
def __init__(self):
super().__init__()
self.links = []

def handle_starttag(self, tag, attrs):
if tag == "a":
for attr in attrs:
if attr[0] == "href":
self.links.append(attr[1])


def test_ahref_parser():
link = "https://example.com/somewhere"
html_message = f"""
<html>
<a href="{link}" extra_tag="ignored"></a>
<html>
"""
parser = AHrefParser()
parser.feed(html_message)
parser.close()

assert parser.links[0] == link


def perform_login(client, email_address, mock_ses_client, valid_redirect_to_url):
d = dict(email_address=email_address, redirect_to=valid_redirect_to_url)
r = client.post("/api/v2/ooniauth/user-login", json=d)
j = r.json()
assert r.status_code == 200
assert j["email_address"] == email_address

from html.parser import HTMLParser

class AHrefParser(HTMLParser):
links = []

def handle_starttag(self, tag, attrs):
if tag == "a":
for attr in attrs:
if attr[0] == "href":
self.links.append(attr[1])

mock_send_email = mock_ses_client.send_email
assert (
mock_send_email.call_args.kwargs["Destination"]["ToAddresses"][0]
Expand Down

0 comments on commit ac0ea13

Please sign in to comment.