コード例 #1
0
def test_scheduled_tasks(app, mocker):
    """Test scheduled tasks."""
    org = app.data["org"]
    task = Task.create(org=org, task_type=TaskType.AFFILIATION)
    rq = app.extensions["rq2"]
    s = rq.get_scheduler()
    s.run(burst=True)
    task = Task.get(task.id)
    utils.process_tasks.queue()
    task = Task.get(task.id)
    assert task.expires_at is not None
    task.expires_at = utils.datetime(1988, 1, 1)
    task.save()
    utils.process_tasks.queue()
    assert Task.select().where(Task.id == task.id).count() == 0

    Organisation.update(webhook_enabled=True,
                        email_notifications_enabled=True).execute()
    User.update(orcid_updated_at=utils.date.today().replace(day=1) -
                utils.timedelta(days=1)).execute()
    send_email = mocker.patch("orcid_hub.utils.send_email")
    utils.send_orcid_update_summary.queue(org_id=org.id)
    send_email.assert_called()
コード例 #2
0
def test_webhook_invokation(client, mocker):
    """Test Organisation webhook invokation."""
    org = client.data["org"]
    user = client.data["user"]
    post = mocker.patch.object(utils.requests, "post", return_value=Mock(status_code=204))
    message = {
        "orcid": user.orcid,
        "url": f"https://sandbox.orcid.org/{user.orcid}",
        "type": "UPDATED",
        "updated-at": User.get(user.id).updated_at.isoformat(timespec="seconds"),
        "email": user.email,
        "eppn": user.eppn,
    }

    org.webhook_enabled = True
    org.webhook_url = "http://test.edu/"
    org.save()
    resp = client.post(f"/services/{user.id}/updated")
    assert resp.status_code == 204
    post.assert_called_with("http://test.edu/", json=message)

    org.webhook_append_orcid = True
    org.save()
    resp = client.post(f"/services/{user.id}/updated")
    assert resp.status_code == 204
    post.assert_called_with(f"http://test.edu/{user.orcid}", json=message)

    org.webhook_url = "http://test.edu"
    org.save()
    resp = client.post(f"/services/{user.id}/updated")
    assert resp.status_code == 204
    post.assert_called_with(f"http://test.edu/{user.orcid}", json=message)

    post = mocker.patch.object(utils.requests, "post", return_value=Mock(status_code=400))
    with pytest.raises(Exception):
        utils.invoke_webhook_handler(org.id, attempts=1, message=message)

    schedule = mocker.patch("orcid_hub.utils.invoke_webhook_handler.schedule")
    resp = client.post(f"/services/{user.id}/updated")
    assert resp.status_code == 204
    schedule.assert_called_with(
        datetime.timedelta(seconds=300), attempts=4, message=message, org_id=org.id
    )

    Organisation.update(webhook_apikey="ABC123").execute()
    schedule.reset_mock()
    resp = client.post(f"/services/{user.id}/updated")
    schedule.assert_called_with(
        datetime.timedelta(seconds=300), attempts=4, message=message, org_id=org.id
    )

    Organisation.update(webhook_apikey=None).execute()
    post = mocker.patch.object(utils.requests, "post", side_effect=Exception("OH! NOHHH!"))
    schedule.reset_mock()
    resp = client.post(f"/services/{user.id}/updated")
    assert resp.status_code == 204
    schedule.assert_not_called()

    with pytest.raises(Exception):
        utils.invoke_webhook_handler(org.id, attempts=1, message=message)

    # Test backward compatibility
    with pytest.raises(Exception):
        utils.invoke_webhook_handler(
            orcid=user.orcid, attempts=2, message=message, legacy_kwarg="abc123"
        )