1% Licensed under the Apache License, Version 2.0 (the "License"); you may not
2% use this file except in compliance with the License. You may obtain a copy of
3% the License at
4%
5%   http://www.apache.org/licenses/LICENSE-2.0
6%
7% Unless required by applicable law or agreed to in writing, software
8% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10% License for the specific language governing permissions and limitations under
11% the License.
12
13-module(couch_replicator_id_too_long_tests).
14
15-include_lib("couch/include/couch_eunit.hrl").
16-include_lib("couch/include/couch_db.hrl").
17-include_lib("couch_replicator/src/couch_replicator.hrl").
18
19
20setup(_) ->
21    Ctx = test_util:start_couch([couch_replicator]),
22    Source = create_db(),
23    create_doc(Source),
24    Target = create_db(),
25    {Ctx, {Source, Target}}.
26
27
28teardown(_, {Ctx, {Source, Target}}) ->
29    delete_db(Source),
30    delete_db(Target),
31    config:set("replicator", "max_document_id_length", "infinity"),
32    ok = test_util:stop_couch(Ctx).
33
34
35id_too_long_replication_test_() ->
36    Pairs = [{remote, remote}],
37    {
38        "Doc id too long tests",
39        {
40            foreachx,
41            fun setup/1, fun teardown/2,
42            [{Pair, fun should_succeed/2} || Pair <- Pairs] ++
43            [{Pair, fun should_fail/2} || Pair <- Pairs]
44        }
45    }.
46
47
48should_succeed({From, To}, {_Ctx, {Source, Target}}) ->
49    RepObject = {[
50        {<<"source">>, db_url(From, Source)},
51        {<<"target">>, db_url(To, Target)}
52    ]},
53    config:set("replicator", "max_document_id_length", "5"),
54    {ok, _} = couch_replicator:replicate(RepObject, ?ADMIN_USER),
55    ?_assertEqual(ok, couch_replicator_test_helper:compare_dbs(Source, Target)).
56
57
58should_fail({From, To}, {_Ctx, {Source, Target}}) ->
59    RepObject = {[
60        {<<"source">>, db_url(From, Source)},
61        {<<"target">>, db_url(To, Target)}
62    ]},
63    config:set("replicator", "max_document_id_length", "4"),
64    {ok, _} = couch_replicator:replicate(RepObject, ?ADMIN_USER),
65    ?_assertError({badmatch, {not_found, missing}},
66        couch_replicator_test_helper:compare_dbs(Source, Target)).
67
68
69create_db() ->
70    DbName = ?tempdb(),
71    {ok, Db} = couch_db:create(DbName, [?ADMIN_CTX]),
72    ok = couch_db:close(Db),
73    DbName.
74
75
76create_doc(DbName) ->
77    {ok, Db} = couch_db:open(DbName, [?ADMIN_CTX]),
78    Doc = couch_doc:from_json_obj({[{<<"_id">>, <<"12345">>}]}),
79    {ok, _} = couch_db:update_doc(Db, Doc, []),
80    couch_db:close(Db).
81
82
83delete_db(DbName) ->
84    ok = couch_server:delete(DbName, [?ADMIN_CTX]).
85
86
87db_url(remote, DbName) ->
88    Addr = config:get("httpd", "bind_address", "127.0.0.1"),
89    Port = mochiweb_socket_server:get(couch_httpd, port),
90    ?l2b(io_lib:format("http://~s:~b/~s", [Addr, Port, DbName])).
91