Пример #1
0
def test_jobrequestcreate_without_permission(rf):
    workspace = WorkspaceFactory()

    request = rf.get("/")
    request.user = UserFactory()

    with pytest.raises(Http404):
        JobRequestCreate.as_view()(
            request,
            org_slug=workspace.project.org.slug,
            project_slug=workspace.project.slug,
            workspace_slug=workspace.name,
        )
Пример #2
0
def test_jobrequestcreate_get_with_project_yaml_errors(rf, mocker, user):
    workspace = WorkspaceFactory()

    BackendMembershipFactory(user=user)
    ProjectMembershipFactory(project=workspace.project,
                             user=user,
                             roles=[ProjectDeveloper])

    mocker.patch(
        "jobserver.views.job_requests.get_project",
        autospec=True,
        side_effect=Exception("test error"),
    )

    request = rf.get("/")
    request.user = user

    response = JobRequestCreate.as_view()(
        request,
        org_slug=workspace.project.org.slug,
        project_slug=workspace.project.slug,
        workspace_slug=workspace.name,
    )

    assert response.status_code == 200

    assert response.context_data["actions"] == []
    assert response.context_data["actions_error"] == "test error"
Пример #3
0
def test_jobrequestcreate_with_no_backends(rf):
    user = UserFactory()
    workspace = WorkspaceFactory()

    ProjectMembershipFactory(project=workspace.project,
                             user=user,
                             roles=[ProjectDeveloper])

    request = rf.get("/")
    request.user = user

    with pytest.raises(Http404):
        JobRequestCreate.as_view()(
            request,
            org_slug=workspace.project.org.slug,
            project_slug=workspace.project.slug,
            workspace_slug=workspace.name,
        )
Пример #4
0
def test_jobrequestcreate_post_with_notifications_override(
        rf, mocker, monkeypatch, user):
    backend = BackendFactory()
    workspace = WorkspaceFactory(should_notify=True)

    BackendMembershipFactory(backend=backend, user=user)
    ProjectMembershipFactory(project=workspace.project,
                             user=user,
                             roles=[ProjectDeveloper])

    dummy_yaml = """
    version: 3
    expectations:
      population_size: 1000
    actions:
      twiddle:
        run: test
        outputs:
          moderately_sensitive:
            cohort: /path/to/output.csv
    """
    mocker.patch(
        "jobserver.views.job_requests.get_project",
        autospec=True,
        return_value=dummy_yaml,
    )

    data = {
        "backend": backend.slug,
        "requested_actions": ["twiddle"],
        "callback_url": "test",
        "will_notify": "False",
    }
    request = rf.post("/", data)
    request.user = user

    response = JobRequestCreate.as_view(get_github_api=FakeGitHubAPI)(
        request,
        org_slug=workspace.project.org.slug,
        project_slug=workspace.project.slug,
        workspace_slug=workspace.name,
    )

    assert response.status_code == 302, response.context_data["form"].errors
    assert response.url == workspace.get_logs_url()

    job_request = JobRequest.objects.first()
    assert job_request.created_by == user
    assert job_request.workspace == workspace
    assert job_request.backend.slug == backend.slug
    assert job_request.requested_actions == ["twiddle"]
    assert job_request.sha == "abc123"
    assert not job_request.will_notify
    assert not job_request.jobs.exists()
Пример #5
0
def test_jobrequestcreate_get_with_all_backends_removed(rf, settings, user):
    settings.DISABLE_CREATING_JOBS = True

    tpp = BackendFactory(slug="tpp")
    emis = BackendFactory(slug="emis")
    workspace = WorkspaceFactory()

    BackendMembershipFactory(backend=tpp, user=user)
    BackendMembershipFactory(backend=emis, user=user)
    ProjectMembershipFactory(project=workspace.project,
                             user=user,
                             roles=[ProjectDeveloper])

    request = rf.get("/")
    request.user = user

    with pytest.raises(Http404):
        JobRequestCreate.as_view()(
            request,
            org_slug=workspace.project.org.slug,
            project_slug=workspace.project.slug,
            workspace_slug=workspace.name,
        )
Пример #6
0
def test_jobrequestcreate_get_success(ref, rf, mocker, user):
    workspace = WorkspaceFactory()

    BackendMembershipFactory(user=user)
    ProjectMembershipFactory(project=workspace.project,
                             user=user,
                             roles=[ProjectDeveloper])

    dummy_yaml = """
    version: 3
    expectations:
      population_size: 1000
    actions:
      twiddle:
        run: test
        outputs:
          moderately_sensitive:
            cohort: /path/to/output.csv
    """
    mocker.patch(
        "jobserver.views.job_requests.get_project",
        autospec=True,
        return_value=dummy_yaml,
    )

    request = rf.get("/")
    request.user = user

    response = JobRequestCreate.as_view()(
        request,
        org_slug=workspace.project.org.slug,
        project_slug=workspace.project.slug,
        workspace_slug=workspace.name,
        ref=ref,
    )

    assert response.status_code == 200

    assert response.context_data["actions"] == [
        {
            "name": "twiddle",
            "needs": []
        },
        {
            "name": "run_all",
            "needs": ["twiddle"]
        },
    ]
    assert response.context_data["workspace"] == workspace
Пример #7
0
def test_jobrequestcreate_unknown_workspace(rf, user):
    org = user.orgs.first()
    project = ProjectFactory(org=org)

    request = rf.get("/")
    request.user = user
    response = JobRequestCreate.as_view()(
        request,
        org_slug=org.slug,
        project_slug=project.slug,
        workspace_slug="test",
    )

    assert response.status_code == 302
    assert response.url == "/"
Пример #8
0
def test_jobrequestcreate_get_with_some_backends_removed(
        rf, mocker, settings, user):
    settings.DISABLE_CREATING_JOBS = True

    backend = BackendFactory()
    emis = BackendFactory(slug="emis")
    tpp = BackendFactory(slug="tpp")

    workspace = WorkspaceFactory()

    BackendMembershipFactory(backend=backend, user=user)
    BackendMembershipFactory(backend=emis, user=user)
    BackendMembershipFactory(backend=tpp, user=user)
    ProjectMembershipFactory(project=workspace.project,
                             user=user,
                             roles=[ProjectDeveloper])

    dummy_yaml = """
    actions:
      twiddle:
    """
    mocker.patch(
        "jobserver.views.job_requests.get_project",
        autospec=True,
        return_value=dummy_yaml,
    )

    request = rf.get("/")
    request.user = user

    response = JobRequestCreate.as_view(get_github_api=FakeGitHubAPI)(
        request,
        org_slug=workspace.project.org.slug,
        project_slug=workspace.project.slug,
        workspace_slug=workspace.name,
    )

    assert response.status_code == 200

    # confirm we only have the one remaining backend in the form here
    assert response.context_data["form"]["backend"].field.choices == [
        (backend.slug, backend.name)
    ]
Пример #9
0
def test_jobrequestcreate_post_with_invalid_backend(rf, mocker, monkeypatch,
                                                    user):
    backend1 = BackendFactory()
    backend2 = BackendFactory()
    workspace = WorkspaceFactory()

    BackendMembershipFactory(backend=backend1, user=user)
    ProjectMembershipFactory(project=workspace.project,
                             user=user,
                             roles=[ProjectDeveloper])

    dummy_yaml = """
    actions:
      twiddle:
    """
    mocker.patch(
        "jobserver.views.job_requests.get_project",
        autospec=True,
        return_value=dummy_yaml,
    )

    data = {
        "backend": backend2.slug,
        "requested_actions": ["twiddle"],
        "callback_url": "test",
    }
    request = rf.post("/", data)
    request.user = user

    response = JobRequestCreate.as_view(get_github_api=FakeGitHubAPI)(
        request,
        org_slug=workspace.project.org.slug,
        project_slug=workspace.project.slug,
        workspace_slug=workspace.name,
    )

    assert response.status_code == 200
    assert response.context_data["form"].errors["backend"] == [
        f"Select a valid choice. {backend2.slug} is not one of the available choices."
    ]
Пример #10
0
def test_jobrequestcreate_with_archived_workspace(rf):
    user = UserFactory()
    workspace = WorkspaceFactory(is_archived=True)

    ProjectMembershipFactory(project=workspace.project,
                             user=user,
                             roles=[ProjectDeveloper])

    request = rf.get("/")
    request.session = "session"
    request._messages = FallbackStorage(request)
    request.user = user

    response = JobRequestCreate.as_view()(
        request,
        org_slug=workspace.project.org.slug,
        project_slug=workspace.project.slug,
        workspace_slug=workspace.name,
    )

    assert response.status_code == 302
    assert response.url == workspace.get_absolute_url()