Esempio n. 1
0
def test_test_database(models):
    """Test of the consitency of the test database."""
    assert Organisation.select().count() == 10
    assert User.select().count() == 63
    assert OrcidToken.select().count() == 60
    assert AffiliationRecord.select().count() == 10
    assert FundingRecord.select().count() == 10
    assert FundingContributor.select().count() == 10
    assert FundingInvitee.select().count() == 10
    assert ExternalId.select().count() == 10
    assert WorkRecord.select().count() == 10
    assert WorkContributor.select().count() == 10
    assert WorkExternalId.select().count() == 10
    assert WorkInvitee.select().count() == 10
    assert PeerReviewRecord.select().count() == 10
    assert PeerReviewExternalId.select().count() == 10
    assert PeerReviewInvitee.select().count() == 10
    assert ResearcherUrlRecord.select().count() == 10
    assert OtherNameRecord.select().count() == 10
    assert KeywordRecord.select().count() == 10
    assert Task.select().count() == 30
    assert UserOrgAffiliation.select().count() == 30

    assert User.get(id=43).admin_for.count() == 10
    assert User.get(id=1).admin_for.count() == 0
    assert User.get(id=42).admin_for.count() > 0
    assert User.get(id=2).organisations.count() > 0
    assert Organisation.get(id=1).admins.count() == 1
    assert Organisation.get(id=5).users.count() > 0
    assert Organisation.get(id=5).admins.count() > 0
    assert User.select().where(User.orcid == User.get(
        email="*****@*****.**").orcid).count() == 3
    assert len(User.get(email="*****@*****.**").org_links) == 3

    user = User.get(email="*****@*****.**")
    available_organisations = user.available_organisations
    assert available_organisations.count() == 10

    admin = User.create(email="*****@*****.**", organisation=user.organisation, confirmed=True,
            first_name="TEST", last_name="ADMIN", roles=Role.ADMIN)
    ui = UserInvitation.create(email=user.email, invitee=user, inviter=admin, token="TOKEN-123")
    admin.delete_instance()
    ui = UserInvitation.get(ui.id)
    assert ui.inviter_id is None
    user.delete_instance()
    assert not UserInvitation.select().where(UserInvitation.id == ui.id).exists()

    org = Organisation.select().limit(1).first()
    user = User.select().limit(1).first()
    ot = OrcidToken.create(user=user, org=org, scope="S1,S2,S3")
    assert len(ot.scopes) == 3

    ot.scopes = ["A", "B", "C", "D"]
    assert ot.scope == "A,B,C,D"
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
Esempio n. 3
0
def test_send_user_invitation(app, mocker):
    """Test to send user invitation."""
    send_email = mocker.patch("orcid_hub.utils.send_email")
    org = app.data["org"]
    inviter = User.create(email="*****@*****.**",
                          name="TEST USER",
                          roles=Role.RESEARCHER,
                          orcid=None,
                          confirmed=True,
                          organisation=org)

    email = "*****@*****.**"
    first_name = "TEST"
    last_name = "Test"
    affiliation_types = {"staff"}
    u = User.create(email=email,
                    name="TEST USER",
                    roles=Role.RESEARCHER,
                    orcid=None,
                    confirmed=True,
                    organisation=org)
    UserOrg.create(user=u, org=org)
    task = Task.create(org=org)

    mock_smtp = mocker.patch("smtplib.SMTP").return_value
    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)
    assert instance.utils.send_user_invitation.called  # noqa: E712
    assert (450, 'Requested mail action not taken: mailbox unavailable'
            ) == result[email]

    send_email = mocker.patch("orcid_hub.utils.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()

    with pytest.raises(Exception) as excinfo:
        send_email.reset_mock()
        dkim_key_path = utils.app.conig["DKIM_KEY_PATH"]
        app.conig["DKIM_KEY_PATH"] = "/file/not/found.key"
        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_not_called()
        utils.app.conig["DKIM_KEY_PATH"] = dkim_key_path
        assert "/file/not/found.key" in str(excinfo.value)