163163
164164
165165@pytest .mark .parametrize (
166- "filter_template_expr" ,
166+ "filter_template_expr, auth_filter, anon_filter " ,
167167 [
168- "(properties.private = false)" ,
169- "{{ '(properties.private = false)' if token is none else true }}" ,
170- """
171- {
172- "op": "=",
173- "args": [{"property": "private"}, true]
174- }
175- """ ,
176- '{{ \' {"op": "=", "args": [{"property": "private"}, true]}\' if token is none else true }}' ,
177- """
178- {
179- "op": "and",
180- "args": [
181- { "op": "=", "args": [{ "property": "id" }, "LC08_L1TP_060247_20180905_20180912_01_T1_L1TP" ] },
182- { "op": "=", "args": [{ "property": "collection" }, "landsat8_l1tp"] }
183- ]
184- }
185- """ ,
168+ # Simple filter, not templated
169+ [
170+ "(properties.private = false)" ,
171+ "(properties.private = false)" ,
172+ "(properties.private = false)" ,
173+ ],
174+ # Simple filter, templated
175+ [
176+ "{{ '(properties.private = false)' if token is none else true }}" ,
177+ "true" ,
178+ "(properties.private = false)" ,
179+ ],
180+ # Complex filter, not templated
181+ [
182+ """{
183+ "op": "=",
184+ "args": [{"property": "private"}, true]
185+ }""" ,
186+ """{
187+ "op": "=",
188+ "args": [{"property": "private"}, true]
189+ }""" ,
190+ """{
191+ "op": "=",
192+ "args": [{"property": "private"}, true]
193+ }""" ,
194+ ],
195+ # Complex filter, templated
196+ [
197+ """{{ '{"op": "=", "args": [{"property": "private"}, true]}' if token is none else true }}""" ,
198+ "true" ,
199+ """{"op": "=", "args": [{"property": "private"}, true]}""" ,
200+ ],
201+ # Not sure what is demonstrated in this...
202+ # [
203+ # """{
204+ # "op": "and",
205+ # "args": [
206+ # { "op": "=", "args": [{ "property": "id" }, "LC08_L1TP_060247_20180905_20180912_01_T1_L1TP" ] },
207+ # { "op": "=", "args": [{ "property": "collection" }, "landsat8_l1tp"] }
208+ # ]
209+ # }"""
210+ # ]
211+ # * 3,
186212 ],
187213)
214+ @pytest .mark .parametrize ("is_authenticated" , [True , False ])
188215@pytest .mark .parametrize ("input_query" , SEARCHES )
189216def test_search_post (
190- mock_upstream , source_api_server , filter_template_expr , input_query
217+ mock_upstream ,
218+ source_api_server ,
219+ filter_template_expr ,
220+ auth_filter ,
221+ anon_filter ,
222+ is_authenticated ,
223+ input_query ,
224+ token_builder ,
191225):
192- filter_template_expr = filter_template_expr .strip ()
193226 # Setup app
194227 app = app_factory (
195228 upstream_url = source_api_server ,
196229 items_filter = {
197230 "cls" : "stac_auth_proxy.filters.Template" ,
198- "args" : [filter_template_expr ],
231+ "args" : [filter_template_expr . strip () ],
199232 },
200233 default_public = True ,
201234 )
202235
203236 # Query API
204- TestClient (app ).post ("/search" , json = input_query )
237+ headers = (
238+ {"Authorization" : f"Bearer { token_builder ({})} " } if is_authenticated else {}
239+ )
240+ response = TestClient (app , headers = headers ).post ("/search" , json = input_query )
241+ response .raise_for_status ()
205242
206243 # Retrieve query from upstream
207244 assert mock_upstream .call_count == 1
208245 [r ] = cast (list [Request ], mock_upstream .call_args [0 ])
209246 output_query = json .loads (r .read ().decode ())
210247
211248 # Parse query from upstream
212- # input_filter_land = input_query.get("filter-lang")
249+ # input_filter_lang = input_query.get("filter-lang")
213250 input_filter = input_query .get ("filter" )
214- filter_exprs = [
251+ expected_filter = auth_filter if is_authenticated else anon_filter
252+ expected_filter_exprs = [
215253 cql2 .Expr (expr ).to_text ()
216- for expr in [input_filter , filter_template_expr ]
254+ for expr in [input_filter , expected_filter . strip () ]
217255 if expr
218256 ]
219- expected_filter_out = cql2 .Expr (" AND " .join (filter_exprs )).to_json ()
257+ expected_filter_out = cql2 .Expr (" AND " .join (expected_filter_exprs )).to_json ()
220258
221259 expected_output_query = {
222260 ** input_query ,
@@ -226,3 +264,6 @@ def test_search_post(
226264 assert (
227265 output_query == expected_output_query
228266 ), "Query should be combined with the filter expression."
267+
268+ # Reset test
269+ mock_upstream .reset_mock ()
0 commit comments