forked from chronicle/api-samples-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchronicle_auth_test.py
More file actions
99 lines (87 loc) · 4.46 KB
/
chronicle_auth_test.py
File metadata and controls
99 lines (87 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Tests for the "chronicle_auth" module."""
import os
import tempfile
import unittest
from unittest import mock
from google.oauth2 import service_account
from . import chronicle_auth
class ChronicleAuthTest(unittest.TestCase):
def setUp(self):
super().setUp()
fd, self.path = tempfile.mkstemp(suffix=".json", text=True)
fake_json_credentials = b"""{
"client_email": "fake-username@fake-project.iam.gserviceaccount.com",
"token_uri": "https://oauth2.googleapis.com/token",
"private_key": "
"""
fake_private_key = b"""-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDemycWcEiVMMKm
/S3f8oRkgxVvbi14D0TWFBUPZq9w1nc7L4Udz7NZ8BKC49DuKi1EgwxF8z0Bve5i
k6UMfb4JeXLkSSQN4Zy5IbZUr9Mm3w0sjIzTeA1JmIqY+r3EbUxeqFjpqc02HW4h
j0L7Wj2on9KTvMd0zFRCsLLz7KoZyykDKW3jbvDBNx9n3uUNBb+Z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SDZPG8Co14ezKto+DRXgLe17+8Kq22GCPkOUtARgr4ARfk
s0Z44u3SE8fyF2Kkm+rhEOsHOlYokkfwYIHA6wctS/D9fTgaP5U3eigJgeRclD5E
LOn9ODvY81HopOSXvuXao+gJcRWCJi/fHNz4Tg0CgYEA0ruTbieHIzCjg8C1Sp40
GBixMpHsZ2ld1OaqQvidYIUL48TutyQhWHaRIqaziSZJBaNIYB73pIQfLdHIi0hx
3KHskc16JPhKgWLsl9cTP5GAIP2cqvSqBmnbvX+ArSRbqy4v7kxJwKPai+iaFi5H
1njcNc79W7qohKZshYNUq/0=
-----END PRIVATE KEY-----
"""
fake_private_key = fake_private_key.replace(b" " * 4, b"")
fake_private_key = fake_private_key.replace(b"\n", b"\\n")
os.write(fd, fake_json_credentials.strip() + fake_private_key + b'"\n}\n')
os.close(fd)
@mock.patch.object(service_account.Credentials, "from_service_account_file")
def test_initialize_http_session(self, mock_from_service_account_file):
chronicle_auth.initialize_http_session("")
mock_from_service_account_file.assert_called_once_with(
str(chronicle_auth.DEFAULT_CREDENTIALS_FILE),
scopes=chronicle_auth.AUTHORIZATION_SCOPES)
@mock.patch.object(service_account.Credentials, "from_service_account_file")
def test_initialize_http_session_with_custom_json_credentials(
self, mock_from_service_account_file):
chronicle_auth.initialize_http_session(self.path)
mock_from_service_account_file.assert_called_once_with(
self.path, scopes=chronicle_auth.AUTHORIZATION_SCOPES)
@mock.patch.object(service_account.Credentials, "from_service_account_file")
def test_initialize_http_session_with_custom_creds_and_scopes(
self, mock_from_service_account_file):
scopes = ["https://www.googleapis.com/auth/malachite-ingestion"]
chronicle_auth.initialize_http_session(self.path, scopes=scopes)
mock_from_service_account_file.assert_called_once_with(
self.path, scopes=scopes)
def tearDown(self):
os.remove(self.path)
super().tearDown()
if __name__ == "__main__":
unittest.main()