summaryrefslogtreecommitdiff
path: root/test/elixir/test/attachment_paths_test.exs
blob: b776feabf57875f7312f8cab5c139b15f891a3b2 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
defmodule AttachmentPathsTest do
  use CouchTestCase

  @moduletag :attachments

  @bin_att_doc """
  {
     "_id": "bin_doc",
     "_attachments": {
       "foo/bar.txt": {
         "content_type": "text/plain",
         "data": "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
       },
       "foo%2Fbaz.txt": {
         "content_type": "text/plain",
         "data": "V2UgbGlrZSBwZXJjZW50IHR3byBGLg=="
       }
     }
   }
  """

  @design_att_doc """
  {
     "_id": "_design/bin_doc",
     "_attachments": {
       "foo/bar.txt": {
         "content_type": "text/plain",
         "data": "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
       },
       "foo%2Fbaz.txt": {
         "content_type": "text/plain",
         "data": "V2UgbGlrZSBwZXJjZW50IHR3byBGLg=="
       }
     }
   }
  """

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

  @tag :with_db_name
  test "manages attachment paths successfully", context do
    db_name =
      URI.encode(
        "#{context[:db_name]}/with_slashes",
        &URI.char_unreserved?(&1)
      )

    create_db(db_name)

    resp = Couch.post("/#{db_name}", body: @bin_att_doc)
    msg = "Should return 201-Created"

    assert resp.status_code in [201, 202], msg

    rev = resp.body["rev"]

    resp = Couch.get("/#{db_name}/bin_doc/foo/bar.txt")
    assert resp.status_code == 200
    assert resp.body == "This is a base64 encoded text"
    assert resp.headers["Content-Type"] == "text/plain"

    resp = Couch.get("/#{db_name}/bin_doc/foo%2Fbar.txt")
    assert resp.status_code == 200
    assert resp.body == "This is a base64 encoded text"
    assert resp.headers["Content-Type"] == "text/plain"

    resp = Couch.get("/#{db_name}/bin_doc/foo/baz.txt")
    assert resp.status_code == 404

    resp = Couch.get("/#{db_name}/bin_doc/foo%252Fbaz.txt")
    assert resp.status_code == 200
    assert resp.body == "We like percent two F."

    resp =
      Couch.put(
        "/#{db_name}/bin_doc/foo/attachment.txt",
        body: "Just some text",
        headers: ["Content-Type": "text/plain;charset=utf-8"]
      )

    assert resp.status_code == 409

    resp =
      Couch.put(
        "/#{db_name}/bin_doc/foo/bar2.txt",
        query: %{rev: rev},
        body: "This is no base64 encoded text",
        headers: ["Content-Type": "text/plain;charset=utf-8"]
      )

    assert resp.status_code in [201, 202]

    resp = Couch.get("/#{db_name}/bin_doc")
    assert resp.status_code == 200

    att_doc = resp.body

    assert att_doc["_attachments"]["foo/bar.txt"]
    assert att_doc["_attachments"]["foo%2Fbaz.txt"]
    assert att_doc["_attachments"]["foo/bar2.txt"]

    ctype = att_doc["_attachments"]["foo/bar2.txt"]["content_type"]
    assert ctype == "text/plain;charset=utf-8"

    assert att_doc["_attachments"]["foo/bar2.txt"]["length"] == 30
    delete_db(db_name)
  end

  @tag :with_db_name
  test "manages attachment paths successfully - design docs", context do
    db_name =
      URI.encode(
        "#{context[:db_name]}/with_slashes",
        &URI.char_unreserved?(&1)
      )

    create_db(db_name)
    resp = Couch.post("/#{db_name}", body: @design_att_doc)
    assert resp.status_code in [201, 202]

    rev = resp.body["rev"]

    resp = Couch.get("/#{db_name}/_design/bin_doc/foo/bar.txt")
    assert resp.status_code == 200
    assert resp.body == "This is a base64 encoded text"
    assert resp.headers["Content-Type"] == "text/plain"

    resp = Couch.get("/#{db_name}/_design/bin_doc/foo%2Fbar.txt")
    assert resp.status_code == 200
    assert resp.body == "This is a base64 encoded text"
    assert resp.headers["Content-Type"] == "text/plain"

    resp = Couch.get("/#{db_name}/_design/bin_doc/foo/baz.txt")
    assert resp.status_code == 404

    resp = Couch.get("/#{db_name}/_design/bin_doc/foo%252Fbaz.txt")
    assert resp.status_code == 200
    assert resp.body == "We like percent two F."

    resp =
      Couch.put(
        "/#{db_name}/_design/bin_doc/foo/attachment.txt",
        body: "Just some text",
        headers: ["Content-Type": "text/plain;charset=utf-8"]
      )

    assert resp.status_code == 409

    resp =
      Couch.put(
        "/#{db_name}/_design/bin_doc/foo/bar2.txt",
        query: %{rev: rev},
        body: "This is no base64 encoded text",
        headers: ["Content-Type": "text/plain;charset=utf-8"]
      )

    assert resp.status_code in [201, 202]

    resp = Couch.get("/#{db_name}/_design/bin_doc")
    assert resp.status_code == 200

    att_doc = resp.body

    assert att_doc["_attachments"]["foo/bar.txt"]
    assert att_doc["_attachments"]["foo%2Fbaz.txt"]
    assert att_doc["_attachments"]["foo/bar2.txt"]

    ctype = att_doc["_attachments"]["foo/bar2.txt"]["content_type"]
    assert ctype == "text/plain;charset=utf-8"

    assert att_doc["_attachments"]["foo/bar2.txt"]["length"] == 30
    delete_db(db_name)
  end
end