summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul J. Davis <paul.joseph.davis@gmail.com>2018-01-26 14:32:31 -0600
committerPaul J. Davis <paul.joseph.davis@gmail.com>2018-01-26 14:56:21 -0600
commit21731d68d418a64572abca448dc9d5377a09ee72 (patch)
treea9f2e3d19f57c350df8fd35c41fc33be1c25316a
parent3589a179bfe848c20512c2461345f30ee32f9f4f (diff)
downloadcouchdb-21731d68d418a64572abca448dc9d5377a09ee72.tar.gz
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`.
-rw-r--r--test/elixir/test/test_helper.exs57
1 files changed, 56 insertions, 1 deletions
diff --git a/test/elixir/test/test_helper.exs b/test/elixir/test/test_helper.exs
index 9baf20432..f84e1a074 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