-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.main.py
132 lines (124 loc) · 4.98 KB
/
test.main.py
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
"""Unit test for lambdahandler"""
import unittest
from unittest import mock
from unittest.mock import MagicMock
from main import lambda_handler
class TestLambdaHandler(unittest.TestCase):
"""test case for lambdahandler"""
@mock.patch('main.MessagePublisher')
@mock.patch('main.IntegrationSignalfx')
@mock.patch('main.getconnection')
@mock.patch('main.isuser')
def test_empty_sources(self, mock_is_user, mock_ldap, mock_source, mock_msg_publisher):
main.sources = []
isUser = MagicMock()
ldap = MagicMock()
source = MagicMock()
msg_publisher = MagicMock()
mock_is_user.return_value = isUser
mock_ldap.return_value = ldap
mock_source.return_value = source
mock_msg_publisher.return_value = msg_publisher
event = {'dryrun': False}
lambda_handler(event, None)
mock_is_user.assert_not_called()
mock_ldap.assert_called_once()
mock_source.assert_not_called()
mock_msg_publisher.assert_not_called()
@mock.patch('main.MessagePublisher')
@mock.patch('main.IntegrationSignalfx')
@mock.patch('main.getconnection')
@mock.patch('main.isuser')
def test_empty_user_from_sources(self, mock_is_user, mock_ldap, mock_source, mock_msg_publisher):
isUser = MagicMock()
ldap = MagicMock()
source = MagicMock()
msg_publisher = MagicMock()
main.sources = [source]
mock_is_user.return_value = isUser
mock_ldap.return_value = ldap
mock_source.return_value = source
mock_msg_publisher.return_value = msg_publisher
source.get_users.return_value = []
event = {'dryrun': False}
lambda_handler(event, None)
mock_is_user.assert_not_called()
mock_ldap.assert_called_once()
mock_source.assert_not_called()
mock_msg_publisher.assert_not_called()
source.get_users.assert_called_once()
@mock.patch('main.MessagePublisher')
@mock.patch('main.IntegrationSignalfx')
@mock.patch('main.getconnection')
@mock.patch('main.isuser')
def test_user_valid(self, mock_is_user, mock_ldap, mock_source, mock_msg_publisher):
isUser = MagicMock()
ldap = MagicMock()
source = MagicMock()
msg_publisher = MagicMock()
main.sources = [source]
mock_is_user.return_value = isUser
mock_ldap.return_value = ldap
mock_source.return_value = source
mock_msg_publisher.return_value = msg_publisher
source.get_users.return_value = [{'email': 'abc@xyz.com', 'id': "abj148djh"}]
isUser.return_value = True
event = {'dryrun': False}
lambda_handler(event, None)
mock_is_user.assert_not_called()
mock_ldap.assert_called_once()
mock_source.assert_not_called()
mock_msg_publisher.assert_not_called()
source.get_users.assert_called_once()
msg_publisher.publish_message.assert_not_called()
source.remove_user.assert_not_called()
@mock.patch('main.MessagePublisher')
@mock.patch('main.IntegrationSignalfx')
@mock.patch('main.getconnection')
@mock.patch('main.isuser')
def test_user_invalid_with_dryrun_enabled(self, mock_is_user, mock_ldap, mock_source, mock_msg_publisher):
isUser = MagicMock()
ldap = MagicMock()
source = MagicMock()
msg_publisher = MagicMock()
main.sources = [source]
mock_is_user.return_value = isUser
mock_ldap.return_value = ldap
mock_source.return_value = source
mock_msg_publisher.return_value = msg_publisher
source.get_users.return_value = [{'email': 'abc@xyz.com', 'id': "abj148djh"}]
isUser.return_value = FALSE
event = {'dryrun': True}
lambda_handler(event, None)
mock_is_user.assert_not_called()
mock_ldap.assert_called_once()
mock_source.assert_not_called()
mock_msg_publisher.assert_not_called()
source.get_users.assert_called_once()
msg_publisher.publish_message.assert_not_called()
source.remove_user.assert_not_called()
@mock.patch('main.MessagePublisher')
@mock.patch('main.IntegrationSignalfx')
@mock.patch('main.getconnection')
@mock.patch('main.isuser')
def test_user_invalid_with_dryrun_disabled(self, mock_is_user, mock_ldap, mock_source, mock_msg_publisher):
isUser = MagicMock()
ldap = MagicMock()
source = MagicMock()
msg_publisher = MagicMock()
main.sources = [source]
mock_is_user.return_value = isUser
mock_ldap.return_value = ldap
mock_source.return_value = source
mock_msg_publisher.return_value = msg_publisher
source.get_users.return_value = [{'email': 'abc@xyz.com', 'id': "abj148djh"}]
isUser.return_value = FALSE
event = {'dryrun': False}
lambda_handler(event, None)
mock_is_user.assert_not_called()
mock_ldap.assert_called_once()
mock_source.assert_not_called()
mock_msg_publisher.assert_not_called()
source.get_users.assert_called_once()
msg_publisher.publish_message.assert_called_once()
source.remove_user.assert_called_once()