iilyak commented on a change in pull request #2007: Port javascript tests auth cache, cookie auth and users db to elixir URL: https://github.com/apache/couchdb/pull/2007#discussion_r295947665
########## File path: test/elixir/test/cookie_auth_test.exs ########## @@ -0,0 +1,415 @@ +defmodule CookieAuthTest do + use CouchTestCase + + @moduletag :authentication + + @users_db "_users" + + @password "3.141592653589" + + test "test cookie auth" 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", + query: [feed: "longpoll", timeout: 5000, filter: "_design"] + ) + + assert resp.body + + run_on_modified_server(server_config, &test_fun/0) + end + + defp login(user, password) do + sess = Couch.login(user, password) + assert sess.cookie, "Login correct is expected" + sess + end + + defp logout(session) do + assert Couch.Session.logout(session).body["ok"] + end + + defp loginAsUser(user) do + pws = %{ + "jan" => "apple", + "Jason Davies" => @password, + "jchris" => "funnybone" + } + + user1 = Regex.replace(~r/[0-9]$/, user, "") + login(user1, pws[user]) + end + + defp create_doc_expect_error(db_name, doc, status_code, msg) do + resp = Couch.post("/#{db_name}", body: doc) + assert resp.status_code == status_code + assert resp.body["error"] == msg + resp + end + + defp open_as(db_name, doc_id, options) do + use_session = Keyword.get(options, :use_session) + user = Keyword.get(options, :user) + expect_response = Keyword.get(options, :expect_response, 200) + expect_message = Keyword.get(options, :error_message) + + session = + if use_session == nil do + loginAsUser(user) + else + use_session + end + + resp = + Couch.get( + "/#{db_name}/#{URI.encode(doc_id)}", + headers: [ + Cookie: session.cookie, + "X-CouchDB-www-Authenticate": "Cookie" + ] + ) + + if use_session == nil do + logout(session) + end + + assert resp.status_code == expect_response + + if expect_message != nil do + assert resp.body["error"] == expect_message + end + + resp.body + end + + defp save_as(db_name, doc, options) do + use_session = Keyword.get(options, :use_session) + user = Keyword.get(options, :user) + expect_response = Keyword.get(options, :expect_response, [201, 202]) + expect_message = Keyword.get(options, :error_message) + + session = Review comment: Same as above `session = use_session || loginAsUser(user)`. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services