summaryrefslogtreecommitdiff
path: root/src/chttpd/test/chttpd_welcome_test.erl
blob: af9732f571f0280177e053a6bfa056eaf51eb6d8 (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
% 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(chttpd_welcome_test).

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

-define(USER, "chttpd_db_test_admin").
-define(PASS, "pass").
-define(AUTH, {basic_auth, {?USER, ?PASS}}).
-define(CONTENT_JSON, {"Content-Type", "application/json"}).


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


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


welcome_test_() ->
    {
        "chttpd welcome endpoint tests",
        {
            setup,
            fun chttpd_test_util:start_couch/0, fun chttpd_test_util:stop_couch/1,
            {
                foreach,
                fun setup/0, fun teardown/1,
                [
                    fun should_have_version/1,
                    fun should_have_features/1
                ]
            }
        }
    }.


should_have_version(Url) ->
    ?_test(begin
        {ok, Status, _, Body} = test_request:get(Url, [?CONTENT_JSON, ?AUTH]),
        ?assertEqual(200, Status),
        {Json} = ?JSON_DECODE(Body),
        Version = couch_util:get_value(<<"version">>, Json, undefined),
        CouchDB = couch_util:get_value(<<"couchdb">>, Json, undefined),
        Features = couch_util:get_value(<<"features">>, Json, undefined),
        ?assertEqual(<<"Welcome">>, CouchDB),
        RealVersion = list_to_binary(couch_server:get_version()),
        ?assertEqual(RealVersion, Version),
        ?assert(is_list(Features))
    end).


should_have_features(Url) ->
    ?_test(begin
        config:enable_feature(snek),
        {ok, 200, _, Body1} = test_request:get(Url, [?CONTENT_JSON, ?AUTH]),
        {Json1} = ?JSON_DECODE(Body1),
        Features1 = couch_util:get_value(<<"features">>, Json1, undefined),
        ?assert(is_list(Features1)),
        ?assert(lists:member(<<"snek">>, Features1)),
        config:disable_feature(snek),
        {ok, 200, _, Body2} = test_request:get(Url, [?CONTENT_JSON, ?AUTH]),
        {Json2} = ?JSON_DECODE(Body2),
        Features2 = couch_util:get_value(<<"features">>, Json2, undefined),
        ?assert(is_list(Features2)),
        ?assertNot(lists:member(<<"snek">>, Features2))
    end).