def test_network_rest_delete(
    workspace: Workspace,
    user: User,
    authenticated_api_client: APIClient,
    permission: WorkspaceRoleChoice,
    is_owner: bool,
    status_code: int,
    success: bool,
):
    """Tests deleting a network on a workspace for which the user is at least a writer."""
    if permission is not None:
        workspace.set_user_permission(user, permission)
    elif is_owner:
        workspace.set_owner(user)
    network = populated_network(workspace)
    r = authenticated_api_client.delete(
        f'/api/workspaces/{workspace.name}/networks/{network.name}/')

    assert r.status_code == status_code

    if success:
        # Assert relevant objects are deleted
        assert not Network.objects.filter(name=network.name).exists()
        assert not workspace.get_arango_db().has_graph(network.name)
    else:
        # Assert objects are not deleted
        assert Network.objects.filter(name=network.name).exists()
        assert workspace.get_arango_db().has_graph(network.name)
def test_table_rest_delete(
    table_factory: TableFactory,
    workspace: Workspace,
    user: User,
    authenticated_api_client: APIClient,
    permission: WorkspaceRoleChoice,
    is_owner: bool,
    status_code: int,
    success: bool,
):
    if permission is not None:
        workspace.set_user_permission(user, permission)
    elif is_owner:
        workspace.set_owner(user)

    table: Table = table_factory(workspace=workspace)

    r = authenticated_api_client.delete(
        f'/api/workspaces/{workspace.name}/tables/{table.name}/')

    assert r.status_code == status_code

    if success:
        # Assert relevant objects are deleted
        assert not Table.objects.filter(name=table.name).exists()
        assert not workspace.get_arango_db().has_collection(table.name)
    else:
        assert Table.objects.filter(name=table.name).exists()
        assert workspace.get_arango_db().has_collection(table.name)
def test_network_rest_create(
    workspace: Workspace,
    user: User,
    authenticated_api_client: APIClient,
    permission: WorkspaceRoleChoice,
    is_owner: bool,
    status_code: int,
    success: bool,
):
    if permission is not None:
        workspace.set_user_permission(user, permission)
    elif is_owner:
        workspace.set_owner(user)

    edge_table = populated_table(workspace, True)
    node_table_name = list(edge_table.find_referenced_node_tables().keys())[0]
    node_table = Table.objects.get(name=node_table_name)
    network_name = 'network'

    r = authenticated_api_client.post(
        f'/api/workspaces/{workspace.name}/networks/',
        {
            'name': network_name,
            'edge_table': edge_table.name
        },
        format='json',
    )
    assert r.status_code == status_code

    if success:
        assert r.json() == {
            'name': network_name,
            'node_count': len(node_table.get_rows()),
            'edge_count': len(edge_table.get_rows()),
            'id': INTEGER_ID_RE,
            'created': TIMESTAMP_RE,
            'modified': TIMESTAMP_RE,
            'workspace': {
                'id': workspace.pk,
                'name': workspace.name,
                'created': TIMESTAMP_RE,
                'modified': TIMESTAMP_RE,
                'arango_db_name': workspace.arango_db_name,
                'public': False,
            },
        }

        # Django will raise an exception if this fails, implicitly validating that the object exists
        network: Network = Network.objects.get(name=network_name)
        # Assert that object was created in arango
        assert workspace.get_arango_db().has_graph(network.name)
    else:
        assert not Network.objects.filter(name=network_name).exists()
        assert not workspace.get_arango_db().has_graph(network_name)
def test_table_rest_create(
    workspace: Workspace,
    user: User,
    authenticated_api_client: APIClient,
    edge: bool,
    permission: WorkspaceRoleChoice,
    is_owner: bool,
    status_code: int,
    success: bool,
):
    if permission is not None:
        workspace.set_user_permission(user, permission)
    elif is_owner:
        workspace.set_owner(user)

    table_name = Faker().pystr()
    r = authenticated_api_client.post(
        f'/api/workspaces/{workspace.name}/tables/',
        {
            'name': table_name,
            'edge': edge
        },
        format='json',
    )
    assert r.status_code == status_code

    if success:
        r_json = r.json()

        assert r_json == {
            'name': table_name,
            'edge': edge,
            'id': INTEGER_ID_RE,
            'created': TIMESTAMP_RE,
            'modified': TIMESTAMP_RE,
            'workspace': {
                'id': workspace.pk,
                'name': workspace.name,
                'created': TIMESTAMP_RE,
                'modified': TIMESTAMP_RE,
                'arango_db_name': workspace.arango_db_name,
                'public': False,
            },
        }

        # Django will raise an exception if this fails, implicitly validating that the object exists
        table: Table = Table.objects.get(name=table_name)
        # Assert that object was created in arango
        assert workspace.get_arango_db().has_collection(table.name)
    else:
        assert not Table.objects.filter(name=table_name).exists()
        assert not workspace.get_arango_db().has_collection(table_name)
def test_network_rest_list(
    network_factory: NetworkFactory,
    workspace: Workspace,
    user: User,
    authenticated_api_client: APIClient,
    permission: WorkspaceRoleChoice,
    is_owner: bool,
    status_code: int,
    success: bool,
):
    if permission is not None:
        workspace.set_user_permission(user, permission)
    elif is_owner:
        workspace.set_owner(user)
    fake = Faker()
    network_names: List[str] = [
        network_factory(name=fake.pystr(), workspace=workspace).name
        for _ in range(3)
    ]

    r = authenticated_api_client.get(
        f'/api/workspaces/{workspace.name}/networks/')
    assert r.status_code == status_code

    if success:
        r_json = r.json()

        # Test that we get the expected results from both django and arango
        arango_db = workspace.get_arango_db()
        assert r_json['count'] == len(network_names)
        for network in r_json['results']:
            assert network['name'] in network_names
            assert arango_db.has_graph(network['name'])
def test_network_rest_delete_unauthorized(workspace: Workspace,
                                          api_client: APIClient):
    """Tests deleting a network from a workspace with an unauthorized request."""
    network = populated_network(workspace)

    r = api_client.delete(
        f'/api/workspaces/{workspace.name}/networks/{network.name}/')

    assert r.status_code == 401

    # Assert relevant objects are not deleted
    assert Network.objects.filter(name=network.name).exists()
    assert workspace.get_arango_db().has_graph(network.name)
def test_table_rest_delete_unauthorized(table_factory: TableFactory,
                                        workspace: Workspace, user: User,
                                        api_client: APIClient):
    workspace.set_user_permission(user, WorkspaceRoleChoice.WRITER)
    table: Table = table_factory(workspace=workspace)
    r = api_client.delete(
        f'/api/workspaces/{workspace.name}/tables/{table.name}/')

    assert r.status_code == 401

    # Assert relevant objects are not deleted
    assert Table.objects.filter(name=table.name).exists()
    assert workspace.get_arango_db().has_collection(table.name)
def test_table_rest_list_public(table_factory: TableFactory,
                                public_workspace: Workspace,
                                api_client: APIClient):
    """Test whether a user can see all tables on a public workspace."""
    fake = Faker()
    table_names: List[str] = [
        table_factory(name=fake.pystr(), workspace=public_workspace).name
        for _ in range(3)
    ]
    r = api_client.get(f'/api/workspaces/{public_workspace.name}/tables/')
    r_json = r.json()

    # Test that we get the expected results from both django and arango
    arango_db = public_workspace.get_arango_db()
    assert r_json['count'] == len(table_names)
    for table in r_json['results']:
        assert table['name'] in table_names
        assert arango_db.has_collection(table['name'])