diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-18 20:24:52 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-18 21:57:48 +0100 |
| commit | 62123e577ee5d4066d53f8ad7e5ac3d2eb190430 (patch) | |
| tree | f569686630681a9eceb2685f69b55fe5a2bac17c /test | |
| parent | 7087a0c2c84e796e474784cbd28d4a4e574cf2c0 (diff) | |
Securized the queries in the database with a dedicated type for the Identifiers
Diffstat (limited to 'test')
| -rw-r--r-- | test/common.ml | 33 | ||||
| -rw-r--r-- | test/identifier.ml | 42 | ||||
| -rw-r--r-- | test/insert.ml | 6 | ||||
| -rw-r--r-- | test/parent.ml | 5 | ||||
| -rw-r--r-- | test/tests.ml | 1 | ||||
| -rw-r--r-- | test/update.ml | 16 |
6 files changed, 76 insertions, 27 deletions
diff --git a/test/common.ml b/test/common.ml index 0ef5cb3..98871a3 100644 --- a/test/common.ml +++ b/test/common.ml @@ -31,12 +31,10 @@ let content_as_json : let () = Seq.iter (fun field -> - Buffer.add_char buff '\''; - Buffer.add_string buff (Model.Field.name field); - - Buffer.add_string buff "',"; - Buffer.add_string buff (Model.Field.name field); - Buffer.add_string buff ",") + let name = Model.Field.name field in + Buffer.add_string buff + ( Croquette_common.Sql.quote_literal (Model.Identifier.to_string name) + ^ "," ^ Model.Identifier.quote name ^ "," )) fields in @@ -46,21 +44,28 @@ let content_as_json : let length = Buffer.length buff in if length > 1 then Buffer.truncate buff (length - 1); () - | Some _ -> Buffer.add_string buff "'parent',parent" + | Some _ -> + Buffer.add_string buff + ( Croquette_common.Sql.quote_literal "parent" ^ "," + ^ Model.Identifier.quote (Model.Identifier.parent) ) in let q = Buffer.contents buff in let open Caqti.Templater in direct T.(int64 -->! string) - ("SELECT json_object(" ^ q ^ ") from " ^ Model.Content.id content - ^ " where id=?") + ("SELECT json_object(" ^ q ^ ") from " + ^ Model.Identifier.quote (Model.Content.id content) + ^ " where " ^ Model.Identifier.quote (Model.Identifier.id) ^ "=?") -let query_values (table : Model.Reference.t Model.Content.t) (feature : string) - = +let query_values (table : Model.Reference.t Model.Content.t) + (feature : Model.Identifier.t) = let open Caqti.Templater in direct T.(int64 -->* string) - ("SELECT json_object('" ^ feature ^ "', target) from \"" - ^ Model.Content.id table ^ "." ^ feature ^ "\" where parent=?") + ("SELECT json_object(" + ^ Croquette_common.Sql.quote_literal (Model.Identifier.to_string feature) + ^ ", " ^ Model.Identifier.quote (Model.Identifier.target) ^ ") from " + ^ Model.Identifier.quote (Model.Identifier.dot (Model.Content.id table) feature) + ^ " where " ^ Model.Identifier.quote (Model.Identifier.parent) ^ "=?") (** Query all the multireferences tables for the given content and the given id. Collect all the values and return them in the same json format as the same @@ -73,7 +78,7 @@ let collect_references : fun db content id -> let module Db = (val db) in let ( let* ) = Lwt.bind in - let content_id = Model.Content.id content in + let content_id = Model.Identifier.to_string (Model.Content.id content) in (* Only keep the multivaluated fields *) let fields = diff --git a/test/identifier.ml b/test/identifier.ml new file mode 100644 index 0000000..322f459 --- /dev/null +++ b/test/identifier.ml @@ -0,0 +1,42 @@ +let name = __FILE__ + +let description = + "Check the [Model.Identifier] module: validation of the names given as \ + arguments, their quoted SQL form, the predefined columns and the composite \ + names" + +let speclist () = [] + +type t = string list + +let pp : Format.formatter -> t -> unit = + Format.pp_print_list ~pp_sep:Format.pp_print_newline Format.pp_print_string + +(** A valid name is printed in its quoted SQL form, an invalid one is + reported *) +let check : string -> string = + fun s -> + match Model.Identifier.of_string_opt s with + | Some id -> Model.Identifier.quote id + | None -> "invalid: \"" ^ s ^ "\"" + +let exec : string list -> t = + fun args -> + (* The anonymous arguments are collected in reverse order by the test + harness *) + match List.rev args with + | "check" :: names -> List.map check names + | "predefined" :: [] -> + [ + "id = " ^ Model.Identifier.quote Model.Identifier.id; + "parent = " ^ Model.Identifier.quote Model.Identifier.parent; + "target = " ^ Model.Identifier.quote Model.Identifier.target; + ] + | "dot" :: [ a; b ] -> + [ + Model.Identifier.quote + (Model.Identifier.dot + (Model.Identifier.of_string a) + (Model.Identifier.of_string b)); + ] + | _ -> failwith "Invalid arguments" diff --git a/test/insert.ml b/test/insert.ml index bc65422..451d857 100644 --- a/test/insert.ml +++ b/test/insert.ml @@ -35,7 +35,7 @@ let insert_and_get_result : Query.insert ?parent db content (fun v -> match List.assoc_opt v entry with Some v -> v | None -> Model.Typ.None) in - let content_id = Model.Content.id content in + let content_id = Model.Identifier.to_string (Model.Content.id content) in let module Db = (val db) in let* json_result = Db.find (Common.content_as_json content) id in let* json = Caqti_lwt.or_fail json_result in @@ -144,7 +144,7 @@ module Make (S : Decoders.Decode.S) = struct [ ( "content", fun v -> - match (Configuration.Ast.get_content conf) key with + match (Configuration.Ast.get_content conf) (Model.Identifier.of_string key) with | None -> Error (Decoders.Error.make "no content") | Some subcontent -> let values = self#values subcontent v in @@ -172,7 +172,7 @@ module Make (S : Decoders.Decode.S) = struct fun name -> (* In this syntax, the content is supposed to follow the definition and will never be None *) - let content = Configuration.Ast.get_content conf name |> Option.get in + let content = Configuration.Ast.get_content conf (Model.Identifier.of_string name) |> Option.get in let* properties = S.field_opt_or ~default:(Lwt.return []) "values" (self#values content) diff --git a/test/parent.ml b/test/parent.ml index 232de34..6e07c90 100644 --- a/test/parent.ml +++ b/test/parent.ml @@ -33,10 +33,11 @@ let exec : string list -> t = function let+ configuration = Configuration.Read_conf.read_string_conf stdin in let ( let+ ) opt cont = Option.map cont opt in let+ subcontent = - Configuration.Ast.get_content configuration subcontent_id + Configuration.Ast.get_content configuration + (Model.Identifier.of_string subcontent_id) in let+ v = !parent subcontent in - Model.Content.id v + Model.Identifier.to_string (Model.Content.id v) in parent_id | _ -> failwith "Invalid arguments" diff --git a/test/tests.ml b/test/tests.ml index d396ac0..0f0fdc5 100644 --- a/test/tests.ml +++ b/test/tests.ml @@ -35,6 +35,7 @@ let () = register (module Configuration_); register (module Insert); register (module Update); + register (module Identifier); ] let args = ref [] diff --git a/test/update.ml b/test/update.ml index 1e94fc2..6f22466 100644 --- a/test/update.ml +++ b/test/update.ml @@ -67,11 +67,11 @@ let configuration = let speclist () = let (lazy configuration) = configuration in - let content = Configuration.Ast.get_content configuration "entity" in + let content = Configuration.Ast.get_content configuration (Model.Identifier.of_string "entity") in let fields = Model.Content.fields (Option.get content) in Seq.fold_left (fun acc feature -> - let key = "--" ^ Model.Field.name feature in + let key = "--" ^ Model.Identifier.to_string (Model.Field.name feature) in let (Model.Field.T type_) = Model.Field.type_ feature in let argument = match type_ with @@ -81,7 +81,7 @@ let speclist () = Arg.String (fun value -> actions := - Text (Model.Field.name feature, value) :: !actions), + Text (Model.Identifier.to_string (Model.Field.name feature), value) :: !actions), "<v> Set field with the value <v>" ) | Model.Typ.Integer -> Some @@ -89,7 +89,7 @@ let speclist () = Arg.Int (fun value -> actions := - Integer (Model.Field.name feature, value) :: !actions), + Integer (Model.Identifier.to_string (Model.Field.name feature), value) :: !actions), "<n> Set field with the value <n>" ) | Model.Typ.MonoReference target -> Some @@ -97,7 +97,7 @@ let speclist () = Arg.Int (fun value -> actions := - Reference (target, Model.Field.name feature, value) + Reference (target, Model.Identifier.to_string (Model.Field.name feature), value) :: !actions), "<id> Set field with the id" ) | Model.Typ.MultiReference target -> @@ -112,7 +112,7 @@ let speclist () = in actions := - References (target, Model.Field.name feature, ids) + References (target, Model.Identifier.to_string (Model.Field.name feature), ids) :: !actions), "<id,..> Set this list of values" ) in @@ -133,7 +133,7 @@ let exec : string list -> t = fun _ -> let (lazy configuration) = configuration in let content = - Configuration.Ast.get_content configuration "entity" |> Option.get + Configuration.Ast.get_content configuration (Model.Identifier.of_string "entity") |> Option.get in Lwt_main.run @@ -162,7 +162,7 @@ let exec : string list -> t = let field = Model.Content.fields content |> Seq.find (fun f -> - String.equal key (Model.Field.name f)) + String.equal key (Model.Identifier.to_string (Model.Field.name f))) |> Option.get in let (Model.Field.T t) = Model.Field.type_ field in |
