Пример #1
0
def test_releaseworkspaceapi_get_with_anonymous_user(api_rf):
    workspace = WorkspaceFactory()

    request = api_rf.get("/")

    response = ReleaseWorkspaceAPI.as_view()(request, workspace_name=workspace.name)

    assert response.status_code == 403
Пример #2
0
def test_releaseworkspaceapi_post_without_backend_token(api_rf):
    workspace = WorkspaceFactory()

    request = api_rf.post("/")

    response = ReleaseWorkspaceAPI.as_view()(request, workspace_name=workspace.name)

    assert response.status_code == 403
Пример #3
0
def test_releaseworkspaceapi_post_with_bad_backend_token(api_rf):
    workspace = WorkspaceFactory()
    BackendFactory(auth_token="test")

    request = api_rf.post("/", HTTP_AUTHORIZATION="invalid")

    response = ReleaseWorkspaceAPI.as_view()(request, workspace_name=workspace.name)

    assert response.status_code == 403
Пример #4
0
def test_releaseworkspaceapi_get_without_permission(api_rf):
    workspace = WorkspaceFactory()

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

    response = ReleaseWorkspaceAPI.as_view()(request, workspace_name=workspace.name)

    assert response.status_code == 403
Пример #5
0
def test_releaseworkspaceapi_get_with_permission(api_rf, build_release_with_files):
    workspace = WorkspaceFactory()
    backend1 = BackendFactory(slug="backend1")
    backend2 = BackendFactory(slug="backend2")
    user = UserFactory()
    ProjectMembershipFactory(
        user=user, project=workspace.project, roles=[ProjectCollaborator]
    )

    # two release for same filename but different content
    release1 = build_release_with_files(
        ["file1.txt"], workspace=workspace, backend=backend1, created_by=user
    )
    rfile1 = release1.files.first()
    release2 = build_release_with_files(
        ["file1.txt"], workspace=workspace, backend=backend2, created_by=user
    )
    rfile2 = release2.files.first()

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

    response = ReleaseWorkspaceAPI.as_view()(request, workspace_name=workspace.name)

    assert response.status_code == 200
    assert response.data == {
        "files": [
            {
                "name": "backend2/file1.txt",
                "id": rfile2.pk,
                "url": f"/api/v2/releases/file/{rfile2.id}",
                "user": rfile2.created_by.username,
                "date": rfile2.created_at.isoformat(),
                "size": rfile2.size,
                "sha256": rfile2.filehash,
                "is_deleted": False,
                "backend": release2.backend.name,
                "metadata": None,
                "review": None,
            },
            {
                "name": "backend1/file1.txt",
                "id": rfile1.pk,
                "url": f"/api/v2/releases/file/{rfile1.id}",
                "user": rfile1.created_by.username,
                "date": rfile1.created_at.isoformat(),
                "size": rfile1.size,
                "sha256": rfile1.filehash,
                "is_deleted": False,
                "backend": release1.backend.name,
                "metadata": None,
                "review": None,
            },
        ],
    }
Пример #6
0
def test_releaseworkspaceapi_post_create_release(api_rf, slack_messages):
    user = UserFactory(roles=[OutputChecker])
    workspace = WorkspaceFactory()
    ProjectMembershipFactory(user=user, project=workspace.project)

    backend = BackendFactory(auth_token="test", name="test-backend")
    BackendMembershipFactory(backend=backend, user=user)

    assert Release.objects.count() == 0

    data = {
        "files": [
            {
                "name": "file1.txt",
                "url": "url",
                "size": 7,
                "sha256": "hash",
                "date": timezone.now(),
                "metadata": {},
                "review": None,
            }
        ],
        "metadata": {},
        "review": None,
    }
    request = api_rf.post(
        "/",
        data=data,
        format="json",
        HTTP_AUTHORIZATION="test",
        HTTP_OS_USER=user.username,
    )

    response = ReleaseWorkspaceAPI.as_view()(request, workspace_name=workspace.name)

    assert response.status_code == 201, response.data
    assert Release.objects.count() == 1

    release = Release.objects.first()
    assert response["Release-Id"] == str(release.id)
    assert response["Location"] == f"http://testserver{release.get_api_url()}"

    assert len(slack_messages) == 1
    text, channel = slack_messages[0]

    assert channel == "opensafely-releases"
    assert f"{user.get_staff_url()}|{user.name}>" in text
    assert f"{release.get_absolute_url()}|release>" in text
    assert f"{workspace.get_absolute_url()}|{workspace.name}>" in text
    assert backend.name in text
Пример #7
0
def test_releaseworkspaceapi_post_release_already_exists(api_rf):
    user = UserFactory(roles=[OutputChecker])

    release = ReleaseFactory()
    rfile = ReleaseFileFactory(
        release=release,
        created_by=user,
        name="file.txt",
        filehash="hash",
    )

    BackendMembershipFactory(backend=release.backend, user=user)
    ProjectMembershipFactory(project=release.workspace.project, user=user)

    data = {
        "files": [
            {
                "name": rfile.name,
                "url": "url",
                "size": 7,
                "sha256": rfile.filehash,
                "date": timezone.now(),
                "metadata": {},
                "review": None,
            }
        ],
        "metadata": {},
        "review": {},
    }
    request = api_rf.post(
        "/",
        data=data,
        format="json",
        HTTP_AUTHORIZATION=release.backend.auth_token,
        HTTP_OS_USER=user.username,
    )

    response = ReleaseWorkspaceAPI.as_view()(
        request, workspace_name=release.workspace.name
    )

    assert response.status_code == 400
    assert "file.txt" in response.data["detail"]
    assert "already been uploaded" in response.data["detail"]
Пример #8
0
def test_releaseworkspaceapi_post_with_bad_json(api_rf):
    user = UserFactory(roles=[OutputChecker])
    workspace = WorkspaceFactory()
    ProjectMembershipFactory(user=user, project=workspace.project)

    backend = BackendFactory(auth_token="test")
    BackendMembershipFactory(backend=backend, user=user)

    request = api_rf.post(
        "/",
        content_type="application/json",
        data=json.dumps({}),
        HTTP_CONTENT_DISPOSITION="attachment; filename=release.zip",
        HTTP_AUTHORIZATION="test",
        HTTP_OS_USER=user.username,
    )

    response = ReleaseWorkspaceAPI.as_view()(request, workspace_name=workspace.name)

    assert response.status_code == 400
Пример #9
0
     SnapshotAPI.as_view(),
     name="snapshot",
 ),
 path(
     "workspaces/<str:workspace_id>/snapshots/<int:snapshot_id>/publish",
     SnapshotPublishAPI.as_view(),
     name="snapshot-publish",
 ),
 path(
     "workspaces/<str:workspace_id>/status",
     WorkspaceStatusAPI.as_view(),
     name="workspace-status",
 ),
 path(
     "releases/workspace/<str:workspace_name>",
     ReleaseWorkspaceAPI.as_view(),
     name="release-workspace",
 ),
 path(
     "releases/release/<str:release_id>",
     ReleaseAPI.as_view(),
     name="release",
 ),
 path(
     "releases/release/<str:release_id>/reviews",
     ReviewAPI.as_view(),
     name="release-review",
 ),
 path(
     "releases/file/<file_id>",
     ReleaseFileAPI.as_view(),
Пример #10
0
def test_releaseworkspaceapi_post_unknown_workspace(api_rf):
    request = api_rf.post("/")

    response = ReleaseWorkspaceAPI.as_view()(request, workspace_name="")

    assert response.status_code == 404