-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtest_lookup.py
More file actions
279 lines (219 loc) · 10.2 KB
/
test_lookup.py
File metadata and controls
279 lines (219 loc) · 10.2 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""
Pytest tests for the Rover_Lookup package.
These tests verify the functionality of the github_username_to_emails function
and related components.
"""
import logging
from unittest.mock import Mock, patch
# Import the function we're testing
from Rover_Lookup import github_username_to_emails
class TestGithubUsernameToEmails:
"""Test cases for the github_username_to_emails function."""
def test_empty_username_returns_none(self, caplog):
"""Test that empty username returns None and logs error."""
with caplog.at_level(logging.ERROR):
result = github_username_to_emails("")
assert result is None
assert "GitHub username cannot be empty" in caplog.text
def test_none_username_returns_none(self, caplog):
"""Test that None username returns None and logs error."""
with caplog.at_level(logging.ERROR):
result = github_username_to_emails(None)
assert result is None
assert "GitHub username cannot be empty" in caplog.text
@patch("Rover_Lookup.lookup.Connection")
def test_successful_lookup_single_record(self, mock_connection_class, caplog):
"""Test successful LDAP lookup with single record containing multiple emails."""
# Mock the LDAP server connection
mock_conn = Mock()
mock_connection_class.return_value = mock_conn
mock_conn.search.return_value = True
# Mock LDAP entry with email attributes
mock_entry = Mock()
mock_entry.entry_dn = "cn=testuser,ou=users,dc=redhat,dc=com"
# Mock email attributes
mock_primary_mail = Mock()
mock_primary_mail.value = "user@redhat.com"
mock_entry.rhatPrimaryMail = mock_primary_mail
mock_mail = Mock()
mock_mail.value = ["user@example.com", "user.alias@redhat.com"]
mock_entry.mail = mock_mail
mock_preferred_alias = Mock()
# Duplicate value to test deduplication
mock_preferred_alias.value = "user@redhat.com"
mock_entry.rhatPreferredAlias = mock_preferred_alias
mock_conn.entries = [mock_entry]
with caplog.at_level(logging.DEBUG):
result = github_username_to_emails("test-user")
# Verify result
assert result == [
"user.alias@redhat.com",
"user@example.com",
"user@redhat.com",
]
# Verify LDAP query was called correctly
mock_conn.search.assert_called_once()
search_args = mock_conn.search.call_args
assert (
search_args[1]["search_filter"]
== "(rhatSocialURL=Github->https://github.com/test-user)"
)
assert "rhatPrimaryMail" in search_args[1]["attributes"]
assert "mail" in search_args[1]["attributes"]
assert "rhatPreferredAlias" in search_args[1]["attributes"]
mock_conn.unbind.assert_called_once()
@patch("Rover_Lookup.lookup.Connection")
def test_successful_lookup_multiple_records(self, mock_connection_class):
"""Test successful LDAP lookup with multiple records."""
# Mock the LDAP server connection
mock_conn = Mock()
mock_connection_class.return_value = mock_conn
mock_conn.search.return_value = True
# Create two mock entries
mock_entry1 = Mock()
mock_entry1.entry_dn = "cn=user1,ou=users,dc=redhat,dc=com"
mock_primary_mail1 = Mock()
mock_primary_mail1.value = "user1@redhat.com"
mock_entry1.rhatPrimaryMail = mock_primary_mail1
mock_entry1.mail = Mock()
mock_entry1.mail.value = "zmail@redhat.com"
mock_entry1.rhatPreferredAlias = Mock()
mock_entry1.rhatPreferredAlias.value = "team@redhat.com"
mock_entry2 = Mock()
mock_entry2.entry_dn = "cn=user2,ou=users,dc=redhat,dc=com"
mock_primary_mail2 = Mock()
mock_primary_mail2.value = "user2@redhat.com"
mock_entry2.rhatPrimaryMail = mock_primary_mail2
mock_entry2.mail = Mock()
mock_entry2.mail.value = None
mock_entry2.rhatPreferredAlias = Mock()
mock_entry2.rhatPreferredAlias.value = "team@redhat.com"
mock_conn.entries = [mock_entry1, mock_entry2]
result = github_username_to_emails("test-user")
# Verify result combines emails from both records
assert result == [
"team@redhat.com",
"user1@redhat.com",
"user2@redhat.com",
"zmail@redhat.com",
]
@patch("Rover_Lookup.lookup.Connection")
def test_no_records_found(self, mock_connection_class, caplog):
"""Test LDAP lookup when no records are found."""
mock_conn = Mock()
mock_connection_class.return_value = mock_conn
mock_conn.search.return_value = True
mock_conn.entries = [] # No entries found
with caplog.at_level(logging.INFO):
result = github_username_to_emails("nonexistent-user")
assert result == []
assert "No LDAP entries found" in caplog.text
mock_conn.unbind.assert_called_once()
@patch("Rover_Lookup.lookup.Connection")
def test_records_with_no_emails(self, mock_connection_class):
"""Test LDAP lookup when records are found but contain no email addresses."""
mock_conn = Mock()
mock_connection_class.return_value = mock_conn
mock_conn.search.return_value = True
# Mock entry with no email attributes
mock_entry = Mock()
mock_entry.entry_dn = "cn=testuser,ou=users,dc=redhat,dc=com"
# Mock attributes that don't exist or are empty
mock_entry.rhatPrimaryMail = Mock()
mock_entry.rhatPrimaryMail.value = None
mock_entry.mail = Mock()
mock_entry.mail.value = []
mock_entry.rhatPreferredAlias = Mock()
mock_entry.rhatPreferredAlias.value = ""
mock_conn.entries = [mock_entry]
result = github_username_to_emails("test-user")
assert result == []
@patch("Rover_Lookup.lookup.Connection")
def test_ldap_search_failure(self, mock_connection_class, caplog):
"""Test LDAP search failure."""
mock_conn = Mock()
mock_connection_class.return_value = mock_conn
mock_conn.search.return_value = False # Search failed
with caplog.at_level(logging.INFO):
result = github_username_to_emails("test-user")
assert result is None
assert "LDAP search failed" in caplog.text
mock_conn.unbind.assert_called_once()
@patch("Rover_Lookup.lookup.Connection")
def test_connection_ldap_exception(self, mock_connection_class, caplog):
"""Test LDAP exception handling."""
from Rover_Lookup.lookup import LDAPException
mock_connection_class.side_effect = LDAPException("Connection refused")
with caplog.at_level(logging.ERROR):
result = github_username_to_emails("test-user")
assert result is None
assert "Error connecting to LDAP server" in caplog.text
assert "Connection refused" in caplog.text
@patch("Rover_Lookup.lookup.Connection")
def test_connection_ldap_vpn_exception(self, mock_connection_class, caplog):
"""Test LDAP exception handling."""
from Rover_Lookup.lookup import LDAPException
mock_connection_class.side_effect = LDAPException("invalid server address")
with caplog.at_level(logging.ERROR):
result = github_username_to_emails("test-user")
assert result is None
assert "Error connecting to LDAP server" in caplog.text
assert "is the VPN active?" in caplog.text
@patch("Rover_Lookup.lookup.Connection")
def test_connection_unexpected_exception(self, mock_connection_class, caplog):
"""Test unexpected exception handling."""
mock_connection_class.side_effect = ValueError("An error occurred")
with caplog.at_level(logging.ERROR):
result = github_username_to_emails("test-user")
assert result is None
assert "Unexpected error connecting to LDAP server" in caplog.text
assert "An error occurred" in caplog.text
@patch("Rover_Lookup.lookup.Connection")
def test_search_ldap_exceptions(self, mock_connection_class, caplog):
"""Test LDAP exception handling."""
from Rover_Lookup.lookup import LDAPException
mock_conn = Mock()
mock_connection_class.return_value = mock_conn
mock_conn.search.side_effect = LDAPException("Connection failed")
with caplog.at_level(logging.ERROR):
result = github_username_to_emails("test-user")
assert result is None
assert "LDAP error while looking up" in caplog.text
assert "Connection failed" in caplog.text
@patch("Rover_Lookup.lookup.Connection")
def test_search_unexpected_exceptions(self, mock_connection_class, caplog):
"""Test unexpected exception handling."""
mock_conn = Mock()
mock_connection_class.return_value = mock_conn
mock_conn.search.side_effect = ValueError("An error occurred")
with caplog.at_level(logging.ERROR):
result = github_username_to_emails("test-user")
assert result is None
assert "Unexpected error while looking up" in caplog.text
assert "An error occurred" in caplog.text
@patch("Rover_Lookup.lookup.Connection")
def test_custom_ldap_parameters(self, mock_connection_class):
"""Test function with custom LDAP parameters."""
mock_conn = Mock()
mock_connection_class.return_value = mock_conn
mock_conn.search.return_value = True
mock_conn.entries = []
result = github_username_to_emails(
"test-user",
ldap_server="ldap://custom.server.com",
ldap_base_dn="ou=people,dc=custom,dc=com",
ldap_bind_dn="cn=bind,dc=custom,dc=com",
ldap_password="secret",
)
# Verify custom parameters were used
mock_connection_class.assert_called_with(
"ldap://custom.server.com",
"cn=bind,dc=custom,dc=com",
"secret",
auto_bind=True,
raise_exceptions=True,
)
# Verify custom base DN was used in search
search_args = mock_conn.search.call_args
assert search_args[1]["search_base"] == "ou=people,dc=custom,dc=com"
assert result == []