diff options
author | Alessio Biancalana <dottorblaster@gmail.com> | 2019-03-17 11:19:45 +0100 |
---|---|---|
committer | garren smith <garren.smith@gmail.com> | 2019-03-17 12:19:45 +0200 |
commit | 8efe9b29015549cc5d2d64033b2cf1e26fccfa53 (patch) | |
tree | 6460d4cbcb8660332f1bf6385448022893188890 | |
parent | f88d35babaf67687a3a3684fff19b4b3e82d585f (diff) | |
download | couchdb-8efe9b29015549cc5d2d64033b2cf1e26fccfa53.tar.gz |
test: port invalid_docids to Elixir test suite (#1968)
-rw-r--r-- | test/elixir/README.md | 2 | ||||
-rw-r--r-- | test/elixir/test/invalid_docids_test.exs | 85 |
2 files changed, 86 insertions, 1 deletions
diff --git a/test/elixir/README.md b/test/elixir/README.md index 883afb512..6f0fa2942 100644 --- a/test/elixir/README.md +++ b/test/elixir/README.md @@ -51,7 +51,7 @@ X means done, - means partially - [ ] Port etags_views.js - [ ] Port form_submit.js - [ ] Port http.js - - [ ] Port invalid_docids.js + - [X] Port invalid_docids.js - [ ] Port jsonp.js - [X] Port large_docs.js - [ ] Port list_views.js diff --git a/test/elixir/test/invalid_docids_test.exs b/test/elixir/test/invalid_docids_test.exs new file mode 100644 index 000000000..edce5cc65 --- /dev/null +++ b/test/elixir/test/invalid_docids_test.exs @@ -0,0 +1,85 @@ +defmodule InvalidDocIDsTest do + use CouchTestCase + + @moduletag :invalid_doc_ids + + @moduledoc """ + Test invalid document ids + This is a port of the invalid_docids.js suite + """ + + @tag :with_db + test "_local-prefixed ids are illegal", context do + db_name = context[:db_name] + + [ + "/#{db_name}/_local", + "/#{db_name}/_local/", + "/#{db_name}/_local%2F", + "/#{db_name}/_local/foo/bar" + ] + |> Enum.each(fn url -> + %{status_code: status, body: body} = Couch.put(url, body: %{}) + assert status === 400 + assert body["error"] === "bad_request" + end) + end + + @tag :with_db + test "using a non-string id is forbidden", context do + db_name = context[:db_name] + %{status_code: status, body: body} = Couch.post("/#{db_name}", body: %{:_id => 1}) + assert status === 400 + assert body["error"] === "illegal_docid" + assert body["reason"] === "Document id must be a string" + end + + @tag :with_db + test "a PUT request with absent _id is forbidden", context do + db_name = context[:db_name] + %{status_code: status, body: body} = Couch.put("/#{db_name}/_other", body: %{}) + assert status === 400 + assert body["error"] === "illegal_docid" + end + + @tag :with_db + test "accidental POST to form handling code", context do + db_name = context[:db_name] + %{status_code: status, body: body} = Couch.put("/#{db_name}/_tmp_view", body: %{}) + assert status === 400 + assert body["error"] === "illegal_docid" + end + + @tag :with_db + test "invalid _prefix", context do + db_name = context[:db_name] + + %{status_code: status, body: body} = + Couch.post("/#{db_name}", body: %{:_id => "_invalid"}) + + assert status === 400 + assert body["error"] === "illegal_docid" + assert body["reason"] === "Only reserved document ids may start with underscore." + end + + @tag :with_db + test "explicit _bulk_docks policy", context do + db_name = context[:db_name] + docs = [%{:_id => "_design/foo"}, %{:_id => "_local/bar"}] + + %{status_code: status} = Couch.post("/#{db_name}/_bulk_docs", body: %{docs: docs}) + + assert status in [201, 202] + + Enum.each(docs, fn %{:_id => id} -> + %{:body => %{"_id" => document_id}} = Couch.get("/#{db_name}/#{id}") + assert document_id === id + end) + + %{status_code: invalid_status, body: invalid_body} = + Couch.post("/#{db_name}/_bulk_docs", body: %{docs: [%{:_id => "_invalid"}]}) + + assert invalid_status === 400 + assert invalid_body["error"] === "illegal_docid" + end +end |