Skip to content

Commit bab37a4

Browse files
author
admin
committed
fix for app specific
1 parent 7a5875c commit bab37a4

File tree

1 file changed

+99
-55
lines changed

1 file changed

+99
-55
lines changed

app/locustio/common_utils.py

Lines changed: 99 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@
7373
'atl_token': '',
7474
'login': 'Log in'
7575
}
76+
LOGIN_BODY_CONFLUENCE = {
77+
'os_username': '',
78+
'os_password': '',
79+
'os_cookie': True,
80+
'os_destination': '',
81+
'login': 'Log in'
82+
}
7683

7784
JIRA_TOKEN_PATTERN = r'name="atlassian-token" content="(.+?)">'
7885
CONFLUENCE_TOKEN_PATTERN = r'"ajs-atl-token" content="(.+?)"'
@@ -241,7 +248,7 @@ def wrapper(*args, **kwargs):
241248
if total < confluence_action_time:
242249
sleep = (confluence_action_time - total)
243250
logger.info(f'action: {interaction}, action_execution_time: {total}, sleep {sleep}')
244-
time.sleep(sleep)
251+
time.sleep(5)
245252
return result
246253
return wrapper
247254
return deco_wrapper
@@ -349,6 +356,86 @@ def raise_if_login_failed(locust):
349356
raise exception.StopUser('Action login_and_view_dashboard failed')
350357

351358

359+
def do_confluence_login(locust, usr, pwd):
360+
locust.client.cookies.clear()
361+
r = locust.get('/dologin.action', catch_response=True)
362+
content = r.content.decode('utf-8')
363+
is_legacy_login_form = 'loginform' in content
364+
365+
if is_legacy_login_form:
366+
367+
login_body = LOGIN_BODY_CONFLUENCE
368+
login_body['os_username'] = usr
369+
login_body['os_password'] = pwd
370+
371+
locust.post('/dologin.action',
372+
login_body,
373+
TEXT_HEADERS,
374+
catch_response=True)
375+
else:
376+
377+
login_body = {'username': usr,
378+
'password': pwd,
379+
'rememberMe': 'True',
380+
'targetUrl': ''
381+
}
382+
383+
headers = {
384+
"Content-Type": "application/json"
385+
}
386+
387+
# 15 /rest/tsv/1.0/authenticate
388+
locust.post('/rest/tsv/1.0/authenticate',
389+
json=login_body,
390+
headers=headers,
391+
catch_response=True)
392+
393+
394+
def do_login_jira(locust, usr, pwd):
395+
locust.client.cookies.clear()
396+
body = LOGIN_BODY
397+
body['os_username'] = usr
398+
body['os_password'] = pwd
399+
400+
legacy_form = False
401+
402+
# Check if 2sv login form
403+
r = locust.get('/login.jsp', catch_response=True)
404+
content = r.content.decode('utf-8')
405+
if 'login-form-remember-me' in content:
406+
legacy_form = True
407+
408+
# 100 /login.jsp
409+
if legacy_form:
410+
locust.post('/login.jsp', body,
411+
TEXT_HEADERS,
412+
catch_response=True)
413+
else:
414+
login_body = {'username': usr,
415+
'password': pwd,
416+
'rememberMe': 'True',
417+
'targetUrl': ''
418+
}
419+
420+
headers = {
421+
"Content-Type": "application/json"
422+
}
423+
424+
# 15 /rest/tsv/1.0/authenticate
425+
locust.post('/rest/tsv/1.0/authenticate',
426+
json=login_body,
427+
headers=headers,
428+
catch_response=True)
429+
430+
r = locust.get('/', catch_response=True)
431+
if not r.content:
432+
raise Exception('Please check server hostname in jira.yml file')
433+
if locust.session_data_storage['token_pattern']:
434+
content = r.content.decode('utf-8')
435+
token = fetch_by_re(locust.session_data_storage['token_pattern'], content)
436+
locust.session_data_storage["token"] = token
437+
438+
352439
def run_as_specific_user(username=None, password=None):
353440
if not (username and password):
354441
raise SystemExit(f'The credentials are not valid: {{username: {username}, password: {password}}}.')
@@ -367,72 +454,29 @@ def wrapper(*args, **kwargs):
367454
session_user_name = locust.session_data_storage["username"]
368455
session_user_password = locust.session_data_storage["password"]
369456
app = locust.session_data_storage['app']
457+
locust.session_data_storage['token_pattern'] = None
370458
app_type = locust.session_data_storage.get('app_type', None)
371459
token_pattern = None
372460

373461
# Jira or JSM Agent - redefine token value
374462
if app == JIRA or (app == JSM and app_type == TYPE_AGENT):
375-
token_pattern = JIRA_TOKEN_PATTERN
463+
locust.session_data_storage['token_pattern'] = JIRA_TOKEN_PATTERN
376464
# Confluence - redefine token value
377465
elif app == CONFLUENCE:
378-
token_pattern = CONFLUENCE_TOKEN_PATTERN
379-
380-
def do_login_jira(usr, pwd):
381-
locust.client.cookies.clear()
382-
body = LOGIN_BODY
383-
body['os_username'] = usr
384-
body['os_password'] = pwd
385-
386-
legacy_form = False
387-
388-
# Check if 2sv login form
389-
r = locust.get('/login.jsp', catch_response=True)
390-
content = r.content.decode('utf-8')
391-
if 'login-form-remember-me' in content:
392-
legacy_form = True
393-
394-
# 100 /login.jsp
395-
if legacy_form:
396-
locust.post('/login.jsp', body,
397-
TEXT_HEADERS,
398-
catch_response=True)
399-
else:
400-
login_body = {'username': usr,
401-
'password': pwd,
402-
'rememberMe': 'True',
403-
'targetUrl': ''
404-
}
405-
406-
headers = {
407-
"Content-Type": "application/json"
408-
}
409-
410-
# 15 /rest/tsv/1.0/authenticate
411-
locust.post('/rest/tsv/1.0/authenticate',
412-
json=login_body,
413-
headers=headers,
414-
catch_response=True)
415-
416-
r = locust.get('/', catch_response=True)
417-
if not r.content:
418-
raise Exception('Please check server hostname in jira.yml file')
419-
if token_pattern:
420-
content = r.content.decode('utf-8')
421-
token = fetch_by_re(token_pattern, content)
422-
locust.session_data_storage["token"] = token
466+
locust.session_data_storage['token_pattern'] = CONFLUENCE_TOKEN_PATTERN
423467

424468
# send requests by the specific user
425469
if app == JIRA or (app == JSM and app_type == TYPE_AGENT):
426-
do_login_jira(usr=username, pwd=password)
427-
else:
428-
raise SystemExit(f"Unsupported app type: {app}")
470+
do_login_jira(locust, username, password)
471+
func(*args, **kwargs)
472+
do_login_jira(locust, session_user_name, session_user_password)
429473

430-
func(*args, **kwargs)
474+
if app == CONFLUENCE:
475+
do_confluence_login(locust, username, password)
476+
func(*args, **kwargs)
477+
do_confluence_login(locust, session_user_name, session_user_password)
431478

432-
# send requests by the session user
433-
if app == JIRA or (app == JSM and app_type == TYPE_AGENT):
434-
do_login_jira(usr=session_user_name, pwd=session_user_password)
435-
else:
479+
if app not in [CONFLUENCE, JIRA, JSM]:
436480
raise SystemExit(f"Unsupported app type: {app}")
437481

438482
else:

0 commit comments

Comments
 (0)