forked from GoogleCloudPlatform/functions-framework-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_view_functions.py
More file actions
221 lines (174 loc) · 7.19 KB
/
Copy pathtest_view_functions.py
File metadata and controls
221 lines (174 loc) · 7.19 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import pretend
import pytest
import werkzeug
from cloudevents.http import from_http
import functions_framework
def test_http_view_func_wrapper():
function = pretend.call_recorder(lambda request: "Hello")
request_object = pretend.stub()
local_proxy = pretend.stub(_get_current_object=lambda: request_object, headers={})
view_func = functions_framework._http_view_func_wrapper(function, local_proxy)
view_func("/some/path")
assert function.calls == [pretend.call(request_object)]
def test_http_view_func_wrapper_attribute_copied():
def function(_):
pass
function.attribute = "foo"
view_func = functions_framework._http_view_func_wrapper(function, pretend.stub())
assert view_func.__name__ == "function"
assert view_func.attribute == "foo"
def test_event_view_func_wrapper(monkeypatch):
data = pretend.stub()
json = {
"context": {
"eventId": "some-eventId",
"timestamp": "some-timestamp",
"eventType": "some-eventType",
"resource": "some-resource",
},
"data": data,
}
request = pretend.stub(headers={}, get_json=lambda: json)
context_stub = pretend.stub()
context_class = pretend.call_recorder(lambda *a, **kw: context_stub)
monkeypatch.setattr(functions_framework, "Context", context_class)
function = pretend.call_recorder(lambda data, context: "Hello")
view_func = functions_framework._event_view_func_wrapper(function, request)
view_func("/some/path")
assert function.calls == [pretend.call(data, context_stub)]
assert context_class.calls == [
pretend.call(
eventId="some-eventId",
timestamp="some-timestamp",
eventType="some-eventType",
resource="some-resource",
)
]
def test_event_view_func_wrapper_bad_request(monkeypatch):
request = pretend.stub(headers={}, get_json=lambda: None)
context_stub = pretend.stub()
context_class = pretend.call_recorder(lambda *a, **kw: context_stub)
monkeypatch.setattr(functions_framework, "Context", context_class)
function = pretend.call_recorder(lambda data, context: "Hello")
view_func = functions_framework._event_view_func_wrapper(function, request)
with pytest.raises(werkzeug.exceptions.BadRequest):
view_func("/some/path")
def test_run_cloud_event():
headers = {"Content-Type": "application/cloudevents+json"}
data = json.dumps(
{
"source": "from-galaxy-far-far-away",
"type": "cloud_event.greet.you",
"specversion": "1.0",
"id": "f6a65fcd-eed2-429d-9f71-ec0663d83025",
"time": "2020-08-13T02:12:14.946587+00:00",
"data": {"name": "john"},
}
)
request = pretend.stub(headers=headers, get_data=lambda: data)
function = pretend.call_recorder(lambda cloud_event: "hello")
functions_framework._run_cloud_event(function, request)
expected_cloud_event = from_http(request.headers, request.get_data())
assert function.calls == [pretend.call(expected_cloud_event)]
def test_cloud_event_view_func_wrapper():
headers = {"Content-Type": "application/cloudevents+json"}
data = json.dumps(
{
"source": "from-galaxy-far-far-away",
"type": "cloud_event.greet.you",
"specversion": "1.0",
"id": "f6a65fcd-eed2-429d-9f71-ec0663d83025",
"time": "2020-08-13T02:12:14.946587+00:00",
"data": {"name": "john"},
}
)
request = pretend.stub(headers=headers, get_data=lambda: data)
event = from_http(request.headers, request.get_data())
function = pretend.call_recorder(lambda cloud_event: cloud_event)
view_func = functions_framework._cloud_event_view_func_wrapper(function, request)
view_func("/some/path")
assert function.calls == [pretend.call(event)]
def test_binary_cloud_event_view_func_wrapper():
headers = {
"ce-specversion": "1.0",
"ce-source": "from-galaxy-far-far-away",
"ce-type": "cloud_event.greet.you",
"ce-id": "f6a65fcd-eed2-429d-9f71-ec0663d83025",
"ce-time": "2020-08-13T02:12:14.946587+00:00",
}
data = json.dumps({"name": "john"})
request = pretend.stub(headers=headers, get_data=lambda: data)
event = from_http(request.headers, request.get_data())
function = pretend.call_recorder(lambda cloud_event: cloud_event)
view_func = functions_framework._cloud_event_view_func_wrapper(function, request)
view_func("/some/path")
assert function.calls == [pretend.call(event)]
def test_binary_event_view_func_wrapper(monkeypatch):
data = pretend.stub()
request = pretend.stub(
headers={
"ce-type": "something",
"ce-specversion": "something",
"ce-source": "something",
"ce-id": "something",
"ce-eventId": "some-eventId",
"ce-timestamp": "some-timestamp",
"ce-eventType": "some-eventType",
"ce-resource": "some-resource",
},
get_data=lambda: data,
)
context_stub = pretend.stub()
context_class = pretend.call_recorder(lambda *a, **kw: context_stub)
monkeypatch.setattr(functions_framework, "Context", context_class)
function = pretend.call_recorder(lambda data, context: "Hello")
view_func = functions_framework._event_view_func_wrapper(function, request)
view_func("/some/path")
assert function.calls == [pretend.call(data, context_stub)]
assert context_class.calls == [
pretend.call(
eventId="some-eventId",
timestamp="some-timestamp",
eventType="some-eventType",
resource="some-resource",
)
]
def test_legacy_event_view_func_wrapper(monkeypatch):
data = pretend.stub()
json = {
"eventId": "some-eventId",
"timestamp": "some-timestamp",
"eventType": "some-eventType",
"resource": "some-resource",
"data": data,
}
request = pretend.stub(headers={}, get_json=lambda: json)
context_stub = pretend.stub()
context_class = pretend.call_recorder(lambda *a, **kw: context_stub)
monkeypatch.setattr(functions_framework, "Context", context_class)
function = pretend.call_recorder(lambda data, context: "Hello")
view_func = functions_framework._event_view_func_wrapper(function, request)
view_func("/some/path")
assert function.calls == [pretend.call(data, context_stub)]
assert context_class.calls == [
pretend.call(
eventId="some-eventId",
timestamp="some-timestamp",
eventType="some-eventType",
resource="some-resource",
)
]