@@ -21,6 +21,7 @@ defmodule Electric.Plug.DeleteShapePlugTest do
2121 flags: % { selects_all_columns: true }
2222 }
2323 @ test_pg_id "12345"
24+ @ inspector { __MODULE__ , [ ] }
2425
2526 def load_column_info ( @ users_oid , _ ) ,
2627 do:
@@ -38,6 +39,8 @@ defmodule Electric.Plug.DeleteShapePlugTest do
3839 Plug.Test . conn ( method , "/" <> query_string )
3940 end
4041
42+ def query_string ( params ) , do: "?" <> URI . encode_query ( params )
43+
4144 def call_delete_shape_plug ( conn , ctx , allow \\ true ) do
4245 config =
4346 Electric.Shapes.Api . plug_opts (
@@ -47,7 +50,7 @@ defmodule Electric.Plug.DeleteShapePlugTest do
4750 pg_id: @ test_pg_id ,
4851 shape_cache: { Electric.ShapeCache , [ ] } ,
4952 storage: { Mock.Storage , [ ] } ,
50- inspector: { __MODULE__ , [ ] } ,
53+ inspector: @ inspector ,
5154 registry: @ registry ,
5255 long_poll_timeout: Access . get ( ctx , :long_poll_timeout , 20_000 ) ,
5356 max_age: Access . get ( ctx , :max_age , 60 ) ,
@@ -135,6 +138,24 @@ defmodule Electric.Plug.DeleteShapePlugTest do
135138 }
136139 end
137140
141+ test "returns 400 for a malformed request-only param" , ctx do
142+ # Request-only params such as `live` are ignored by delete validation,
143+ # but they still go through schema casting, so malformed values are
144+ # rejected rather than silently dropped.
145+ conn =
146+ ctx
147+ |> conn ( "DELETE" , query_string ( table: "public.users" , live: "banana" ) )
148+ |> call_delete_shape_plug ( ctx )
149+
150+ assert conn . status == 400
151+ assert Plug.Conn . get_resp_header ( conn , "cache-control" ) == [ "no-cache" ]
152+
153+ assert Jason . decode! ( conn . resp_body ) == % {
154+ "message" => "Invalid request" ,
155+ "errors" => % { "live" => [ "is invalid" ] }
156+ }
157+ end
158+
138159 test "should clean shape based on shape definition" , ctx do
139160 % { stack_id: stack_id } = ctx
140161
@@ -151,6 +172,63 @@ defmodule Electric.Plug.DeleteShapePlugTest do
151172 assert Plug.Conn . get_resp_header ( conn , "cache-control" ) == [ "no-cache" ]
152173 end
153174
175+ test "should clean shape based on shape definition with a where clause" , ctx do
176+ % { stack_id: stack_id } = ctx
177+
178+ shape = Shape . new! ( "public.users" , where: "id = 1" , inspector: @ inspector )
179+ { :ok , shape_handle } = Electric.ShapeCache.ShapeStatus . add_shape ( stack_id , shape )
180+
181+ expect_shape_cache ( clean_shape: fn ^ shape_handle , ^ stack_id -> :ok end )
182+
183+ conn =
184+ ctx
185+ |> conn ( :delete , query_string ( table: "public.users" , where: "id = 1" ) )
186+ |> call_delete_shape_plug ( ctx )
187+
188+ assert conn . status == 202
189+ end
190+
191+ test "should clean shape based on shape definition with a parameterised where clause" , ctx do
192+ % { stack_id: stack_id } = ctx
193+
194+ shape =
195+ Shape . new! ( "public.users" ,
196+ where: "id = $1" ,
197+ params: % { "1" => "1" } ,
198+ inspector: @ inspector
199+ )
200+
201+ { :ok , shape_handle } = Electric.ShapeCache.ShapeStatus . add_shape ( stack_id , shape )
202+
203+ expect_shape_cache ( clean_shape: fn ^ shape_handle , ^ stack_id -> :ok end )
204+
205+ conn =
206+ ctx
207+ |> conn (
208+ :delete ,
209+ query_string ( [ { "table" , "public.users" } , { "where" , "id = $1" } , { "params[1]" , "1" } ] )
210+ )
211+ |> call_delete_shape_plug ( ctx )
212+
213+ assert conn . status == 202
214+ end
215+
216+ test "should clean shape based on shape definition with a column selection" , ctx do
217+ % { stack_id: stack_id } = ctx
218+
219+ shape = Shape . new! ( "public.users" , columns: [ "id" ] , inspector: @ inspector )
220+ { :ok , shape_handle } = Electric.ShapeCache.ShapeStatus . add_shape ( stack_id , shape )
221+
222+ expect_shape_cache ( clean_shape: fn ^ shape_handle , ^ stack_id -> :ok end )
223+
224+ conn =
225+ ctx
226+ |> conn ( :delete , query_string ( table: "public.users" , columns: "id" ) )
227+ |> call_delete_shape_plug ( ctx )
228+
229+ assert conn . status == 202
230+ end
231+
154232 test "should clean shape based only on shape_handle" , ctx do
155233 % { stack_id: stack_id } = ctx
156234
0 commit comments