-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_scenario_lifetime_user_create_or_update.py
248 lines (214 loc) · 9.12 KB
/
test_scenario_lifetime_user_create_or_update.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
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
from http import HTTPStatus
import pytest as pytest
import no_ssl_verification as SSL
import stubbing_utils as WireMockStubbing
from platform_api.facades.lifetime_facade import LifetimeFacade
from platform_api.facades.lifetime_model import LifetimeCredentials, LifetimeError, \
LifetimeUser
from wiremock_service import WireMockService, WIREMOCK_DEFAULT_URL
EXPECTED_USER_CREATE_OR_UPDATE_REQUEST_TEMPLATE = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:out="http://www.outsystems.com">
<soapenv:Header/>
<soapenv:Body>
<out:User_CreateOrUpdate>
<out:Authentication>
<out:Username>${{xmlunit.ignore}}</out:Username>
<out:Password>${{xmlunit.ignore}}</out:Password>
</out:Authentication>
<out:Username>{username}</out:Username>
<out:Password>{password}</out:Password>
<out:EncryptPassword>{encrypt_password}</out:EncryptPassword>
<out:Name>{name}</out:Name>
<out:Email>{email}</out:Email>
<out:RoleName>{role_name}</out:RoleName>
</out:User_CreateOrUpdate>
</soapenv:Body>
</soapenv:Envelope>"""
EXPECTED_USER_CREATE_OR_UPDATE_RESPONSE_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<User_CreateOrUpdateResponse xmlns="http://www.outsystems.com">
<Success>{success}</Success>
<Status>
<Id>{status_id}</Id>
<ResponseId>{response_id}</ResponseId>
<ResponseMessage>{response_message}</ResponseMessage>
<ResponseAdditionalInfo/>
</Status>
<PlatformUser>
<Id>{{{{randomValue length=4 type='NUMERIC'}}}}</Id>
<Username>{{{{xPath request.body '/User_CreateOrUpdate/Username/text()'}}}}</Username>
<Name>{{{{xPath request.body '/User_CreateOrUpdate/Name/text()'}}}}</Name>
<Email>{{{{xPath request.body '/User_CreateOrUpdate/Email/text()'}}}}</Email>
<RoleName>{{{{xPath request.body '/User_CreateOrUpdate/RoleName/text()'}}}}</RoleName>
</PlatformUser>
</User_CreateOrUpdateResponse>
</soap:Body>
</soap:Envelope>"""
USER_MANAGEMENT_SOAP_OPERATIONS_URL = "/LifeTimeServices/UserManagementService.asmx?wsdl"
wiremock = WireMockService(WIREMOCK_DEFAULT_URL)
def _setup_mappings_for_user_create_or_update(run_id: str):
happy_request = EXPECTED_USER_CREATE_OR_UPDATE_REQUEST_TEMPLATE.format(
username="username",
password="password",
encrypt_password="true",
name="name",
email="email",
role_name="role_name",
)
happy_response = EXPECTED_USER_CREATE_OR_UPDATE_RESPONSE_TEMPLATE.format(
success="true",
status_id="1",
response_id="2",
response_message="smooth",
)
WireMockStubbing.register_soap_mapping(
wiremock=wiremock,
run_id=run_id,
soap_operations_url=USER_MANAGEMENT_SOAP_OPERATIONS_URL,
expected_request=happy_request,
expected_response=happy_response
)
expected_request_for_unsuccessful_operation = EXPECTED_USER_CREATE_OR_UPDATE_REQUEST_TEMPLATE.format(
username="unsuccessful_user",
password="password",
encrypt_password="true",
name="unsuccessful_name",
email="unsuccessful_email",
role_name="unsuccessful_role_name",
)
expected_response_for_unsuccessful_operation = EXPECTED_USER_CREATE_OR_UPDATE_RESPONSE_TEMPLATE.format(
success="false",
status_id="-1",
response_id="1000",
response_message="The email is invalid",
)
WireMockStubbing.register_soap_mapping(
wiremock=wiremock,
run_id=run_id,
soap_operations_url=USER_MANAGEMENT_SOAP_OPERATIONS_URL,
expected_request=expected_request_for_unsuccessful_operation,
expected_response=expected_response_for_unsuccessful_operation
)
expected_request_for_internal_lifetime_error = EXPECTED_USER_CREATE_OR_UPDATE_REQUEST_TEMPLATE.format(
username="internal_invalid_state_user",
password="internal_invalid_state_password",
encrypt_password="true",
name="internal_invalid_state_name",
email="internal_invalid_state_email",
role_name="internal_invalid_state_role_name",
)
expected_response_for_internal_lifetime_error = EXPECTED_USER_CREATE_OR_UPDATE_RESPONSE_TEMPLATE.format(
success="false",
status_id="10",
response_id="999",
response_message="Lifetime internal error",
)
WireMockStubbing.register_soap_mapping(
wiremock=wiremock,
run_id=run_id,
soap_operations_url=USER_MANAGEMENT_SOAP_OPERATIONS_URL,
expected_request=expected_request_for_internal_lifetime_error,
expected_response=expected_response_for_internal_lifetime_error
)
expected_request_for_network_error = EXPECTED_USER_CREATE_OR_UPDATE_REQUEST_TEMPLATE.format(
username="kaboom_user",
password="kaboom_password",
encrypt_password="true",
name="kaboom_name",
email="kaboom_email",
role_name="kaboom_role_name",
)
WireMockStubbing.register_soap_mapping(
wiremock=wiremock,
run_id=run_id,
soap_operations_url=USER_MANAGEMENT_SOAP_OPERATIONS_URL,
expected_request=expected_request_for_network_error,
expected_response=None,
http_status_code=HTTPStatus.INTERNAL_SERVER_ERROR
)
DEFAULT_DOMAIN = "localhost:8433"
@pytest.fixture(autouse=True, scope="session")
def boostrap():
run_id = WireMockStubbing.new_run_id()
with SSL.do_not_verify():
_setup_mappings_for_user_create_or_update(run_id)
yield run_id
with SSL.do_not_verify():
wiremock.delete_by_run_id(run_id)
def test_when_user_create_or_update_is_successful():
with SSL.do_not_verify():
lifetime = LifetimeFacade()
user = lifetime.create_or_update_user(
domain=DEFAULT_DOMAIN,
authentication=LifetimeCredentials(username="admin_username", password="admin_password"),
user=LifetimeUser(
username="username",
password="password",
name="name",
email="email",
role="role_name",
),
encrypt_password=True
)
assert user.identifier > 0
assert user.username == "username"
assert user.password == "password"
assert user.name == "name"
assert user.email == "email"
assert user.role == "role_name"
def test_when_user_create_or_update_is_unsuccessful():
with SSL.do_not_verify():
lifetime = LifetimeFacade()
with pytest.raises(LifetimeError) as e:
_ = lifetime.create_or_update_user(
domain=DEFAULT_DOMAIN,
authentication=LifetimeCredentials(username="admin_username", password="admin_password"),
user=LifetimeUser(
username="unsuccessful_user",
password="password",
name="unsuccessful_name",
email="unsuccessful_email",
role="unsuccessful_role_name",
),
encrypt_password=True
)
assert e.value.error_code == 1000
assert e.value.error_message == "The email is invalid"
assert e.value.http_status_code == HTTPStatus.BAD_REQUEST
def test_when_user_create_or_update_with_internal_lifetime_error():
with SSL.do_not_verify():
lifetime = LifetimeFacade()
with pytest.raises(LifetimeError) as e:
_ = lifetime.create_or_update_user(
domain=DEFAULT_DOMAIN,
authentication=LifetimeCredentials(username="admin_username", password="admin_password"),
user=LifetimeUser(
username="internal_invalid_state_user",
password="internal_invalid_state_password",
name="internal_invalid_state_name",
email="internal_invalid_state_email",
role="internal_invalid_state_role_name",
),
encrypt_password=True
)
assert e.value.error_code == 999
assert e.value.error_message == "Lifetime internal error"
def test_when_user_create_or_update_with_catastrophic_error():
with SSL.do_not_verify():
lifetime = LifetimeFacade()
with pytest.raises(LifetimeError) as e:
_ = lifetime.create_or_update_user(
domain=DEFAULT_DOMAIN,
authentication=LifetimeCredentials(username="admin_username", password="admin_password"),
user=LifetimeUser(
username="kaboom_user",
password="kaboom_password",
name="kaboom_name",
email="kaboom_email",
role="kaboom_role_name",
),
encrypt_password=True
)
assert e.value.error_code == ""
assert e.value.error_message == "Server Error"
assert e.value.http_status_code == HTTPStatus.INTERNAL_SERVER_ERROR