Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added user management and converted unit tests to use authentication.

The NuMongoDB unit tests now test (and require) the use of authentication
in MongoDB. See the notes in test/test_access.nu. This also requires that
MongoDB be run with the --auth option.
  • Loading branch information...
commit 5509a713b8288389fc210e7c46729fd89015a2da 1 parent ac21990
@timburks authored
Showing with 66 additions and 19 deletions.
  1. +2 −0  objc/NuMongoDB.h
  2. +27 −19 objc/NuMongoDB.m
  3. +37 −0 test/test_access.nu
View
2  objc/NuMongoDB.h
@@ -61,6 +61,8 @@ limitations under the License.
/*! Connect to a MongoDB database. Options can include @"host" and @"port". */
- (int) connectWithOptions:(NSDictionary *) options;
+/*! Add user to named database. */
+- (void) addUser:(NSString *) user withPassword:(NSString *) password forDatabase:(NSString *) database;
/*! Authenticate to named database. */
- (BOOL) authenticateUser:(NSString *) user withPassword:(NSString *) password forDatabase:(NSString *) database;
/*! Convenience method for database searches. Query should be a dictionary. */
View
46 objc/NuMongoDB.m
@@ -91,6 +91,13 @@ - (int) connectWithOptions:(NSDictionary *) options
return mongo_connect(conn, &opts);
}
+- (void) addUser:(NSString *) user withPassword:(NSString *) password forDatabase:(NSString *) database
+{
+ mongo_cmd_add_user(conn, [database cStringUsingEncoding:NSUTF8StringEncoding],
+ [user cStringUsingEncoding:NSUTF8StringEncoding],
+ [password cStringUsingEncoding:NSUTF8StringEncoding]);
+}
+
- (BOOL) authenticateUser:(NSString *) user withPassword:(NSString *) password forDatabase:(NSString *) database
{
return mongo_cmd_authenticate(conn, [database cStringUsingEncoding:NSUTF8StringEncoding],
@@ -180,8 +187,9 @@ - (id) runCommand:(id) command inDatabase:(NSString *) database
return result ? [[[[NuBSON alloc] initWithBSON:bsonResult] autorelease] dictionaryValue] : nil;
}
-- (BOOL) dropDatabase:(NSString *) database {
- return mongo_cmd_drop_db(conn, [database cStringUsingEncoding:NSUTF8StringEncoding]);
+- (BOOL) dropDatabase:(NSString *) database
+{
+ return mongo_cmd_drop_db(conn, [database cStringUsingEncoding:NSUTF8StringEncoding]);
}
- (BOOL) dropCollection:(NSString *) collection inDatabase:(NSString *) database
@@ -194,23 +202,23 @@ - (BOOL) dropCollection:(NSString *) collection inDatabase:(NSString *) database
- (id) getCollectionNamesInDatabase:(NSString *) database
{
- NSArray *names = [self findArray:nil inCollection:[database stringByAppendingString:@".system.namespaces"]];
- NSMutableArray *result = [NSMutableArray array];
- for (int i = 0; i < [names count]; i++) {
- id name = [[[names objectAtIndex:i] objectForKey:@"name"]
- stringByReplacingOccurrencesOfString:[database stringByAppendingString:@"."]
- withString:@""];
- NSRange match = [name rangeOfString:@".$_id_"];
- if (match.location != NSNotFound) {
- continue;
- }
- match = [name rangeOfString:@"system.indexes"];
- if (match.location != NSNotFound) {
- continue;
- }
- [result addObject:name];
- }
- return result;
+ NSArray *names = [self findArray:nil inCollection:[database stringByAppendingString:@".system.namespaces"]];
+ NSMutableArray *result = [NSMutableArray array];
+ for (int i = 0; i < [names count]; i++) {
+ id name = [[[names objectAtIndex:i] objectForKey:@"name"]
+ stringByReplacingOccurrencesOfString:[database stringByAppendingString:@"."]
+ withString:@""];
+ NSRange match = [name rangeOfString:@".$_id_"];
+ if (match.location != NSNotFound) {
+ continue;
+ }
+ match = [name rangeOfString:@"system.indexes"];
+ if (match.location != NSNotFound) {
+ continue;
+ }
+ [result addObject:name];
+ }
+ return result;
}
- (BOOL) ensureCollection:(NSString *) collection hasIndex:(NSObject *) key withOptions:(int) options
View
37 test/test_access.nu
@@ -7,6 +7,18 @@
(set connection nil)
+;; the NuMongoDB unit tests now use (and test) authentication
+;; test username and password
+(set username "numongodb")
+(set password "numongodb")
+;;
+;; add these to the admin database with
+;; > use admin
+;; > db.addUser("numongodb", "numongodb")
+;; verify with
+;; > db.system.users.find()
+;;
+
(class TestAccess is NuTestCase
(- testSession is
@@ -21,6 +33,8 @@
(unless (eq connected 0)
(puts "could not connect to database. Is mongod running?")
(return))
+ (mongo authenticateUser:username withPassword:password forDatabase:"admin")
+
;; start clean
(mongo dropCollection:collection inDatabase:database)
@@ -133,6 +147,7 @@
(unless (eq connected 0)
(puts "could not connect to database. Is mongod running?")
(return))
+ (mongo authenticateUser:username withPassword:password forDatabase:"admin")
(mongo dropCollection:collection inDatabase:database)
@@ -155,6 +170,8 @@
(unless (eq connected 0)
(puts "could not connect to database. Is mongod running?")
(return))
+ (mongo authenticateUser:username withPassword:password forDatabase:"admin")
+
(mongo dropDatabase:database)
(set collectionNames (array "a" "b" "c" "d"))
(collectionNames each:
@@ -181,6 +198,8 @@
(unless (eq connected 0)
(puts "could not connect to database. Is mongod running?")
(return))
+ (mongo authenticateUser:username withPassword:password forDatabase:"admin")
+
(mongo dropCollection:collection inDatabase:database)
;; insert an object, make sure that it's in the collection
@@ -204,6 +223,22 @@
;; clean up
(mongo dropCollection:collection inDatabase:database))
+ (- testAuthentication is
+ (set database "protected")
+ (set mongo (NuMongoDB new))
+ (set connected (mongo connectWithOptions:connection))
+ (assert_equal 0 connected)
+ (unless (eq connected 0)
+ (puts "could not connect to database. Is mongod running?")
+ (return))
+ (mongo authenticateUser:username withPassword:password forDatabase:"admin")
+ (mongo dropDatabase:database)
+ (mongo insertObject:(dict foo:1) intoCollection:"protected.data")
+ (mongo addUser:"user" withPassword:"password" forDatabase:database)
+ (set result (mongo authenticateUser:"user" withPassword:"password" forDatabase:database))
+ (assert_equal 1 result)
+ (mongo dropDatabase:database))
+
(- testTypes is
(set database "test")
(set collection "types")
@@ -215,6 +250,8 @@
(unless (eq connected 0)
(puts "could not connect to database. Is mongod running?")
(return))
+ (mongo authenticateUser:username withPassword:password forDatabase:"admin")
+
(mongo dropCollection:collection inDatabase:database)
(set object (dict int:123
Please sign in to comment.
Something went wrong with that request. Please try again.