Beispiel #1
0
def test_read_notebook_revisions(fake_user, two_test_notebooks, client):
    test_notebook = two_test_notebooks[0]

    # add another revision to the main notebook that we are testing
    NotebookRevision.objects.create(notebook=test_notebook,
                                    title="Revision 2",
                                    content="*fake notebook content 2*")

    # add a revision for another notebook, to make sure that doesn't get mixed in
    NotebookRevision.objects.create(notebook=two_test_notebooks[1],
                                    title="Revision for another notebook",
                                    content="*fake notebook 2 content 2*")

    # verify that we can list notebook revisions and that we only get what we
    # expect in the expected order (latest first)
    resp = client.get(reverse('notebook-revisions-list', kwargs={'notebook_id': test_notebook.id}))
    assert resp.status_code == 200
    assert resp.json() == [{
        'created': get_rest_framework_time_string(revision.created),
        'id': revision.id,
        'title': revision.title
    } for revision in NotebookRevision.objects.filter(notebook=test_notebook)]

    # verify that we can get the details of a single revision as well
    test_revision = NotebookRevision.objects.filter(notebook=test_notebook).first()
    resp = client.get(reverse('notebook-revisions-detail', kwargs={
        'notebook_id': test_notebook.id,
        'pk': test_revision.id
    }))
    assert resp.json() == {
        'content': test_revision.content,
        'created': get_rest_framework_time_string(test_revision.created),
        'id': test_revision.id,
        'title': test_revision.title
    }
def test_read_notebook_revisions(fake_user, two_test_notebooks_and_revisions,
                                 client):
    test_notebook = two_test_notebooks_and_revisions[0]

    # verify that we can list notebook revisions and that we only get what we
    # expect in the expected order (latest first)
    resp = client.get(
        reverse("notebook-revisions-list",
                kwargs={"notebook_id": test_notebook.id}))
    assert resp.status_code == 200
    assert resp.json() == [{
        "created":
        get_rest_framework_time_string(revision.created),
        "id":
        revision.id,
        "title":
        revision.title,
    } for revision in NotebookRevision.objects.filter(notebook=test_notebook)]

    # verify that the full parameter gives us the content for each revision
    # as well
    resp = client.get(
        reverse("notebook-revisions-list",
                kwargs={"notebook_id": test_notebook.id}) + "?full=1")
    assert resp.status_code == 200
    assert resp.json() == [{
        "content":
        revision.content,
        "created":
        get_rest_framework_time_string(revision.created),
        "id":
        revision.id,
        "title":
        revision.title,
    } for revision in NotebookRevision.objects.filter(notebook=test_notebook)]

    # verify that we can get the details of a single revision as well
    test_revision = NotebookRevision.objects.filter(
        notebook=test_notebook).first()
    resp = client.get(
        reverse(
            "notebook-revisions-detail",
            kwargs={
                "notebook_id": test_notebook.id,
                "pk": test_revision.id
            },
        ))
    assert resp.json() == {
        "content": test_revision.content,
        "created": get_rest_framework_time_string(test_revision.created),
        "id": test_revision.id,
        "title": test_revision.title,
    }
Beispiel #3
0
def test_post_to_file_api(fake_user, client, test_notebook):
    client.force_login(user=fake_user)
    with tempfile.NamedTemporaryFile(mode="w+") as f:
        f.write("hello")
        f.seek(0)
        resp = client.post(
            reverse("files-list"),
            {
                "metadata":
                json.dumps({
                    "filename": "my cool file.csv",
                    "notebook_id": test_notebook.id
                }),
                "file":
                open(f.name),
            },
        )
        assert resp.status_code == 201
        assert File.objects.count() == 1
        created_file = File.objects.get(id=1)
        assert created_file.content.tobytes() == b"hello"
        assert resp.json() == {
            "id":
            created_file.id,
            "last_updated":
            get_rest_framework_time_string(created_file.last_updated),
            "filename":
            "my cool file.csv",
            "notebook_id":
            test_notebook.id,
        }
Beispiel #4
0
def test_put_to_file_api(fake_user, api_client, test_notebook, test_file):
    # we use the django rest framework's api client for this test, as it
    # lets us use the same payload for a put request as for post
    api_client.force_authenticate(user=fake_user)
    with tempfile.NamedTemporaryFile(mode="w+") as f:
        f.write("new-information")
        f.seek(0)
        resp = api_client.put(
            reverse("files-detail", kwargs={"pk": test_file.id}),
            {
                "metadata":
                json.dumps({
                    "filename": "test-2.csv",
                    "notebook_id": test_notebook.id
                }),
                "file":
                open(f.name),
            },
        )
        assert resp.status_code == 201
        assert File.objects.count() == 1
        updated_file = File.objects.get(id=test_file.id)
        assert updated_file.content.tobytes() == b"new-information"
        assert updated_file.content.tobytes() != test_file.content
        assert resp.json() == {
            "id":
            updated_file.id,
            "last_updated":
            get_rest_framework_time_string(updated_file.last_updated),
            "filename":
            "test-2.csv",
            "notebook_id":
            test_notebook.id,
        }
def test_read_multiple_revisions(fake_user, test_notebook, client):
    """
    tests that reading multiple revisions at the same time (but not all of
    them) works
    """
    secondary_revisions = [
        NotebookRevision.objects.create(
            notebook=test_notebook,
            title="Revision %s" % i,
            content="*fake notebook content %s*" % i,
        ) for i in range(2, 4)
    ]
    secondary_revisions.reverse()
    url = (
        reverse("notebook-revisions-list",
                kwargs={"notebook_id": test_notebook.id}) + "?" +
        "&".join(
            ["id=%s" % str(revision.id) for revision in secondary_revisions]))

    resp = client.get(url)
    assert resp.status_code == 200
    assert resp.json() == [{
        "created":
        get_rest_framework_time_string(revision.created),
        "id":
        revision.id,
        "title":
        revision.title,
    } for revision in secondary_revisions]
def test_create_notebook_revision(fake_user, test_notebook, client):
    last_revision = NotebookRevision.objects.filter(
        notebook_id=test_notebook.id).first()
    post_blob = {
        "parent_revision_id": last_revision.id,
        "title": "My cool notebook",
        "content": "*modified test content",
    }

    # should be able to create a revision if we are the owner
    client.force_login(user=fake_user)
    resp = client.post(
        reverse("notebook-revisions-list",
                kwargs={"notebook_id": test_notebook.id}), post_blob)
    assert resp.status_code == 201
    assert NotebookRevision.objects.count() == 2
    new_notebook_revision = NotebookRevision.objects.first()
    assert new_notebook_revision.title == post_blob["title"]
    assert new_notebook_revision.content == post_blob["content"]

    # make sure that the title gets updated too
    test_notebook.refresh_from_db()
    assert test_notebook.title == post_blob["title"]

    # also validate that the response is what we expect
    assert resp.json() == {
        "content": post_blob["content"],
        "created":
        get_rest_framework_time_string(new_notebook_revision.created),
        "id": new_notebook_revision.id,
        "title": post_blob["title"],
    }
Beispiel #7
0
def test_create_notebook_logged_in(fake_user, client, notebook_post_blob,
                                   specify_owner):
    # should be able to create notebook if logged in
    client.force_login(user=fake_user)
    post_blob = {**notebook_post_blob}
    if specify_owner:
        post_blob.update({"owner": fake_user.username})
    resp = client.post(reverse("notebooks-list"), post_blob)
    assert resp.status_code == 201
    assert Notebook.objects.count() == 1
    notebook = Notebook.objects.first()
    assert notebook.title == post_blob["title"]
    assert notebook.owner == fake_user
    notebook_revision = NotebookRevision.objects.get(notebook=notebook)

    # the response should also contain the revision id and other
    # data that we want
    assert resp.json() == {
        "forked_from": None,
        "id": notebook.id,
        "owner": fake_user.username,
        "title": notebook.title,
        "latest_revision": {
            "content": notebook_revision.content,
            "created":
            get_rest_framework_time_string(notebook_revision.created),
            "id": notebook_revision.id,
            "title": notebook_revision.title,
        },
    }
    # should have a first revision to go along with the new notebook
    assert NotebookRevision.objects.count() == 1
Beispiel #8
0
def test_post_to_file_api(fake_user, client, test_notebook):
    client.force_login(user=fake_user)
    with tempfile.NamedTemporaryFile(mode='w+') as f:
        f.write('hello')
        f.seek(0)
        resp = client.post(
            reverse('files-list'), {
                'metadata':
                json.dumps({
                    'filename': 'my cool file.csv',
                    'notebook_id': test_notebook.id
                }),
                'file':
                open(f.name)
            })
        assert resp.status_code == 201
        assert File.objects.count() == 1
        created_file = File.objects.get(id=1)
        assert created_file.content.tobytes() == b'hello'
        assert resp.json() == {
            'id':
            created_file.id,
            'last_updated':
            get_rest_framework_time_string(created_file.last_updated),
            'filename':
            'my cool file.csv',
            'notebook_id':
            test_notebook.id
        }
Beispiel #9
0
def test_put_to_file_api(fake_user, api_client, test_notebook, test_file):
    # we use the django rest framework's api client for this test, as it
    # lets us use the same payload for a put request as for post
    api_client.force_authenticate(user=fake_user)
    with tempfile.NamedTemporaryFile(mode='w+') as f:
        f.write('new-information')
        f.seek(0)
        resp = api_client.put(
            reverse('files-detail', kwargs={'pk': test_file.id}), {
                'metadata':
                json.dumps({
                    'filename': 'test-2.csv',
                    'notebook_id': test_notebook.id
                }),
                'file':
                open(f.name)
            })
        assert resp.status_code == 201
        assert File.objects.count() == 1
        updated_file = File.objects.get(id=test_file.id)
        assert updated_file.content.tobytes() == b'new-information'
        assert updated_file.content.tobytes() != test_file.content
        assert resp.json() == {
            'id':
            updated_file.id,
            'last_updated':
            get_rest_framework_time_string(updated_file.last_updated),
            'filename':
            'test-2.csv',
            'notebook_id':
            test_notebook.id
        }
def test_read_multiple_revisions(fake_user, test_notebook, client):
    '''
    tests that reading multiple revisions at the same time (but not all of
    them) works
    '''
    secondary_revisions = [
        NotebookRevision.objects.create(notebook=test_notebook,
                                        title="Revision %s" % i,
                                        content="*fake notebook content %s*" %
                                        i) for i in range(2, 4)
    ]
    secondary_revisions.reverse()
    url = (
        reverse('notebook-revisions-list',
                kwargs={'notebook_id': test_notebook.id}) + '?' +
        '&'.join(
            ['id=%s' % str(revision.id) for revision in secondary_revisions]))

    resp = client.get(url)
    assert resp.status_code == 200
    assert resp.json() == [{
        'created':
        get_rest_framework_time_string(revision.created),
        'id':
        revision.id,
        'title':
        revision.title
    } for revision in secondary_revisions]
Beispiel #11
0
def test_notebook_detail(client, test_notebook):
    initial_revision = NotebookRevision.objects.filter(
        notebook=test_notebook).last()
    resp = client.get(
        reverse('notebooks-detail', kwargs={'pk': test_notebook.id}))
    assert resp.status_code == 200
    assert resp.json() == {
        "id": test_notebook.id,
        "owner": "testuser1",
        "title": initial_revision.title,
        "forked_from": None,
        "latest_revision": {
            "content": initial_revision.content,
            "created":
            get_rest_framework_time_string(initial_revision.created),
            "id": initial_revision.id,
            "title": initial_revision.title
        }
    }

    # add another revision, make sure all return values are updated
    # appropriately
    new_revision = NotebookRevision.objects.create(
        notebook=test_notebook,
        title="Second revision",
        content="*updated fake notebook content*")
    resp = client.get(
        reverse('notebooks-detail', kwargs={'pk': test_notebook.id}))
    assert resp.status_code == 200
    assert resp.json() == {
        "id": test_notebook.id,
        "owner": "testuser1",
        "title": "Second revision",
        "forked_from": None,
        "latest_revision": {
            "content": new_revision.content,
            "created": get_rest_framework_time_string(new_revision.created),
            "id": new_revision.id,
            "title": new_revision.title
        }
    }
Beispiel #12
0
def test_post_to_file_api(fake_user, client, test_notebook):
    client.force_login(user=fake_user)
    with tempfile.NamedTemporaryFile(mode="w+") as f:
        resp = post_file(f, client, test_notebook)
        assert resp.status_code == 201
        assert File.objects.count() == 1
        created_file = File.objects.get(id=1)
        assert created_file.content.tobytes() == b"hello"
        assert resp.json() == {
            "id":
            created_file.id,
            "last_updated":
            get_rest_framework_time_string(created_file.last_updated),
            "filename":
            "my cool file.csv",
            "notebook_id":
            test_notebook.id,
        }
Beispiel #13
0
def test_put_to_file_api(fake_user, api_client, test_notebook, test_file):
    # we use the django rest framework's api client for this test, as it
    # lets us use the same payload for a put request as for post
    api_client.force_authenticate(user=fake_user)
    with tempfile.NamedTemporaryFile(mode="w+") as f:
        resp = put_file(f, api_client, test_file, test_notebook)
        assert resp.status_code == 201
        assert File.objects.count() == 1
        updated_file = File.objects.get(id=test_file.id)
        assert updated_file.content.tobytes() == b"new-information"
        assert updated_file.content.tobytes() != test_file.content
        assert resp.json() == {
            "id":
            updated_file.id,
            "last_updated":
            get_rest_framework_time_string(updated_file.last_updated),
            "filename":
            "test-2.csv",
            "notebook_id":
            test_notebook.id,
        }
Beispiel #14
0
def test_read_notebook_revisions(fake_user, two_test_notebooks, client):
    test_notebook = two_test_notebooks[0]

    # add another revision to the main notebook that we are testing
    NotebookRevision.objects.create(notebook=test_notebook,
                                    title="Revision 2",
                                    content="*fake notebook content 2*")

    # add a revision for another notebook, to make sure that doesn't get mixed in
    NotebookRevision.objects.create(
        notebook=two_test_notebooks[1],
        title="Revision for another notebook",
        content="*fake notebook 2 content 2*",
    )

    # verify that we can list notebook revisions and that we only get what we
    # expect in the expected order (latest first)
    resp = client.get(
        reverse("notebook-revisions-list",
                kwargs={"notebook_id": test_notebook.id}))
    assert resp.status_code == 200
    assert resp.json() == [{
        "created":
        get_rest_framework_time_string(revision.created),
        "id":
        revision.id,
        "title":
        revision.title,
    } for revision in NotebookRevision.objects.filter(notebook=test_notebook)]

    # verify that the full parameter gives us the content for each revision
    # as well
    resp = client.get(
        reverse("notebook-revisions-list",
                kwargs={"notebook_id": test_notebook.id}) + "?full=1")
    assert resp.status_code == 200
    assert resp.json() == [{
        "content":
        revision.content,
        "created":
        get_rest_framework_time_string(revision.created),
        "id":
        revision.id,
        "title":
        revision.title,
    } for revision in NotebookRevision.objects.filter(notebook=test_notebook)]

    # verify that we can get the details of a single revision as well
    test_revision = NotebookRevision.objects.filter(
        notebook=test_notebook).first()
    resp = client.get(
        reverse(
            "notebook-revisions-detail",
            kwargs={
                "notebook_id": test_notebook.id,
                "pk": test_revision.id
            },
        ))
    assert resp.json() == {
        "content": test_revision.content,
        "created": get_rest_framework_time_string(test_revision.created),
        "id": test_revision.id,
        "title": test_revision.title,
    }