Skip to content

Commit c1073f6

Browse files
author
Luiko Czub
committed
new helper method - ensureUserExist #141
1 parent 69f70cf commit c1073f6

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

CHANGES.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ support for TL 1.9.20_fixed changes and py39
77

88
main topic is to support TL 1.9.20_fixed api changes
99

10+
implement 1.9.20_fixed new api interfaces - #141
11+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
13+
new TestlinkAPIGeneric and TestlinkAPIClient api method
14+
15+
- createUser(<login>, <firstname>, <lastname>, <email>, [password=<password>])
16+
17+
new TestlinkAPIClient helper method
18+
19+
- ensureUserExist(<login>, [firstname=<firstname>], [lastname=<lastname>],
20+
[email=<email>, [password=<password>])
21+
1022
implement 1.9.20_fixed changed api interfaces - #139
1123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1224

example/TestLinkExample.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,16 @@
121121
print(myTestLink.connectionInfo())
122122
print("")
123123

124-
# CHANGE this name into a valid account, known in your TL application
125-
myTestUserName="pyTLapi"
126-
myTestUserName2="admin"
124+
# ensure tester and expert users exists
125+
myTestUserName="myTester"
126+
myTestUser1_ID=myTestLink.ensureUserExist(myTestUserName,
127+
firstname="myFirstName", lastname="myLastName", mail="myTester@example.com")
128+
print("ensureUserExist", myTestUserName, myTestUser1_ID)
129+
130+
myTestUserName2="myExpert"
131+
myTestUser2_ID=myTestLink.ensureUserExist(myTestUserName2)
132+
print("checkUser", myTestUserName2, myTestUser2_ID)
133+
127134
# get user information
128135
response = myTestLink.getUserByLogin(myTestUserName)
129136
print("getUserByLogin", response)

src/testlink/testlinkapi.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from __future__ import print_function
2323
from .testlinkapigeneric import TestlinkAPIGeneric, TestLinkHelper
24-
from .testlinkerrors import TLArgError
24+
from .testlinkerrors import TLArgError, TLResponseError
2525
import sys
2626

2727

@@ -512,6 +512,36 @@ def getProjectIDByName(self, projectName):
512512
result = project['id']
513513
break
514514
return result
515+
516+
def ensureUserExist(self, login, **userArgs):
517+
""" combines getUserByLogin() + createUser()
518+
creates new user only, when login not exist
519+
520+
returns userID
521+
522+
userArgs defines optional key value pairs used to create new user
523+
- firstname Default 'unknown'
524+
- lastname Default 'via pyTLapi'
525+
- email Default 'unknown@example.com'
526+
- password Default None
527+
"""
528+
529+
try:
530+
response = self.getUserByLogin(login)
531+
userID = response[0]['dbID']
532+
except TLResponseError as tl_err:
533+
if tl_err.code == 10000:
534+
# Cannot Find User Login create new user
535+
name1 = userArgs.get('firstname', 'unknown')
536+
name2 = userArgs.get('lastname', 'via pyTLapi')
537+
mail = userArgs.get('email', 'unknown@example.com')
538+
pw = userArgs.get('password')
539+
userID = self.createUser(login, name1, name2, mail, password=pw)
540+
else:
541+
# seems to be another response failure - we forward it
542+
raise
543+
544+
return userID
515545

516546

517547
if __name__ == "__main__":

0 commit comments

Comments
 (0)