-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathtest_auth.py
More file actions
162 lines (134 loc) · 5.89 KB
/
Copy pathtest_auth.py
File metadata and controls
162 lines (134 loc) · 5.89 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""A module containing unit tests for the auth module.
This module contains unit tests for the functions in the auth module
that authenticate to github.
Classes:
TestAuthToGithub: A class to test the auth_to_github function.
"""
import unittest
from unittest.mock import MagicMock, patch
from auth import auth_to_github, get_github_app_installation_token
from github import Github
class TestAuthToGithub(unittest.TestCase):
"""Test the auth_to_github function."""
@patch("auth.Auth")
@patch("auth.Github")
def test_auth_to_github_with_github_app(self, mock_github_cls, mock_auth):
"""
Test the auth_to_github function when GitHub app
parameters provided.
"""
mock_app_auth = MagicMock()
mock_auth.AppAuth.return_value = mock_app_auth
mock_installation_auth = MagicMock()
mock_app_auth.get_installation_auth.return_value = mock_installation_auth
mock_github_instance = MagicMock()
mock_github_cls.return_value = mock_github_instance
result = auth_to_github("", 12345, 678910, b"hello", "", False)
mock_auth.AppAuth.assert_called_once_with(12345, "hello")
mock_app_auth.get_installation_auth.assert_called_once_with(678910)
mock_github_cls.assert_called_once_with(auth=mock_installation_auth)
self.assertEqual(result, mock_github_instance)
def test_auth_to_github_with_token(self):
"""
Test the auth_to_github function when the token is provided.
"""
result = auth_to_github("token", None, None, b"", "", False)
self.assertIsInstance(result, Github)
def test_auth_to_github_without_authentication_information(self):
"""
Test the auth_to_github function when authentication information is not provided.
Expect a ValueError to be raised.
"""
with self.assertRaises(ValueError):
auth_to_github("", None, None, b"", "", False)
def test_auth_to_github_with_ghe(self):
"""
Test the auth_to_github function when the GitHub Enterprise URL is provided.
"""
result = auth_to_github(
"token", None, None, b"", "https://github.example.com", False
)
self.assertIsInstance(result, Github)
@patch("auth.Auth")
@patch("auth.Github")
def test_auth_to_github_with_ghe_and_ghe_app(self, mock_github_cls, mock_auth):
"""
Test the auth_to_github function when the GitHub Enterprise URL \
is provided and the app was created in GitHub Enterprise URL.
"""
mock_app_auth = MagicMock()
mock_auth.AppAuth.return_value = mock_app_auth
mock_installation_auth = MagicMock()
mock_app_auth.get_installation_auth.return_value = mock_installation_auth
mock_github_instance = MagicMock()
mock_github_cls.return_value = mock_github_instance
result = auth_to_github(
"", 123, 456, b"123", "https://github.example.com", True
)
mock_auth.AppAuth.assert_called_once_with(123, "123")
mock_app_auth.get_installation_auth.assert_called_once_with(456)
mock_github_cls.assert_called_once_with(
base_url="https://github.example.com/api/v3",
auth=mock_installation_auth,
)
self.assertEqual(result, mock_github_instance)
@patch("auth.GithubIntegration")
@patch("auth.Auth")
def test_get_github_app_installation_token(self, mock_auth, mock_gi_cls):
"""
Test the get_github_app_installation_token function.
"""
dummy_token = "dummytoken"
mock_app_auth = MagicMock()
mock_auth.AppAuth.return_value = mock_app_auth
mock_gi = MagicMock()
mock_gi_cls.return_value = mock_gi
mock_access_token = MagicMock()
mock_access_token.token = dummy_token
mock_gi.get_access_token.return_value = mock_access_token
result = get_github_app_installation_token(
"", 12345, b"gh_private_token", 678910
)
self.assertEqual(result, dummy_token)
@patch("auth.GithubIntegration")
@patch("auth.Auth")
def test_get_github_app_installation_token_with_ghe(self, mock_auth, mock_gi_cls):
"""
Test the get_github_app_installation_token function with a GHE URL.
"""
dummy_token = "dummytoken"
mock_app_auth = MagicMock()
mock_auth.AppAuth.return_value = mock_app_auth
mock_gi = MagicMock()
mock_gi_cls.return_value = mock_gi
mock_access_token = MagicMock()
mock_access_token.token = dummy_token
mock_gi.get_access_token.return_value = mock_access_token
result = get_github_app_installation_token(
"https://github.example.com", 12345, b"gh_private_token", 678910
)
mock_gi_cls.assert_called_once_with(
auth=mock_app_auth, base_url="https://github.example.com/api/v3"
)
self.assertEqual(result, dummy_token)
def test_get_github_app_installation_token_returns_none_for_missing_ids(self):
"""
Test that get_github_app_installation_token returns None when app IDs are None.
"""
result = get_github_app_installation_token("", None, b"private_key", 678910)
self.assertIsNone(result)
result = get_github_app_installation_token("", 12345, b"private_key", None)
self.assertIsNone(result)
@patch("auth.Auth")
def test_get_github_app_installation_token_request_failure(self, mock_auth):
"""
Test the get_github_app_installation_token function returns None when the request fails.
"""
mock_auth.AppAuth.side_effect = ValueError("Auth failed")
result = get_github_app_installation_token(
ghe="https://api.github.com",
gh_app_id=12345,
gh_app_private_key_bytes=b"private_key",
gh_app_installation_id=678910,
)
self.assertIsNone(result)