You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2018/01/26 20:55:17 UTC
[couchdb] 04/05: Add the user tag to create users declaratively
This is an automated email from the ASF dual-hosted git repository.
davisp pushed a commit to branch elixir-suite-davisp
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 654d0fa9f43f30ece58ca5a408e773325006b5b8
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Fri Jan 26 14:32:31 2018 -0600
Add the user tag to create users declaratively
The `user` tag allows tests to declaratively request user creation. This
can be used as such:
@tag user: [name: "username", password: "secret", roles: ["a_role"]]
test "this is a user test", context do
sess = Couch.login(context[:userinfo])
resp Couch.Session.get("/_session")
assert resp.body["ok"]
assert resp.body["userCtx"]["name"] == "username"
assert Couch.Session.logout(sess).body["ok"]
end
This also demonstrates how to use the recently add Couch.Session support
for handling user sessions.
Tests that specify a `user` tag will have a `:user` key in the context
that is the current user doc as a map as well as a `:userinfo` key that
is the `username:password` that can be passed directly to
`Couch.login/1`.
---
test/elixir/test/test_helper.exs | 57 +++++++++++++++++++++++++++++++++++++++-
1 file changed, 56 insertions(+), 1 deletion(-)
diff --git a/test/elixir/test/test_helper.exs b/test/elixir/test/test_helper.exs
index 9baf204..f84e1a0 100644
--- a/test/elixir/test/test_helper.exs
+++ b/test/elixir/test/test_helper.exs
@@ -12,7 +12,8 @@ defmodule CouchTestCase do
setup context do
setup_funs = [
&set_db_context/1,
- &set_config_context/1
+ &set_config_context/1,
+ &set_user_context/1
]
context = Enum.reduce(setup_funs, context, fn setup_fun, acc ->
setup_fun.(acc)
@@ -55,6 +56,23 @@ defmodule CouchTestCase do
context
end
+ def set_user_context(context) do
+ case Map.get(context, :user) do
+ nil ->
+ context
+ user when is_list(user) ->
+ user = create_user(user)
+ on_exit(fn ->
+ query = %{:rev => user["_rev"]}
+ resp = Couch.delete("/_users/#{user["_id"]}", query: query)
+ assert HTTPotion.Response.success? resp
+ end)
+ context = Map.put(context, :user, user)
+ userinfo = user["name"] <> ":" <> user["password"]
+ Map.put(context, :userinfo, userinfo)
+ end
+ end
+
def random_db_name do
random_db_name("random-test-db")
end
@@ -97,6 +115,43 @@ defmodule CouchTestCase do
end)
end
+ def create_user(user) do
+ required = [:name, :password, :roles]
+ Enum.each(required, fn key ->
+ assert Keyword.has_key?(user, key), "User missing key: #{key}"
+ end)
+
+ name = Keyword.get(user, :name)
+ password = Keyword.get(user, :password)
+ roles = Keyword.get(user, :roles)
+
+ assert is_binary(name), "User name must be a string"
+ assert is_binary(password), "User password must be a string"
+ assert is_list(roles), "Roles must be a list of strings"
+ Enum.each(roles, fn role ->
+ assert is_binary(role), "Roles must be a list of strings"
+ end)
+
+ user_doc = %{
+ "_id" => "org.couchdb.user:" <> name,
+ "type" => "user",
+ "name" => name,
+ "roles" => roles,
+ "password" => password
+ }
+ resp = Couch.get("/_users/#{user_doc["_id"]}")
+ user_doc = case resp.status_code do
+ 404 ->
+ user_doc
+ sc when sc >= 200 and sc < 300 ->
+ Map.put(user_doc, "_rev", resp.body["_rev"])
+ end
+ resp = Couch.post("/_users", body: user_doc)
+ assert HTTPotion.Response.success? resp
+ assert resp.body["ok"]
+ Map.put(user_doc, "_rev", resp.body["rev"])
+ end
+
def create_db(db_name) do
resp = Couch.put("/#{db_name}")
assert resp.status_code == 201
--
To stop receiving notification emails like this one, please contact
davisp@apache.org.