You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ch...@apache.org on 2015/06/02 21:36:11 UTC

[40/50] couch commit: updated refs/heads/2080-port-cors-to-chttpd to 529339b

Add test suite for global_changes

COUCHDB-2667


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/65ed0851
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/65ed0851
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/65ed0851

Branch: refs/heads/2080-port-cors-to-chttpd
Commit: 65ed085166309d656dc3e091425c734d3213b74d
Parents: f7b9dae
Author: ILYA Khlopotov <ii...@ca.ibm.com>
Authored: Wed Apr 29 07:20:47 2015 -0700
Committer: ILYA Khlopotov <ii...@ca.ibm.com>
Committed: Thu May 7 08:10:20 2015 -0700

----------------------------------------------------------------------
 test/global_changes_tests.erl | 116 +++++++++++++++++++++++++++++++++++++
 1 file changed, 116 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/65ed0851/test/global_changes_tests.erl
----------------------------------------------------------------------
diff --git a/test/global_changes_tests.erl b/test/global_changes_tests.erl
new file mode 100644
index 0000000..aa73898
--- /dev/null
+++ b/test/global_changes_tests.erl
@@ -0,0 +1,116 @@
+-module(global_changes_tests).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+setup() ->
+    Host = get_host(),
+    add_admin("admin", <<"pass">>),
+    DbName = "foo/" ++ ?b2l(?tempdb()),
+    fabric:create_db(DbName, [?ADMIN_CTX]),
+    {Host, DbName}.
+
+teardown({_, DbName}) ->
+    delete_admin("admin"),
+    ok = fabric:delete_db(?l2b(DbName), [?ADMIN_CTX]),
+    ok.
+
+global_changes_test_() ->
+    {
+        "Checking global_changes endpoint",
+        {
+            setup,
+            fun() -> test_util:start_couch([chttpd, global_changes]) end,
+            fun test_util:stop/1,
+            [
+                check_response()
+            ]
+        }
+    }.
+
+check_response() ->
+    {
+        "Check response",
+        {
+            foreach,
+            fun setup/0, fun teardown/1,
+            [
+                fun should_return_correct_response_on_create/1,
+                fun should_return_correct_response_on_update/1
+            ]
+        }
+    }.
+
+should_return_correct_response_on_create({Host, DbName}) ->
+    ?_test(begin
+        Headers = [{basic_auth, {"admin", "pass"}}],
+        create_doc(Host, DbName, "bar/baz"),
+        {Status, Events} = request_updates(Host, DbName, Headers),
+        ?assertEqual(200, Status),
+        ?assertEqual([<<"created">>, <<"updated">>], Events)
+    end).
+
+should_return_correct_response_on_update({Host, DbName}) ->
+    ?_test(begin
+        Headers = [{basic_auth, {"admin", "pass"}}],
+        create_doc(Host, DbName, "bar/baz"),
+        update_doc(Host, DbName, "bar/baz", "new_value"),
+        {Status, Events} = request_updates(Host, DbName, Headers),
+        ?assertEqual(200, Status),
+        ?assertEqual([<<"created">>, <<"updated">>], Events)
+    end).
+
+create_doc(Host, DbName, Id) ->
+    Headers = [{basic_auth, {"admin", "pass"}}],
+    Url = Host ++ "/" ++ escape(DbName) ++ "/" ++ escape(Id),
+    Body = jiffy:encode({[
+        {key, "value"}
+    ]}),
+    {ok, 201, _Headers, _Body} = test_request:put(Url, Headers, Body),
+    timer:sleep(1000),
+    ok.
+
+update_doc(Host, DbName, Id, Value) ->
+    Headers = [{basic_auth, {"admin", "pass"}}],
+    Url = Host ++ "/" ++ escape(DbName) ++ "/" ++ escape(Id),
+    {ok, 200, _Headers0, BinBody} = test_request:get(Url, Headers),
+    [Rev] = decode_response(BinBody, [<<"_rev">>]),
+    Body = jiffy:encode({[
+        {key, Value},
+        {'_rev', Rev}
+    ]}),
+    {ok, 201, _Headers1, _Body} = test_request:put(Url, Headers, Body),
+    timer:sleep(1000),
+    ok.
+
+request_updates(Host, DbName, Headers) ->
+    Url = Host ++ "/_db_updates",
+    {ok, Status, _Headers, BinBody} = test_request:get(Url, Headers),
+    [Results] = decode_response(BinBody, [<<"results">>]),
+    ToDecode = [<<"db_name">>, <<"type">>],
+    Values = [decode_result(Result, ToDecode) || Result <- Results],
+    Result = [Type || [DB, Type] <- Values, DB == ?l2b(DbName)],
+    {Status, lists:sort(Result)}.
+
+decode_result({Props}, ToDecode) ->
+    [couch_util:get_value(Key, Props) || Key <- ToDecode].
+
+decode_response(BinBody, ToDecode) ->
+    {Body} = jiffy:decode(BinBody),
+    [couch_util:get_value(Key, Body) || Key <- ToDecode].
+
+add_admin(User, Pass) ->
+    Hashed = couch_passwords:hash_admin_password(Pass),
+    config:set("admins", User, ?b2l(Hashed), false).
+
+delete_admin(User) ->
+    config:delete("admins", User, false).
+
+get_host() ->
+    Addr = config:get("httpd", "bind_address", "127.0.0.1"),
+    Port = config:get("chttpd", "port", "5984"),
+    Host = "http://" ++ Addr ++ ":" ++ Port,
+    Host.
+
+escape(Path) ->
+    re:replace(Path, "/", "%2f", [global, {return, list}]).