@@ -82,169 +82,160 @@ def fetch_url_mock(self, mocker):
82
82
def test_without_parameters (self ):
83
83
"""Failure if no parameters set"""
84
84
with self .assertRaises (AnsibleFailJson ):
85
- set_module_args ({})
86
- self .module .main ()
85
+ with set_module_args ({}):
86
+ self .module .main ()
87
87
88
88
def test_without_content (self ):
89
89
"""Failure if customer and match are missing"""
90
- set_module_args ({
90
+ with set_module_args ({
91
91
'alerta_url' : "http://localhost:8080" ,
92
92
'api_username' :
"[email protected] " ,
93
93
'api_password' : "password"
94
- })
95
- with self .assertRaises (AnsibleFailJson ):
96
- self .module .main ()
94
+ }):
95
+ with self .assertRaises (AnsibleFailJson ):
96
+ self .module .main ()
97
97
98
98
def test_successful_existing_customer_creation (self ):
99
99
"""Test the customer creation (already exists)."""
100
- set_module_args ({
100
+ with set_module_args ({
101
101
'alerta_url' : "http://localhost:8080" ,
102
102
'api_username' :
"[email protected] " ,
103
103
'api_password' : "password" ,
104
104
'customer' : 'Developer' ,
105
105
106
- })
107
-
108
- with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
109
- fetch_url_mock .return_value = customer_response_page1 ()
110
- with self .assertRaises (AnsibleExitJson ):
111
- self .module .main ()
112
- self .assertTrue (fetch_url_mock .call_count , 1 )
106
+ }):
107
+ with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
108
+ fetch_url_mock .return_value = customer_response_page1 ()
109
+ with self .assertRaises (AnsibleExitJson ):
110
+ self .module .main ()
111
+ self .assertTrue (fetch_url_mock .call_count , 1 )
113
112
114
113
def test_successful_customer_creation (self ):
115
114
"""Test the customer creation."""
116
- set_module_args ({
115
+ with set_module_args ({
117
116
'alerta_url' : "http://localhost:8080" ,
118
117
'api_username' :
"[email protected] " ,
119
118
'api_password' : "password" ,
120
119
'customer' : 'Developer' ,
121
120
122
- })
121
+ }):
122
+ with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
123
+ fetch_url_mock .return_value = customer_response_page1 ()
124
+ with self .assertRaises (AnsibleExitJson ):
125
+ self .module .main ()
123
126
124
- with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
125
- fetch_url_mock .return_value = customer_response_page1 ()
126
- with self .assertRaises (AnsibleExitJson ):
127
- self .module .main ()
128
-
129
- self .assertTrue (fetch_url_mock .call_count , 1 )
130
- call_data = json .loads (fetch_url_mock .call_args [1 ]['data' ])
131
- assert call_data [
'match' ]
== "[email protected] "
132
- assert call_data ['customer' ] == "Developer"
127
+ self .assertTrue (fetch_url_mock .call_count , 1 )
128
+ call_data = json .loads (fetch_url_mock .call_args [1 ]['data' ])
129
+ assert call_data [
'match' ]
== "[email protected] "
130
+ assert call_data ['customer' ] == "Developer"
133
131
134
132
def test_successful_customer_creation_key (self ):
135
133
"""Test the customer creation using api_key."""
136
- set_module_args ({
134
+ with set_module_args ({
137
135
'alerta_url' : "http://localhost:8080" ,
138
136
'api_key' : "demo-key" ,
139
137
'customer' : 'Developer' ,
140
138
141
- })
142
-
143
- with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
144
- fetch_url_mock .return_value = customer_response_page1 ()
145
- with self .assertRaises (AnsibleExitJson ):
146
- self .module .main ()
139
+ }):
140
+ with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
141
+ fetch_url_mock .return_value = customer_response_page1 ()
142
+ with self .assertRaises (AnsibleExitJson ):
143
+ self .module .main ()
147
144
148
- self .assertTrue (fetch_url_mock .call_count , 1 )
149
- call_data = json .loads (fetch_url_mock .call_args [1 ]['data' ])
150
- assert call_data [
'match' ]
== "[email protected] "
151
- assert call_data ['customer' ] == "Developer"
145
+ self .assertTrue (fetch_url_mock .call_count , 1 )
146
+ call_data = json .loads (fetch_url_mock .call_args [1 ]['data' ])
147
+ assert call_data [
'match' ]
== "[email protected] "
148
+ assert call_data ['customer' ] == "Developer"
152
149
153
150
def test_failed_not_found (self ):
154
151
"""Test failure with wrong URL."""
155
152
156
- set_module_args ({
153
+ with set_module_args ({
157
154
'alerta_url' : "http://localhost:8080/s" ,
158
155
'api_username' :
"[email protected] " ,
159
156
'api_password' : "password" ,
160
157
'customer' : 'Developer' ,
161
158
162
- })
163
-
164
- with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
165
- fetch_url_mock .return_value = (None , {"status" : 404 , 'msg' : 'Not found for request GET on http://localhost:8080/a/api/customers' })
166
- with self .assertRaises (AnsibleFailJson ):
167
- self .module .main ()
159
+ }):
160
+ with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
161
+ fetch_url_mock .return_value = (None , {"status" : 404 , 'msg' : 'Not found for request GET on http://localhost:8080/a/api/customers' })
162
+ with self .assertRaises (AnsibleFailJson ):
163
+ self .module .main ()
168
164
169
165
def test_failed_forbidden (self ):
170
166
"""Test failure with wrong user."""
171
167
172
- set_module_args ({
168
+ with set_module_args ({
173
169
'alerta_url' : "http://localhost:8080" ,
174
170
'api_username' :
"[email protected] " ,
175
171
'api_password' : "password" ,
176
172
'customer' : 'Developer' ,
177
173
178
- })
179
-
180
- with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
181
- fetch_url_mock .return_value = (None , {"status" : 403 , 'msg' : 'Permission Denied for GET on http://localhost:8080/api/customers' })
182
- with self .assertRaises (AnsibleFailJson ):
183
- self .module .main ()
174
+ }):
175
+ with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
176
+ fetch_url_mock .return_value = (None , {"status" : 403 , 'msg' : 'Permission Denied for GET on http://localhost:8080/api/customers' })
177
+ with self .assertRaises (AnsibleFailJson ):
178
+ self .module .main ()
184
179
185
180
def test_failed_unauthorized (self ):
186
181
"""Test failure with wrong username or password."""
187
182
188
- set_module_args ({
183
+ with set_module_args ({
189
184
'alerta_url' : "http://localhost:8080" ,
190
185
'api_username' :
"[email protected] " ,
191
186
'api_password' : "password_wrong" ,
192
187
'customer' : 'Developer' ,
193
188
194
- })
195
-
196
- with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
197
- fetch_url_mock .return_value = (None , {"status" : 401 , 'msg' : 'Unauthorized to request GET on http://localhost:8080/api/customers' })
198
- with self .assertRaises (AnsibleFailJson ):
199
- self .module .main ()
189
+ }):
190
+ with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
191
+ fetch_url_mock .return_value = (None , {"status" : 401 , 'msg' : 'Unauthorized to request GET on http://localhost:8080/api/customers' })
192
+ with self .assertRaises (AnsibleFailJson ):
193
+ self .module .main ()
200
194
201
195
def test_successful_customer_deletion (self ):
202
196
"""Test the customer deletion."""
203
197
204
- set_module_args ({
198
+ with set_module_args ({
205
199
'alerta_url' : "http://localhost:8080" ,
206
200
'api_username' :
"[email protected] " ,
207
201
'api_password' : "password" ,
208
202
'customer' : 'Developer' ,
209
203
210
204
'state' : 'absent'
211
- })
212
-
213
- with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
214
- fetch_url_mock .return_value = customer_response_page1 ()
215
- with self .assertRaises (AnsibleExitJson ):
216
- self .module .main ()
205
+ }):
206
+ with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
207
+ fetch_url_mock .return_value = customer_response_page1 ()
208
+ with self .assertRaises (AnsibleExitJson ):
209
+ self .module .main ()
217
210
218
211
def test_successful_customer_deletion_page2 (self ):
219
212
"""Test the customer deletion on the second page."""
220
213
221
- set_module_args ({
214
+ with set_module_args ({
222
215
'alerta_url' : "http://localhost:8080" ,
223
216
'api_username' :
"[email protected] " ,
224
217
'api_password' : "password" ,
225
218
'customer' : 'Developer' ,
226
219
227
220
'state' : 'absent'
228
- })
229
-
230
- with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
231
- fetch_url_mock .return_value = customer_response_page2 ()
232
- with self .assertRaises (AnsibleExitJson ):
233
- self .module .main ()
221
+ }):
222
+ with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
223
+ fetch_url_mock .return_value = customer_response_page2 ()
224
+ with self .assertRaises (AnsibleExitJson ):
225
+ self .module .main ()
234
226
235
227
def test_successful_nonexisting_customer_deletion (self ):
236
228
"""Test the customer deletion (non existing)."""
237
229
238
- set_module_args ({
230
+ with set_module_args ({
239
231
'alerta_url' : "http://localhost:8080" ,
240
232
'api_username' :
"[email protected] " ,
241
233
'api_password' : "password" ,
242
234
'customer' : 'Billing' ,
243
235
244
236
'state' : 'absent'
245
- })
246
-
247
- with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
248
- fetch_url_mock .return_value = customer_response_page1 ()
249
- with self .assertRaises (AnsibleExitJson ):
250
- self .module .main ()
237
+ }):
238
+ with patch .object (alerta_customer , "fetch_url" ) as fetch_url_mock :
239
+ fetch_url_mock .return_value = customer_response_page1 ()
240
+ with self .assertRaises (AnsibleExitJson ):
241
+ self .module .main ()
0 commit comments