summaryrefslogtreecommitdiff
path: root/src/couch/test/eunit/couch_db_mpr_tests.erl
blob: 37137975e514b363a3ffffaea3ecb8866a35703c (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
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(couch_db_mpr_tests).


-include_lib("couch/include/couch_eunit.hrl").
-include_lib("couch/include/couch_db.hrl").

-define(TIMEOUT, 30).

-define(USER, "couch_db_admin").
-define(PASS, "pass").
-define(AUTH, {basic_auth, {?USER, ?PASS}}).
-define(CONTENT_JSON, {"Content-Type", "application/json"}).
-define(JSON_BODY, "{\"foo\": \"bar\"}").
-define(CONTENT_MULTI_RELATED,
        {"Content-Type", "multipart/related;boundary=\"bound\""}).


setup() ->
    Hashed = couch_passwords:hash_admin_password(?PASS),
    ok = config:set("admins", ?USER, ?b2l(Hashed), _Persist=false),
    TmpDb = ?tempdb(),
    Addr = config:get("chttpd", "bind_address", "127.0.0.1"),
    Port = mochiweb_socket_server:get(chttpd, port),
    Url = lists:concat(["http://", Addr, ":", Port, "/", ?b2l(TmpDb)]),
    Url.


teardown(Url) ->
    catch delete_db(Url),
    ok = config:delete("admins", ?USER, _Persist=false).


create_db(Url) ->
    {ok, Status, _, _} = test_request:put(Url, [?CONTENT_JSON, ?AUTH], "{}"),
    ?assert(Status =:= 201 orelse Status =:= 202).


delete_db(Url) ->
    {ok, 200, _, _} = test_request:delete(Url, [?AUTH]).


create_doc(Url, Id, Body, Type) ->
    test_request:put(Url ++ "/" ++ Id, [Type, ?AUTH], Body).


delete_doc(Url, Id, Rev) ->
    test_request:delete(Url ++ "/" ++ Id ++ "?rev=" ++ ?b2l(Rev)).


couch_db_mpr_test_() ->
    {
        "multi-part attachment tests",
        {
            setup,
            fun() ->
                test_util:start_couch([chttpd])
            end,
            fun(Ctx) ->
                test_util:stop_couch(Ctx)
            end,
            {
                foreach,
                fun setup/0,
                fun teardown/1,
                [
                    fun recreate_with_mpr/1
                ]
            }
        }
    }.


recreate_with_mpr(Url) ->
    {timeout, ?TIMEOUT, ?_test(begin
        DocId1 = "foo",
        DocId2 = "bar",

        create_db(Url),
        create_and_delete_doc(Url, DocId1),
        Rev1 = create_with_mpr(Url, DocId1),
        delete_db(Url),

        create_db(Url),
        create_and_delete_doc(Url, DocId1),
        % We create a second unrelated doc to change the
        % position on disk where the attachment is written
        % so that we can assert that the position on disk
        % is not included when calculating a revision.
        create_and_delete_doc(Url, DocId2),
        Rev2 = create_with_mpr(Url, DocId1),
        delete_db(Url),

        ?assertEqual(Rev1, Rev2)
    end)}.


create_and_delete_doc(Url, DocId) ->
    {ok, _, _, Resp} = create_doc(Url, DocId, ?JSON_BODY, ?CONTENT_JSON),
    {Props} = ?JSON_DECODE(Resp),
    Rev = couch_util:get_value(<<"rev">>, Props, undefined),
    ?assert(is_binary(Rev)),
    {ok, _, _, _} = delete_doc(Url, DocId, Rev).


create_with_mpr(Url, DocId) ->
    {ok, _, _, Resp} = create_doc(Url, DocId, mpr(), ?CONTENT_MULTI_RELATED),
    {Props} = ?JSON_DECODE(Resp),
    Rev = couch_util:get_value(<<"rev">>, Props, undefined),
    ?assert(is_binary(Rev)),
    Rev.


mpr() ->
    lists:concat([
        "--bound\r\n",
        "Content-Type: application/json\r\n\r\n",
        "{",
            "\"body\":\"stuff\","
            "\"_attachments\":",
            "{\"foo.txt\":{",
                "\"follows\":true,",
                "\"content_type\":\"text/plain\","
                "\"length\":21",
            "}}"
        "}",
        "\r\n--bound\r\n\r\n",
        "this is 21 chars long",
        "\r\n--bound--epilogue"
    ]).