summaryrefslogtreecommitdiff
path: root/test/elixir/test/invalid_docids_test.exs
blob: edce5cc6595383649a560494ce5af406e1d6c4e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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