diff options
author | Jan Lehnardt <jan@apache.org> | 2019-07-19 14:18:36 +0200 |
---|---|---|
committer | Jan Lehnardt <jan@apache.org> | 2019-07-19 14:18:36 +0200 |
commit | e0f7251dd2d76afa9e6af1e36a4ad83a22d6616c (patch) | |
tree | 2ab53ad38ce55348004c00be84c9c9d2a80a6501 | |
parent | f1e765c6d236e653df6e4283367daa9128da51bf (diff) | |
download | couchdb-e0f7251dd2d76afa9e6af1e36a4ad83a22d6616c.tar.gz |
add test suite boilerplate
-rw-r--r-- | src/couch/test/couchdb_access_tests.erl | 83 | ||||
-rwxr-xr-x | t.sh | 41 |
2 files changed, 108 insertions, 16 deletions
diff --git a/src/couch/test/couchdb_access_tests.erl b/src/couch/test/couchdb_access_tests.erl new file mode 100644 index 000000000..83bc613c1 --- /dev/null +++ b/src/couch/test/couchdb_access_tests.erl @@ -0,0 +1,83 @@ +% 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_access_tests). + +-include_lib("couch/include/couch_eunit.hrl"). + +make_url(User, Addr, PortType) -> + lists:concat(["http://", User, ":", User, "@", Addr, ":", port(PortType)]). + +setup(PortType) -> + Hashed = couch_passwords:hash_admin_password("a"), + ok = config:set("admins", "a", binary_to_list(Hashed), _Persist=false), + Addr = config:get("httpd", "bind_address", "127.0.0.1"), + Url = lists:concat(["http://", Addr, ":", port(PortType)]), + + AdminUrl = make_url("a", Addr, PortType), + XUrl = make_url("x", Addr, PortType), + YUrl = make_url("y", Addr, PortType), + + % create users + UserDbUrl = AdminUrl ++ "/_users", + {ok, 201, _, _} = test_request:put(UserDbUrl, ""), + + UserXUrl = AdminUrl ++ "/_users/x", + UserXBody = "{ \"name\":\"x\", \"roles\": [], \"password\":\"x\", \"type\": \"user\" }", + {ok, 201, _, _} = test_request:put(UserXUrl, UserXBody), + + UserYUrl = AdminUrl ++ "/_users/y", + UserYBody = "{ \"name\":\"y\", \"roles\": [], \"password\":\"y\", \"type\": \"user\" }", + {ok, 201, _, _} = test_request:put(UserYUrl, UserYBody), + + {ok, _, _, _} = test_request:delete(AdminUrl ++ "/db"), + {ok, _, _, _} = test_request:put(AdminUrl ++ "/db?access=true", ""), + + {AdminUrl, XUrl, YUrl}. + +teardown(_, _) -> + ok. + + +access_test_() -> + Tests = [ + fun should_let_admin_create_doc_with_access/2 + ], + { + "Auth tests", + { + setup, + fun() -> test_util:start_couch([chttpd]) end, fun test_util:stop_couch/1, + [ + make_test_cases(clustered, Tests) + ] + } + }. + +make_test_cases(Mod, Funs) -> + { + lists:flatten(io_lib:format("~s", [Mod])), + {foreachx, fun setup/1, fun teardown/2, [{Mod, Fun} || Fun <- Funs]} + }. + +should_let_admin_create_doc_with_access(_PortType, {AdminUrl, XUrl, YUrl}) -> + {ok, Code, _, _} = test_request:put(AdminUrl ++ "/db/a", "{\"a\":1,\"_access\":[\"x\"]}"), + ?_assertEqual(201, Code). + +%% ------------------------------------------------------------------ +%% Internal Function Definitions +%% ------------------------------------------------------------------ + +port(clustered) -> + integer_to_list(mochiweb_socket_server:get(chttpd, port)); +port(backdoor) -> + integer_to_list(mochiweb_socket_server:get(couch_httpd, port)). @@ -1,41 +1,50 @@ #!/bin/sh -x -DB=http://a:a@127.0.0.1:15984 +DB=http://a:a@127.0.0.1:15984 # XDB=http://x:x@127.0.0.1:15984 YDB=http://y:y@127.0.0.1:15984 - -curl -sX PUT $DB/_users/org.couchdb.user:x -d @user.json > /dev/null -curl -sX PUT $DB/_users/org.couchdb.user:y -d @user2.json > /dev/null +curl -sX PUT $DB/_users/org.couchdb.user:x -d @user.json > /dev/null # +curl -sX PUT $DB/_users/org.couchdb.user:y -d @user2.json > /dev/null # curl -sX DELETE $DB/db + + curl -sX PUT $DB/db?q=1'&access=true' +############## + + -# curl -sX PUT $DB/db/a -d '{"a":1,"_access":["x"]}' +curl -sX PUT $DB/db/a -d '{"a":1,"_access":["x"]}' curl -sX PUT $DB/db/b -d '{"b":2,"_access":["x"]}' -# curl -sX PUT $DB/db/c -d '{"c":3,"_access":["y"]}' -# curl -sX PUT $XDB/db/c?rev="1-0865d643568aa9be6bcdc15d88b25912" -d '{"c":6,"_access":["y"]}' +curl -sX PUT $DB/db/c -d '{"c":3,"_access":["y"]}' +curl -X PUT $XDB/db/c?rev="1-0865d643568aa9be6bcdc15d88b25912" -d '{"c":6,"_access":["y"]}' # -# curl -sX PUT $DB/db/d -d '{"d":4,"_access":["y"]}' +curl -sX PUT $DB/db/d -d '{"d":4,"_access":["y"]}' # -# curl -sX DELETE $DB/db/a?rev="1-79b9365c8c21f839b206c730ec3d4fc3" +curl -sX DELETE $DB/db/a?rev="1-967a00dff5e02add41819138abb3284d" + +echo +echo "setup done" +echo + # # curl -s $DB/db/_all_docs?include_docs=true # curl -s $XDB/db/_all_docs?include_docs=true # curl -s $YDB/db/_all_docs?include_docs=true -# +# # # curl -s $DB/db/_changes?include_docs=true # curl -s $XDB/db/_changes?include_docs=true # curl -s $YDB/db/_changes?include_docs=true -# +# # # curl -s $DB/db/a -curl -s $DB/db/b +# curl -s $DB/db/b # curl -s $DB/db/c # curl -s $DB/db/d # -# curl -s $XDB/db/a -# curl -s $XDB/db/b -# curl -s $XDB/db/c -# curl -s $XDB/db/d +curl -s $XDB/db/a +curl -s $XDB/db/b +curl -s $XDB/db/c +curl -s $XDB/db/d # # curl -s $YDB/db/a # curl -s $YDB/db/b |