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

couch commit: updated refs/heads/master to 5e81713

Repository: couchdb-couch
Updated Branches:
  refs/heads/master a5aa4e17c -> 5e81713f4


Explicitly authorize test requests

Create admin user and use its credentials for requests

Use http requests to create and delete test dbs to avoid potential db
name munging issues downstream

COUCHDB-2897


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

Branch: refs/heads/master
Commit: 5e81713f46a30834a225a3259779b5820bc662a8
Parents: a5aa4e1
Author: Jay Doane <ja...@gmail.com>
Authored: Fri Nov 20 21:40:14 2015 -0800
Committer: Jay Doane <ja...@gmail.com>
Committed: Fri Nov 20 21:40:14 2015 -0800

----------------------------------------------------------------------
 test/couchdb_mrview_cors_tests.erl | 45 ++++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/5e81713f/test/couchdb_mrview_cors_tests.erl
----------------------------------------------------------------------
diff --git a/test/couchdb_mrview_cors_tests.erl b/test/couchdb_mrview_cors_tests.erl
index 0d61361..f9155ce 100644
--- a/test/couchdb_mrview_cors_tests.erl
+++ b/test/couchdb_mrview_cors_tests.erl
@@ -24,8 +24,14 @@
     ]}}
 ]}).
 
+-define(USER, "mrview_cors_test_admin").
+-define(PASS, "pass").
+-define(AUTH, {basic_auth, {?USER, ?PASS}}).
+
+
 start() ->
     Ctx = test_util:start_couch([chttpd]),
+    ok = config:set("admins", ?USER, ?PASS, _Persist=false),
     ok = config:set("httpd", "enable_cors", "true", false),
     ok = config:set("vhosts", "example.com", "/", false),
     Ctx.
@@ -37,11 +43,14 @@ setup(PortType) ->
     config:set("cors", "credentials", "false", false),
     config:set("cors", "origins", "http://example.com", false),
 
-    Addr = config:get("httpd", "bind_address", "127.0.0.1"),
-    Host = "http://" ++ Addr ++ ":" ++ port(PortType),
+    Host = host_url(PortType),
     upload_ddoc(Host, ?b2l(DbName)),
     {Host, ?b2l(DbName)}.
 
+teardown(Ctx) ->
+    ok = config:delete("admins", ?USER, _Persist=false),
+    test_util:stop_couch(Ctx).
+
 teardown(PortType, {_Host, DbName}) ->
     delete_db(PortType, ?l2b(DbName)),
     ok.
@@ -51,7 +60,7 @@ cors_test_() ->
         "CORS for mrview",
         {
             setup,
-            fun start/0, fun test_util:stop_couch/1,
+            fun start/0, fun teardown/1,
             [show_tests()]
         }
     }.
@@ -75,7 +84,7 @@ should_make_shows_request(_, {Host, DbName}) ->
     ?_test(begin
          ReqUrl = Host ++ "/" ++ DbName ++ "/_design/foo/_show/bar",
          Headers = [{"Origin", "http://example.com"},
-                    {"Access-Control-Request-Method", "GET"}],
+                    {"Access-Control-Request-Method", "GET"}, ?AUTH],
          {ok, _, Resp, Body} = test_request:get(ReqUrl, Headers),
          Origin = proplists:get_value("Access-Control-Allow-Origin", Resp),
          ?assertEqual("http://example.com", Origin),
@@ -86,14 +95,36 @@ create_db(backdoor, DbName) ->
     {ok, Db} = couch_db:create(DbName, [?ADMIN_CTX]),
     couch_db:close(Db);
 create_db(clustered, DbName) ->
-    ok = fabric:create_db(DbName, [?ADMIN_CTX]).
+    {ok, Status, _, _} = test_request:put(db_url(DbName), [?AUTH], ""),
+    assert_success(create_db, Status),
+    ok.
 
 delete_db(backdoor, DbName) ->
     couch_server:delete(DbName, [?ADMIN_CTX]);
 delete_db(clustered, DbName) ->
-    ok = fabric:delete_db(DbName, [?ADMIN_CTX]).
+    {ok, Status, _, _} = test_request:delete(db_url(DbName), [?AUTH]),
+    assert_success(delete_db, Status),
+    ok.
+
+assert_success(create_db, Status) ->
+    true = lists:member(Status, [201, 202]);
+assert_success(delete_db, Status) ->
+    true = lists:member(Status, [200, 202]).
+    
+
+host_url(PortType) ->
+    "http://" ++ bind_address(PortType) ++ ":" ++ port(PortType).
+
+bind_address(PortType) ->
+    config:get(section(PortType), "bind_address", "127.0.0.1").
 
+section(backdoor) -> "http";
+section(clustered) -> "chttpd".
 
+db_url(DbName) when is_binary(DbName) ->
+    db_url(binary_to_list(DbName));
+db_url(DbName) when is_list(DbName) ->
+    host_url(clustered) ++ "/" ++ DbName.
 
 port(clustered) ->
     integer_to_list(mochiweb_socket_server:get(chttpd, port));
@@ -104,5 +135,5 @@ port(backdoor) ->
 upload_ddoc(Host, DbName) ->
     Url = Host ++ "/" ++ DbName ++ "/_design/foo",
     Body = couch_util:json_encode(?DDOC),
-    {ok, 201, _Resp, _Body} = test_request:put(Url, Body),
+    {ok, 201, _Resp, _Body} = test_request:put(Url, [?AUTH], Body),
     ok.