Skip to content

Commit

Permalink
Merge pull request #26 from galaxy-genome-annotation/update_email
Browse files Browse the repository at this point in the history
Improve user update method + add tests
  • Loading branch information
abretaud authored Apr 22, 2020
2 parents 39a970b + 2e4e549 commit 065c398
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ Or with the Arrow client:
History
-------

- 4.2
- Improve user update method
- Add tests for user api
- 4.1
- Fix loading attributes from gff3
- Better handling of genome sequence update, with or without the no_reload_sequences option
Expand Down
22 changes: 19 additions & 3 deletions apollo/users/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def delete_user(self, user):
"""
return self.post('deleteUser', {'userToDelete': user})

def update_user(self, email, first_name, last_name, password, metadata={}):
def update_user(self, email, first_name, last_name, password=None, metadata={}, new_email=None):
"""
Update an existing user
Expand All @@ -233,21 +233,37 @@ def update_user(self, email, first_name, last_name, password, metadata={}):
:param last_name: User's last name
:type password: str
:param password: User's password
:param password: User's password (omit to keep untouched)
:type metadata: dict
:param metadata: User metadata
:type new_email: str
:param new_email: User's new email (if you want to change it)
:rtype: dict
:return: a dictionary containing user information
"""
data = {
'email': email,
'firstName': first_name,
'lastName': last_name,
'newPassword': password,
'metadata': metadata,
}

if password:
data['newPassword'] = password

# If updating the email, we need to give apollo a userId
if new_email:
user = self.show_user(email)
if not isinstance(user, list):
user = [user]
user = self._assert_user(user)

data['email'] = new_email
data['userId'] = user['userId']

response = self.post('updateUser', data)
return self._handle_empty(email, response)

Expand Down
2 changes: 1 addition & 1 deletion arrow/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '4.1'
__version__ = '4.2'
15 changes: 12 additions & 3 deletions arrow/commands/users/update_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,29 @@
@click.argument("email", type=str)
@click.argument("first_name", type=str)
@click.argument("last_name", type=str)
@click.argument("password", type=str)
@click.option(
"--password",
help="User's password (omit to keep untouched)",
type=str
)
@click.option(
"--metadata",
help="User metadata",
type=str
)
@click.option(
"--new_email",
help="User's new email (if you want to change it)",
type=str
)
@pass_context
@custom_exception
@dict_output
def cli(ctx, email, first_name, last_name, password, metadata={}):
def cli(ctx, email, first_name, last_name, password="", metadata={}, new_email=""):
"""Update an existing user
Output:
a dictionary containing user information
"""
return ctx.gi.users.update_user(email, first_name, last_name, password, metadata=metadata)
return ctx.gi.users.update_user(email, first_name, last_name, password=password, metadata=metadata, new_email=new_email)
8 changes: 5 additions & 3 deletions docs/commands/users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ Update the permissions of a user on a specified organism

**Usage**::

arrow users update_user [OPTIONS] EMAIL FIRST_NAME LAST_NAME PASSWORD
arrow users update_user [OPTIONS] EMAIL FIRST_NAME LAST_NAME

**Help**

Expand All @@ -286,6 +286,8 @@ Update an existing user
**Options**::


--metadata TEXT User metadata
-h, --help Show this message and exit.
--password TEXT User's password (omit to keep untouched)
--metadata TEXT User metadata
--new_email TEXT User's new email (if you want to change it)
-h, --help Show this message and exit.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
requests
biopython
cachetools
cachetools<4
click>=6.7
wrapt
pyyaml
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup(
name="apollo",
version='4.1',
version='4.2',
description="Apollo API library",
long_description=readme,
author="Helena Rasche;Anthony Bretaudeau",
Expand Down
36 changes: 34 additions & 2 deletions test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ def waitOrgDeleted(self, org_id):
"""

org_info = wa.organisms.show_organism(org_id)
if 'directory' in org_info:
tries = 1
while 'directory' in org_info and tries < 10:
time.sleep(1)
org_info = wa.organisms.show_organism(org_id)
tries += 1

return org_info

Expand All @@ -29,8 +31,38 @@ def waitOrgCreated(self, org_id):
"""

org_info = wa.organisms.show_organism(org_id)
if 'directory' not in org_info:
tries = 1
while 'directory' not in org_info and tries < 10:
time.sleep(1)
org_info = wa.organisms.show_organism(org_id)
tries += 1

return org_info

def waitUserDeleted(self, user_id):
"""
Wait for an user to be really deleted from Apollo
"""

user_info = wa.users.show_user(user_id)
tries = 1
while 'userId' in user_info and tries < 10:
time.sleep(1)
user_info = wa.users.show_user(user_id)
tries += 1

return user_info

def waitUserCreated(self, user_id):
"""
Wait for an user to be really created from Apollo
"""

user_info = wa.users.show_user(user_id)
tries = 1
while 'userId' not in user_info and tries < 10:
time.sleep(1)
user_info = wa.users.show_user(user_id)
tries += 1

return user_info
132 changes: 132 additions & 0 deletions test/user_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import time

from . import ApolloTestCase, wa


class UserTest(ApolloTestCase):

def test_get_users(self):

users = wa.users.get_users()

# We at least have admin + junior + temp from setup
assert len(users) >= 3

first_user = users[0]

assert 'firstName' in first_user
assert 'lastName' in first_user
assert 'inactive' in first_user
assert 'role' in first_user
assert 'availableGroups' in first_user
assert 'userCount' in first_user
assert 'searchName' in first_user
assert 'groups' in first_user
assert 'userId' in first_user
assert 'organismPermissions' in first_user
assert 'username' in first_user

def test_show_user(self):

users = wa.users.get_users()

user_id = users[0]['userId']

user_info = wa.users.show_user(user_id)

# userCount changes depending on what was requested to Apollo
del user_info['userCount']
del users[0]['userCount']

assert user_info == users[0]

def test_create_user(self):

meta = {"bla": "bli"}
res = wa.users.create_user("[email protected]", 'Poutrelle', 'Lapinou', 'superpassword', role="user", metadata=meta)
self.waitUserCreated(res['userId'])

res = wa.users.show_user('[email protected]')

assert res['username'] == '[email protected]'
assert res['firstName'] == 'Poutrelle'
assert res['lastName'] == 'Lapinou'
assert res['role'] == 'USER'

def test_delete_user(self):

user_info = wa.users.create_user("[email protected]", 'Tempxx', 'oraryxx', 'coolpasswordxx', role="user")
self.waitUserCreated(user_info['userId'])

wa.users.delete_user(user_info['username'])
self.waitUserDeleted(user_info['userId'])

users = wa.users.get_users()

for user in users:
assert user['username'] != '[email protected]'

def test_update_user(self):

user_info = wa.users.create_user("[email protected]", 'Tempxx', 'oraryxx', 'coolpasswordxx', role="user")
self.waitUserCreated(user_info['userId'])

wa.users.update_user(user_info['username'], 'firstname2', 'lastname2')

time.sleep(3)
res = wa.users.show_user('[email protected]')

assert res['username'] == '[email protected]'
assert res['firstName'] == 'firstname2'
assert res['lastName'] == 'lastname2'
assert res['role'] == 'USER'

def test_update_user_email(self):

user_info = wa.users.create_user("[email protected]", 'Tempxx', 'oraryxx', 'coolpasswordxx', role="user")
self.waitUserCreated(user_info['userId'])

wa.users.update_user(user_info['username'], 'firstname2', 'lastname2', new_email='[email protected]')

time.sleep(3)
res = wa.users.show_user('[email protected]')

assert res['username'] == '[email protected]'
assert res['firstName'] == 'firstname2'
assert res['lastName'] == 'lastname2'
assert res['role'] == 'USER'

def setUp(self):
# Make sure the user is not already there
temp_user_info = wa.users.show_user('[email protected]')
if 'username' in temp_user_info:
wa.users.delete_user(temp_user_info['username'])
self.waitUserDeleted(temp_user_info['userId'])

temp_user_info = wa.users.show_user('[email protected]')
if 'username' in temp_user_info:
wa.users.delete_user(temp_user_info['username'])
self.waitUserDeleted(temp_user_info['userId'])

temp_user_info = wa.users.show_user('[email protected]')
if 'username' in temp_user_info:
wa.users.delete_user(temp_user_info['username'])
self.waitUserDeleted(temp_user_info['userId'])

wa.users.create_user("[email protected]", 'Temp', 'orary', 'coolpassword', role="user")

def tearDown(self):
temp_user_info = wa.users.show_user('[email protected]')
if temp_user_info and 'username' in temp_user_info:
wa.users.delete_user(temp_user_info['username'])
self.waitUserDeleted(temp_user_info['userId'])

temp_user_info = wa.users.show_user('[email protected]')
if 'username' in temp_user_info:
wa.users.delete_user(temp_user_info['username'])
self.waitUserDeleted(temp_user_info['userId'])

temp_user_info = wa.users.show_user('[email protected]')
if 'username' in temp_user_info:
wa.users.delete_user(temp_user_info['username'])
self.waitUserDeleted(temp_user_info['userId'])

0 comments on commit 065c398

Please sign in to comment.