Пример #1
0
def test_inviting_a_speaker_adds_the_speaker(client: Client, user: User,
                                             send_mail: Mock) -> None:
    talk = Talk(title="My Talk", length=25)
    talk.add_speaker(user, InvitationStatus.CONFIRMED)
    db.session.add(talk)

    alice = User(email="*****@*****.**", fullname="Alice Example")
    db.session.add(alice)
    db.session.commit()

    # reload from DB to avoid "not attached to session" error
    talk = Talk.query.filter_by(title="My Talk").one()

    client.get("/test-login/{}".format(user.user_id))
    resp = client.get("/talks/{}/speakers".format(talk.talk_id))
    assert_html_response(resp)
    csrf_token = extract_csrf_from(resp)

    postdata = {"email": "*****@*****.**", "csrf_token": csrf_token}
    resp = client.post("/talks/{}/speakers".format(talk.talk_id),
                       data=postdata,
                       follow_redirects=True)

    assert_html_response_contains(resp, "Alice Example")

    send_mail.assert_called_once_with(to=["*****@*****.**"],
                                      template="email/co-presenter-invite",
                                      talk=ANY)

    _, kwargs = send_mail.call_args
    assert kwargs["talk"].talk_id == talk.talk_id

    assert_talk_has_speakers(talk, ["*****@*****.**"])
Пример #2
0
def test_inviting_a_speaker_emails_the_speaker(client: Client, user: User,
                                               send_mail: Mock) -> None:
    talk = Talk(title="My Talk", length=25)
    talk.add_speaker(user, InvitationStatus.CONFIRMED)
    db.session.add(talk)
    db.session.commit()

    assert User.query.filter_by(
        email="*****@*****.**").one_or_none() is None

    # reload from DB to avoid "not attached to session" error
    talk = Talk.query.filter_by(title="My Talk").one()

    client.get("/test-login/{}".format(user.user_id))
    resp = client.get("/talks/{}/speakers".format(talk.talk_id))
    assert_html_response(resp)
    csrf_token = extract_csrf_from(resp)

    # this speaker doesn't exist, but we should still send the email
    postdata = {"email": "*****@*****.**", "csrf_token": csrf_token}
    resp = client.post("/talks/{}/speakers".format(talk.talk_id),
                       data=postdata,
                       follow_redirects=True)

    assert_html_response_contains(resp, "*****@*****.**")

    send_mail.assert_called_once_with(to=["*****@*****.**"],
                                      template="email/co-presenter-invite",
                                      talk=ANY)

    _, kwargs = send_mail.call_args
    assert kwargs["talk"].talk_id == talk.talk_id

    # this also implies a user with that email was created
    assert_talk_has_speakers(talk, ["*****@*****.**"])
Пример #3
0
def test_site_admins_can_access_admin(client: Client, user: User) -> None:
    client.get("/test-login/{}".format(user.user_id), follow_redirects=True)

    user.site_admin = True
    db.session.add(user)
    db.session.commit()

    resp = client.get("/manage/")
    assert_html_response(resp, status=200)
Пример #4
0
def test_talks_list_page_shows_proposed_and_withdrawn_talks(
    client: Client, user: User
) -> None:
    in_talk = Talk(title="In Talk", length=25)
    in_talk.add_speaker(user, InvitationStatus.CONFIRMED)

    out_talk = Talk(title="Out Talk", length=40, state=TalkStatus.WITHDRAWN)
    out_talk.add_speaker(user, InvitationStatus.CONFIRMED)

    db.session.add(in_talk)
    db.session.add(out_talk)
    db.session.commit()

    client.get("/test-login/{}".format(user.user_id))
    resp = client.get("/talks")
    body = assert_html_response(resp)
    soup = bs4.BeautifulSoup(body, "html.parser")

    talks = soup.find_all("div", class_="talk")
    assert len(talks) == 2

    talk_row_texts = [
        re.sub(r"\s+", " ", talk.parent.get_text()).strip() for talk in talks
    ]
    talk_row_texts.sort()

    assert re.match("In Talk.*Withdraw", talk_row_texts[0])
    assert re.match("Out Talk.*Re-Submit", talk_row_texts[1])

    # lazy: also test withdraw/resubmit here in the same test
    client.get(f"/talks/1/withdraw")
    client.get(f"/talks/2/resubmit")

    resp = client.get("/talks")
    body = assert_html_response(resp)
    soup = bs4.BeautifulSoup(body, "html.parser")

    talks = soup.find_all("div", class_="talk")
    assert len(talks) == 2

    talk_row_texts = [
        re.sub(r"\s+", " ", talk.parent.get_text()).strip() for talk in talks
    ]
    talk_row_texts.sort()

    assert re.match("In Talk.*Re-Submit", talk_row_texts[0])
    assert re.match("Out Talk.*Withdraw", talk_row_texts[1])
Пример #5
0
def test_talks_list_page_doesnt_show_resubmit_after_proposal_window(
    client: Client, user: User
) -> None:
    conf = Conference.query.get(1)
    conf.proposals_begin = datetime.utcnow() - timedelta(days=3)
    conf.proposals_end = datetime.utcnow() - timedelta(days=1)

    withdrawn_talk = Talk(title="Withdrawn Talk", length=40, state=TalkStatus.WITHDRAWN)
    withdrawn_talk.add_speaker(user, InvitationStatus.CONFIRMED)
    db.session.add(withdrawn_talk)
    db.session.commit()

    client.get("/test-login/{}".format(user.user_id))
    resp = client.get("/talks")
    assert_html_response_doesnt_contain(resp, "Re-Submit")

    # also make sure to prevent it server-side
    resp = client.get(f"/talks/1/resubmit")
    assert_html_response(resp, status=400)
Пример #6
0
def test_talk_editing_not_allowed_while_voting(user: User,
                                               client: Client) -> None:
    conf = Conference.query.get(1)
    conf.voting_begin = datetime.utcnow() - timedelta(days=1)
    conf.voting_end = datetime.utcnow() + timedelta(days=1)
    db.session.add(conf)

    talk = Talk(title="My Talk", length=25)
    talk.add_speaker(user, InvitationStatus.CONFIRMED)
    db.session.add(talk)
    db.session.commit()

    db.session.refresh(talk)
    client.get("/test-login/{}".format(user.user_id), follow_redirects=True)
    resp = client.get(f"/talks/{talk.talk_id}")
    assert_html_response(resp, status=200)

    postdata = {"csrf_token": extract_csrf_from(resp)}
    resp = client.post(f"/talks/{talk.talk_id}", data=postdata)
    assert_html_response(resp, status=400)
Пример #7
0
def test_talk_creation_only_allowed_in_window(user: User,
                                              client: Client) -> None:
    conf = Conference.query.get(1)
    conf.proposals_begin = datetime.utcnow() - timedelta(days=1)
    conf.proposals_end = datetime.utcnow() + timedelta(days=1)
    db.session.add(conf)
    db.session.commit()

    client.get("/test-login/{}".format(user.user_id), follow_redirects=True)
    resp = client.get("/talks/new")
    assert_html_response(resp, status=200)

    conf = Conference.query.get(1)
    conf.proposals_begin = datetime.utcnow() - timedelta(days=3)
    conf.proposals_end = datetime.utcnow() - timedelta(days=1)
    db.session.add(conf)
    db.session.commit()

    resp = client.get("/talks/new")
    assert_html_response(resp, status=400)
Пример #8
0
def test_email_magic_link_tokens_only_work_once(
    client: Client, send_mail: Mock
) -> None:
    resp = client.get("/login/email")
    csrf_token = extract_csrf_from(resp)

    postdata = {"email": "*****@*****.**", "csrf_token": csrf_token}
    client.post("/login/email", data=postdata, follow_redirects=True)

    _, kwargs = send_mail.call_args
    magic_link_url = kwargs["magic_link"]
    assert not urlparse(magic_link_url).query

    magic_link_path = urlparse(magic_link_url).path

    # first time should work
    resp = client.get(magic_link_path)
    assert_redirected(resp, "/profile")  # new user, go to profile

    # second time should get a 401
    resp = client.get(magic_link_path)
    assert_html_response(resp, status=401)
Пример #9
0
def test_saving_a_talk_clears_categories(
    client: Client, conference: Conference, user: User
) -> None:
    category = Category(conference=conference, name="The Category")
    talk = Talk(title="Old Title", length=25)
    talk.categories.append(category)
    talk.add_speaker(user, InvitationStatus.CONFIRMED)
    db.session.add(talk)
    db.session.add(category)
    db.session.commit()

    client.get("/test-login/{}".format(user.user_id))
    resp = client.get("/talks/1")

    csrf_token = extract_csrf_from(resp)
    postdata = {"title": "New Title", "csrf_token": csrf_token}
    resp = client.post("/talks/1", data=postdata, follow_redirects=True)
    assert_html_response(resp, status=200)

    talk = Talk.query.first()
    assert talk.title == "New Title"
    assert talk.categories == []
Пример #10
0
def test_talks_list_page_lists_talks(client: Client, user: User) -> None:
    alice = User(email="*****@*****.**", fullname="Alice Example")
    bob = User(email="*****@*****.**", fullname="Bob Example")
    db.session.add(alice)
    db.session.add(bob)
    db.session.commit()

    one_talk = Talk(title="My Talk", length=25)
    one_talk.add_speaker(user, InvitationStatus.CONFIRMED)

    two_talk = Talk(title="Our Talk", length=40)
    two_talk.add_speaker(user, InvitationStatus.CONFIRMED)
    two_talk.add_speaker(alice, InvitationStatus.CONFIRMED)

    all_talk = Talk(title="All Our Talk", length=25)
    all_talk.add_speaker(user, InvitationStatus.CONFIRMED)
    all_talk.add_speaker(alice, InvitationStatus.CONFIRMED)
    all_talk.add_speaker(bob, InvitationStatus.CONFIRMED)

    db.session.add(one_talk)
    db.session.add(two_talk)
    db.session.add(all_talk)
    db.session.commit()

    client.get("/test-login/{}".format(user.user_id))
    resp = client.get("/talks")
    body = assert_html_response(resp)
    soup = bs4.BeautifulSoup(body, "html.parser")

    talks = soup.find_all("div", class_="talk")
    assert len(talks) == 3

    talk_row_texts = [re.sub(r"\s+", " ", talk.get_text()).strip() for talk in talks]
    assert sorted(talk_row_texts) == sorted(
        [
            "My Talk (25 Minutes)",
            "Our Talk (40 Minutes, Alice Example and You)",
            "All Our Talk (25 Minutes, Alice Example, Bob Example, and You)",
        ]
    )
Пример #11
0
def test_manage_speakers_page_shows_other_speakers(client: Client,
                                                   user: User) -> None:
    alice = User(email="*****@*****.**", fullname="Alice Example")
    bob = User(email="*****@*****.**", fullname="Bob Example")
    charlie = User(email="*****@*****.**", fullname="Charlie Example")
    db.session.add(alice)
    db.session.add(bob)
    db.session.add(charlie)

    talk = Talk(title="My Talk", length=25)
    talk.add_speaker(user, InvitationStatus.CONFIRMED)
    talk.add_speaker(alice, InvitationStatus.PENDING)
    talk.add_speaker(bob, InvitationStatus.REJECTED)
    talk.add_speaker(charlie, InvitationStatus.DELETED)
    db.session.add(talk)
    db.session.commit()

    # reload from DB to avoid "not attached to session" error
    talk = Talk.query.filter_by(title="My Talk").one()

    client.get("/test-login/{}".format(user.user_id))
    resp = client.get("/talks/{}/speakers".format(talk.talk_id))

    body = assert_html_response(resp)
    soup = bs4.BeautifulSoup(body, "html.parser")

    speakers = soup.find_all("div", class_="speaker")
    assert len(speakers) == 4

    row_texts = [
        re.sub(r"\s+", " ", row.get_text()).strip() for row in speakers
    ]
    assert sorted(row_texts) == sorted([
        "{} ({}) Confirmed".format(user.fullname, user.email),
        "Alice Example ([email protected]) Pending Uninvite",
        "Bob Example ([email protected]) Rejected",
        "Charlie Example ([email protected]) Deleted Reinvite",
    ])
Пример #12
0
def test_manage_speakers_page_shows_primary_speaker(client: Client,
                                                    user: User) -> None:
    talk = Talk(title="My Talk", length=25)
    talk.add_speaker(user, InvitationStatus.CONFIRMED)
    db.session.add(talk)
    db.session.commit()

    # reload from DB to avoid "not attached to session" error
    talk = Talk.query.filter_by(title="My Talk").one()

    client.get("/test-login/{}".format(user.user_id))
    resp = client.get("/talks/{}/speakers".format(talk.talk_id))

    body = assert_html_response(resp)
    soup = bs4.BeautifulSoup(body, "html.parser")

    speakers = soup.find_all("div", class_="speaker")
    assert len(speakers) == 1

    row_texts = [
        re.sub(r"\s+", " ", row.get_text()).strip() for row in speakers
    ]
    assert row_texts == ["{} ({}) Confirmed".format(user.fullname, user.email)]
Пример #13
0
def test_ordinary_users_cant_access_admin(client: Client, user: User) -> None:
    client.get("/test-login/{}".format(user.user_id), follow_redirects=True)
    resp = client.get("/manage/")
    assert_html_response(resp, status=404)
Пример #14
0
def test_anonymous_users_cant_access_admin(client: Client) -> None:
    resp = client.get("/manage/")
    assert_html_response(resp, status=404)
Пример #15
0
def test_invalid_email_magic_link_login(client: Client) -> None:
    with patch.object(views, "parse_magic_link_token") as parse:
        parse.return_value = None
        resp = client.get("/login/token/any-token-here", follow_redirects=True)

    assert_html_response(resp, status=404)