summaryrefslogtreecommitdiff
path: root/test/elixir/test/attachments_test.exs
blob: f1dd3ef61b948bee526f1f407e90ba2eeaf21593 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
defmodule AttachmentsTest do
  use CouchTestCase

  @moduletag :attachments

  #  MD5 Digests of compressible attachments and therefore Etags
  #  will vary depending on platform gzip implementation.
  #  These MIME types are defined in [attachments] compressible_types
  @bin_att_doc %{
    _id: "bin_doc",
    _attachments: %{
      "foo.txt": %{
        content_type: "application/octet-stream",
        data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
      }
    }
  }

  @moduledoc """
  Test CouchDB attachments
  This is a port of the attachments.js suite
  """

  @tag :with_db
  test "saves attachment successfully", context do
    db_name = context[:db_name]

    resp = Couch.put("/#{db_name}/bin_doc", body: @bin_att_doc, query: %{w: 3})
    assert resp.status_code in [201, 202]
    assert resp.body["ok"]
  end

  @tag :with_db
  test "errors for bad attachment", context do
    db_name = context[:db_name]

    bad_att_doc = %{
      _id: "bad_doc",
      _attachments: %{
        "foo.txt": %{
          content_type: "text/plain",
          data: "notBase64Encoded="
        }
      }
    }

    resp = Couch.put("/#{db_name}/bad_doc", body: bad_att_doc, query: %{w: 3})
    assert resp.status_code == 400
  end

  @tag :with_db
  test "reads attachment successfully", context do
    db_name = context[:db_name]

    resp = Couch.put("/#{db_name}/bin_doc", body: @bin_att_doc, query: %{w: 3})
    assert resp.status_code in [201, 202]

    resp = Couch.get("/#{db_name}/bin_doc/foo.txt", body: @bin_att_doc)

    assert resp.body == "This is a base64 encoded text"
    assert resp.headers["Content-Type"] == "application/octet-stream"
    assert resp.headers["Etag"] == "\"aEI7pOYCRBLTRQvvqYrrJQ==\""
  end

  @tag :with_db
  test "update attachment", context do
    db_name = context[:db_name]

    bin_att_doc2 = %{
      _id: "bin_doc2",
      _attachments: %{
        "foo.txt": %{
          content_type: "text/plain",
          data: ""
        }
      }
    }

    resp = Couch.put("/#{db_name}/bin_doc2", body: bin_att_doc2, query: %{w: 3})
    assert resp.status_code in [201, 202]
    rev = resp.body["rev"]

    resp = Couch.get("/#{db_name}/bin_doc2/foo.txt")

    assert resp.headers["Content-Type"] == "text/plain"
    assert resp.body == ""

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

    assert resp.status_code in [201, 202]
    assert Regex.match?(~r/bin_doc2\/foo2.txt/, resp.headers["location"])
  end

  @tag :with_db
  test "delete attachment", context do
    db_name = context[:db_name]

    resp = Couch.put("/#{db_name}/bin_doc", body: @bin_att_doc, query: %{w: 3})
    assert resp.status_code in [201, 202]
    rev = resp.body["rev"]

    resp = Couch.delete("/#{db_name}/bin_doc/foo.txt", query: %{w: 3})
    assert resp.status_code == 409

    resp = Couch.delete("/#{db_name}/bin_doc/foo.txt", query: %{w: 3, rev: "4-garbage"})
    assert resp.status_code == 409
    assert resp.body["error"] == "not_found"
    assert resp.body["reason"] == "missing_rev"

    resp = Couch.delete("/#{db_name}/bin_doc/notexisting.txt", query: %{w: 3, rev: rev})
    assert resp.status_code == 404
    assert resp.body["error"] == "not_found"
    assert resp.body["reason"] == "Document is missing attachment"

    resp = Couch.delete("/#{db_name}/bin_doc/foo.txt", query: %{w: 3, rev: rev})
    assert resp.status_code == 200
    assert resp.headers["location"] == nil
  end

  @tag :with_db
  test "delete attachment request with a payload should not block following requests", context do
    db_name = context[:db_name]

    resp = Couch.put("/#{db_name}/bin_doc", body: @bin_att_doc, query: %{w: 3})
    assert resp.status_code in [201, 202]
    rev = resp.body["rev"]

    resp = Couch.delete("/#{db_name}/bin_doc/foo.txt", body: 'some payload', query: %{w: 3, rev: rev}, ibrowse: [{:max_sessions, 1}, {:max_pipeline_size, 1}])
    assert resp.status_code == 200

    resp = Couch.get("/", timeout: 1000, ibrowse: [{:max_sessions, 1}, {:max_pipeline_size, 1}])
    assert resp.status_code == 200
  end

  @tag :with_db
  test "saves binary", context do
    db_name = context[:db_name]

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

    resp =
      Couch.put(
        "/#{db_name}/bin_doc3/attachment.txt",
        body: bin_data,
        headers: ["Content-Type": "text/plain;charset=utf-8"],
        query: %{w: 3}
      )

    assert resp.status_code in [201, 202]
    assert resp.body["ok"]

    rev = resp.body["rev"]

    resp = Couch.get("/#{db_name}/bin_doc3/attachment.txt")
    assert resp.body == bin_data

    resp =
      Couch.put("/#{db_name}/bin_doc3/attachment.txt", body: bin_data, query: %{w: 3})

    assert resp.status_code == 409

    # non-existent rev
    resp =
      Couch.put(
        "/#{db_name}/bin_doc3/attachment.txt",
        query: %{rev: "1-adae8575ecea588919bd08eb020c708e", w: 3},
        headers: ["Content-Type": "text/plain;charset=utf-8"],
        body: bin_data
      )

    assert resp.status_code == 409

    # current rev
    resp =
      Couch.put(
        "/#{db_name}/bin_doc3/attachment.txt",
        query: %{rev: rev, w: 3},
        headers: ["Content-Type": "text/plain;charset=utf-8"],
        body: bin_data
      )

    assert resp.status_code in [201, 202]

    rev = resp.body["rev"]

    resp = Couch.get("/#{db_name}/bin_doc3/attachment.txt")
    assert String.downcase(resp.headers["Content-Type"]) == "text/plain;charset=utf-8"
    assert resp.body == bin_data

    resp = Couch.get("/#{db_name}/bin_doc3/attachment.txt", query: %{rev: rev})
    assert String.downcase(resp.headers["Content-Type"]) == "text/plain;charset=utf-8"
    assert resp.body == bin_data

    resp = Couch.delete("/#{db_name}/bin_doc3/attachment.txt", query: %{rev: rev, w: 3})
    assert resp.status_code == 200

    resp = Couch.get("/#{db_name}/bin_doc3/attachment.txt")
    assert resp.status_code == 404

    resp = Couch.get("/#{db_name}/bin_doc3/attachment.txt", query: %{rev: rev})
    assert String.downcase(resp.headers["Content-Type"]) == "text/plain;charset=utf-8"
    assert resp.body == bin_data
  end

  @tag :with_db
  test "empty attachments", context do
    db_name = context[:db_name]

    resp =
      Couch.put(
        "/#{db_name}/bin_doc4/attachment.txt",
        body: "",
        headers: ["Content-Type": "text/plain;charset=utf-8"],
        query: %{w: 3}
      )

    assert resp.status_code in [201, 202]
    assert resp.body["ok"]

    rev = resp.body["rev"]

    resp = Couch.get("/#{db_name}/bin_doc4/attachment.txt")
    assert resp.status_code == 200
    assert resp.body == ""

    resp =
      Couch.put(
        "/#{db_name}/bin_doc4/attachment.txt",
        query: %{rev: rev, w: 3},
        headers: ["Content-Type": "text/plain;charset=utf-8"],
        body: "This is a string"
      )

    assert resp.status_code in [201, 202]

    resp = Couch.get("/#{db_name}/bin_doc4/attachment.txt")
    assert resp.status_code == 200
    assert resp.body == "This is a string"
  end

  @tag :with_db
  test "large attachments COUCHDB-366", context do
    db_name = context[:db_name]

    lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
    range = 1..10

    large_att = Enum.reduce(range, lorem, fn _, acc -> lorem <> acc end)

    resp =
      Couch.put(
        "/#{db_name}/bin_doc5/attachment.txt",
        body: large_att,
        query: %{w: 3},
        headers: ["Content-Type": "text/plain;charset=utf-8"]
      )

    assert resp.status_code in [201, 202]
    assert resp.body["ok"]

    resp = Couch.get("/#{db_name}/bin_doc5/attachment.txt")
    assert String.downcase(resp.headers["Content-Type"]) == "text/plain;charset=utf-8"
    assert resp.body == large_att

    lorem_b64 =
      "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4g"

    range = 1..10

    large_b64_att = Enum.reduce(range, lorem_b64, fn _, acc -> lorem_b64 <> acc end)

    resp =
      Couch.get(
        "/#{db_name}/bin_doc5",
        query: %{attachments: true},
        headers: [Accept: "application/json"]
      )

    assert large_b64_att == resp.body["_attachments"]["attachment.txt"]["data"]
  end

  @tag :with_db
  test "etags for attachments", context do
    db_name = context[:db_name]

    lorem_att = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "

    resp =
      Couch.put(
        "/#{db_name}/bin_doc6/attachment.txt",
        body: lorem_att,
        headers: ["Content-Type": "text/plain;charset=utf-8"],
        query: %{w: 3}
      )

    assert resp.status_code in [201, 202]
    assert resp.body["ok"]

    resp = Couch.get("/#{db_name}/bin_doc6/attachment.txt")
    assert resp.status_code == 200
    etag = resp.headers["etag"]

    resp =
      Couch.get("/#{db_name}/bin_doc6/attachment.txt", headers: ["if-none-match": etag])

    assert resp.status_code == 304
  end

  @tag :with_db
  test "COUCHDB-497 - empty attachments", context do
    db_name = context[:db_name]

    lorem_att = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "

    resp =
      Couch.put(
        "/#{db_name}/bin_doc7/attachment.txt",
        body: lorem_att,
        headers: ["Content-Type": "text/plain;charset=utf-8"],
        query: %{w: 3}
      )

    assert resp.status_code in [201, 202]
    assert resp.body["ok"]

    rev = resp.body["rev"]

    resp =
      Couch.put(
        "/#{db_name}/bin_doc7/empty.txt",
        query: %{rev: rev, w: 3},
        body: "",
        headers: ["Content-Type": "text/plain;charset=utf-8"]
      )

    assert resp.status_code in [201, 202]
    rev = resp.body["rev"]

    resp =
      Couch.put(
        "/#{db_name}/bin_doc7/empty.txt",
        query: %{rev: rev, w: 3},
        body: "",
        headers: ["Content-Type": "text/plain;charset=utf-8"]
      )

    assert resp.status_code in [201, 202]
  end

  @tag :with_db
  test "implicit doc creation allows creating docs with a reserved id. COUCHDB-565",
       context do
    db_name = context[:db_name]

    resp =
      Couch.put(
        "/#{db_name}/_nonexistant/attachment.txt",
        body: "ATTACHMENT INFO",
        headers: ["Content-Type": "text/plain;charset=utf-8"],
        query: %{w: 3}
      )

    assert resp.status_code == 400
  end

  @tag :with_db
  test "COUCHDB-809 - stubs should only require the 'stub' field", context do
    db_name = context[:db_name]

    stub_doc = %{
      _id: "stub_doc",
      _attachments: %{
        "foo.txt": %{
          content_type: "text/plain",
          data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
        }
      }
    }

    resp =
      Couch.put(
        "/#{db_name}/stub_doc",
        body: stub_doc,
        query: %{w: 3}
      )

    assert resp.status_code in [201, 202]
    assert resp.body["ok"]

    rev = resp.body["rev"]

    stub_doc =
      Map.merge(stub_doc, %{
        _rev: rev,
        _attachments: %{"foo.txt": %{stub: true}}
      })

    resp =
      Couch.put(
        "/#{db_name}/stub_doc",
        query: %{rev: rev, w: 3},
        body: stub_doc
      )

    assert resp.status_code in [201, 202]
    assert resp.body["ok"]

    rev = resp.body["rev"]

    stub_doc =
      Map.merge(stub_doc, %{
        _rev: rev,
        _attachments: %{"foo.txt": %{stub: true, revpos: 10}}
      })

    resp =
      Couch.put(
        "/#{db_name}/stub_doc",
        query: %{rev: rev},
        body: stub_doc
      )

    assert resp.status_code == 412
    assert resp.body["error"] == "missing_stub"
  end

  @tag :with_db
  test "md5 header for attachments", context do
    db_name = context[:db_name]
    md5 = "MntvB0NYESObxH4VRDUycw=="

    bin_data = "foo bar"

    resp =
      Couch.put(
        "/#{db_name}/bin_doc8/attachment.txt",
        body: bin_data,
        headers: ["Content-Type": "application/octet-stream", "Content-MD5": md5],
        query: %{w: 3}
      )

    assert resp.status_code in [201, 202]
    assert resp.body["ok"]

    resp = Couch.get("/#{db_name}/bin_doc8/attachment.txt")
    assert resp.status_code == 200
    assert md5 == resp.headers["Content-MD5"]
  end

  @tag :with_db
  test "attachment via multipart/form-data", context do
    db_name = context[:db_name]

    form_data_doc = %{
      _id: "form-data-doc"
    }

    resp =
      Couch.put(
        "/#{db_name}/form_data_doc",
        body: form_data_doc,
        query: %{w: 3}
      )

    assert resp.status_code in [201, 202]
    assert resp.body["ok"]
    rev = resp.body["rev"]

    body =
      "------TF\r\n" <>
        "Content-Disposition: form-data; name=\"_rev\"\r\n\r\n" <>
        rev <>
        "\r\n" <>
        "------TF\r\n" <>
        "Content-Disposition: form-data; name=\"_attachments\"; filename=\"file.txt\"\r\n" <>
        "Content-Type: text/plain\r\n\r\n" <>
        "contents of file.txt\r\n\r\n" <> "------TF--"

    resp =
      Couch.post(
        "/#{db_name}/form_data_doc",
        body: body,
        query: %{w: 3},
        headers: [
          Referer: "http://127.0.0.1:15984",
          "Content-Type": "multipart/form-data; boundary=----TF",
          "Content-Length": byte_size(body)
        ]
      )

    assert resp.status_code in [201, 202]
    assert resp.body["ok"]

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

    doc = resp.body
    assert doc["_attachments"]["file.txt"]["length"] == 22
  end
end