This is an automated email from the ASF dual-hosted git repository.
jan pushed a commit to branch access
in repository https://gitbox.apache.org/repos/asf/couchdb.git
The following commit(s) were added to refs/heads/access by this push:
new 659139f make erlang access tests work
659139f is described below
commit 659139f1bffc9f498217b7a2ed7ab5e57fec9d69
Author: Jan Lehnardt <[email protected]>
AuthorDate: Sat Jul 20 19:44:33 2019 +0200
make erlang access tests work
---
src/couch/src/couch_access_native_proc.erl | 1 -
src/couch/test/couchdb_access_tests.erl | 203 +++++++++++++++--------------
t.sh | 6 +-
3 files changed, 107 insertions(+), 103 deletions(-)
diff --git a/src/couch/src/couch_access_native_proc.erl
b/src/couch/src/couch_access_native_proc.erl
index 357fae1..4f62a10 100644
--- a/src/couch/src/couch_access_native_proc.erl
+++ b/src/couch/src/couch_access_native_proc.erl
@@ -121,7 +121,6 @@ code_change(_OldVsn, St, _Extra) ->
%
{"id":"account/bongel","key":"account/bongel","value":{"rev":"1-967a00dff5e02add41819138abb3284d"}},
map_doc(_St, {Doc}) ->
- couch_log:info("Mebbe Indexing: Doc: ~p", [Doc]),
case couch_util:get_value(<<"_access">>, Doc) of
undefined ->
[[],[]]; % do not index this doc
diff --git a/src/couch/test/couchdb_access_tests.erl
b/src/couch/test/couchdb_access_tests.erl
index df58805..bd7370e 100644
--- a/src/couch/test/couchdb_access_tests.erl
+++ b/src/couch/test/couchdb_access_tests.erl
@@ -14,67 +14,70 @@
-include_lib("couch/include/couch_eunit.hrl").
-make_url(User, Addr) ->
- lists:concat(["http://", User, ":", User, "@", Addr, ":", port()]).
+-define(CONTENT_JSON, {"Content-Type", "application/json"}).
+-define(ADMIN_REQ_HEADERS, [?CONTENT_JSON, {basic_auth, {"a", "a"}}]).
+-define(USERX_REQ_HEADERS, [?CONTENT_JSON, {basic_auth, {"x", "x"}}]).
+-define(USERY_REQ_HEADERS, [?CONTENT_JSON, {basic_auth, {"y", "y"}}]).
-setup(_) ->
- Hashed = couch_passwords:hash_admin_password("a"),
- ok = config:set("admins", "a", binary_to_list(Hashed), _Persist=false),
+url() ->
Addr = config:get("httpd", "bind_address", "127.0.0.1"),
- Url = lists:concat(["http://", Addr, ":", port()]),
+ lists:concat(["http://", Addr, ":", port()]).
- AdminUrl = make_url("a", Addr),
- XUrl = make_url("x", Addr),
- YUrl = make_url("y", Addr),
- ?debugFmt("~nA: ~p, X:~p, Y:~p~n", [AdminUrl, XUrl, YUrl]),
- % cleanup and setup
- {ok, _, _, _} = test_request:delete(AdminUrl ++ "/db"),
- {ok, _, _, _} = test_request:put(AdminUrl ++ "/db?q=1&n=1&access=true",
""),
+before_each(_) ->
+ {ok, _, _, _} = test_request:put(url() ++ "/db?q=1&n=1&access=true",
?ADMIN_REQ_HEADERS, ""),
+ url().
- % create users
- UserDbUrl = AdminUrl ++ "/_users",
- {ok, 201, _, _} = test_request:put(UserDbUrl, ""),
+after_each(_, Url) ->
+ {ok, _, _, _} = test_request:delete(Url ++ "/db", ?ADMIN_REQ_HEADERS),
+ ok.
- UserXUrl = AdminUrl ++ "/_users/x",
- UserXBody = "{ \"name\":\"x\", \"roles\": [], \"password\":\"x\",
\"type\": \"user\" }",
- {ok, 201, _, _} = test_request:put(UserXUrl, UserXBody),
+before_all() ->
+ Couch = test_util:start_couch([chttpd]),
+ Hashed = couch_passwords:hash_admin_password("a"),
+ ok = config:set("admins", "a", binary_to_list(Hashed), _Persist=false),
- UserYUrl = AdminUrl ++ "/_users/y",
- UserYBody = "{ \"name\":\"y\", \"roles\": [], \"password\":\"y\",
\"type\": \"user\" }",
- {ok, 201, _, _} = test_request:put(UserYUrl, UserYBody),
+ % cleanup and setup
+ {ok, _, _, _} = test_request:delete(url() ++ "/db", ?ADMIN_REQ_HEADERS),
+ {ok, _, _, _} = test_request:put(url() ++ "/db?q=1&n=1&access=true",
?ADMIN_REQ_HEADERS, ""),
- ?debugFmt("~n~p", [test_request:get(UserXUrl)]),
- ?debugFmt("~n~p", [test_request:get(UserYUrl)]),
+ % create users
+ UserDbUrl = url() ++ "/_users?q=1&n=1",
+ {ok, _, _, _} = test_request:delete(UserDbUrl, ?ADMIN_REQ_HEADERS, ""),
+ {ok, 201, _, _} = test_request:put(UserDbUrl, ?ADMIN_REQ_HEADERS, ""),
- timer:sleep(10000),
- {AdminUrl, XUrl, YUrl}.
+ UserXDocUrl = url() ++ "/_users/org.couchdb.user:x",
+ UserXDocBody = "{ \"name\":\"x\", \"roles\": [], \"password\":\"x\",
\"type\": \"user\" }",
+ {ok, 201, _, _} = test_request:put(UserXDocUrl, ?ADMIN_REQ_HEADERS,
UserXDocBody),
-teardown(_, {AdminUrl, _, _}) ->
- {ok, _, _, _} = test_request:delete(AdminUrl ++ "/db"),
- ok.
+ UserYDocUrl = url() ++ "/_users/org.couchdb.user:y",
+ UserYDocBody = "{ \"name\":\"y\", \"roles\": [], \"password\":\"y\",
\"type\": \"user\" }",
+ {ok, 201, _, _} = test_request:put(UserYDocUrl, ?ADMIN_REQ_HEADERS,
UserYDocBody),
+ Couch.
+after_all(_) ->
+ test_util:stop_couch(done).
access_test_() ->
Tests = [
- % fun should_let_admin_create_doc_with_access/2,
- fun should_let_user_create_doc_for_themselves/2
- % fun should_not_let_user_create_doc_for_someone_else/2,
- % fun should_let_admin_read_doc_with_access/2,
- % fun user_with_access_can_read_doc/2,
- % fun user_without_access_can_not_read_doc/2,
- % fun should_let_admin_delete_doc_with_access/2,
- % fun should_let_user_delete_doc_for_themselves/2,
- % fun should_not_let_user_delete_doc_for_someone_else/2,
- % fun should_let_admin_fetch_all_docs/2,
- % fun should_let_user_fetch_their_own_all_docs/2,
- % fun should_let_admin_fetch_changes/2,
- % fun should_let_user_fetch_their_own_changes/2
+ fun should_let_admin_create_doc_with_access/2,
+ fun should_let_user_create_doc_for_themselves/2,
+ fun should_not_let_user_create_doc_for_someone_else/2,
+ fun should_let_admin_read_doc_with_access/2,
+ fun user_with_access_can_read_doc/2,
+ fun user_without_access_can_not_read_doc/2,
+ % fun should_let_admin_delete_doc_with_access/2,
+ % fun should_let_user_delete_doc_for_themselves/2,
+ % fun should_not_let_user_delete_doc_for_someone_else/2,
+ fun should_let_admin_fetch_all_docs/2,
+ fun should_let_user_fetch_their_own_all_docs/2,
+ fun should_let_admin_fetch_changes/2,
+ fun should_let_user_fetch_their_own_changes/2
],
{
"Access tests",
{
setup,
- fun() -> test_util:start_couch([chttpd]) end, fun
test_util:stop_couch/1,
+ fun before_all/0, fun after_all/1,
[
make_test_cases(clustered, Tests)
]
@@ -84,89 +87,91 @@ access_test_() ->
make_test_cases(Mod, Funs) ->
{
lists:flatten(io_lib:format("~s", [Mod])),
- {foreachx, fun setup/1, fun teardown/2, [{Mod, Fun} || Fun <- Funs]}
+ {foreachx, fun before_each/1, fun after_each/2, [{Mod, Fun} || Fun <-
Funs]}
}.
% Doc creation
-should_let_admin_create_doc_with_access(_PortType, {AdminUrl, _XUrl, Y_Url}) ->
- {ok, Code, _, _} = test_request:put(AdminUrl ++ "/db/a",
"{\"a\":1,\"_access\":[\"x\"]}"),
+should_let_admin_create_doc_with_access(_PortType, Url) ->
+ {ok, Code, _, _} = test_request:put(Url ++ "/db/a",
"{\"a\":1,\"_access\":[\"x\"]}"),
?_assertEqual(201, Code).
-should_let_user_create_doc_for_themselves(_PortType, {_AdminUrl, XUrl, _YUrl})
->
- {ok, Code, _, _} = test_request:put(XUrl ++ "/db/b",
"{\"a\":1,\"_access\":[\"x\"]}"),
+should_let_user_create_doc_for_themselves(_PortType, Url) ->
+ {ok, Code, _, _} = test_request:put(Url ++ "/db/b", ?USERX_REQ_HEADERS,
"{\"a\":1,\"_access\":[\"x\"]}"),
?_assertEqual(201, Code).
-should_not_let_user_create_doc_for_someone_else(_PortType, {_AdminUrl, _XUrl,
YUrl}) ->
- {ok, Code, _, _} = test_request:put(YUrl ++ "/db/c",
"{\"a\":1,\"_access\":[\"x\"]}"),
- ?_assertEqual(401, Code).
+should_not_let_user_create_doc_for_someone_else(_PortType, Url) ->
+ {ok, Code, _, _} = test_request:put(Url ++ "/db/c", ?USERY_REQ_HEADERS,
"{\"a\":1,\"_access\":[\"x\"]}"),
+ ?_assertEqual(403, Code).
% Doc reads
-should_let_admin_read_doc_with_access(_PortType, {AdminUrl, XUrl, _YUrl}) ->
- {ok, 201, _, _} = test_request:put(XUrl ++ "/db/a",
"{\"a\":1,\"_access\":[\"x\"]}"),
- {ok, Code, _, _} = test_request:get(AdminUrl ++ "/db/a"),
+should_let_admin_read_doc_with_access(_PortType, Url) ->
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/a", ?USERX_REQ_HEADERS,
"{\"a\":1,\"_access\":[\"x\"]}"),
+ {ok, Code, _, _} = test_request:get(Url ++ "/db/a", ?ADMIN_REQ_HEADERS),
?_assertEqual(200, Code).
-user_with_access_can_read_doc(_PortType, {AdminUrl, XUrl, _YUrl}) ->
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/a",
"{\"a\":1,\"_access\":[\"x\"]}"),
- {ok, Code, _, _} = test_request:get(XUrl ++ "/db/a"),
+user_with_access_can_read_doc(_PortType, Url) ->
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/a", ?ADMIN_REQ_HEADERS,
"{\"a\":1,\"_access\":[\"x\"]}"),
+ {ok, Code, _, _} = test_request:get(Url ++ "/db/a", ?USERX_REQ_HEADERS),
?_assertEqual(200, Code).
-user_without_access_can_not_read_doc(_PortType, {AdminUrl, _XUrl, YUrl}) ->
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/a",
"{\"a\":1,\"_access\":[\"x\"]}"),
- {ok, Code, _, _} = test_request:get(YUrl ++ "/db/a"),
- ?_assertEqual(401, Code).
+user_without_access_can_not_read_doc(_PortType, Url) ->
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/a", ?ADMIN_REQ_HEADERS,
"{\"a\":1,\"_access\":[\"x\"]}"),
+ {ok, Code, _, _} = test_request:get(Url ++ "/db/a", ?USERY_REQ_HEADERS),
+ ?_assertEqual(403, Code).
% Doc deletes
-should_let_admin_delete_doc_with_access(_PortType, {AdminUrl, XUrl, _YUrl}) ->
- {ok, 201, _, _} = test_request:put(XUrl ++ "/db/a",
"{\"a\":1,\"_access\":[\"x\"]}"),
- {ok, Code, _, _} = test_request:delete(AdminUrl ++
"/db/a?rev=1-967a00dff5e02add41819138abb3284d"),
+should_let_admin_delete_doc_with_access(_PortType, Url) ->
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/a", ?USERX_REQ_HEADERS,
"{\"a\":1,\"_access\":[\"x\"]}"),
+ {ok, Code, _, _} = test_request:delete(Url ++
"/db/a?rev=1-967a00dff5e02add41819138abb3284d", ?ADMIN_REQ_HEADERS),
?_assertEqual(200, Code).
-should_let_user_delete_doc_for_themselves(_PortType, {AdminUrl, XUrl, _YUrl})
->
- {ok, 201, _, _} = test_request:put(XUrl ++ "/db/a",
"{\"a\":1,\"_access\":[\"x\"]}"),
- {ok, Code, _, _} = test_request:delete(XUrl ++
"/db/a?rev=1-967a00dff5e02add41819138abb3284d"),
+should_let_user_delete_doc_for_themselves(_PortType, Url) ->
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/a", ?USERX_REQ_HEADERS,
"{\"a\":1,\"_access\":[\"x\"]}"),
+ {ok, Code, _, _} = test_request:delete(Url ++
"/db/a?rev=1-967a00dff5e02add41819138abb3284d"),
?_assertEqual(200, Code).
-should_not_let_user_delete_doc_for_someone_else(_PortType, {_AdminUrl, XUrl,
YUrl}) ->
- {ok, 201, _, _} = test_request:put(XUrl ++ "/db/a",
"{\"a\":1,\"_access\":[\"x\"]}"),
- {ok, Code, _, _} = test_request:delete(YUrl ++
"/db/a?rev=1-967a00dff5e02add41819138abb3284d"),
+should_not_let_user_delete_doc_for_someone_else(_PortType, Url) ->
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/a", ?USERX_REQ_HEADERS,
"{\"a\":1,\"_access\":[\"x\"]}"),
+ {ok, Code, _, _} = test_request:delete(Url ++
"/db/a?rev=1-967a00dff5e02add41819138abb3284d", ?USERY_REQ_HEADERS),
?_assertEqual(401, Code).
% _all_docs with include_docs
-should_let_admin_fetch_all_docs(_PortType, {AdminUrl, _XUrl, _YUrl}) ->
- Admin_Fetch_Docs_Body =
<<"{\"total_rows\":4,\"offset\":0,\"rows\":[\r\n{\"id\":\"a\",\"key\":\"a\",\"value\":{\"rev\":\"1-967a00dff5e02add41819138abb3284d\"},\"doc\":{\"_id\":\"a\",\"_rev\":\"1-967a00dff5e02add41819138abb3284d\",\"_access\":{}}},\r\n{\"id\":\"b\",\"key\":\"b\",\"value\":{\"rev\":\"1-967a00dff5e02add41819138abb3284d\"},\"doc\":{\"_id\":\"b\",\"_rev\":\"1-967a00dff5e02add41819138abb3284d\",\"_access\":{}}},\r\n{\"id\":\"c\",\"key\":\"c\",\"value\":{\"rev\":\"1-967a00d
[...]
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/a",
"{\"a\":1,\"_access\":[\"x\"]}"),
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/b",
"{\"b\":2,\"_access\":[\"x\"]}"),
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/c",
"{\"c\":3,\"_access\":[\"y\"]}"),
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/d",
"{\"d\":4,\"_access\":[\"y\"]}"),
- {ok, 200, _, Body} = test_request:get(AdminUrl ++
"/db/_all_docs?include_docs=true"),
- ?_assertEqual(Admin_Fetch_Docs_Body, Body).
-
-should_let_user_fetch_their_own_all_docs(_PortType, {AdminUrl, XUrl, YUrl}) ->
- Admin_Fetch_Docs_Body =
<<"{\"total_rows\":2,\"offset\":0,\"rows\":[\r\n{\"id\":\"a\",\"key\":\"a\",\"value\":{\"rev\":\"1-967a00dff5e02add41819138abb3284d\"},\"doc\":{\"_id\":\"a\",\"_rev\":\"1-967a00dff5e02add41819138abb3284d\",\"_access\":{}}},\r\n{\"id\":\"b\",\"key\":\"b\",\"value\":{\"rev\":\"1-967a00dff5e02add41819138abb3284d\"},\"doc\":{\"_id\":\"b\",\"_rev\":\"1-967a00dff5e02add41819138abb3284d\",\"_access\":{}}}]}\n">>,
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/a",
"{\"a\":1,\"_access\":[\"x\"]}"),
- {ok, 201, _, _} = test_request:put(XUrl ++ "/db/b",
"{\"b\":2,\"_access\":[\"x\"]}"),
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/c",
"{\"c\":3,\"_access\":[\"y\"]}"),
- {ok, 201, _, _} = test_request:put(YUrl ++ "/db/d",
"{\"d\":4,\"_access\":[\"y\"]}"),
- {ok, 200, _, Body} = test_request:get(XUrl ++
"/db/_all_docs?include_docs=true"),
- ?_assertEqual(Admin_Fetch_Docs_Body, Body).
+should_let_admin_fetch_all_docs(_PortType, Url) ->
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/a", ?ADMIN_REQ_HEADERS,
"{\"a\":1,\"_access\":[\"x\"]}"),
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/b", ?ADMIN_REQ_HEADERS,
"{\"b\":2,\"_access\":[\"x\"]}"),
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/c", ?ADMIN_REQ_HEADERS,
"{\"c\":3,\"_access\":[\"y\"]}"),
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/d", ?ADMIN_REQ_HEADERS,
"{\"d\":4,\"_access\":[\"y\"]}"),
+ {ok, 200, _, Body} = test_request:get(Url ++
"/db/_all_docs?include_docs=true", ?ADMIN_REQ_HEADERS),
+ {Json} = jiffy:decode(Body),
+ ?_assertEqual(4, proplists:get_value(<<"total_rows">>, Json)).
+
+should_let_user_fetch_their_own_all_docs(_PortType, Url) ->
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/a", ?ADMIN_REQ_HEADERS,
"{\"a\":1,\"_access\":[\"x\"]}"),
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/b", ?USERX_REQ_HEADERS,
"{\"b\":2,\"_access\":[\"x\"]}"),
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/c", ?ADMIN_REQ_HEADERS,
"{\"c\":3,\"_access\":[\"y\"]}"),
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/d", ?USERY_REQ_HEADERS,
"{\"d\":4,\"_access\":[\"y\"]}"),
+ {ok, 200, _, Body} = test_request:get(Url ++
"/db/_all_docs?include_docs=true", ?USERX_REQ_HEADERS),
+ {Json} = jiffy:decode(Body),
+ ?_assertEqual(2, length(proplists:get_value(<<"rows">>, Json))).
+% TODO ?_assertEqual(2, proplists:get_value(<<"total_rows">>, Json)).
+
% _changes
-should_let_admin_fetch_changes(_PortType, {AdminUrl, _XUrl, _YUrl}) ->
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/a",
"{\"a\":1,\"_access\":[\"x\"]}"),
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/b",
"{\"b\":2,\"_access\":[\"x\"]}"),
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/c",
"{\"c\":3,\"_access\":[\"y\"]}"),
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/d",
"{\"d\":4,\"_access\":[\"y\"]}"),
- {ok, 200, _, Body} = test_request:get(AdminUrl ++ "/db/_changes"),
+should_let_admin_fetch_changes(_PortType, Url) ->
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/a", ?ADMIN_REQ_HEADERS,
"{\"a\":1,\"_access\":[\"x\"]}"),
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/b", ?ADMIN_REQ_HEADERS,
"{\"b\":2,\"_access\":[\"x\"]}"),
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/c", ?ADMIN_REQ_HEADERS,
"{\"c\":3,\"_access\":[\"y\"]}"),
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/d", ?ADMIN_REQ_HEADERS,
"{\"d\":4,\"_access\":[\"y\"]}"),
+ {ok, 200, _, Body} = test_request:get(Url ++ "/db/_changes",
?ADMIN_REQ_HEADERS),
{Json} = jiffy:decode(Body),
AmountOfDocs = length(proplists:get_value(<<"results">>, Json)),
?_assertEqual(4, AmountOfDocs).
-should_let_user_fetch_their_own_changes(_PortType, {AdminUrl, XUrl, _YUrl}) ->
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/a",
"{\"a\":1,\"_access\":[\"x\"]}"),
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/b",
"{\"b\":2,\"_access\":[\"x\"]}"),
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/c",
"{\"c\":3,\"_access\":[\"y\"]}"),
- {ok, 201, _, _} = test_request:put(AdminUrl ++ "/db/d",
"{\"d\":4,\"_access\":[\"y\"]}"),
- {ok, 200, _, Body} = test_request:get(XUrl ++ "/db/_changes"),
+should_let_user_fetch_their_own_changes(_PortType, Url) ->
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/a", ?ADMIN_REQ_HEADERS,
"{\"a\":1,\"_access\":[\"x\"]}"),
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/b", ?ADMIN_REQ_HEADERS,
"{\"b\":2,\"_access\":[\"x\"]}"),
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/c", ?ADMIN_REQ_HEADERS,
"{\"c\":3,\"_access\":[\"y\"]}"),
+ {ok, 201, _, _} = test_request:put(Url ++ "/db/d", ?ADMIN_REQ_HEADERS,
"{\"d\":4,\"_access\":[\"y\"]}"),
+ {ok, 200, _, Body} = test_request:get(Url ++ "/db/_changes",
?USERX_REQ_HEADERS),
{Json} = jiffy:decode(Body),
AmountOfDocs = length(proplists:get_value(<<"results">>, Json)),
?_assertEqual(2, AmountOfDocs).
diff --git a/t.sh b/t.sh
index d2c4d67..ee6dc18 100755
--- a/t.sh
+++ b/t.sh
@@ -1,7 +1,7 @@
#!/bin/sh -x
-DB=http://a:[email protected]:15984 #
-XDB=http://x:[email protected]:15984
-YDB=http://y:[email protected]:15984
+DB=http://a:[email protected]:63136 #
+XDB=http://x:[email protected]:63136
+YDB=http://y:[email protected]:63136
curl -sX DELETE $DB/_users
curl -sX PUT $DB/_users?q=1