1- from fauxfactory import gen_alphanumeric , gen_string
1+ from fauxfactory import gen_alphanumeric
22import pytest
33
44
5+ class UserFactory :
6+ """Helper class for more complex operations with users that can be reused in fixtures."""
7+
8+ @staticmethod
9+ def create_user (target_sat , ** params ):
10+ """Create and return a user object. Set the password if not specified.
11+ Args:
12+ target_sat: Satellite object
13+ params: parameters passed to the User object
14+ Returns:
15+ User object
16+ """
17+ params .setdefault ('password' , gen_alphanumeric ())
18+ user = target_sat .api .User (** params ).create ()
19+ user .password = params ['password' ]
20+ return user
21+
22+
23+ @pytest .fixture (scope = 'session' )
24+ def viewer_role (session_target_sat ):
25+ """Viewer role."""
26+ return session_target_sat .api .Role ().search (query = {'search' : 'name="Viewer"' })[0 ]
27+
28+
529@pytest .fixture (scope = 'class' )
630def class_user_password ():
731 """Generate a random password for a user, and capture it so a test has access to it"""
@@ -21,19 +45,27 @@ def module_user(module_target_sat, module_org, module_location):
2145
2246
2347@pytest .fixture (scope = 'module' )
24- def default_viewer_role (module_target_sat , module_org , default_location ):
48+ def default_viewer_role (module_target_sat , module_org , default_location , viewer_role ):
2549 """Custom user with viewer role for tests validating visibility of entities or fields created
2650 by some other user. Created only when accessed, unlike `module_user`.
2751 """
28- viewer_role = module_target_sat .api .Role ().search (query = {'search' : 'name="Viewer"' })[0 ]
29- custom_password = gen_string ('alphanumeric' )
30- custom_user = module_target_sat .api .User (
52+ return UserFactory .create_user (
53+ target_sat = module_target_sat ,
3154 admin = False ,
3255 default_organization = module_org ,
3356 location = [default_location ],
3457 organization = [module_org ],
3558 role = [viewer_role ],
36- password = custom_password ,
37- ).create ()
38- custom_user .password = custom_password
39- return custom_user
59+ )
60+
61+
62+ @pytest .fixture (scope = 'module' )
63+ def module_user_viewer (module_target_sat , module_org , module_location , viewer_role ):
64+ """Non-admin user with Viewer role."""
65+ return UserFactory .create_user (
66+ target_sat = module_target_sat ,
67+ admin = False ,
68+ location = [module_location ],
69+ organization = [module_org ],
70+ role = [viewer_role ],
71+ )
0 commit comments