-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.py
176 lines (142 loc) · 6.03 KB
/
main_test.py
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import flask
import os
import pytest
import re
import responses
import requests
from responses import matchers
from unittest.mock import patch, Mock
from werkzeug.exceptions import HTTPException
from test_common import *
from gcp_test import get_smsc_mock, setup_gcp_responses, GCF_PROJECT_ID
from common import X_BZ_SHARED_SECRET
from gcp import GcpLogger
from main import gcp_iconik_handler
import iconik
# Make a Flask app so we can do app.test_request_context()
@pytest.fixture(scope="module")
def app():
return flask.Flask(__name__)
# Make a Flask app so we can do app.test_request_context()
@pytest.fixture(scope="module")
def logger():
return GcpLogger(GCF_PROJECT_ID)
@pytest.fixture(scope="function", autouse=True)
def setup_secrets(request):
setup_gcp_responses()
with patch("gcp.secretmanager.SecretManagerServiceClient", new_callable=get_smsc_mock) as mock_smc:
yield mock_smc
@responses.activate
def test_iconik_handler_add(app, logger):
with app.test_request_context(
path=f'/add?b2_storage_id={B2_STORAGE_ID}&ll_storage_id={LL_STORAGE_ID}',
method='POST',
json=PAYLOAD,
headers={X_BZ_SHARED_SECRET: SHARED_SECRET}):
response = flask.Response(gcp_iconik_handler(flask.request))
assert 200 == response.status_code
assert 'OK' == response.get_data(as_text=True)
assert_copy_call_counts(LL_STORAGE_ID, format_count=2)
@responses.activate
def test_iconik_handler_remove(app, logger):
with app.test_request_context(
path=f'/remove?b2_storage_id={B2_STORAGE_ID}&ll_storage_id={LL_STORAGE_ID}',
method='POST',
json=PAYLOAD,
headers={X_BZ_SHARED_SECRET: SHARED_SECRET}):
response = flask.Response(gcp_iconik_handler(flask.request))
assert 200 == response.status_code
assert 'OK' == response.get_data(as_text=True)
assert_copy_call_counts(B2_STORAGE_ID, format_count=1)
assert_delete_call_counts()
@responses.activate
def test_iconik_handler_400_invalid_content(app, logger):
with app.test_request_context(
path=f'/add?b2_storage_id={B2_STORAGE_ID}&ll_storage_id={LL_STORAGE_ID}',
method='POST',
data='This is not JSON!',
headers={X_BZ_SHARED_SECRET: SHARED_SECRET}):
with pytest.raises(HTTPException) as httperror:
response = gcp_iconik_handler(flask.request)
assert 400 == httperror.value.code
@responses.activate
def test_iconik_handler_400_missing_content(app, logger):
with app.test_request_context(
path=f'/add?b2_storage_id={B2_STORAGE_ID}&ll_storage_id={LL_STORAGE_ID}',
method='POST',
headers={X_BZ_SHARED_SECRET: SHARED_SECRET}):
with pytest.raises(HTTPException) as httperror:
response = gcp_iconik_handler(flask.request)
assert 400 == httperror.value.code
@responses.activate
def test_iconik_handler_400_invalid_context(app, logger):
json = dict(PAYLOAD)
json["context"] = "INVALID"
with app.test_request_context(
path=f'/add?b2_storage_id={B2_STORAGE_ID}&ll_storage_id={LL_STORAGE_ID}',
method='POST',
json=json,
headers={X_BZ_SHARED_SECRET: SHARED_SECRET}):
with pytest.raises(HTTPException) as httperror:
response = gcp_iconik_handler(flask.request)
assert 400 == httperror.value.code
@responses.activate
def test_iconik_handler_400_missing_context(app, logger):
json = dict(PAYLOAD)
del json["context"]
with app.test_request_context(
path=f'/add?b2_storage_id={B2_STORAGE_ID}&ll_storage_id={LL_STORAGE_ID}',
method='POST',
json=json,
headers={X_BZ_SHARED_SECRET: SHARED_SECRET}):
with pytest.raises(HTTPException) as httperror:
response = gcp_iconik_handler(flask.request)
assert 400 == httperror.value.code
@responses.activate
def test_iconik_handler_401_invalid(app, logger):
with app.test_request_context(
path=f'/add?b2_storage_id={B2_STORAGE_ID}&ll_storage_id={LL_STORAGE_ID}',
method='POST',
json=PAYLOAD,
headers={X_BZ_SHARED_SECRET: 'dummy'}):
with pytest.raises(HTTPException) as httperror:
response = gcp_iconik_handler(flask.request)
assert 401 == httperror.value.code
@responses.activate
def test_iconik_handler_401_missing(app, logger):
with app.test_request_context(
path=f'/add?b2_storage_id={B2_STORAGE_ID}&ll_storage_id={LL_STORAGE_ID}',
method='POST',
json=PAYLOAD):
with pytest.raises(HTTPException) as httperror:
response = gcp_iconik_handler(flask.request)
assert 401 == httperror.value.code
@responses.activate
def test_iconik_handler_404(app, logger):
with app.test_request_context(
path=f'/invalid?b2_storage_id={B2_STORAGE_ID}&ll_storage_id={LL_STORAGE_ID}',
method='POST',
json=PAYLOAD,
headers={X_BZ_SHARED_SECRET: SHARED_SECRET}):
with pytest.raises(HTTPException) as httperror:
response = gcp_iconik_handler(flask.request)
assert 404 == httperror.value.code
@responses.activate
def test_iconik_handler_405(app, logger):
with app.test_request_context(
path=f'/add?b2_storage_id={B2_STORAGE_ID}&ll_storage_id={LL_STORAGE_ID}',
method='GET',
headers={X_BZ_SHARED_SECRET: SHARED_SECRET}):
with pytest.raises(HTTPException) as httperror:
response = gcp_iconik_handler(flask.request)
assert 405 == httperror.value.code
@responses.activate
def test_iconik_handler_500(app, logger):
with app.test_request_context(
path=f'/add?b2_storage_id={B2_STORAGE_ID}&ll_storage_id={INVALID_STORAGE_ID}',
method='POST',
json=PAYLOAD,
headers={X_BZ_SHARED_SECRET: SHARED_SECRET}):
with pytest.raises(HTTPException) as httperror:
response = gcp_iconik_handler(flask.request)
assert 500 == httperror.value.code