sabuhish / fastapi-mail

Fastapi mail system sending mails(individual, bulk) attachments(individual, bulk)
https://sabuhish.github.io/fastapi-mail/
MIT License
673 stars 82 forks source link

Get href from email body #208

Closed JWDobken closed 8 months ago

JWDobken commented 8 months ago

Hi, I am using fastapi-mail to send a verification token to users and in my unit test I try to get this token via the body:

def test_request_verify_token(
    client: TestClient, outbox: List
):
    response = client.post(
        "...",
        json={"email": USER_EMAIL}
    )
    assert response.status_code == 202
    assert len(outbox) == 2
    assert outbox[-1]["To"] == USER_EMAIL
    assert outbox[-1].get_payload() == "something?"
JWDobken commented 8 months ago

ok solved. quite cumbersome

def test_request_verify_token(
    client: TestClient, outbox: List
):
    response = client.post(
        "...",
        json={"email": USER_EMAIL}
    )
    assert response.status_code == 202
    assert len(outbox) == 2
    assert outbox[-1]["To"] == USER_EMAIL
    payload = outbox[-1].get_payload(0).get_payload()
    body_text = base64.b64decode(payload).decode("utf-8")
    soup = BeautifulSoup(body_text, "html.parser")
    first_href = soup.find("a")["href"] if soup.find("a") else None
    parsed_url = urlparse(first_href)
    query_parameters = parse_qs(parsed_url.query)
    token = query_parameters['token']
    assert token