Esempio n. 1
0
def test_send_work_funding_peer_review_invitation(test_db, request_ctx):
    """Test to send user invitation."""
    org = Organisation(id=1,
                       name="THE ORGANISATION",
                       tuakiri_name="THE ORGANISATION",
                       confirmed=True,
                       orcid_client_id="CLIENT ID",
                       orcid_secret="Client Secret",
                       city="CITY",
                       country="COUNTRY",
                       disambiguation_org_id="ID",
                       disambiguation_org_source="SOURCE")

    inviter = User(email="*****@*****.**",
                   name="TEST USER",
                   username="******",
                   roles=Role.RESEARCHER,
                   orcid=None,
                   confirmed=True,
                   organisation=org)

    u = User(email="*****@*****.**",
             name="TEST USER",
             username="******",
             roles=Role.RESEARCHER,
             orcid=None,
             confirmed=True,
             organisation=org)
    u.save()
    user_org = UserOrg(user=u, org=org)
    user_org.save()
    task = Task(org=org, task_type=1)
    task.save()
    email = "*****@*****.**"
    fr = FundingRecord(task=task.id, title="xyz", type="Award")
    fr.save()
    fc = FundingInvitees(funding_record=fr.id,
                         email=email,
                         first_name="Alice",
                         last_name="Bob")
    fc.save()
    with request_ctx("/") as ctxx:
        utils.send_work_funding_peer_review_invitation(inviter=inviter,
                                                       org=org,
                                                       email=email,
                                                       name=u.name,
                                                       task_id=task.id)
        rv = ctxx.app.full_dispatch_request()
        assert rv.status_code == 200
Esempio n. 2
0
def test_send_user_invitation(test_db, request_ctx):
    """Test to send user invitation."""
    org = Organisation.create(name="THE ORGANISATION",
                              tuakiri_name="THE ORGANISATION",
                              confirmed=True,
                              orcid_client_id="CLIENT ID",
                              orcid_secret="Client Secret",
                              city="CITY",
                              country="COUNTRY",
                              disambiguation_org_id="ID",
                              disambiguation_org_source="SOURCE")

    inviter = User.create(email="*****@*****.**",
                          name="TEST USER",
                          username="******",
                          roles=Role.RESEARCHER,
                          orcid=None,
                          confirmed=True,
                          organisation=org)

    u = User(email="*****@*****.**",
             name="TEST USER",
             username="******",
             roles=Role.RESEARCHER,
             orcid=None,
             confirmed=True,
             organisation=org)
    u.save()
    user_org = UserOrg(user=u, org=org)
    user_org.save()
    task = Task(id=123, org=org)
    task.save()
    email = "*****@*****.**"
    first_name = "TEST"
    last_name = "Test"
    affiliation_types = {"staff"}
    with patch("smtplib.SMTP") as mock_smtp, request_ctx("/") as ctxx:

        instance = mock_smtp.return_value
        error = {
            email:
            (450, "Requested mail action not taken: mailbox unavailable")
        }
        instance.utils.send_user_invitation.return_value = error
        result = instance.utils.send_user_invitation(
            inviter=inviter,
            org=org,
            email=email,
            first_name=first_name,
            last_name=last_name,
            affiliation_types=affiliation_types,
            task_id=task.id)
        rv = ctxx.app.full_dispatch_request()
        assert rv.status_code == 200
        assert instance.utils.send_user_invitation.called  # noqa: E712
        assert (450, 'Requested mail action not taken: mailbox unavailable'
                ) == result[email]

    with patch("orcid_hub.utils.send_email") as send_email:
        result = utils.send_user_invitation(
            inviter=inviter.id,
            org=org.id,
            email=email,
            first_name=first_name,
            last_name=last_name,
            affiliation_types=affiliation_types,
            start_date=[1971, 1, 1],
            end_date=[2018, 5, 29],
            task_id=task.id)
        send_email.assert_called_once()
        assert result == UserInvitation.select().order_by(
            UserInvitation.id.desc()).first().id