3
3
"""
4
4
5
5
import unittest
6
- from flask import Flask , Request , jsonify as _jsonify
6
+ from flask import Flask , Request
7
7
from werkzeug .test import EnvironBuilder
8
8
9
9
from firebase_functions import core , https_fn
@@ -71,13 +71,14 @@ def func(_):
71
71
72
72
self .assertEqual ("world" , hello )
73
73
74
+
74
75
def test_callable_encoding (self ):
75
76
app = Flask (__name__ )
76
77
77
78
@https_fn .on_call ()
78
79
def add (req : https_fn .CallableRequest [int ]):
79
80
return req .data + 1
80
-
81
+
81
82
with app .test_request_context ("/" ):
82
83
environ = EnvironBuilder (
83
84
method = "POST" ,
@@ -90,18 +91,23 @@ def add(req: https_fn.CallableRequest[int]):
90
91
response = add (request )
91
92
self .assertEqual (response .status_code , 200 )
92
93
self .assertEqual (response .get_json (), { "result" : 2 })
93
-
94
+
95
+
94
96
def test_callable_errors (self ):
95
97
app = Flask (__name__ )
96
98
97
99
@https_fn .on_call ()
98
100
def throw_generic_error (req ):
101
+ # pylint: disable=broad-exception-raised
99
102
raise Exception ("Invalid type" )
100
103
101
104
@https_fn .on_call ()
102
105
def throw_access_denied (req ):
103
- raise https_fn .HttpsError (https_fn .FunctionsErrorCode .PERMISSION_DENIED , "Permission is denied" )
104
-
106
+ raise https_fn .HttpsError (
107
+ https_fn .FunctionsErrorCode .PERMISSION_DENIED ,
108
+ "Permission is denied"
109
+ )
110
+
105
111
with app .test_request_context ("/" ):
106
112
environ = EnvironBuilder (
107
113
method = "POST" ,
@@ -113,11 +119,18 @@ def throw_access_denied(req):
113
119
114
120
response = throw_generic_error (request )
115
121
self .assertEqual (response .status_code , 500 )
116
- self .assertEqual (response .get_json (), { "error" : { "message" : "INTERNAL" , "status" : "INTERNAL" } })
122
+ self .assertEqual (response .get_json (), {
123
+ "error" : { "message" : "INTERNAL" , "status" : "INTERNAL" }
124
+ })
117
125
118
126
response = throw_access_denied (request )
119
127
self .assertEqual (response .status_code , 403 )
120
- self .assertEqual (response .get_json (), { "error" : { "message" : "Permission is denied" , "status" : "PERMISSION_DENIED" }})
128
+ self .assertEqual (response .get_json (), {
129
+ "error" : {
130
+ "message" : "Permission is denied" ,
131
+ "status" : "PERMISSION_DENIED"
132
+ }
133
+ })
121
134
122
135
def test_yielding_without_streaming (self ):
123
136
app = Flask (__name__ )
@@ -130,7 +143,10 @@ def yielder(req: https_fn.CallableRequest[int]):
130
143
@https_fn .on_call ()
131
144
def yield_thrower (req : https_fn .CallableRequest [int ]):
132
145
yield from range (req .data )
133
- raise https_fn .HttpsError (https_fn .FunctionsErrorCode .PERMISSION_DENIED , "Can't read anymore" )
146
+ raise https_fn .HttpsError (
147
+ https_fn .FunctionsErrorCode .PERMISSION_DENIED ,
148
+ "Can't read anymore"
149
+ )
134
150
135
151
with app .test_request_context ("/" ):
136
152
environ = EnvironBuilder (
@@ -158,7 +174,9 @@ def yield_thrower(req: https_fn.CallableRequest[int]):
158
174
response = yield_thrower (request )
159
175
160
176
self .assertEqual (response .status_code , 403 )
161
- self .assertEqual (response .get_json (), { "error" : { "message" : "Can't read anymore" , "status" : "PERMISSION_DENIED" }})
177
+ self .assertEqual (response .get_json (), {
178
+ "error" : { "message" : "Can't read anymore" , "status" : "PERMISSION_DENIED" }
179
+ })
162
180
163
181
164
182
def test_yielding_with_streaming (self ):
@@ -190,7 +208,12 @@ def yield_thrower(req: https_fn.CallableRequest[int]):
190
208
191
209
self .assertEqual (response .status_code , 200 )
192
210
chunks = list (response .response )
193
- self .assertEqual (chunks , ['data: {"message": 0}\n \n ' , 'data: {"message": 1}\n \n ' , 'data: {"result": "OK"}\n \n ' , "END" ])
211
+ self .assertEqual (chunks , [
212
+ 'data: {"message": 0}\n \n ' ,
213
+ 'data: {"message": 1}\n \n ' ,
214
+ 'data: {"result": "OK"}\n \n ' ,
215
+ "END"
216
+ ])
194
217
195
218
with app .test_request_context ("/" ):
196
219
environ = EnvironBuilder (
@@ -208,4 +231,9 @@ def yield_thrower(req: https_fn.CallableRequest[int]):
208
231
209
232
self .assertEqual (response .status_code , 200 )
210
233
chunks = list (response .response )
211
- self .assertEqual (chunks , ['data: {"message": 0}\n \n ' , 'data: {"message": 1}\n \n ' , 'error: {"error": {"status": "INTERNAL", "message": "Throwing"}}\n \n ' , "END" ])
234
+ self .assertEqual (chunks , [
235
+ 'data: {"message": 0}\n \n ' ,
236
+ 'data: {"message": 1}\n \n ' ,
237
+ 'error: {"error": {"status": "INTERNAL", "message": "Throwing"}}\n \n ' ,
238
+ "END"
239
+ ])
0 commit comments