summaryrefslogtreecommitdiff
path: root/test/elixir/test/replicator_db_bad_rep_id_test.exs
blob: 693c9d85d5f64165ffbc03068377e9123ec004e9 (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
defmodule ReplicationBadIdTest do
  use CouchTestCase

  @moduledoc """
  This is a port of the replicator_db_bad_rep_id.js suite
  """

  @docs [
    %{
      _id: "foo1",
      value: 11
    },
    %{
      _id: "foo2",
      value: 22
    },
    %{
      _id: "foo3",
      value: 33
    }
  ]

  test "replication doc with bad rep id" do
    name = random_db_name()
    src_db_name = name <> "_src"
    tgt_db_name = name <> "_tgt"

    create_db(src_db_name)
    bulk_save(src_db_name, @docs)
    create_db(tgt_db_name)
    delete_db_on_exit([src_db_name, tgt_db_name])

    src_db_url = Couch.process_url("/#{src_db_name}")
    tgt_db_url = Couch.process_url("/#{tgt_db_name}")

    replication_doc = %{
      _id: "foo_rep_#{name}",
      source: src_db_url,
      target: tgt_db_url,
      replication_id: "1234abc"
    }

    {:ok, repdoc} = create_doc("_replicator", replication_doc)
    delete_doc_on_exit("_replicator", repdoc.body["id"])

    retry_until(fn ->
      resp = Couch.get("/_replicator/#{replication_doc[:_id]}")
      assert resp.body["_replication_state"] == "completed"
      resp
    end)

    Enum.each(@docs, fn doc ->
      copy_resp = Couch.get("/#{tgt_db_name}/#{doc[:_id]}")
      assert copy_resp.status_code == 200
      assert copy_resp.body["value"] === doc.value
    end)

    resp = Couch.get("/_replicator/#{replication_doc[:_id]}")
    assert resp.status_code == 200
    assert resp.body["source"] == replication_doc.source
    assert resp.body["target"] == replication_doc.target
    assert resp.body["_replication_state"] == "completed"
    {:ok, _, _} = DateTime.from_iso8601(resp.body["_replication_state_time"])
    assert resp.body["_replication_id"] == nil
  end

  def delete_db_on_exit(db_names) when is_list(db_names) do
    on_exit(fn ->
      Enum.each(db_names, fn name ->
        delete_db(name)
      end)
    end)
  end

  def delete_doc_on_exit(db_name, doc_id) do
    on_exit(fn ->
      resp = Couch.get("/#{db_name}/#{doc_id}")
      Couch.delete("/#{db_name}/#{doc_id}?rev=#{resp.body["_rev"]}")
    end)
  end
end