couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From klaus_trai...@apache.org
Subject couchdb-peruser git commit: Refactor and add tests
Date Thu, 13 Aug 2015 01:53:16 GMT
Repository: couchdb-peruser
Updated Branches:
  refs/heads/master c3a59aab0 -> 1b4641e18


Refactor and add tests


Project: http://git-wip-us.apache.org/repos/asf/couchdb-peruser/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-peruser/commit/1b4641e1
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-peruser/tree/1b4641e1
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-peruser/diff/1b4641e1

Branch: refs/heads/master
Commit: 1b4641e1870a27bd1aa7c8aafc388803936836f8
Parents: c3a59aa
Author: Klaus Trainer <klaus_trainer@posteo.de>
Authored: Thu Aug 13 03:31:00 2015 +0200
Committer: Klaus Trainer <klaus_trainer@posteo.de>
Committed: Thu Aug 13 03:52:13 2015 +0200

----------------------------------------------------------------------
 src/couchdb_peruser.app.src   |   2 +-
 src/couchdb_peruser.erl       |  53 +++++++------
 test/couchdb_peruser_test.erl | 152 +++++++++++++++++++++++++++++++++++++
 3 files changed, 183 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-peruser/blob/1b4641e1/src/couchdb_peruser.app.src
----------------------------------------------------------------------
diff --git a/src/couchdb_peruser.app.src b/src/couchdb_peruser.app.src
index 9255068..19b1a78 100644
--- a/src/couchdb_peruser.app.src
+++ b/src/couchdb_peruser.app.src
@@ -15,6 +15,6 @@
     {vsn, "1.1.0"},
     {modules, []},
     {registered, [couchdb_peruser]},
-    {applications, [kernel, stdlib]},
+    {applications, [kernel, stdlib, config, couch, fabric]},
     {env, []}
 ]}.

http://git-wip-us.apache.org/repos/asf/couchdb-peruser/blob/1b4641e1/src/couchdb_peruser.erl
----------------------------------------------------------------------
diff --git a/src/couchdb_peruser.erl b/src/couchdb_peruser.erl
index 2bc2f6b..ce0ec54 100644
--- a/src/couchdb_peruser.erl
+++ b/src/couchdb_peruser.erl
@@ -62,19 +62,26 @@ handle_config_change(_Section, _Key, _Value, _Persist, Server) ->
 handle_config_terminate(_Self, Reason, _Server) ->
     {stop, Reason}.
 
-init_changes_handler(State) ->
-    {ok, Db} = couch_db:open_int(State#state.db_name, [?ADMIN_CTX, sys_db]),
-    FunAcc = {fun ?MODULE:changes_handler/3, State},
-    (couch_changes:handle_db_changes(
-         #changes_args{feed="continuous", timeout=infinity},
-         {json_req, null},
-         Db))(FunAcc).
+init_changes_handler(#state{db_name=DbName} = State) ->
+    try
+        {ok, Db} = couch_db:open_int(DbName, [?ADMIN_CTX, sys_db]),
+        FunAcc = {fun ?MODULE:changes_handler/3, State},
+        (couch_changes:handle_db_changes(
+             #changes_args{feed="continuous", timeout=infinity},
+             {json_req, null},
+             Db))(FunAcc)
+    catch error:database_does_not_exist ->
+        ok
+    end.
 
 changes_handler({change, {Doc}, _Prepend}, _ResType, State=#state{}) ->
-    Deleted = couch_util:get_value(<<"deleted">>, Doc, false),
-    case lists:keyfind(<<"id">>, 1, Doc) of
-    {_Key, <<"org.couchdb.user:", User/binary>>} ->
-        case Deleted of
+    case couch_util:get_value(<<"id">>, Doc) of
+    <<"org.couchdb.user:",User/binary>> ->
+        case couch_util:get_value(<<"deleted">>, Doc, false) of
+        false ->
+            UserDb = ensure_user_db(User),
+            ensure_security(User, UserDb),
+            State;
         true ->
             case State#state.delete_dbs of
             true ->
@@ -82,11 +89,7 @@ changes_handler({change, {Doc}, _Prepend}, _ResType, State=#state{}) ->
                 State;
             false ->
                 State
-            end;
-        false ->
-            UserDb = ensure_user_db(User),
-            ensure_security(User, UserDb),
-            State
+            end
         end;
     _ ->
         State
@@ -102,7 +105,7 @@ terminate(_Reason, _State) ->
 delete_user_db(User) ->
     UserDb = user_db_name(User),
     try
-        fabric_db_delete:go(UserDb, [?ADMIN_CTX])
+        fabric:delete_db(UserDb, [?ADMIN_CTX])
     catch error:database_does_not_exist ->
         ok
     end,
@@ -111,9 +114,9 @@ delete_user_db(User) ->
 ensure_user_db(User) ->
     UserDb = user_db_name(User),
     try
-        fabric_db_info:go(UserDb)
+        fabric:get_db_info(UserDb)
     catch error:database_does_not_exist ->
-        fabric_db_create:go(UserDb, [?ADMIN_CTX])
+        fabric:create_db(UserDb, [?ADMIN_CTX])
     end,
     UserDb.
 
@@ -134,7 +137,7 @@ add_user(User, Prop, {Modified, SecProps}) ->
     end.
 
 ensure_security(User, UserDb) ->
-    {ok, Shards} = fabric_db_meta:get_all_security(UserDb, [?ADMIN_CTX]),
+    {ok, Shards} = fabric:get_all_security(UserDb, [?ADMIN_CTX]),
     {_ShardInfo, {SecProps}} = hd(Shards),
     % assert that shards have the same security object
     true = lists:all(fun({_, {SecProps1}}) ->
@@ -147,20 +150,24 @@ ensure_security(User, UserDb) ->
     {false, _} ->
         ok;
     {true, SecProps1} ->
-        fabric_db_meta:set_security(UserDb, {SecProps1}, [?ADMIN_CTX])
+        fabric:set_security(UserDb, {SecProps1}, [?ADMIN_CTX])
     end.
 
 user_db_name(User) ->
     HexUser = list_to_binary(
         [string:to_lower(integer_to_list(X, 16)) || <<X>> <= User]),
-    <<?USERDB_PREFIX, HexUser/binary>>.
+    <<?USERDB_PREFIX,HexUser/binary>>.
 
 handle_call(_Msg, _From, State) ->
     {reply, error, State}.
 
-handle_cast(stop, State) ->
+handle_cast(stop, State) when State#state.changes_pid =/= undefined ->
     % we don't want to have multiple changes handler at the same time
     exit(State#state.changes_pid, kill),
+    {Pid, Ref} = spawn_opt(
+        ?MODULE, init_changes_handler, [State], [link, monitor]),
+    {noreply, State#state{changes_pid=Pid, changes_ref=Ref}};
+handle_cast(stop, State) ->
     {stop, normal, State};
 handle_cast(_Msg, State) ->
     {noreply, State}.

http://git-wip-us.apache.org/repos/asf/couchdb-peruser/blob/1b4641e1/test/couchdb_peruser_test.erl
----------------------------------------------------------------------
diff --git a/test/couchdb_peruser_test.erl b/test/couchdb_peruser_test.erl
new file mode 100644
index 0000000..9ea408b
--- /dev/null
+++ b/test/couchdb_peruser_test.erl
@@ -0,0 +1,152 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couchdb_peruser_test).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+-define(ADMIN_USERNAME, "admin").
+-define(ADMIN_PASSWORD, "secret").
+
+start_couch() ->
+    Deps = [
+        couchdb_peruser,
+        jiffy,
+        mochiweb,
+        chttpd
+    ],
+    test_util:start_couch(Deps).
+
+stop_couch(TestCtx) ->
+    test_util:stop_couch(TestCtx).
+
+setup() ->
+    TestAuthDb = ?tempdb(),
+    config:set("admins", ?ADMIN_USERNAME, ?ADMIN_PASSWORD),
+    do_request(put, get_base_url() ++ "/" ++ ?b2l(TestAuthDb)),
+    set_config("couch_httpd_auth", "authentication_db", ?b2l(TestAuthDb)),
+    set_config("couchdb_peruser", "enable", "true"),
+    TestAuthDb.
+
+teardown(TestAuthDb) ->
+    set_config("couch_httpd_auth", "authentication_db", "_users"),
+    set_config("couchdb_peruser", "enable", "false"),
+    set_config("couchdb_peruser", "delete_dbs", "false"),
+    do_request(delete, get_base_url() ++ "/" ++ ?b2l(TestAuthDb)),
+    config:delete("admins", ?ADMIN_USERNAME).
+
+set_config(Section, Key, Value) ->
+    Url = lists:concat([
+        get_base_url(), "/_config/", Section, "/", Key]),
+    do_request(put, Url, "\"" ++ Value ++ "\"").
+
+do_request(Method, Url) ->
+    Headers = [{basic_auth, {?ADMIN_USERNAME, ?ADMIN_PASSWORD}}],
+    {ok, _, _, _} = test_request:request(Method, Url, Headers).
+
+do_request(Method, Url, Body) ->
+    Headers = [
+        {basic_auth, {?ADMIN_USERNAME, ?ADMIN_PASSWORD}},
+        {"Content-Type", "application/json"}],
+    {ok, _, _, _} = test_request:request(Method, Url, Headers, Body).
+
+create_user(AuthDb, Name) ->
+    Body = "{\"name\":\"" ++ Name ++
+        "\",\"type\":\"user\",\"roles\":[],\"password\":\"secret\"}",
+    Url = lists:concat([
+        get_base_url(), "/", ?b2l(AuthDb), "/org.couchdb.user:", Name]),
+    do_request(put, Url, Body).
+
+delete_user(AuthDb, Name) ->
+    Url = lists:concat([get_base_url(), "/", ?b2l(AuthDb),
+        "/org.couchdb.user:", Name]),
+    {ok, 200, _, Body} = do_request(get, Url),
+    {DocProps} = jiffy:decode(Body),
+    Rev = proplists:get_value(<<"_rev">>, DocProps),
+    {ok, 200, _, _} = do_request(delete, Url ++ "?rev=" ++ ?b2l(Rev)).
+
+all_dbs() ->
+    {ok, 200, _, Body} = do_request(get, get_cluster_base_url() ++ "/_all_dbs"),
+    jiffy:decode(Body).
+
+get_base_url() ->
+    Addr = config:get("httpd", "bind_address", "127.0.0.1"),
+    Port = integer_to_list(mochiweb_socket_server:get(couch_httpd, port)),
+    "http://" ++ Addr ++ ":" ++ Port.
+
+get_cluster_base_url() ->
+    Addr = config:get("httpd", "bind_address", "127.0.0.1"),
+    Port = integer_to_list(mochiweb_socket_server:get(chttpd, port)),
+    "http://" ++ Addr ++ ":" ++ Port.
+
+should_create_user_db(TestAuthDb) ->
+    create_user(TestAuthDb, "foo"),
+    ?_assert(lists:member(<<"userdb-666f6f">>, all_dbs())).
+
+should_not_delete_user_db(TestAuthDb) ->
+    User = "foo",
+    UserDbName = <<"userdb-666f6f">>,
+    create_user(TestAuthDb, User),
+    ?assert(lists:member(UserDbName, all_dbs())),
+    delete_user(TestAuthDb, User),
+    ?_assert(lists:member(UserDbName, all_dbs())).
+
+should_delete_user_db(TestAuthDb) ->
+    User = "foo",
+    UserDbName = <<"userdb-666f6f">>,
+    set_config("couchdb_peruser", "delete_dbs", "true"),
+    create_user(TestAuthDb, User),
+    ?assert(lists:member(UserDbName, all_dbs())),
+    delete_user(TestAuthDb, User),
+    ?_assert(not lists:member(UserDbName, all_dbs())).
+
+should_reflect_config_changes(TestAuthDb) ->
+    User = "foo",
+    UserDbName = <<"userdb-666f6f">>,
+    set_config("couchdb_peruser", "delete_dbs", "true"),
+    create_user(TestAuthDb, User),
+    ?assert(lists:member(UserDbName, all_dbs())),
+    delete_user(TestAuthDb, User),
+    ?assert(not lists:member(UserDbName, all_dbs())),
+    create_user(TestAuthDb, User),
+    timer:sleep(100),
+    ?assert(lists:member(UserDbName, all_dbs())),
+    set_config("couchdb_peruser", "delete_dbs", "false"),
+    delete_user(TestAuthDb, User),
+    ?assert(lists:member(UserDbName, all_dbs())),
+    create_user(TestAuthDb, User),
+    set_config("couchdb_peruser", "delete_dbs", "true"),
+    delete_user(TestAuthDb, User),
+    ?assert(not lists:member(UserDbName, all_dbs())),
+    set_config("couchdb_peruser", "enable", "false"),
+    create_user(TestAuthDb, User),
+    ?_assert(not lists:member(UserDbName, all_dbs())).
+
+couchdb_peruser_test_() ->
+    {
+        "couchdb_peruser test",
+        {
+            setup,
+            fun start_couch/0, fun stop_couch/1,
+            {
+                foreach,
+                fun setup/0, fun teardown/1,
+                [
+                    fun should_create_user_db/1,
+                    fun should_not_delete_user_db/1,
+                    fun should_delete_user_db/1,
+                    fun should_reflect_config_changes/1
+                ]
+            }
+        }
+    }.


Mime
View raw message