Return-Path: X-Original-To: apmail-couchdb-commits-archive@www.apache.org Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 56111188BA for ; Thu, 21 May 2015 21:13:27 +0000 (UTC) Received: (qmail 54124 invoked by uid 500); 21 May 2015 21:13:27 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 54023 invoked by uid 500); 21 May 2015 21:13:27 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 53693 invoked by uid 99); 21 May 2015 21:13:26 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 21 May 2015 21:13:26 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id B1676E51CE; Thu, 21 May 2015 21:13:26 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: chewbranca@apache.org To: commits@couchdb.apache.org Date: Thu, 21 May 2015 21:13:32 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [7/9] couch commit: updated refs/heads/2657-fix-cassim-fabric-calls-revised to 1d28fd3 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/2657-fix-cassim-fabric-calls-revised Commit: 65ed085166309d656dc3e091425c734d3213b74d Parents: f7b9dae Author: ILYA Khlopotov Authored: Wed Apr 29 07:20:47 2015 -0700 Committer: ILYA Khlopotov 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}]).