summaryrefslogtreecommitdiff
path: root/test/elixir/test/attachment_names_test.exs
blob: 4593a85046263ded00a2716bdbf87257e4b9917a (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
86
87
88
89
90
91
92
93
94
95
96
97
defmodule AttachmentNamesTest do
  use CouchTestCase

  @moduletag :attachments

  @good_doc """
   {
    "_id": "good_doc",
    "_attachments": {
      "Kолян.txt": {
        "content_type": "application/octet-stream",
        "data": "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
      }
    }
  }
  """

  @bin_att_doc %{
    _id: "bin_doc",
    _attachments: %{
      footxt: %{
        content_type: "text/plain",
        data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
      }
    }
  }

  @bin_data "JHAPDO*AU£PN ){(3u[d 93DQ9¡€])}    ææøo'∂ƒæ≤çæππ•¥∫¶®#†π¶®¥π€ª®˙π8np"

  @leading_underscores_att """
   {
    "_id": "bin_doc2",
    "_attachments": {
      "_foo.txt": {
        "content_type": "text/plain",
        "data": "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
      }
    }
  }
  """

  @moduledoc """
  Test CouchDB attachment names
  This is a port of the attachment_names.js suite
  """

  @tag :with_db
  test "saves attachment names successfully", context do
    db_name = context[:db_name]
    filename = URI.encode("Kолян.txt", &URI.char_unreserved?(&1))
    resp = Couch.post("/#{db_name}", body: @good_doc)
    msg = "Should return 201-Created"
    assert resp.status_code in [201, 202], msg

    resp = Couch.get("/#{db_name}/good_doc/#{filename}")
    assert resp.body == "This is a base64 encoded text"
    assert resp.headers["Content-Type"] == "application/octet-stream"
    assert resp.headers["Etag"] == ~s("aEI7pOYCRBLTRQvvqYrrJQ==")

    resp = Couch.post("/#{db_name}", body: @bin_att_doc)
    assert(resp.status_code == 201)

    # standalone docs
    resp =
      Couch.put(
        "/#{db_name}/bin_doc3/attachmenttxt",
        body: @bin_data,
        headers: ["Content-Type": "text/plain;charset=utf-8"]
      )

    assert(resp.status_code == 201)

    # bulk docs
    docs = %{
      docs: [@bin_att_doc]
    }

    resp =
      Couch.post(
        "/#{db_name}/_bulk_docs",
        body: docs
      )

    assert(resp.status_code == 201)

    resp =
      Couch.put(
        "/#{db_name}/bin_doc2",
        body: @leading_underscores_att
      )

    assert resp.status_code == 400

    assert resp.body["reason"] ==
             "Attachment name '_foo.txt' starts with prohibited character '_'"
  end
end