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"""
@@ -26,19 +50,27 @@ def module_user(module_target_sat, module_org, module_location):
2650
2751
2852@pytest .fixture (scope = 'module' )
29- def default_viewer_role (module_target_sat , module_org , default_location ):
53+ def default_viewer_role (module_target_sat , module_org , default_location , viewer_role ):
3054 """Custom user with viewer role for tests validating visibility of entities or fields created
3155 by some other user. Created only when accessed, unlike `module_user`.
3256 """
33- viewer_role = module_target_sat .api .Role ().search (query = {'search' : 'name="Viewer"' })[0 ]
34- custom_password = gen_string ('alphanumeric' )
35- custom_user = module_target_sat .api .User (
57+ return UserFactory .create_user (
58+ target_sat = module_target_sat ,
3659 admin = False ,
3760 default_organization = module_org ,
3861 location = [default_location ],
3962 organization = [module_org ],
4063 role = [viewer_role ],
41- password = custom_password ,
42- ).create ()
43- custom_user .password = custom_password
44- return custom_user
64+ )
65+
66+
67+ @pytest .fixture (scope = 'module' )
68+ def module_user_viewer (module_target_sat , module_org , module_location , viewer_role ):
69+ """Non-admin user with Viewer role."""
70+ return UserFactory .create_user (
71+ target_sat = module_target_sat ,
72+ admin = False ,
73+ location = [module_location ],
74+ organization = [module_org ],
75+ role = [viewer_role ],
76+ )
0 commit comments