Exemplo n.º 1
0
def test_create_release_note_not_exist(db, admin_client, entity):
    """
    Test that release notes may only be made for entities that exist
    """
    study = StudyFactory()
    study_id = to_global_id("StudyNode", study.kf_id)
    release = ReleaseFactory(studies=[study])
    release_id = to_global_id("ReleaseNode", release.kf_id)

    if entity == "study":
        study.delete()
    if entity == "release":
        release.delete()

    variables = {
        "input": {
            "description": "Test note",
            "study": study_id,
            "release": release_id,
        }
    }

    resp = admin_client.post(
        "/graphql",
        format="json",
        data={
            "query": CREATE_RELEASE_NOTE,
            "variables": variables
        },
    )

    assert "errors" in resp.json()
    error = resp.json()["errors"]
    assert "does not exist" in error[0]["message"]
Exemplo n.º 2
0
def test_list_all_permissions(db, test_client, user_type, expected):
    """
    ADMIN - Can query all releases
    DEV - Can query all releases
    USER - Can query published releases and releases that their studies are in
    anonomous - Can only query published releases
    """

    releases = ReleaseFactory.create_batch(10, state='staged')
    releases = ReleaseFactory.create_batch(10, state='published')

    client = test_client(user_type)
    resp = client.post("/graphql", data={"query": ALL_RELEASES})
    # Test that the correct number of releases are returned
    assert len(resp.json()["data"]["allReleases"]["edges"]) == expected
Exemplo n.º 3
0
def test_create_release_note_permissions(db, test_client, user_type, expected):
    """
    ADMIN - Can create new release notes
    DEV - Can create new release notes
    USER - May not create relase notes
    anonomous - May not create release notes
    """
    study = StudyFactory()
    study_id = to_global_id("StudyNode", study.kf_id)
    release = ReleaseFactory(studies=[study])
    release_id = to_global_id("ReleaseNode", release.kf_id)

    variables = {
        "input": {
            "description": "Test note",
            "study": study_id,
            "release": release_id,
        }
    }

    client = test_client(user_type)
    resp = client.post(
        "/graphql",
        format="json",
        data={
            "query": CREATE_RELEASE_NOTE,
            "variables": variables
        },
    )

    if expected:
        assert ("kfId"
                in resp.json()["data"]["createReleaseNote"]["releaseNote"])
    else:
        assert "errors" in resp.json()
Exemplo n.º 4
0
def test_create_release_note_study_not_in_release(db, admin_client):
    """
    Test that notes may only be created for releases that contain the desired
    study
    """
    study = StudyFactory()
    study_id = to_global_id("StudyNode", study.kf_id)
    # The release will not contain the above study
    release = ReleaseFactory()
    release_id = to_global_id("ReleaseNode", release.kf_id)

    variables = {
        "input": {
            "description": "Test note",
            "study": study_id,
            "release": release_id,
        }
    }

    resp = admin_client.post(
        "/graphql",
        format="json",
        data={
            "query": CREATE_RELEASE_NOTE,
            "variables": variables
        },
    )

    assert "errors" in resp.json()
    error = resp.json()["errors"]
    assert "is not in release" in error[0]["message"]
def test_update_release_permissions(db, test_client, user_type, expected):
    """
    ADMIN - Can update a new release
    DEV - Can update a new release
    USER - May not update relases
    anonomous - May not update releases
    """
    release = ReleaseFactory()

    variables = {
        "release": to_global_id("ReleaseNode", release.kf_id),
        "input": {
            "name": "Edited Title"
        },
    }

    client = test_client(user_type)
    resp = client.post(
        "/graphql",
        format="json",
        data={
            "query": UPDATE_RELEASE,
            "variables": variables
        },
    )

    print(resp.json())
    if expected:
        assert "kfId" in resp.json()["data"]["updateRelease"]["release"]
    else:
        assert "errors" in resp.json()
def test_publish_release_from_state(db, admin_client, mocker, state, allowed):
    """
    Test that a release may only be published from the staged state.
    """
    mock_rq = mocker.patch("coordinator.graphql.releases.django_rq")

    release = ReleaseFactory(state=state)
    variables = {"release": to_global_id("ReleaseNode", release.kf_id)}

    resp = admin_client.post(
        "/graphql",
        format="json",
        data={"query": PUBLISH_RELEASE, "variables": variables},
    )

    if allowed:
        # Check that the release task was queued
        assert mock_rq.enqueue.call_count == 1
        # The state shouldn't have changed yet as the publish task won't have
        # been executed
        assert (
            resp.json()["data"]["publishRelease"]["release"]["state"]
            == "staged"
        )
    else:
        assert "errors" in resp.json()
Exemplo n.º 7
0
def test_create_release_note(db, admin_client):
    """
    Test that releases are created correctly.
    """
    study = StudyFactory()
    study_id = to_global_id("StudyNode", study.kf_id)
    release = ReleaseFactory(studies=[study])
    release_id = to_global_id("ReleaseNode", release.kf_id)

    variables = {
        "input": {
            "description": "Test note",
            "study": study_id,
            "release": release_id,
        }
    }

    resp = admin_client.post(
        "/graphql",
        format="json",
        data={
            "query": CREATE_RELEASE_NOTE,
            "variables": variables
        },
    )

    release = resp.json()["data"]["createReleaseNote"]["releaseNote"]
    assert ReleaseNote.objects.count() == 1
    assert release["kfId"] == ReleaseNote.objects.first().kf_id
    assert release["author"] == "bobby"
def test_update_release(db, admin_client, mocker):
    """
    Test that releases are created correctly.
    """
    release = ReleaseFactory()

    variables = {
        "release": to_global_id("ReleaseNode", release.kf_id),
        "input": {
            "name": "Edited Title",
            "description": "Updated Description",
        },
    }

    resp = admin_client.post(
        "/graphql",
        format="json",
        data={
            "query": UPDATE_RELEASE,
            "variables": variables
        },
    )

    release = resp.json()["data"]["updateRelease"]["release"]
    assert release["kfId"] == Release.objects.first().kf_id
    assert release["name"] == "Edited Title"
    assert release["description"] == "Updated Description"

    assert Release.objects.get(kf_id=release["kfId"]).name == "Edited Title"
def test_subquery(db, admin_client):
    """
    Test that a study's releases may be subqueried correctly
    """
    study = StudyFactory()
    other_study = StudyFactory()
    release1 = ReleaseFactory(
        state="published", studies=[study], version="1.1.1"
    )
    release2 = ReleaseFactory(state="staged", studies=[study], version="1.1.2")
    # This release won't include the study of interest
    release3 = ReleaseFactory(
        state="published", studies=[other_study], version="1.1.3"
    )

    variables = {"state": "published", "kfId": study.kf_id}
    resp = admin_client.post(
        "/graphql",
        format="json",
        data={"query": ALL_STUDIES, "variables": variables},
    )

    # Test that the correct number of releases are returned
    assert len(resp.json()["data"]["allStudies"]["edges"]) == 1
    study = resp.json()["data"]["allStudies"]["edges"][0]["node"]
    assert len(study["releases"]["edges"]) == 1
    release = study["releases"]["edges"][0]["node"]
    assert release["version"] == "1.1.1"
    assert release["kfId"] == release1.kf_id

    variables = {"state": "published", "kfId": other_study.kf_id}
    resp = admin_client.post(
        "/graphql",
        format="json",
        data={"query": ALL_STUDIES, "variables": variables},
    )
    assert len(resp.json()["data"]["allStudies"]["edges"]) == 1
    study = resp.json()["data"]["allStudies"]["edges"][0]["node"]
    assert len(study["releases"]["edges"]) == 1
    release = study["releases"]["edges"][0]["node"]
    assert release["version"] == "1.1.3"
    assert release["kfId"] == release3.kf_id
Exemplo n.º 10
0
def test_list_all_events_permissions(db, test_client, user_type, expected):
    """
    ADMIN - Can query all events
    DEV - Can query all events
    USER - Can query all events for releases that have been published or which
        contain a study that the user is in the group of
    anonomous - Can query all events for releases that have been published
    """
    release_pub = ReleaseFactory(state="published")
    EventFactory.create_batch(10, release=release_pub)
    release_staged = ReleaseFactory(state="staged")
    EventFactory.create_batch(10, release=release_staged)
    study = StudyFactory(kf_id="SD_00000001")
    release_user = ReleaseFactory(state="staged", studies=[study])
    EventFactory.create_batch(5, release=release_user)

    client = test_client(user_type)
    resp = client.post("/graphql", data={"query": ALL_EVENTS})
    # Test that the correct number of releases are returned
    assert len(resp.json()["data"]["allEvents"]["edges"]) == expected()
Exemplo n.º 11
0
def test_list_all_tasks_permissions(db, test_client, user_type, expected):
    """
    ADMIN - Can query all tasks
    DEV - Can query all tasks
    USER - Can query tasks from published releases, or releases that they
           have a study in.
    anonomous - Can query tasks from published releases
    """
    study = Study(kf_id="SD_00000001")
    study.save()

    releases = ReleaseFactory.create_batch(10, state="staged")
    releases = ReleaseFactory.create_batch(10, state="published")
    releases = ReleaseFactory.create_batch(
        10, state="staging", studies=[study]
    )

    client = test_client(user_type)
    resp = client.post("/graphql", data={"query": ALL_TASKS})
    # Test that the correct number of releases are returned
    assert len(resp.json()["data"]["allTasks"]["edges"]) == expected()
def test_list_all_permissions(db, test_client, user_type, expected):
    """
    ADMIN - Can query all release notes
    DEV - Can query all release notes
    USER - Can query release notes from published releases and releases that
        their studies are in
    ANON - Can only query release notes from published release notes
    """
    study = StudyFactory(kf_id="SD_00000001")
    release_study = ReleaseFactory(state="staging", studies=[study])
    release_staged = ReleaseFactory(state="staged")
    release_pub = ReleaseFactory(state="published")

    release_notes = ReleaseNoteFactory.create_batch(10, release=release_staged)
    releases_notes = ReleaseNoteFactory.create_batch(10, release=release_pub)
    releases_notes = ReleaseNoteFactory.create_batch(10,
                                                     release=release_study,
                                                     study=study)

    client = test_client(user_type)
    resp = client.post("/graphql", data={"query": ALL_RELEASE_NOTES})
    # Test that the correct number of release notes are returned
    assert len(resp.json()["data"]["allReleaseNotes"]["edges"]) == expected
def test_list_all_tasks_permissions(db, test_client, user_type, expected):
    """
    ADMIN - Can query all task services
    DEV - Can query all task services
    USER - Cannot query any task services
    anonomous - Cannot query any task services

    The TaskServiceFactory maxes at 3 unique task services, so we should never
    expect more than than.
    """
    study = Study(kf_id="SD_00000001")
    study.save()

    releases = ReleaseFactory.create_batch(10, state="staged")
    releases = ReleaseFactory.create_batch(10, state="published")
    releases = ReleaseFactory.create_batch(10,
                                           state="staging",
                                           studies=[study])

    client = test_client(user_type)
    resp = client.post("/graphql", data={"query": ALL_TASK_SERVICES})
    # Test that the correct number of releases are returned
    assert len(resp.json()["data"]["allTaskServices"]["edges"]) == expected()
def test_release_permissions(test_client, user_type, endpoint, method,
                             status_code):
    release = ReleaseFactory()
    endpoint = endpoint.replace("<kf_id>", release.kf_id)
    body = None
    if method in ["post", "patch", "put"]:
        body = {
            "name": release.name,
            "description": release.description,
            "studies": [release.studies.first().kf_id],
        }
    client = test_client(user_type)
    call = getattr(client, method)
    resp = call(endpoint, data=body)
    assert resp.status_code == status_code
Exemplo n.º 15
0
def test_cancel_permissions(db, test_client, user_type, expected):
    """
    ADMIN - Can cancel a release
    DEV - Can cancel a release
    USER - May not cancel relases
    anonomous - May not cancel releases
    """
    release = ReleaseFactory(state="running")
    variables = {"release": to_global_id("ReleaseNode", release.kf_id)}

    client = test_client(user_type)
    resp = client.post(
        "/graphql",
        format="json",
        data={"query": CANCEL_RELEASE, "variables": variables},
    )

    if expected:
        assert "kfId" in resp.json()["data"]["cancelRelease"]["release"]
    else:
        assert "errors" in resp.json()
        assert "Not authenticated" in resp.json()["errors"][0]["message"]
Exemplo n.º 16
0
def test_cancel_release_from_state(db, admin_client, mocker, state, allowed):
    """
    Test that a release is canceled correctly from any state
    """
    mock_rq = mocker.patch("coordinator.graphql.releases.django_rq")

    release = ReleaseFactory(state=state)
    variables = {"release": to_global_id("ReleaseNode", release.kf_id)}

    resp = admin_client.post(
        "/graphql",
        format="json",
        data={"query": CANCEL_RELEASE, "variables": variables},
    )

    if allowed:
        # Check that the release task was queued
        assert mock_rq.enqueue.call_count == 1
        assert (
            resp.json()["data"]["cancelRelease"]["release"]["state"]
            == "canceling"
        )
    else:
        assert "errors" in resp.json()
Exemplo n.º 17
0
 def handle(self, *args, **options):
     ReleaseFactory.create_batch(100)
     self.stdout.write(self.style.SUCCESS("Created releases"))