From 11c25659c5057e45f0fbff44e4baf64ec7f92fe4 Mon Sep 17 00:00:00 2001 From: ILYA Khlopotov Date: Mon, 18 Jul 2016 11:49:51 -0700 Subject: [PATCH 1/2] Use couch_epi:decide for validate_dbname COUCHDB-3066 --- src/couch_db.erl | 8 ++------ src/couch_db_plugin.erl | 19 ++++++++++++++----- test/couch_db_plugin_tests.erl | 21 +++++++++++++++------ 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/couch_db.erl b/src/couch_db.erl index 8260a5c2..3eff99fd 100644 --- a/src/couch_db.erl +++ b/src/couch_db.erl @@ -1536,12 +1536,8 @@ validate_dbname(DbName) when is_list(DbName) -> validate_dbname(?l2b(DbName)); validate_dbname(DbName) when is_binary(DbName) -> Normalized = normalize_dbname(DbName), - case couch_db_plugin:validate_dbname(DbName, Normalized) of - true -> - ok; - false -> - validate_dbname_int(DbName, Normalized) - end. + couch_db_plugin:validate_dbname( + DbName, Normalized, fun validate_dbname_int/2). validate_dbname_int(DbName, Normalized) when is_binary(DbName) -> case re:run(DbName, ?DBNAME_REGEX, [{capture,none}, dollar_endonly]) of diff --git a/src/couch_db_plugin.erl b/src/couch_db_plugin.erl index 1a6d2ea7..774e9e09 100644 --- a/src/couch_db_plugin.erl +++ b/src/couch_db_plugin.erl @@ -13,7 +13,7 @@ -module(couch_db_plugin). -export([ - validate_dbname/2, + validate_dbname/3, before_doc_update/2, after_doc_read/2, validate_docid/1, @@ -29,10 +29,8 @@ %% API Function Definitions %% ------------------------------------------------------------------ -validate_dbname(DbName, Normalized) -> - Handle = couch_epi:get_handle(?SERVICE_ID), - %% callbacks return true only if it specifically allow the given Id - couch_epi:any(Handle, ?SERVICE_ID, validate_dbname, [DbName, Normalized], []). +validate_dbname(DbName, Normalized, Default) -> + maybe_handle(validate_dbname, [DbName, Normalized], Default). before_doc_update(#db{before_doc_update = Fun} = Db, Doc0) -> case with_pipe(before_doc_update, [Doc0, Db]) of @@ -70,3 +68,14 @@ with_pipe(Func, Args) -> do_apply(Func, Args, Opts) -> Handle = couch_epi:get_handle(?SERVICE_ID), couch_epi:apply(Handle, ?SERVICE_ID, Func, Args, Opts). + +maybe_handle(Func, Args, Default) -> + Handle = couch_epi:get_handle(?SERVICE_ID), + case couch_epi:decide(Handle, ?SERVICE_ID, Func, Args, []) of + no_decision when is_function(Default) -> + apply(Default, Args); + no_decision -> + Default; + {decided, Result} -> + Result + end. diff --git a/test/couch_db_plugin_tests.erl b/test/couch_db_plugin_tests.erl index 337207e2..bbc01c7c 100644 --- a/test/couch_db_plugin_tests.erl +++ b/test/couch_db_plugin_tests.erl @@ -52,9 +52,10 @@ setup() -> teardown(Ctx) -> couch_tests:teardown(Ctx). -validate_dbname({true, _Db}, _) -> true; -validate_dbname({false, _Db}, _) -> false; -validate_dbname({fail, _Db}, _) -> throw(validate_dbname). +validate_dbname({true, _Db}, _) -> {decided, true}; +validate_dbname({false, _Db}, _) -> {decided, false}; +validate_dbname({fail, _Db}, _) -> throw(validate_dbname); +validate_dbname({pass, _Db}, _) -> no_decision. before_doc_update({fail, _Doc}, _Db) -> throw(before_doc_update); before_doc_update({true, Doc}, Db) -> [{true, [before_doc_update|Doc]}, Db]; @@ -85,6 +86,7 @@ callback_test_() -> fun validate_dbname_match/0, fun validate_dbname_no_match/0, fun validate_dbname_throw/0, + fun validate_dbname_pass/0, fun before_doc_update_match/0, fun before_doc_update_no_match/0, @@ -111,15 +113,22 @@ callback_test_() -> validate_dbname_match() -> - ?assert(couch_db_plugin:validate_dbname({true, [db]}, db)). + ?assert(couch_db_plugin:validate_dbname( + {true, [db]}, db, fun(_, _) -> pass end)). validate_dbname_no_match() -> - ?assertNot(couch_db_plugin:validate_dbname({false, [db]}, db)). + ?assertNot(couch_db_plugin:validate_dbname( + {false, [db]}, db, fun(_, _) -> pass end)). validate_dbname_throw() -> ?assertThrow( validate_dbname, - couch_db_plugin:validate_dbname({fail, [db]}, db)). + couch_db_plugin:validate_dbname( + {fail, [db]}, db, fun(_, _) -> pass end)). + +validate_dbname_pass() -> + ?assertEqual(pass, couch_db_plugin:validate_dbname( + {pass, [db]}, db, fun(_, _) -> pass end)). before_doc_update_match() -> ?assertMatch( From 7d2e1a734e31aa220fcb43b1c6472da1098f9be2 Mon Sep 17 00:00:00 2001 From: ILYA Khlopotov Date: Mon, 18 Jul 2016 11:52:01 -0700 Subject: [PATCH 2/2] Name test cases COUCHDB-3066 --- test/couch_db_plugin_tests.erl | 48 +++++++++++++++++----------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/test/couch_db_plugin_tests.erl b/test/couch_db_plugin_tests.erl index bbc01c7c..ea9b230b 100644 --- a/test/couch_db_plugin_tests.erl +++ b/test/couch_db_plugin_tests.erl @@ -83,30 +83,30 @@ callback_test_() -> { setup, fun setup/0, fun teardown/1, [ - fun validate_dbname_match/0, - fun validate_dbname_no_match/0, - fun validate_dbname_throw/0, - fun validate_dbname_pass/0, - - fun before_doc_update_match/0, - fun before_doc_update_no_match/0, - fun before_doc_update_throw/0, - - fun after_doc_read_match/0, - fun after_doc_read_no_match/0, - fun after_doc_read_throw/0, - - fun validate_docid_match/0, - fun validate_docid_no_match/0, - fun validate_docid_throw/0, - - fun check_is_admin_match/0, - fun check_is_admin_no_match/0, - fun check_is_admin_throw/0, - - fun on_delete_match/0, - fun on_delete_no_match/0, - fun on_delete_throw/0 + {"validate_dbname_match", fun validate_dbname_match/0}, + {"validate_dbname_no_match", fun validate_dbname_no_match/0}, + {"validate_dbname_throw", fun validate_dbname_throw/0}, + {"validate_dbname_pass", fun validate_dbname_pass/0}, + + {"before_doc_update_match", fun before_doc_update_match/0}, + {"before_doc_update_no_match", fun before_doc_update_no_match/0}, + {"before_doc_update_throw", fun before_doc_update_throw/0}, + + {"after_doc_read_match", fun after_doc_read_match/0}, + {"after_doc_read_no_match", fun after_doc_read_no_match/0}, + {"after_doc_read_throw", fun after_doc_read_throw/0}, + + {"validate_docid_match", fun validate_docid_match/0}, + {"validate_docid_no_match", fun validate_docid_no_match/0}, + {"validate_docid_throw", fun validate_docid_throw/0}, + + {"check_is_admin_match", fun check_is_admin_match/0}, + {"check_is_admin_no_match", fun check_is_admin_no_match/0}, + {"check_is_admin_throw", fun check_is_admin_throw/0}, + + {"on_delete_match", fun on_delete_match/0}, + {"on_delete_no_match", fun on_delete_no_match/0}, + {"on_delete_throw", fun on_delete_throw/0} ] } }.