diff options
author | Juanjo Rodriguez <jjrodrig@gmail.com> | 2019-08-28 19:30:03 +0200 |
---|---|---|
committer | Juanjo Rodriguez <jjrodrig@gmail.com> | 2019-08-28 19:30:03 +0200 |
commit | 9fa66bfe74d126b123b975b389d306ad172c1ddf (patch) | |
tree | ee1e666296959313e9ead6a710e1a7769c452fe7 | |
parent | 377789900ed8e71bcb78373b75c240e185100e49 (diff) | |
download | couchdb-9fa66bfe74d126b123b975b389d306ad172c1ddf.tar.gz |
Convert test setup/teardown logic into idiomatic ExUnit and use @moduletag config
-rw-r--r-- | test/elixir/test/cookie_auth_test.exs | 78 | ||||
-rw-r--r-- | test/elixir/test/users_db_test.exs | 95 |
2 files changed, 93 insertions, 80 deletions
diff --git a/test/elixir/test/cookie_auth_test.exs b/test/elixir/test/cookie_auth_test.exs index 35791a2ea..ac1110be2 100644 --- a/test/elixir/test/cookie_auth_test.exs +++ b/test/elixir/test/cookie_auth_test.exs @@ -5,35 +5,35 @@ defmodule CookieAuthTest do @users_db "_users" + @moduletag config: [ + { + "chttpd_auth", + "authentication_db", + @users_db + }, + { + "couch_httpd_auth", + "authentication_db", + @users_db + }, + { + "couch_httpd_auth", + "iterations", + "1" + }, + { + "admins", + "jan", + "apple" + } + ] + @password "3.141592653589" - test "cookie auth" do + setup do # Create db if not exists Couch.put("/#{@users_db}") - server_config = [ - %{ - :section => "chttpd_auth", - :key => "authentication_db", - :value => @users_db - }, - %{ - :section => "couch_httpd_auth", - :key => "authentication_db", - :value => @users_db - }, - %{ - :section => "couch_httpd_auth", - :key => "iterations", - :value => "1" - }, - %{ - :section => "admins", - :key => "jan", - :value => "apple" - } - ] - resp = Couch.get( "/#{@users_db}/_changes", @@ -42,7 +42,20 @@ defmodule CookieAuthTest do assert resp.body - run_on_modified_server(server_config, &test_fun/0) + on_exit(&tear_down/0) + + :ok + end + + defp tear_down do + # delete users + user = URI.encode("org.couchdb.user:jchris") + user_doc = Couch.get("/#{@users_db}/#{URI.encode(user)}").body + Couch.delete("/#{@users_db}/#{user}", query: [rev: user_doc["_rev"]]) + + user = URI.encode("org.couchdb.user:Jason Davies") + user_doc = Couch.get("/#{@users_db}/#{user}").body + Couch.delete("/#{@users_db}/#{user}", query: [rev: user_doc["_rev"]]) end defp login(user, password) do @@ -199,7 +212,7 @@ defmodule CookieAuthTest do logout(sess) end - defp test_fun do + test "cookie auth" do # test that the users db is born with the auth ddoc ddoc = open_as(@users_db, "_design/_auth", user: "jan") assert ddoc["validate_doc_update"] != nil @@ -229,6 +242,7 @@ defmodule CookieAuthTest do {:password, "eh, Boo-Boo?"} ]) + # make sure we cant create duplicate users create_doc_expect_error(@users_db, duplicate_jchris_user_doc, 409, "conflict") # we can't create _names @@ -348,9 +362,6 @@ defmodule CookieAuthTest do user: "jan" ) - # wait for auth cache invalidation - :timer.sleep(500) - # test that you can't save system (underscore) roles even if you are admin jchris_user_doc = jchris_user_doc @@ -388,14 +399,5 @@ defmodule CookieAuthTest do # log in one last time so run_on_modified_server can clean up the admin account login("jan", "apple") - after - # delete users - user = URI.encode("org.couchdb.user:jchris") - user_doc = Couch.get("/#{@users_db}/#{URI.encode(user)}").body - Couch.delete("/#{@users_db}/#{user}", query: [rev: user_doc["_rev"]]) - - user = URI.encode("org.couchdb.user:Jason Davies") - user_doc = Couch.get("/#{@users_db}/#{user}").body - Couch.delete("/#{@users_db}/#{user}", query: [rev: user_doc["_rev"]]) end end diff --git a/test/elixir/test/users_db_test.exs b/test/elixir/test/users_db_test.exs index 0a74bc31b..71ab2f7e7 100644 --- a/test/elixir/test/users_db_test.exs +++ b/test/elixir/test/users_db_test.exs @@ -5,33 +5,30 @@ defmodule UsersDbTest do @users_db_name "_users" - @server_config [ - %{ - :section => "chttpd_auth", - :key => "authentication_db", - :value => @users_db_name - }, - %{ - :section => "couch_httpd_auth", - :key => "authentication_db", - :value => @users_db_name - }, - %{ - :section => "couch_httpd_auth", - :key => "iterations", - :value => "1" - }, - %{ - :section => "admins", - :key => "jan", - :value => "apple" - } - ] - - @tag :with_db - test "users db", context do - db_name = context[:db_name] - + @moduletag config: [ + { + "chttpd_auth", + "authentication_db", + @users_db_name + }, + { + "couch_httpd_auth", + "authentication_db", + @users_db_name + }, + { + "couch_httpd_auth", + "iterations", + "1" + }, + { + "admins", + "jan", + "apple" + } + ] + + setup do # Create db if not exists Couch.put("/#{@users_db_name}") @@ -42,7 +39,15 @@ defmodule UsersDbTest do ) assert resp.body - run_on_modified_server(@server_config, fn -> test_fun(db_name) end) + + on_exit(&tear_down/0) + + :ok + end + + defp tear_down do + delete_db(@users_db_name) + create_db(@users_db_name) end defp replicate(source, target, rep_options \\ []) do @@ -117,7 +122,9 @@ defmodule UsersDbTest do assert Couch.Session.logout(session).body["ok"] end - defp test_fun(db_name) do + @tag :with_db + test "users db", context do + db_name = context[:db_name] # test that the users db is born with the auth ddoc ddoc = Couch.get("/#{@users_db_name}/_design/_auth") assert ddoc.body["validate_doc_update"] != nil @@ -181,16 +188,23 @@ defmodule UsersDbTest do logout(session) # wait for auth_cache invalidation - :timer.sleep(5000) - - resp = - Couch.get( - "/_session", - headers: [authorization: "Basic #{:base64.encode("jchris@apache.org:funnybone")}"] - ) - - assert resp.body["error"] == "unauthorized" - assert String.contains?(resp.body["reason"], "conflict") + retry_until( + fn -> + resp = + Couch.get( + "/_session", + headers: [ + authorization: "Basic #{:base64.encode("jchris@apache.org:funnybone")}" + ] + ) + + assert resp.body["error"] == "unauthorized" + assert String.contains?(resp.body["reason"], "conflict") + resp + end, + 500, + 20_000 + ) # You can delete a user doc session = login("jan", "apple") @@ -304,8 +318,5 @@ defmodule UsersDbTest do ) assert resp.body["userCtx"]["name"] == "foo@example.org" - after - delete_db(@users_db_name) - create_db(@users_db_name) end end |