Skip to content

Commit 2aeecdd

Browse files
committed
add admin user in test_mongoc_client_authenticate
Required for MongoDB >= 2.7.1, see SERVER 12621. Won't work with MongoDB 3.0.2+ because removing the "admin" user will no longer restore the localhost exception, see SERVER 18415
1 parent d298a81 commit 2aeecdd

File tree

1 file changed

+63
-19
lines changed

1 file changed

+63
-19
lines changed

tests/test-mongoc-client.c

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ gen_test_user (void)
4040

4141

4242
static char *
43-
gen_good_uri (const char *username)
43+
gen_good_uri (const char *username,
44+
const char *dbname)
4445
{
4546
char *host = test_framework_get_host ();
46-
char *uri = bson_strdup_printf ("mongodb://%s:testpass@%s/test",
47+
char *uri = bson_strdup_printf ("mongodb://%s:testpass@%s/%s",
4748
username,
48-
host);
49+
host,
50+
dbname);
4951

5052
bson_free (host);
5153
return uri;
@@ -55,28 +57,58 @@ gen_good_uri (const char *username)
5557
static void
5658
test_mongoc_client_authenticate (void)
5759
{
58-
mongoc_collection_t *collection;
60+
char *admin_username;
61+
mongoc_client_t *admin_client;
62+
mongoc_database_t *admin_database;
63+
bson_t admin_roles;
64+
char *admin_uri;
65+
char *username;
66+
char *uri;
67+
bson_t roles;
5968
mongoc_database_t *database;
60-
mongoc_client_t *client;
69+
mongoc_collection_t *collection;
6170
mongoc_client_t *auth_client;
6271
mongoc_cursor_t *cursor;
6372
const bson_t *doc;
6473
bson_error_t error;
65-
char *username;
66-
char *uri;
6774
bool r;
6875
bson_t q;
6976

70-
username = gen_test_user ();
71-
uri = gen_good_uri (username);
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+
92+
/*
93+
* Log in as admin.
94+
*/
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);
7299

73100
/*
74101
* Add a user to the test database.
75102
*/
76-
client = test_framework_client_new (NULL);
77-
database = mongoc_client_get_database(client, "test");
103+
username = gen_test_user ();
104+
uri = gen_good_uri (username, "test");
105+
106+
database = mongoc_client_get_database (admin_client, "test");
78107
mongoc_database_remove_user (database, username, &error);
79-
r = mongoc_database_add_user(database, username, "testpass", NULL, NULL, &error);
108+
bson_init (&roles);
109+
BCON_APPEND (&roles,
110+
"0", "{", "role", "read", "db", "test", "}");
111+
r = mongoc_database_add_user(database, username, "testpass", &roles, NULL, &error);
80112
ASSERT_CMPINT(r, ==, 1);
81113
mongoc_database_destroy(database);
82114

@@ -96,21 +128,33 @@ test_mongoc_client_authenticate (void)
96128
}
97129
assert(!r);
98130
}
99-
mongoc_cursor_destroy(cursor);
100-
mongoc_collection_destroy(collection);
101131

102132
/*
103133
* Remove all test users.
104134
*/
105-
database = mongoc_client_get_database (auth_client, "test");
135+
database = mongoc_client_get_database (admin_client, "test");
106136
r = mongoc_database_remove_all_users (database, &error);
107137
assert (r);
108-
mongoc_database_destroy (database);
109-
mongoc_client_destroy (auth_client);
110-
mongoc_client_destroy (client);
111138

112-
bson_free (username);
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+
146+
mongoc_cursor_destroy (cursor);
147+
mongoc_collection_destroy (collection);
148+
mongoc_client_destroy (auth_client);
149+
bson_destroy (&roles);
113150
bson_free (uri);
151+
bson_free (username);
152+
bson_free (admin_uri);
153+
mongoc_database_destroy (database);
154+
bson_destroy (&admin_roles);
155+
mongoc_database_destroy (admin_database);
156+
mongoc_client_destroy (admin_client);
157+
bson_free (admin_username);
114158
}
115159

116160

0 commit comments

Comments
 (0)