Skip to content

Commit 3f75f1b

Browse files
committed
Update API tests
1 parent 2dce549 commit 3f75f1b

1 file changed

Lines changed: 33 additions & 34 deletions

File tree

‎tests/substack/test_api.py‎

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,60 @@
99
load_dotenv()
1010

1111

12+
def _api_from_env() -> Api:
13+
cookies_string = os.getenv("COOKIES_STRING")
14+
cookies_path = os.getenv("COOKIES_PATH")
15+
publication_url = os.getenv("PUBLICATION_URL")
16+
if cookies_string or cookies_path:
17+
return Api(
18+
cookies_string=cookies_string,
19+
cookies_path=cookies_path,
20+
publication_url=publication_url,
21+
)
22+
return Api(
23+
email=os.getenv("EMAIL"),
24+
password=os.getenv("PASSWORD"),
25+
publication_url=publication_url,
26+
)
27+
28+
29+
_e2e = unittest.skipUnless(
30+
os.getenv("RUN_SUBSTACK_E2E"),
31+
"set RUN_SUBSTACK_E2E=1 and configure credentials to run live API tests",
32+
)
33+
34+
1235
class ApiTest(unittest.TestCase):
1336
def test_api_exception(self):
1437
with self.assertRaises(SubstackAPIException):
1538
Api(email="", password="")
1639

17-
def test_login(self):
18-
api = Api(
19-
email=os.getenv("EMAIL"),
20-
password=os.getenv("PASSWORD"),
21-
)
22-
self.assertIsNotNone(api)
23-
40+
@_e2e
2441
def test_get_posts(self):
25-
api = Api(email=os.getenv("EMAIL"), password=os.getenv("PASSWORD"))
42+
api = _api_from_env()
2643
posts = api.get_posts()
2744
self.assertIsNotNone(posts)
2845

46+
@_e2e
2947
def test_get_drafts(self):
30-
api = Api(
31-
email=os.getenv("EMAIL"),
32-
password=os.getenv("PASSWORD"),
33-
)
48+
api = _api_from_env()
3449
drafts = api.get_drafts()
3550
self.assertIsNotNone(drafts)
3651

37-
def test_post_draft(self):
38-
api = Api(
39-
email=os.getenv("EMAIL"),
40-
password=os.getenv("PASSWORD"),
41-
)
42-
posted_draft = api.post_draft([{"id": os.getenv("USER_ID"), "is_guest": False}])
43-
self.assertIsNotNone(posted_draft)
44-
52+
@_e2e
4553
def test_publication_users(self):
46-
api = Api(
47-
email=os.getenv("EMAIL"),
48-
password=os.getenv("PASSWORD"),
49-
)
54+
api = _api_from_env()
5055
users = api.get_publication_users()
5156
self.assertIsNotNone(users)
5257

53-
def test_put_draft(self):
54-
api = Api(
55-
email=os.getenv("EMAIL"),
56-
password=os.getenv("PASSWORD"),
57-
)
58-
posted_draft = api.put_draft("")
59-
self.assertIsNotNone(posted_draft)
60-
58+
@_e2e
6159
def test_get_categories(self):
62-
api = Api()
60+
api = _api_from_env()
6361
categories = api.get_categories()
6462
self.assertIsNotNone(categories)
6563

64+
@_e2e
6665
def test_get_single_category(self):
67-
api = Api()
66+
api = _api_from_env()
6867
category = api.get_single_category(4, "all", limit=100)
6968
self.assertIsNotNone(category)

0 commit comments

Comments
 (0)