Skip to content

Commit 3a6c709

Browse files
committed
DCA-2451 Locust Jira - run as app specific user 2sv
1 parent d5974aa commit 3a6c709

File tree

1 file changed

+69
-33
lines changed

1 file changed

+69
-33
lines changed

app/locustio/common_utils.py

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
import functools
2-
3-
from locust import events
4-
import time
51
import csv
6-
import re
2+
import functools
3+
import inspect
4+
import json
75
import logging
86
import random
9-
import string
10-
import json
7+
import re
118
import socket
12-
from logging.handlers import RotatingFileHandler
9+
import string
10+
import time
1311
from datetime import datetime
12+
from logging.handlers import RotatingFileHandler
13+
14+
from locust import exception, TaskSet, events
15+
1416
from util.conf import JIRA_SETTINGS, CONFLUENCE_SETTINGS, JSM_SETTINGS, BAMBOO_SETTINGS, BaseAppSettings
1517
from util.project_paths import ENV_TAURUS_ARTIFACT_DIR
16-
from locust import exception
17-
import inspect
18-
from locust import TaskSet
19-
2018

2119
TEXT_HEADERS = {
2220
'Accept-Language': 'en-US,en;q=0.5',
@@ -66,10 +64,16 @@
6664
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
6765
"X-Atlassian-Token": "no-check"
6866
}
67+
LOGIN_BODY = {
68+
'os_username': '',
69+
'os_password': '',
70+
'os_destination': '',
71+
'os_cookie': True,
72+
'user_role': '',
73+
'atl_token': '',
74+
'login': 'Log in'
75+
}
6976

70-
JIRA_API_URL = '/'
71-
CONFLUENCE_API_URL = '/'
72-
BAMBOO_API_URL = '/'
7377
JIRA_TOKEN_PATTERN = r'name="atlassian-token" content="(.+?)">'
7478
CONFLUENCE_TOKEN_PATTERN = r'"ajs-atl-token" content="(.+?)"'
7579

@@ -368,36 +372,68 @@ def wrapper(*args, **kwargs):
368372

369373
# Jira or JSM Agent - redefine token value
370374
if app == JIRA or (app == JSM and app_type == TYPE_AGENT):
371-
url = JIRA_API_URL
372375
token_pattern = JIRA_TOKEN_PATTERN
373-
# JSM Customer
374-
elif app == JSM and app_type == TYPE_CUSTOMER:
375-
url = JIRA_API_URL
376376
# Confluence - redefine token value
377377
elif app == CONFLUENCE:
378-
url = CONFLUENCE_API_URL
379378
token_pattern = CONFLUENCE_TOKEN_PATTERN
380-
# Bamboo
381-
elif app == BAMBOO:
382-
url = BAMBOO_API_URL
383-
else:
384-
raise Exception(f'The "{app}" application type is not known.')
385379

386-
def do_login(usr, pwd):
380+
def do_login_jira(usr, pwd):
387381
locust.client.cookies.clear()
388-
r = locust.get(url, auth=(usr, pwd), catch_response=True)
389-
if token_pattern:
390-
content = r.content.decode('utf-8')
391-
token = fetch_by_re(token_pattern, content)
392-
locust.session_data_storage["token"] = token
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
393423

394424
# send requests by the specific user
395-
do_login(usr=username, pwd=password)
425+
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}")
396429

397430
func(*args, **kwargs)
398431

399432
# send requests by the session user
400-
do_login(usr=session_user_name, pwd=session_user_password)
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:
436+
raise SystemExit(f"Unsupported app type: {app}")
401437

402438
else:
403439
raise SystemExit(f"There is no 'locust' object in the '{func.__name__}' function.")

0 commit comments

Comments
 (0)