1+ import os
12import random
23import time
34
1112load_dotenv ()
1213
1314
15+ @pytest .fixture (scope = "module" )
16+ def integration_client ():
17+ token = os .getenv ("ESA_TOKEN" )
18+ team_name = os .getenv ("ESA_TEAM_NAME" )
19+ if not token or not team_name :
20+ pytest .skip ("ESA_TOKEN or ESA_TEAM_NAME not set for integration tests. Skipping integration tests." )
21+ try :
22+ client = EsaClient (token = token , team_name = team_name )
23+ return client
24+ except ValueError as e :
25+ pytest .fail (f"Failed to initialize EsaClient for integration tests: { e } . Is .env configured correctly?" )
26+ except Exception as e :
27+ pytest .fail (f"An unexpected error occurred during EsaClient initialization for integration tests: { e } " )
28+
29+
1430# Note: These tests require a valid .env file with ESA_TOKEN and ESA_TEAM_NAME
1531# and will make actual API calls to esa.io.
1632# They might be skipped by default unless explicitly run.
1733
1834
1935@pytest .mark .integration
20- def test_get_user_integration ():
36+ def test_get_user_integration (integration_client ):
2137 "Test get_user against the actual esa.io API."
38+ client = integration_client
2239 try :
23- # Create a real client instance (loads .env)
24- client = EsaClient ()
25- print (f"\n Running integration test for team: { client .team_name } " )
40+ print (f"\n Running get_user_integration test for team: { client .team_name } " )
2641 user_data = client .get_user ()
2742 print (f"Received user data: { user_data } " )
2843
@@ -32,19 +47,17 @@ def test_get_user_integration():
3247 assert "email" in user_data
3348 print ("Integration test passed." )
3449
35- except ValueError as e :
36- pytest .fail (f"Integration test setup failed: { e } . Is .env configured correctly?" )
3750 except requests .exceptions .RequestException as e :
3851 pytest .fail (f"Integration test API call failed: { e } " )
3952 except Exception as e :
4053 pytest .fail (f"Integration test unexpected error: { e } " )
4154
4255
4356@pytest .mark .integration
44- def test_get_posts_integration ():
57+ def test_get_posts_integration (integration_client ):
4558 "Test get_posts against the actual esa.io API (no params)."
59+ client = integration_client
4660 try :
47- client = EsaClient ()
4861 print (f"\n Running get_posts integration test for team: { client .team_name } " )
4962 posts_data = client .get_posts ()
5063 posts_list = posts_data .get ("posts" , [])
@@ -79,12 +92,12 @@ def test_get_posts_integration():
7992
8093
8194@pytest .mark .integration
82- def test_get_posts_pagination_integration ():
95+ def test_get_posts_pagination_integration (integration_client ):
8396 "Test get_posts pagination against the actual esa.io API."
97+ client = integration_client
8498 test_page = 2
8599 test_per_page = 5
86100 try :
87- client = EsaClient ()
88101 print (f"\n Running get_posts pagination integration test (page={ test_page } , per_page={ test_per_page } )" )
89102 posts_data = client .get_posts (page = test_page , per_page = test_per_page )
90103 posts_list = posts_data .get ("posts" , [])
@@ -122,13 +135,13 @@ def test_get_posts_pagination_integration():
122135
123136
124137@pytest .mark .integration
125- def test_get_post_integration ():
138+ def test_get_post_integration (integration_client ):
126139 "Test get_post against the actual esa.io API (existing post)."
140+ client = integration_client
127141 # Note: Assumes post number 1 exists in the test team.
128142 # Adjust post_number if necessary for your test environment.
129143 test_post_number = 1
130144 try :
131- client = EsaClient ()
132145 print (f"\n Running get_post integration test for post #{ test_post_number } " )
133146 post_data = client .get_post (post_number = test_post_number )
134147 print (f"Received post title: { post_data .get ('name' )} " )
@@ -153,12 +166,12 @@ def test_get_post_integration():
153166
154167
155168@pytest .mark .integration
156- def test_get_post_not_found_integration ():
169+ def test_get_post_not_found_integration (integration_client ):
157170 "Test get_post against the actual esa.io API (non-existent post)."
171+ client = integration_client
158172 # Use a post number that is guaranteed not to exist (e.g., 0 or a very large number)
159173 test_post_number = 0
160174 try :
161- client = EsaClient ()
162175 print (f"\n Running get_post integration test for non-existent post #{ test_post_number } " )
163176 with pytest .raises (requests .exceptions .HTTPError ) as exc_info :
164177 client .get_post (post_number = test_post_number )
@@ -180,9 +193,9 @@ def test_get_post_not_found_integration():
180193
181194
182195@pytest .mark .integration
183- def test_create_post_integration ():
196+ def test_create_post_integration (integration_client ):
184197 """Test create_post and verify the created post."""
185- client = EsaClient ()
198+ client = integration_client
186199 unique_name = f"Test Post - { int (time .time ())} "
187200 post_payload = {
188201 "name" : unique_name ,
@@ -240,9 +253,9 @@ def test_create_post_integration():
240253
241254
242255@pytest .mark .integration
243- def test_delete_post_integration ():
256+ def test_delete_post_integration (integration_client ):
244257 """Test create_post, then delete_post, and verify deletion."""
245- client = EsaClient ()
258+ client = integration_client
246259 unique_name = f"Test Post for Deletion - { int (time .time ())} "
247260 post_payload = {
248261 "name" : unique_name ,
@@ -284,9 +297,9 @@ def test_delete_post_integration():
284297
285298
286299@pytest .mark .integration
287- def test_delete_post_non_existent_integration ():
300+ def test_delete_post_non_existent_integration (integration_client ):
288301 """Test delete_post with a non-existent post_number."""
289- client = EsaClient ()
302+ client = integration_client
290303 non_existent_post_number = 0
291304 with pytest .raises (requests .exceptions .HTTPError ) as excinfo :
292305 client .delete_post (non_existent_post_number )
@@ -295,9 +308,9 @@ def test_delete_post_non_existent_integration():
295308
296309
297310@pytest .mark .integration
298- def test_update_post_integration ():
311+ def test_update_post_integration (integration_client ):
299312 """Test creating, updating, and verifying a post."""
300- client = EsaClient ()
313+ client = integration_client
301314 original_name = f"Original Post Title - { int (time .time ())} "
302315 updated_name = f"Updated Post Title - { int (time .time ())} "
303316 post_payload = {
@@ -354,9 +367,9 @@ def test_update_post_integration():
354367
355368
356369@pytest .mark .integration
357- def test_update_post_not_found_integration ():
370+ def test_update_post_not_found_integration (integration_client ):
358371 """Test updating a non-existent post."""
359- client = EsaClient ()
372+ client = integration_client
360373 non_existent_post_number = 0 # A post number that is guaranteed not to exist
361374 update_payload = {"name" : "Attempt to update non-existent post" , "body_md" : "This should not succeed." }
362375
@@ -368,9 +381,9 @@ def test_update_post_not_found_integration():
368381
369382
370383@pytest .mark .integration
371- def test_create_post_with_all_fields_integration ():
384+ def test_create_post_with_all_fields_integration (integration_client ):
372385 """Test create_post with all optional fields and verify them."""
373- client = EsaClient ()
386+ client = integration_client
374387 unique_suffix = int (time .time ())
375388 post_name = f"Test All Fields - { unique_suffix } "
376389 post_body = f"Body for all fields test created at { unique_suffix } ."
0 commit comments