def test_workspacedetail_get_with_authenticated_user(rf):
    """
    Check GlobalWorkspaceDetail renders the controls for Archiving, Notifications,
    and selecting Actions for authenticated Users.
    """
    workspace = WorkspaceFactory(is_archived=False)

    # Build a RequestFactory instance
    request = rf.get(MEANINGLESS_URL)
    request.user = UserFactory(is_superuser=False, roles=[])

    dummy_yaml = """
    actions:
      twiddle:
    """
    with patch("jobserver.views.workspaces.can_run_jobs",
               return_value=True,
               autospec=True), patch("jobserver.views.workspaces.get_project",
                                     new=lambda *args: dummy_yaml):
        response = GlobalWorkspaceDetail.as_view()(request,
                                                   name=workspace.name)

    assert "Archive" in response.rendered_content
    assert "Turn Notifications" in response.rendered_content
    assert "twiddle" in response.rendered_content
    assert "Pick a backend to run your Jobs in" not in response.rendered_content
def test_workspacedetail_get_success(rf):
    workspace = WorkspaceFactory()
    user = UserFactory()

    # Build a RequestFactory instance
    request = rf.get(MEANINGLESS_URL)
    request.user = user

    dummy_yaml = """
    actions:
      twiddle:
    """
    with patch("jobserver.views.workspaces.can_run_jobs",
               return_value=True,
               autospec=True), patch("jobserver.views.workspaces.get_project",
                                     new=lambda *args: dummy_yaml):
        response = GlobalWorkspaceDetail.as_view()(request,
                                                   name=workspace.name)

    assert response.status_code == 200

    assert response.context_data["actions"] == [
        {
            "name": "twiddle",
            "needs": [],
            "status": "-"
        },
        {
            "name": "run_all",
            "needs": ["twiddle"],
            "status": "-"
        },
    ]
    assert response.context_data["branch"] == workspace.branch
def test_workspacedetail_unknown_workspace(rf):
    # Build a RequestFactory instance
    request = rf.get(MEANINGLESS_URL)
    request.user = UserFactory()
    response = GlobalWorkspaceDetail.as_view()(request, name="test")

    assert response.status_code == 302
    assert response.url == "/"
def test_workspacedetail_get_with_unauthenticated_user(rf):
    """
    Check GlobalWorkspaceDetail does not render the controls for Archiving,
    Notifications, and selecting Actions for unauthenticated Users.
    """
    workspace = WorkspaceFactory(is_archived=False)

    # Build a RequestFactory instance
    request = rf.get(MEANINGLESS_URL)
    request.user = AnonymousUser()
    response = GlobalWorkspaceDetail.as_view()(request, name=workspace.name)

    assert "Archive" not in response.rendered_content
    assert "Turn Notifications" not in response.rendered_content
    assert "twiddle" not in response.rendered_content
def test_workspacedetail_post_archived_workspace(rf):
    workspace = WorkspaceFactory(is_archived=True)

    request = rf.post(MEANINGLESS_URL)
    request.session = "session"
    request._messages = FallbackStorage(request)
    request.user = UserFactory()

    with patch("jobserver.views.workspaces.can_run_jobs",
               return_value=True,
               autospec=True), patch("jobserver.views.workspaces.get_actions",
                                     return_value=[],
                                     autospec=True):
        response = GlobalWorkspaceDetail.as_view()(request,
                                                   name=workspace.name)

    assert response.status_code == 302
    assert response.url == workspace.get_absolute_url()
def test_workspacedetail_logged_out(rf):
    workspace = WorkspaceFactory()

    # Build a RequestFactory instance
    request = rf.get(MEANINGLESS_URL)
    request.user = AnonymousUser()

    with patch("jobserver.views.workspaces.get_actions",
               autospec=True) as mocked_get_actions:
        response = GlobalWorkspaceDetail.as_view()(request,
                                                   name=workspace.name)

    mocked_get_actions.assert_not_called()

    assert response.status_code == 200

    assert response.context_data["actions"] == []
    assert response.context_data["branch"] == workspace.branch
def test_workspacedetail_post_success_with_superuser(rf, monkeypatch,
                                                     superuser):
    monkeypatch.setenv("BACKENDS", "tpp,emis")

    workspace = WorkspaceFactory()
    user = superuser

    data = {
        "backend": "emis",
        "requested_actions": ["twiddle"],
        "callback_url": "test",
    }

    # Build a RequestFactory instance
    request = rf.post(MEANINGLESS_URL, data)
    request.user = user

    dummy_yaml = """
    actions:
      twiddle:
    """
    with patch("jobserver.views.workspaces.can_run_jobs",
               return_value=True,
               autospec=True), patch(
                   "jobserver.views.workspaces.get_project",
                   new=lambda *args: dummy_yaml), patch(
                       "jobserver.views.workspaces.get_branch_sha",
                       new=lambda r, b: "abc123"):
        response = GlobalWorkspaceDetail.as_view()(request,
                                                   name=workspace.name)

    assert response.status_code == 302, response.context_data["form"].errors
    assert response.url == reverse("workspace-logs",
                                   kwargs={"name": workspace.name})

    job_request = JobRequest.objects.first()
    assert job_request.created_by == user
    assert job_request.workspace == workspace
    assert job_request.backend.name == "emis"
    assert job_request.requested_actions == ["twiddle"]
    assert job_request.sha == "abc123"
    assert not job_request.jobs.exists()
def test_workspacedetail_get_with_superuser(rf, superuser):
    """Check GlobalWorkspaceDetail renders the Backend radio buttons for superusers"""
    workspace = WorkspaceFactory(is_archived=False)

    # Build a RequestFactory instance
    request = rf.get(MEANINGLESS_URL)
    request.user = superuser

    dummy_yaml = """
    actions:
      twiddle:
    """
    with patch("jobserver.views.workspaces.can_run_jobs",
               return_value=True,
               autospec=True), patch("jobserver.views.workspaces.get_project",
                                     new=lambda *args: dummy_yaml):
        response = GlobalWorkspaceDetail.as_view()(request,
                                                   name=workspace.name)

    assert "Pick a backend to run your Jobs in" in response.rendered_content
def test_workspacedetail_redirects_with_project_url(rf):
    org = OrgFactory()
    project = ProjectFactory(org=org)
    workspace = WorkspaceFactory(project=project)

    request = rf.get(MEANINGLESS_URL)
    response = GlobalWorkspaceDetail.as_view()(
        request,
        org_slug=org.slug,
        project_slug=project.slug,
        workspace_slug=workspace.name,
    )

    assert response.status_code == 302
    assert response.url == reverse(
        "project-workspace-detail",
        kwargs={
            "org_slug": org.slug,
            "project_slug": project.slug,
            "workspace_slug": workspace.name,
        },
    )
def test_workspacedetail_project_yaml_errors(rf):
    workspace = WorkspaceFactory()
    user = UserFactory()

    # Build a RequestFactory instance
    request = rf.get(MEANINGLESS_URL)
    request.user = user

    with patch("jobserver.views.workspaces.can_run_jobs",
               return_value=True,
               autospec=True), patch(
                   "jobserver.views.workspaces.get_project",
                   side_effect=Exception("test error"),
                   autospec=True,
               ):
        response = GlobalWorkspaceDetail.as_view()(request,
                                                   name=workspace.name)

    assert response.status_code == 200

    assert response.context_data["actions"] == []
    assert response.context_data["actions_error"] == "test error"