def test_login_provider_load_user(request_ctx): # noqa: D103 """Test to load user.""" u = User(email="*****@*****.**", name="TEST USER", username="******", roles=Role.RESEARCHER, orcid=None, confirmed=True) u.save() user = login_provider.load_user(u.id) assert user == u assert login_provider.load_user(9999999) is None with request_ctx("/"): login_user(u) rv = login_provider.roles_required( Role.RESEARCHER)(lambda: "SUCCESS")() assert rv == "SUCCESS" rv = login_provider.roles_required(Role.SUPERUSER)(lambda: "SUCCESS")() assert rv != "SUCCESS" assert rv.status_code == 302 assert rv.location.startswith("/")
def test_link_already_affiliated(request_ctx): """Test a user affiliation initialization if the uerer is already affilated.""" with request_ctx("/link") as ctx: org = Organisation.get(name="THE ORGANISATION") test_user = User( email="*****@*****.**", name="TEST USER", organisation=org, orcid="ABC123", confirmed=True) test_user.save() orcidtoken = OrcidToken( user=test_user, org=org, scopes="/read-limited", access_token="ABC1234") orcidtoken_write = OrcidToken( user=test_user, org=org, scopes="/read-limited,/activities/update", access_token="ABC234") orcidtoken.save() orcidtoken_write.save() login_user(test_user, remember=True) uo = UserOrg(user=test_user, org=org) uo.save() rv = ctx.app.full_dispatch_request() assert rv.status_code == 302, "If the user is already affiliated, the user should be redirected ..." assert "profile" in rv.location, "redirection to 'profile' showing the ORCID"
def test_profile_wo_orcid(request_ctx): """Test a user profile that doesn't hava an ORCID.""" with request_ctx("/profile") as ctx: org = Organisation(name="THE ORGANISATION", confirmed=True) org.save() test_user = User( email="*****@*****.**", organisation=org, orcid=None, confirmed=True) test_user.save() login_user(test_user, remember=True) rv = ctx.app.full_dispatch_request() assert rv.status_code == 302 assert rv.location == url_for("link")
def test_link_with_unconfirmed_org(request_ctx): """Test a user affiliation initialization if the user Organisation isn't registered yet.""" with request_ctx("/link") as ctx: org = Organisation( name="THE ORGANISATION", confirmed=False, orcid_client_id="Test Client id") org.save() test_user = User( name="TEST USER", email="*****@*****.**", confirmed=True, organisation=org) test_user.save() login_user(test_user, remember=True) rv = ctx.app.full_dispatch_request() assert rv.status_code == 302
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
def test_profile(request_ctx): """Test an affilated user profile and ORCID data retrieval.""" with request_ctx("/profile") as ctx: org = Organisation(name="THE ORGANISATION", confirmed=True) org.save() test_user = User( email="*****@*****.**", organisation=org, orcid="ABC123", confirmed=True) test_user.save() orcidtoken = OrcidToken( user=test_user, org=org, scope="/read-limited,/activities/update", access_token="ABC1234") orcidtoken.save() login_user(test_user, remember=True) rv = ctx.app.full_dispatch_request() assert rv.status_code == 200 assert b"ABC123" in rv.data
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