-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathtest_baseapi.py
More file actions
137 lines (102 loc) · 3.98 KB
/
test_baseapi.py
File metadata and controls
137 lines (102 loc) · 3.98 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import json
import time
import pytest
def test_empty_project_id(base_api):
base_api._project_id = None
with pytest.raises(RuntimeError) as e:
base_api.fcm_end_point
assert str(e.value) == "Please provide a project_id either explicitly or through Google credentials."
def test_json_dumps(base_api):
json_string = base_api.json_dumps([{"test": "Test"}, {"test2": "Test2"}])
assert json_string == b'[{"test":"Test"},{"test2":"Test2"}]'
def test_parse_payload(base_api):
json_string = base_api.parse_payload(
fcm_token="test",
notification_title="test",
notification_body="test",
notification_image="test",
data_payload={"test": "test"},
topic_name="test",
topic_condition="test",
android_config={},
apns_config={},
webpush_config={},
fcm_options={},
dry_run=False,
)
data = json.loads(json_string.decode("utf-8"))
assert data["message"]["notification"] == {
"body": "test",
"title": "test",
"image": "test",
}
assert data["message"]["data"] == {"test": "test"}
def test_send_request_normal(base_api, mocker):
"""Test that send_request called once on success"""
success_response = mocker.Mock()
success_response.headers = {}
mock_session = mocker.Mock()
mock_session.post.side_effect = [success_response]
base_api.thread_local = mocker.Mock()
base_api.thread_local.requests_session = mock_session
base_api.thread_local.token_expiry = time.time() + 1000
# do
result = base_api.send_request(payload="test_payload", timeout=30)
# check
assert mock_session.post.call_count == 1
assert result == success_response
def test_send_request_retry_after(base_api, mocker):
"""Test that send_request retries when Retry-After header is present"""
# Mock time.sleep to avoid actual delays
mock_sleep = mocker.patch("time.sleep")
retry_response = mocker.Mock()
retry_response.headers = {"Retry-After": "2"}
success_response = mocker.Mock()
success_response.headers = {}
mock_session = mocker.Mock()
mock_session.post.side_effect = [retry_response, success_response]
base_api.thread_local = mocker.Mock()
base_api.thread_local.requests_session = mock_session
base_api.thread_local.token_expiry = time.time() + 1000
# do
result = base_api.send_request(payload="test_payload", timeout=30)
# check
assert mock_session.post.call_count == 2
mock_sleep.assert_called_once_with(2)
assert result == success_response
def test_send_request_access_token_expired_retry(base_api, mocker):
"""Test that send_request retries when ACCESS_TOKEN_EXPIRED error occurs"""
# Mock the expired token response
EXPIRED_STATUS_CODE = 401
expired_response = mocker.Mock()
expired_response.status_code = EXPIRED_STATUS_CODE
expired_response.headers = {}
expired_response.json.return_value = {
"error": {
"code": EXPIRED_STATUS_CODE,
"message": "Request had invalid authentication credentials.",
"status": "UNAUTHENTICATED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "ACCESS_TOKEN_EXPIRED",
"domain": "googleapis.com",
}
],
}
}
success_response = mocker.Mock()
success_response.status_code = 200
success_response.headers = {}
mock_session = mocker.Mock()
mock_session.post.side_effect = [expired_response, success_response]
mock_requests_session = mocker.patch.object(
type(base_api), "requests_session", new_callable=mocker.PropertyMock
)
mock_requests_session.return_value = mock_session
# do
result = base_api.send_request(payload="test_payload", timeout=30)
# check
assert mock_session.post.call_count == 2
assert base_api.thread_local.token_expiry == 0
assert result == success_response