|
9 | 9 | load_dotenv() |
10 | 10 |
|
11 | 11 |
|
| 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 | + |
12 | 35 | class ApiTest(unittest.TestCase): |
13 | 36 | def test_api_exception(self): |
14 | 37 | with self.assertRaises(SubstackAPIException): |
15 | 38 | Api(email="", password="") |
16 | 39 |
|
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 |
24 | 41 | def test_get_posts(self): |
25 | | - api = Api(email=os.getenv("EMAIL"), password=os.getenv("PASSWORD")) |
| 42 | + api = _api_from_env() |
26 | 43 | posts = api.get_posts() |
27 | 44 | self.assertIsNotNone(posts) |
28 | 45 |
|
| 46 | + @_e2e |
29 | 47 | def test_get_drafts(self): |
30 | | - api = Api( |
31 | | - email=os.getenv("EMAIL"), |
32 | | - password=os.getenv("PASSWORD"), |
33 | | - ) |
| 48 | + api = _api_from_env() |
34 | 49 | drafts = api.get_drafts() |
35 | 50 | self.assertIsNotNone(drafts) |
36 | 51 |
|
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 |
45 | 53 | def test_publication_users(self): |
46 | | - api = Api( |
47 | | - email=os.getenv("EMAIL"), |
48 | | - password=os.getenv("PASSWORD"), |
49 | | - ) |
| 54 | + api = _api_from_env() |
50 | 55 | users = api.get_publication_users() |
51 | 56 | self.assertIsNotNone(users) |
52 | 57 |
|
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 |
61 | 59 | def test_get_categories(self): |
62 | | - api = Api() |
| 60 | + api = _api_from_env() |
63 | 61 | categories = api.get_categories() |
64 | 62 | self.assertIsNotNone(categories) |
65 | 63 |
|
| 64 | + @_e2e |
66 | 65 | def test_get_single_category(self): |
67 | | - api = Api() |
| 66 | + api = _api_from_env() |
68 | 67 | category = api.get_single_category(4, "all", limit=100) |
69 | 68 | self.assertIsNotNone(category) |
0 commit comments