|
| 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 | + |
0 commit comments