|
| 1 | +# Copyright 2018-present MongoDB, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Run the auth spec tests.""" |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import glob |
| 19 | +import json |
| 20 | +import os |
| 21 | +import sys |
| 22 | +import warnings |
| 23 | + |
| 24 | +sys.path[0:0] = [""] |
| 25 | + |
| 26 | +from test import unittest |
| 27 | +from test.unified_format import generate_test_classes |
| 28 | + |
| 29 | +from pymongo import AsyncMongoClient |
| 30 | +from pymongo.asynchronous.auth_oidc import OIDCCallback |
| 31 | + |
| 32 | +_IS_SYNC = False |
| 33 | + |
| 34 | +_TEST_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "auth") |
| 35 | + |
| 36 | + |
| 37 | +class TestAuthSpec(unittest.IsolatedAsyncioTestCase): |
| 38 | + pass |
| 39 | + |
| 40 | + |
| 41 | +class SampleHumanCallback(OIDCCallback): |
| 42 | + def fetch(self, context): |
| 43 | + pass |
| 44 | + |
| 45 | + |
| 46 | +def create_test(test_case): |
| 47 | + def run_test(self): |
| 48 | + uri = test_case["uri"] |
| 49 | + valid = test_case["valid"] |
| 50 | + credential = test_case.get("credential") |
| 51 | + |
| 52 | + if not valid: |
| 53 | + with warnings.catch_warnings(): |
| 54 | + warnings.simplefilter("default") |
| 55 | + self.assertRaises(Exception, AsyncMongoClient, uri, connect=False) |
| 56 | + else: |
| 57 | + client = AsyncMongoClient(uri, connect=False) |
| 58 | + credentials = client.options.pool_options._credentials |
| 59 | + if credential is None: |
| 60 | + self.assertIsNone(credentials) |
| 61 | + else: |
| 62 | + self.assertIsNotNone(credentials) |
| 63 | + self.assertEqual(credentials.username, credential["username"]) |
| 64 | + self.assertEqual(credentials.password, credential["password"]) |
| 65 | + self.assertEqual(credentials.source, credential["source"]) |
| 66 | + if credential["mechanism"] is not None: |
| 67 | + self.assertEqual(credentials.mechanism, credential["mechanism"]) |
| 68 | + else: |
| 69 | + self.assertEqual(credentials.mechanism, "DEFAULT") |
| 70 | + expected = credential["mechanism_properties"] |
| 71 | + if expected is not None: |
| 72 | + actual = credentials.mechanism_properties |
| 73 | + for key, value in expected.items(): |
| 74 | + self.assertEqual(getattr(actual, key.lower()), value) |
| 75 | + else: |
| 76 | + if credential["mechanism"] == "MONGODB-AWS": |
| 77 | + self.assertIsNone(credentials.mechanism_properties.aws_session_token) |
| 78 | + else: |
| 79 | + self.assertIsNone(credentials.mechanism_properties) |
| 80 | + |
| 81 | + return run_test |
| 82 | + |
| 83 | + |
| 84 | +def create_tests(): |
| 85 | + for filename in glob.glob(os.path.join(_TEST_PATH, "legacy", "*.json")): |
| 86 | + test_suffix, _ = os.path.splitext(os.path.basename(filename)) |
| 87 | + with open(filename) as auth_tests: |
| 88 | + test_cases = json.load(auth_tests)["tests"] |
| 89 | + for test_case in test_cases: |
| 90 | + if test_case.get("optional", False): |
| 91 | + continue |
| 92 | + test_method = create_test(test_case) |
| 93 | + name = str(test_case["description"].lower().replace(" ", "_")) |
| 94 | + setattr(TestAuthSpec, f"test_{test_suffix}_{name}", test_method) |
| 95 | + |
| 96 | + |
| 97 | +create_tests() |
| 98 | + |
| 99 | + |
| 100 | +globals().update( |
| 101 | + generate_test_classes( |
| 102 | + os.path.join(_TEST_PATH, "unified"), |
| 103 | + module=__name__, |
| 104 | + ) |
| 105 | +) |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + unittest.main() |
0 commit comments