Skip to content

Commit 4c81faf

Browse files
authored
Add example for bearer auth (#623)
Additionally turning the kerberos example from being skipped into a mock test to at least catch changes in the driver's API
1 parent 4370018 commit 4c81faf

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
# Copyright (c) "Neo4j"
5+
# Neo4j Sweden AB [http://neo4j.com]
6+
#
7+
# This file is part of Neo4j.
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
21+
22+
import pytest
23+
24+
import neo4j
25+
# tag::bearer-auth-import[]
26+
from neo4j import (
27+
bearer_auth,
28+
GraphDatabase,
29+
)
30+
# end::bearer-auth-import[]
31+
32+
from tests.integration.examples import DriverSetupExample
33+
34+
35+
# python -m pytest tests/integration/examples/test_bearer_auth_example.py -s -v
36+
37+
38+
class BearerAuthExample(DriverSetupExample):
39+
40+
# tag::bearer-auth[]
41+
def __init__(self, uri, token):
42+
self.driver = GraphDatabase.driver(uri, auth=bearer_auth(token))
43+
# end::bearer-auth[]
44+
45+
46+
def test_example(uri, mocker):
47+
# Currently, there is no way of running the test against a server with SSO
48+
# setup.
49+
mocker.patch("neo4j.GraphDatabase.bolt_driver")
50+
mocker.patch("neo4j.GraphDatabase.neo4j_driver")
51+
52+
token = "myToken"
53+
BearerAuthExample(uri, token)
54+
calls = (neo4j.GraphDatabase.bolt_driver.call_args_list
55+
+ neo4j.GraphDatabase.neo4j_driver.call_args_list)
56+
assert len(calls) == 1
57+
args_, kwargs = calls[0]
58+
auth = kwargs.get("auth")
59+
assert isinstance(auth, neo4j.Auth)
60+
assert auth.scheme == "bearer"
61+
assert not hasattr(auth, "principal")
62+
assert auth.credentials == token
63+
assert not hasattr(auth, "parameters")
64+

tests/integration/examples/test_kerberos_auth_example.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
# limitations under the License.
2020

2121

22-
import pytest
23-
22+
import neo4j
2423
# tag::kerberos-auth-import[]
2524
from neo4j import (
2625
GraphDatabase,
@@ -40,5 +39,21 @@ def __init__(self, uri, ticket):
4039
# end::kerberos-auth[]
4140

4241

43-
def test_example():
44-
pytest.skip("Currently no way to test Kerberos auth")
42+
def test_example(uri, mocker):
43+
# Currently, there is no way of running the test against a server with SSO
44+
# setup.
45+
mocker.patch("neo4j.GraphDatabase.bolt_driver")
46+
mocker.patch("neo4j.GraphDatabase.neo4j_driver")
47+
48+
ticket = "myTicket"
49+
KerberosAuthExample(uri, ticket)
50+
calls = (neo4j.GraphDatabase.bolt_driver.call_args_list
51+
+ neo4j.GraphDatabase.neo4j_driver.call_args_list)
52+
assert len(calls) == 1
53+
args_, kwargs = calls[0]
54+
auth = kwargs.get("auth")
55+
assert isinstance(auth, neo4j.Auth)
56+
assert auth.scheme == "kerberos"
57+
assert auth.principal == ""
58+
assert auth.credentials == ticket
59+
assert not hasattr(auth, "parameters")

0 commit comments

Comments
 (0)