Skip to content

Commit c5294e8

Browse files
committed
configurable username and password for tests
Auth tests now work despite SERVER 18415.
1 parent bd80bc8 commit c5294e8

File tree

3 files changed

+75
-36
lines changed

3 files changed

+75
-36
lines changed

CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ have a locally running `mongod` instance available on `127.0.0.1:27017`. All
120120
tests should pass. Alternatively, you can specify `MONGOC_TEST_HOST`
121121
environment variable to specify a non-localhost hostname or ip address.
122122

123+
To test with auth, create a "root" user on the "admin" database and set the
124+
`MONGOC_TEST_USER` and `MONGOC_TEST_PASSWORD` environment variables to its
125+
username and password.
126+
123127
Set the `MONGOC_TEST_SSL` environment variable `on` to connect to the server via
124128
SSL with default options. Configure SSL options with paths
125129
`MONGOC_TEST_SSL_PEM_FILE`, `MONGOC_TEST_SSL_PEM_PWD`,

tests/test-libmongoc.c

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,48 @@ test_framework_get_host (void)
212212
return host ? host : bson_strdup ("localhost");
213213
}
214214

215+
/*
216+
*--------------------------------------------------------------------------
217+
*
218+
* test_framework_get_admin_user --
219+
*
220+
* Get the username of an admin user on the test MongoDB server.
221+
*
222+
* Returns:
223+
* A string you must bson_free, or NULL.
224+
*
225+
* Side effects:
226+
* None.
227+
*
228+
*--------------------------------------------------------------------------
229+
*/
230+
char *
231+
test_framework_get_admin_user (void)
232+
{
233+
return test_framework_getenv ("MONGOC_TEST_USER");
234+
}
235+
236+
/*
237+
*--------------------------------------------------------------------------
238+
*
239+
* test_framework_get_admin_password --
240+
*
241+
* Get the password of an admin user on the test MongoDB server.
242+
*
243+
* Returns:
244+
* A string you must bson_free, or NULL.
245+
*
246+
* Side effects:
247+
* None.
248+
*
249+
*--------------------------------------------------------------------------
250+
*/
251+
char *
252+
test_framework_get_admin_password (void)
253+
{
254+
return test_framework_getenv ("MONGOC_TEST_PASSWORD");
255+
}
256+
215257
/*
216258
*--------------------------------------------------------------------------
217259
*
@@ -271,7 +313,7 @@ uri_has_options (const mongoc_uri_t *uri)
271313
*
272314
* Get the connection string of the test MongoDB server. Pass NULL
273315
* to get the default connection string, or pass a string in to have
274-
* "ssl=true" added if appropriate.
316+
* username, password, and "ssl=true" added if appropriate.
275317
*
276318
* Returns:
277319
* A string you must bson_free.
@@ -285,15 +327,24 @@ char *
285327
test_framework_get_uri_str (const char *uri_str)
286328
{
287329
char *host = test_framework_get_host ();
330+
char *user = test_framework_get_admin_user ();
331+
char *password = test_framework_get_admin_password ();
288332
char *test_uri_str_base = uri_str ?
289333
bson_strdup (uri_str) :
290334
bson_strdup_printf ("mongodb://%s/", host);
291335

292336
mongoc_uri_t *uri_parsed = mongoc_uri_new (test_uri_str_base);
293337
char *test_uri_str;
338+
char *test_uri_str_auth;
294339

295340
assert (uri_parsed);
296341

342+
if ((user && !password) || (!user && password)) {
343+
fprintf (stderr, "Specify neither MONGOC_TEST_USER nor"
344+
" MONGOC_TEST_PASSWORD, or both\n");
345+
abort ();
346+
}
347+
297348
/* add "ssl=true" if needed */
298349
if (test_framework_get_ssl () && !mongoc_uri_get_ssl (uri_parsed)) {
299350
test_uri_str = bson_strdup_printf (
@@ -304,10 +355,27 @@ test_framework_get_uri_str (const char *uri_str)
304355
test_uri_str = bson_strdup (test_uri_str_base);
305356
}
306357

358+
/* must we add "user:password"? */
359+
mongoc_uri_destroy (uri_parsed);
360+
uri_parsed = mongoc_uri_new (test_uri_str);
361+
assert (uri_parsed);
362+
363+
if (user && password && !mongoc_uri_get_username (uri_parsed)) {
364+
/* TODO: uri-escape username and password */
365+
test_uri_str_auth = bson_strdup_printf (
366+
"mongodb://%s:%s@%s",
367+
user, password, test_uri_str + strlen ("mongodb://"));
368+
} else {
369+
test_uri_str_auth = bson_strdup (test_uri_str);
370+
}
371+
307372
mongoc_uri_destroy (uri_parsed);
308373
bson_free (host);
309374
bson_free (test_uri_str_base);
310-
return test_uri_str;
375+
bson_free (test_uri_str);
376+
bson_free (user);
377+
bson_free (password);
378+
return test_uri_str_auth;
311379
}
312380

313381
/*

tests/test-mongoc-client.c

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ gen_good_uri (const char *username,
5757
static void
5858
test_mongoc_client_authenticate (void)
5959
{
60-
char *admin_username;
6160
mongoc_client_t *admin_client;
62-
mongoc_database_t *admin_database;
63-
bson_t admin_roles;
64-
char *admin_uri;
6561
char *username;
6662
char *uri;
6763
bson_t roles;
@@ -74,28 +70,10 @@ test_mongoc_client_authenticate (void)
7470
bool r;
7571
bson_t q;
7672

77-
/*
78-
* Add an admin user first.
79-
*/
80-
admin_username = gen_test_user ();
81-
admin_client = test_framework_client_new (NULL);
82-
admin_database = mongoc_client_get_database (admin_client, "admin");
83-
mongoc_database_remove_user (admin_database, admin_username, NULL);
84-
bson_init (&admin_roles);
85-
BCON_APPEND (&admin_roles,
86-
"0", "{", "role", "root", "db", "admin", "}");
87-
88-
r = mongoc_database_add_user (admin_database, admin_username, "testpass",
89-
&admin_roles, NULL, NULL);
90-
assert (r);
91-
9273
/*
9374
* Log in as admin.
9475
*/
95-
mongoc_database_destroy (admin_database);
96-
mongoc_client_destroy (admin_client);
97-
admin_uri = gen_good_uri (admin_username, "admin");
98-
admin_client = test_framework_client_new (admin_uri);
76+
admin_client = test_framework_client_new (NULL);
9977

10078
/*
10179
* Add a user to the test database.
@@ -136,25 +114,14 @@ test_mongoc_client_authenticate (void)
136114
r = mongoc_database_remove_all_users (database, &error);
137115
assert (r);
138116

139-
/*
140-
* Remove admin user.
141-
*/
142-
admin_database = mongoc_client_get_database (admin_client, "admin");
143-
r = mongoc_database_remove_user (admin_database, admin_username, NULL);
144-
assert (r);
145-
146117
mongoc_cursor_destroy (cursor);
147118
mongoc_collection_destroy (collection);
148119
mongoc_client_destroy (auth_client);
149120
bson_destroy (&roles);
150121
bson_free (uri);
151122
bson_free (username);
152-
bson_free (admin_uri);
153123
mongoc_database_destroy (database);
154-
bson_destroy (&admin_roles);
155-
mongoc_database_destroy (admin_database);
156124
mongoc_client_destroy (admin_client);
157-
bson_free (admin_username);
158125
}
159126

160127

0 commit comments

Comments
 (0)