1717
1818# Helper Functions
1919
20- def is_valid_person (obj ):
21- return isinstance (obj , ciscosparkapi .Person ) and obj .id is not None
22-
23-
24- def are_valid_people (iterable ):
25- return all ([is_valid_person (obj ) for obj in iterable ])
20+ def create_person (api , emails , ** person_attributes ):
21+ return api .people .create (emails , ** person_attributes )
2622
2723
2824def get_person_by_id (api , id ):
@@ -44,10 +40,6 @@ def get_person_by_email(api, email):
4440 return None
4541
4642
47- def create_person (api , emails , ** person_attributes ):
48- return api .people .create (emails , ** person_attributes )
49-
50-
5143def update_person (api , person , ** person_attributes ):
5244 # Get a copy of the person's current attributes
5345 new_attributes = person .json_data
@@ -63,14 +55,31 @@ def delete_person(api, person):
6355 api .people .delete (person .id )
6456
6557
58+ def is_valid_person (obj ):
59+ return isinstance (obj , ciscosparkapi .Person ) and obj .id is not None
60+
61+
62+ def are_valid_people (iterable ):
63+ return all ([is_valid_person (obj ) for obj in iterable ])
64+
65+
66+ def person_exists (api , person ):
67+ try :
68+ get_person_by_id (api , person .id )
69+ except ciscosparkapi .SparkApiError :
70+ return False
71+ else :
72+ return True
73+
74+
6675# pytest Fixtures
6776
6877@pytest .fixture (scope = "session" )
6978def me (api ):
7079 return api .people .me ()
7180
7281@pytest .fixture (scope = "session" )
73- def get_new_test_person (api , get_new_email_address , me , licenses_dict ):
82+ def get_test_person (api , get_new_email_address , me , licenses_dict ):
7483
7584 def inner_function ():
7685 person_email = get_new_email_address ()
@@ -95,10 +104,10 @@ def inner_function():
95104class PeopleManager (object ):
96105 """Creates, tracks and manages test accounts 'people' used by the tests."""
97106
98- def __init__ (self , api , get_new_test_person ):
107+ def __init__ (self , api , get_test_person ):
99108 super (PeopleManager , self ).__init__ ()
100109 self ._api = api
101- self ._get_new_test_person = get_new_test_person
110+ self ._get_new_test_person = get_test_person
102111 self .test_people = {}
103112
104113 def __getitem__ (self , item ):
@@ -133,17 +142,37 @@ def __del__(self):
133142
134143
135144@pytest .fixture (scope = "session" )
136- def test_people (api , get_new_test_person ):
137- test_people = PeopleManager (api , get_new_test_person )
145+ def test_people (api , get_test_person ):
146+ test_people = PeopleManager (api , get_test_person )
138147 yield test_people
139148 del test_people
140149
141150
142151@pytest .fixture ()
143- def temp_person (api , get_new_test_person ):
144- person = get_new_test_person ()
152+ def temp_person (api , get_random_email_address , me , licenses_dict ):
153+ # Get an e-mail address not currently used on Cisco Spark
154+ person_email = None
155+ person = True
156+ while person :
157+ person_email = get_random_email_address ()
158+ person = get_person_by_email (api , person_email )
159+
160+ # Create the person
161+ person = create_person (
162+ api ,
163+ emails = [person_email ],
164+ displayName = "ciscosparkapi" ,
165+ firstName = "ciscosparkapi" ,
166+ lastName = "ciscosparkapi" ,
167+ orgId = me .orgId ,
168+ licenses = [licenses_dict ["Messaging" ].id ],
169+ )
170+ assert is_valid_person (person )
171+
145172 yield person
146- delete_person (api , person )
173+
174+ if person_exists (api , person ):
175+ delete_person (api , person )
147176
148177
149178@pytest .fixture ()
0 commit comments