Repository navigation
Expand file tree
/
Copy pathtest_api.py
More file actions
91 lines (73 loc) · 2.72 KB
/
Copy pathtest_api.py
File metadata and controls
91 lines (73 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
import unittest
from unittest.mock import Mock, patch
from dotenv import load_dotenv
from substack import Api
from substack.exceptions import SubstackAPIException
load_dotenv()
def _api_from_env() -> Api:
cookies_string = os.getenv("COOKIES_STRING")
cookies_path = os.getenv("COOKIES_PATH")
publication_url = os.getenv("PUBLICATION_URL")
if cookies_string or cookies_path:
return Api(
cookies_string=cookies_string,
cookies_path=cookies_path,
publication_url=publication_url,
)
return Api(
email=os.getenv("EMAIL"),
password=os.getenv("PASSWORD"),
publication_url=publication_url,
)
_e2e = unittest.skipUnless(
os.getenv("RUN_SUBSTACK_E2E"),
"set RUN_SUBSTACK_E2E=1 and configure credentials to run live API tests",
)
class ApiTest(unittest.TestCase):
def test_api_exception(self):
response = Mock(status_code=401, text="Unauthorized")
with self.assertRaises(SubstackAPIException):
with patch("requests.Session.post", return_value=response):
Api(email="", password="")
def test_get_publication_subscriber_count_from_legacy_response(self):
api = Api.__new__(Api)
api.publication_url = "https://writer.substack.com/api/v1"
api._session = Mock()
response = Mock(status_code=200)
response.json.return_value = {"subscriberCount": 123}
api._session.get.return_value = response
self.assertEqual(api.get_publication_subscriber_count(), 123)
def test_get_publication_subscriber_count_from_subscribers(self):
api = Api.__new__(Api)
api.publication_url = "https://writer.substack.com/api/v1"
api._session = Mock()
response = Mock(status_code=200)
response.json.return_value = {"subscribers": [{"id": 1}, {"id": 2}]}
api._session.get.return_value = response
self.assertEqual(api.get_publication_subscriber_count(), 2)
@_e2e
def test_get_posts(self):
api = _api_from_env()
posts = api.get_posts()
self.assertIsNotNone(posts)
@_e2e
def test_get_drafts(self):
api = _api_from_env()
drafts = api.get_drafts()
self.assertIsNotNone(drafts)
@_e2e
def test_publication_users(self):
api = _api_from_env()
users = api.get_publication_users()
self.assertIsNotNone(users)
@_e2e
def test_get_categories(self):
api = _api_from_env()
categories = api.get_categories()
self.assertIsNotNone(categories)
@_e2e
def test_get_single_category(self):
api = _api_from_env()
category = api.get_single_category(4, "all", limit=100)
self.assertIsNotNone(category)