summaryrefslogtreecommitdiff
path: root/src/chttpd/test/eunit/chttpd_auth_tests.erl
blob: 7beda9bc7b07fa805823e4577b431374888ad336 (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
% 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_auth_tests).

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

setup() ->
    Addr = config:get("chttpd", "bind_address", "127.0.0.1"),
    Port = mochiweb_socket_server:get(chttpd, port),
    BaseUrl = lists:concat(["http://", Addr, ":", Port]),
    BaseUrl.

teardown(_Url) ->
    ok.

require_valid_user_exception_test_() ->
    {
        "_up",
        {
            setup,
            fun chttpd_test_util:start_couch/0,
            fun chttpd_test_util:stop_couch/1,
            {
                foreach,
                fun setup/0,
                fun teardown/1,
                [
                    fun should_handle_require_valid_user_except_up_on_up_route/1,
                    fun should_handle_require_valid_user_except_up_on_non_up_routes/1
                ]
            }
        }
    }.

set_require_user_false() ->
    ok = config:set("chttpd", "require_valid_user", "false", _Persist = false).

set_require_user_true() ->
    ok = config:set("chttpd", "require_valid_user", "true", _Persist = false).

set_require_user_except_for_up_false() ->
    ok = config:set("chttpd", "require_valid_user_except_for_up", "false", _Persist = false).

set_require_user_except_for_up_true() ->
    ok = config:set("chttpd", "require_valid_user_except_for_up", "true", _Persist = false).

should_handle_require_valid_user_except_up_on_up_route(_Url) ->
    ?_test(begin
        %    require_valid_user |   require_valid_user_except_up | up needs auth
        % 1  F                  |   F                            | F
        % 2  F                  |   T                            | F
        % 3  T                  |   F                            | T
        % 4  T                  |   T                            | F

        UpRequest = #httpd{path_parts = [<<"_up">>]},
        % we use ?ADMIN_USER here because these tests run under admin party
        % so this is equivalent to an unauthenticated request
        ExpectAuth = {unauthorized, <<"Authentication required.">>},
        ExpectNoAuth = #httpd{user_ctx = ?ADMIN_USER, path_parts = [<<"_up">>]},

        % 1
        set_require_user_false(),
        set_require_user_except_for_up_false(),
        Result1 = chttpd_auth:party_mode_handler(UpRequest),
        ?assertEqual(ExpectNoAuth, Result1),

        % 2
        set_require_user_false(),
        set_require_user_except_for_up_true(),
        Result2 = chttpd_auth:party_mode_handler(UpRequest),
        ?assertEqual(ExpectNoAuth, Result2),

        % 3
        set_require_user_true(),
        set_require_user_except_for_up_false(),
        ?assertThrow(ExpectAuth, chttpd_auth:party_mode_handler(UpRequest)),

        % 4
        set_require_user_true(),
        set_require_user_except_for_up_true(),
        Result4 = chttpd_auth:party_mode_handler(UpRequest),
        ?assertEqual(ExpectNoAuth, Result4)
    end).

should_handle_require_valid_user_except_up_on_non_up_routes(_Url) ->
    ?_test(begin
        %    require_valid_user |  require_valid_user_except_up | everything not _up requires auth
        % 5  F                  |  F                            | F
        % 6  F                  |  T                            | T
        % 7  T                  |  F                            | T
        % 8  T                  |  T                            | T

        NonUpRequest = #httpd{path_parts = [<<"/">>]},
        ExpectAuth = {unauthorized, <<"Authentication required.">>},
        ExpectNoAuth = #httpd{user_ctx = ?ADMIN_USER, path_parts = [<<"/">>]},
        % 5
        set_require_user_false(),
        set_require_user_except_for_up_false(),
        Result5 = chttpd_auth:party_mode_handler(NonUpRequest),
        ?assertEqual(ExpectNoAuth, Result5),

        % 6
        set_require_user_false(),
        set_require_user_except_for_up_true(),
        ?assertThrow(ExpectAuth, chttpd_auth:party_mode_handler(NonUpRequest)),

        % 7
        set_require_user_true(),
        set_require_user_except_for_up_false(),
        ?assertThrow(ExpectAuth, chttpd_auth:party_mode_handler(NonUpRequest)),

        % 8
        set_require_user_true(),
        set_require_user_except_for_up_true(),
        ?assertThrow(ExpectAuth, chttpd_auth:party_mode_handler(NonUpRequest))
    end).