33import os
44import sys
55import unittest
6- from irods .exception import UserGroupDoesNotExist
6+ import tempfile
7+ import shutil
8+ from irods .exception import UserGroupDoesNotExist , UserDoesNotExist
79from irods .meta import iRODSMetaCollection , iRODSMeta
810from irods .models import User , UserGroup , UserMeta
11+ from irods .session import iRODSSession
12+ import irods .exception as ex
913import irods .test .helpers as helpers
1014from six .moves import range
1115
@@ -20,6 +24,95 @@ def tearDown(self):
2024 '''
2125 self .sess .cleanup ()
2226
27+ def test_modify_password__328 (self ):
28+ ses = self .sess
29+ if ses .users .get ( ses .username ).type != 'rodsadmin' :
30+ self .skipTest ( 'Only a rodsadmin may run this test.' )
31+
32+ OLDPASS = 'apass'
33+ NEWPASS = 'newpass'
34+ try :
35+ ses .users .create ('alice' , 'rodsuser' )
36+ ses .users .modify ('alice' , 'password' , OLDPASS )
37+
38+ with iRODSSession (user = 'alice' , password = OLDPASS , host = ses .host , port = ses .port , zone = ses .zone ) as alice :
39+ me = alice .users .get (alice .username )
40+ me .modify_password (OLDPASS , NEWPASS )
41+
42+ with iRODSSession (user = 'alice' , password = NEWPASS , host = ses .host , port = ses .port , zone = ses .zone ) as alice :
43+ home = helpers .home_collection ( alice )
44+ alice .collections .get ( home ) # Non-trivial operation to test success!
45+ finally :
46+ try :
47+ ses .users .get ('alice' ).remove ()
48+ except UserDoesNotExist :
49+ pass
50+
51+ @staticmethod
52+ def do_something (session ):
53+ return session .username in [i [User .name ] for i in session .query (User )]
54+
55+ def test_modify_password_with_changing_auth_file__328 (self ):
56+ ses = self .sess
57+ if ses .users .get ( ses .username ).type != 'rodsadmin' :
58+ self .skipTest ( 'Only a rodsadmin may run this test.' )
59+ OLDPASS = 'apass'
60+ def generator (p = OLDPASS ):
61+ n = 1
62+ old_pw = p
63+ while True :
64+ pw = p + str (n )
65+ yield old_pw , pw
66+ n += 1 ; old_pw = pw
67+ password_generator = generator ()
68+ ENV_DIR = tempfile .mkdtemp ()
69+ d = dict (password = OLDPASS , user = 'alice' , host = ses .host , port = ses .port , zone = ses .zone )
70+ (alice_env , alice_auth ) = helpers .make_environment_and_auth_files (ENV_DIR , ** d )
71+ try :
72+ ses .users .create ('alice' , 'rodsuser' )
73+ ses .users .modify ('alice' , 'password' , OLDPASS )
74+ for modify_option , sess_factory in [ (alice_auth , lambda : iRODSSession (** d )),
75+ (True ,
76+ lambda : helpers .make_session (irods_env_file = alice_env ,
77+ irods_authentication_file = alice_auth )) ]:
78+ OLDPASS ,NEWPASS = next (password_generator )
79+ with sess_factory () as alice_ses :
80+ alice = alice_ses .users .get (alice_ses .username )
81+ alice .modify_password (OLDPASS , NEWPASS , modify_irods_authentication_file = modify_option )
82+ d ['password' ] = NEWPASS
83+ with iRODSSession (** d ) as session :
84+ self .do_something (session ) # can we still do stuff with the final value of the password?
85+ finally :
86+ shutil .rmtree (ENV_DIR )
87+ ses .users .remove ('alice' )
88+
89+ def test_modify_password_with_incorrect_old_value__328 (self ):
90+ ses = self .sess
91+ if ses .users .get ( ses .username ).type != 'rodsadmin' :
92+ self .skipTest ( 'Only a rodsadmin may run this test.' )
93+ OLDPASS = 'apass'
94+ NEWPASS = 'newpass'
95+ ENV_DIR = tempfile .mkdtemp ()
96+ try :
97+ ses .users .create ('alice' , 'rodsuser' )
98+ ses .users .modify ('alice' , 'password' , OLDPASS )
99+ d = dict (password = OLDPASS , user = 'alice' , host = ses .host , port = ses .port , zone = ses .zone )
100+ (alice_env , alice_auth ) = helpers .make_environment_and_auth_files (ENV_DIR , ** d )
101+ session_factories = [
102+ (lambda : iRODSSession (** d )),
103+ (lambda : helpers .make_session ( irods_env_file = alice_env , irods_authentication_file = alice_auth )),
104+ ]
105+ for factory in session_factories :
106+ with factory () as alice_ses :
107+ alice = alice_ses .users .get (alice_ses .username )
108+ with self .assertRaises ( ex .CAT_PASSWORD_ENCODING_ERROR ):
109+ alice .modify_password (OLDPASS + "." , NEWPASS )
110+ with iRODSSession (** d ) as alice_ses :
111+ self .do_something (alice_ses )
112+ finally :
113+ shutil .rmtree (ENV_DIR )
114+ ses .users .remove ('alice' )
115+
23116 def test_create_group (self ):
24117 group_name = "test_group"
25118
0 commit comments