blob: cf8a0f9e552c931d21112c5245359ea956b04965 [file] [log] [blame]
C.J. Collier37141e42020-02-13 13:49:49 -08001# Copyright 2016 Google LLC
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -07002#
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
arithmetic1728bb9215a2020-03-20 09:49:44 -070015import os
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -070016import sys
17
18import mock
19import pytest
20
21
arithmetic1728bb9215a2020-03-20 09:49:44 -070022def pytest_configure():
23 """Load public certificate and private key."""
24 pytest.data_dir = os.path.join(os.path.dirname(__file__), "data")
25
26 with open(os.path.join(pytest.data_dir, "privatekey.pem"), "rb") as fh:
27 pytest.private_key_bytes = fh.read()
28
29 with open(os.path.join(pytest.data_dir, "public_cert.pem"), "rb") as fh:
30 pytest.public_cert_bytes = fh.read()
31
32
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -070033@pytest.fixture
34def mock_non_existent_module(monkeypatch):
35 """Mocks a non-existing module in sys.modules.
36
37 Additionally mocks any non-existing modules specified in the dotted path.
38 """
Bu Sun Kim9eec0912019-10-21 17:04:21 -070039
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -070040 def _mock_non_existent_module(path):
Bu Sun Kim9eec0912019-10-21 17:04:21 -070041 parts = path.split(".")
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -070042 partial = []
43 for part in parts:
44 partial.append(part)
Bu Sun Kim9eec0912019-10-21 17:04:21 -070045 current_module = ".".join(partial)
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -070046 if current_module not in sys.modules:
Bu Sun Kim9eec0912019-10-21 17:04:21 -070047 monkeypatch.setitem(sys.modules, current_module, mock.MagicMock())
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -070048
49 return _mock_non_existent_module