summaryrefslogtreecommitdiff
path: root/test/javascript/tests/users_db.js
blob: e59b39856f25ad5cdc86e06839ee1e78875ad612 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// 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.

couchTests.users_db = function(debug) {

  // This tests the users db, especially validations
  // this should also test that you can log into the couch
  
  var users_db_name = get_random_db_name();
  var usersDb = new CouchDB(users_db_name, {"X-Couch-Full-Commit":"false"});
  usersDb.createDb();
  var usersDbAlt = new CouchDB(users_db_name + "_alt", {"X-Couch-Full-Commit":"false"});
  usersDbAlt.createDb();

  // test that you can treat "_user" as a db-name
  // this can complicate people who try to secure the users db with 
  // an http proxy and fail to get both the actual db and the _user path
  // maybe it's not the right approach...
  // hard to know what else to do, as we don't let non-admins inspect the config
  // to determine the actual users db name.

  function testFun() {
    // test that the validation function is installed
    var ddoc = usersDb.open("_design/_auth");
// TODO: validation ddoc does not get installed on cluster
//  T(ddoc.validate_doc_update);
    
    // test that you can login as a user using basic auth
    var jchrisUserDoc = CouchDB.prepareUserDoc({
      name: "jchris@apache.org"
    }, "funnybone");
    T(usersDb.save(jchrisUserDoc).ok);
    
    T(CouchDB.session().userCtx.name == null);

    // test that you can use basic auth aginst the users db
    var s = CouchDB.session({
      headers : {
        //                 base64_encode("jchris@apache.org:funnybone")
        "Authorization" : "Basic amNocmlzQGFwYWNoZS5vcmc6ZnVubnlib25l"
      }
    });
    T(s.userCtx.name == "jchris@apache.org");
    T(s.info.authenticated == "default");
    T(s.info.authentication_db == "" + users_db_name + "");
    T(s.info.authentication_handlers.indexOf("cookie")>=0);
    T(s.info.authentication_handlers.indexOf("default")>=0);
    var s = CouchDB.session({
      headers : {
        "Authorization" : "Basic Xzpf" // name and pass of _:_
      }
    });
    T(s.name == null);
    T(s.info.authenticated == "default");

    // make sure we'll not hit the cache with this
    var kchrisUserDoc = CouchDB.prepareUserDoc({
      name: "kchris@apache.org"
    }, "sadbone");
    T(usersDb.save(kchrisUserDoc).ok);
    
    // save with new_edits=false to force conflict save does no more work => actually replicate and change simultanously
    CouchDB.replicate(usersDb.name, usersDbAlt.name);
    // ok, now create a conflicting edit on the kchris doc, and make sure there's no login.
    var kchrisUser2 = JSON.parse(JSON.stringify(kchrisUserDoc));
    kchrisUser2.foo = "bar";
    T(usersDb.save(kchrisUser2).ok);
    // now the other
    var kchrisUser3 = JSON.parse(JSON.stringify(kchrisUserDoc));
    kchrisUser3.foo = "barrrr";
    T(usersDbAlt.save(kchrisUser3).ok);
    // and replicate back
    CouchDB.replicate(usersDbAlt.name, usersDb.name);

    var kchrisWithConflict = usersDb.open(kchrisUserDoc._id, {conflicts : true, revs_info: true});
    T(kchrisWithConflict._conflicts.length == 1);
    
    // no login with conflicted user doc
    CouchDB.logout();
    var s = null;
    try {
      s = CouchDB.session({
        headers : {
          "Authorization" : "Basic a2NocmlzQGFwYWNoZS5vcmc6ZnVubnlib25l"
        }
      });
    }catch(e){
      // old test had name==null, now we might have an error. Anyway: test below
    }
    T(s == null || s.userCtx.name == null);

    // you can delete a user doc
    s = CouchDB.session().userCtx;
    T(s.name == null);
    T(s.roles.indexOf("_admin") !== -1);
    T(usersDb.deleteDoc(kchrisWithConflict).ok);

    // you can't change doc from type "user"
// TODO: needs design doc (see above)
/*
    jchrisUserDoc = usersDb.open(jchrisUserDoc._id);
    jchrisUserDoc.type = "not user";
    try {
      usersDb.save(jchrisUserDoc);
      T(false && "should only allow us to save doc when type == 'user'");
    } catch(e) {
      T(e.reason == "doc.type must be user");
    }
    jchrisUserDoc.type = "user";

    // "roles" must be an array
    jchrisUserDoc.roles = "not an array";
    try {
      usersDb.save(jchrisUserDoc);
      T(false && "should only allow us to save doc when roles is an array");
    } catch(e) {
      T(e.reason == "doc.roles must be an array");
    }
    jchrisUserDoc.roles = [];

    // "roles" must be an array of strings
    jchrisUserDoc.roles = [12];
    try {
      usersDb.save(jchrisUserDoc);
      T(false && "should only allow us to save doc when roles is an array of strings");
    } catch(e) {
      TEquals(e.reason, "doc.roles can only contain strings");
    }
    jchrisUserDoc.roles = [];

    // "roles" must exist
    delete jchrisUserDoc.roles;
    try {
      usersDb.save(jchrisUserDoc);
      T(false && "should only allow us to save doc when roles exists");
    } catch(e) {
      T(e.reason == "doc.roles must exist");
    }
    jchrisUserDoc.roles = [];

    // character : is not allowed in usernames
    var joeUserDoc = CouchDB.prepareUserDoc({
      name: "joe:erlang"
    }, "qwerty");
    try {
      usersDb.save(joeUserDoc);
      T(false, "shouldn't allow : in usernames");
    } catch(e) {
      TEquals("Character `:` is not allowed in usernames.", e.reason);
    }
*/
    // test that you can login as a user with a password starting with :
    var doc = CouchDB.prepareUserDoc({
      name: "foo@example.org"
    }, ":bar");
    T(usersDb.save(doc).ok);

    T(CouchDB.session().userCtx.name == null);

    // test that you can use basic auth aginst the users db
    var s = CouchDB.session({
      headers : {
        //                 base64_encode("foo@example.org::bar")
        "Authorization" : "Basic Zm9vQGV4YW1wbGUub3JnOjpiYXI="
      }
    });
    T(s.userCtx.name == "foo@example.org");

  };

  run_on_modified_server(
    [{section: "couch_httpd_auth",
      key: "authentication_db", value: usersDb.name},
     {section: "chttpd_auth",
      key: "authentication_db", value: usersDb.name}],
    testFun
  );

  usersDb.deleteDb(); // cleanup
  usersDbAlt.deleteDb(); // cleanup
  
}