@@ -3,11 +3,24 @@ use mongodb::{CommandType, Client, ThreadedClient};
33use mongodb:: db:: ThreadedDatabase ;
44use mongodb:: error:: Error :: OperationError ;
55
6+ fn doc_vec_find ( vec : & Vec < Bson > , key : & str , val : & str ) -> Option < Bson > {
7+ vec. iter ( )
8+ . cloned ( )
9+ . find ( |ref bdoc| match bdoc {
10+ & & Bson :: Document ( ref doc) => match doc. get ( key) {
11+ Some ( & Bson :: String ( ref s) ) => s == val,
12+ _ => false
13+ } ,
14+ _ => false ,
15+ } )
16+ . map ( |ref bson| bson. to_owned ( ) )
17+ }
18+
619#[ test]
720fn invalid_user ( ) {
821 let client = Client :: connect ( "localhost" , 27017 ) . unwrap ( ) ;
9- let db = client. db ( "auth" ) ;
10- let _ = db. drop_all_users ( None ) . unwrap ( ) ;
22+ let db = client. db ( "test- auth-mod-invalid_user " ) ;
23+ let _ = db. drop_user ( "test-auth-mod-invalid_user-saghm" , None ) ;
1124 let doc = doc ! { "connectionStatus" => 1 } ;
1225 let before = db. command ( doc. clone ( ) , CommandType :: Suppressed , None ) . unwrap ( ) ;
1326
@@ -17,11 +30,11 @@ fn invalid_user() {
1730 } ;
1831
1932 match info. get ( "authenticatedUsers" ) {
20- Some ( & Bson :: Array ( ref vec) ) => assert ! ( vec. is_empty ( ) ) ,
33+ Some ( & Bson :: Array ( ref vec) ) => assert ! ( doc_vec_find ( & vec, "user" , "test-auth-mod-invalid_user-saghm" ) . is_none ( ) ) ,
2134 _ => panic ! ( "Invalid array of authenticatedUsers for initial connectionStatus command" ) ,
2235 } ;
2336
24- match db. auth ( "invalid_user" , "some_password" ) {
37+ match db. auth ( "test-auth-mod- invalid_user-saghm " , "some_password" ) {
2538 Err ( OperationError ( _) ) => ( ) ,
2639 Err ( _) => {
2740 panic ! ( "Expected OperationError for invalid authentication, but got some other error \
@@ -36,19 +49,18 @@ fn invalid_user() {
3649 Some ( & Bson :: Document ( ref doc) ) => doc. clone ( ) ,
3750 _ => panic ! ( "Invalid response for subsequent connectionStatus command" ) ,
3851 } ;
39-
52+
4053 match info. get ( "authenticatedUsers" ) {
41- Some ( & Bson :: Array ( ref vec) ) => assert ! ( vec. is_empty ( ) ) ,
42- _ => panic ! ( "Invalid array of authenticatedUsers for subsequent connectionStatus command" ) ,
54+ Some ( & Bson :: Array ( ref vec) ) => assert ! ( doc_vec_find ( & vec, "user" , "test-auth-mod-invalid_user-saghm" ) . is_none ( ) ) ,
55+ _ => panic ! ( "Invalid array of authenticatedUsers for initial connectionStatus command" ) ,
4356 } ;
4457}
4558
46-
4759#[ test]
4860fn invalid_password ( ) {
4961 let client = Client :: connect ( "localhost" , 27017 ) . unwrap ( ) ;
50- let db = client. db ( "auth" ) ;
51- let _ = db. drop_all_users ( None ) . unwrap ( ) ;
62+ let db = client. db ( "test- auth-mod-invalid_password " ) ;
63+ let _ = db. drop_user ( "test-auth-mod-invalid_password-saghm" , None ) ;
5264 let doc = doc ! { "connectionStatus" => 1 } ;
5365 let before = db. command ( doc. clone ( ) , CommandType :: Suppressed , None ) . unwrap ( ) ;
5466
@@ -58,13 +70,13 @@ fn invalid_password() {
5870 } ;
5971
6072 match info. get ( "authenticatedUsers" ) {
61- Some ( & Bson :: Array ( ref vec) ) => assert ! ( vec. is_empty ( ) ) ,
73+ Some ( & Bson :: Array ( ref vec) ) => assert ! ( doc_vec_find ( & vec, "user" , "test-auth-mod-invalid_password-saghm" ) . is_none ( ) ) ,
6274 _ => panic ! ( "Invalid array of authenticatedUsers for initial connectionStatus command" ) ,
6375 } ;
6476
65- db. create_user ( "saghm" , "such_secure_password" , None ) . unwrap ( ) ;
77+ db. create_user ( "test-auth-mod-invalid_password- saghm" , "such_secure_password" , None ) . unwrap ( ) ;
6678
67- match db. auth ( "saghm" , "wrong_password" ) {
79+ match db. auth ( "test-auth-mod-invalid_password- saghm" , "wrong_password" ) {
6880 Err ( OperationError ( _) ) => ( ) ,
6981 Err ( _) => {
7082 panic ! ( "Expected OperationError for invalid authentication, but got some other error \
@@ -81,16 +93,16 @@ fn invalid_password() {
8193 } ;
8294
8395 match info. get ( "authenticatedUsers" ) {
84- Some ( & Bson :: Array ( ref vec) ) => assert ! ( vec. is_empty ( ) ) ,
96+ Some ( & Bson :: Array ( ref vec) ) => assert ! ( doc_vec_find ( & vec, "user" , "test-auth-mod-invalid_password-saghm" ) . is_none ( ) ) ,
8597 _ => panic ! ( "Invalid array of authenticatedUsers for subsequent connectionStatus command" ) ,
8698 } ;
8799}
88100
89101#[ test]
90102fn successful_login ( ) {
91103 let client = Client :: connect ( "localhost" , 27017 ) . unwrap ( ) ;
92- let db = client. db ( "auth" ) ;
93- let _ = db. drop_all_users ( None ) . unwrap ( ) ;
104+ let db = client. db ( "test- auth-mod-successful_login " ) ;
105+ let _ = db. drop_user ( "test-auth-mod-successful_login-saghm" , None ) ;
94106 let doc = doc ! { "connectionStatus" => 1 } ;
95107 let before = db. command ( doc. clone ( ) , CommandType :: Suppressed , None ) . unwrap ( ) ;
96108
@@ -100,12 +112,12 @@ fn successful_login() {
100112 } ;
101113
102114 match info. get ( "authenticatedUsers" ) {
103- Some ( & Bson :: Array ( ref vec) ) => assert ! ( vec. is_empty ( ) ) ,
115+ Some ( & Bson :: Array ( ref vec) ) => assert ! ( doc_vec_find ( & vec, "user" , "test-auth-mod-successful_login-saghm" ) . is_none ( ) ) ,
104116 _ => panic ! ( "Invalid array of authenticatedUsers for initial connectionStatus command" ) ,
105117 } ;
106118
107- db. create_user ( "saghm" , "such_secure_password" , None ) . unwrap ( ) ;
108- db. auth ( "saghm" , "such_secure_password" ) . unwrap ( ) ;
119+ db. create_user ( "test-auth-mod-successful_login- saghm" , "such_secure_password" , None ) . unwrap ( ) ;
120+ db. auth ( "test-auth-mod-successful_login- saghm" , "such_secure_password" ) . unwrap ( ) ;
109121
110122 let after = db. command ( doc, CommandType :: Suppressed , None ) . unwrap ( ) ;
111123
@@ -119,20 +131,20 @@ fn successful_login() {
119131 _ => panic ! ( "Invalid array of authenticatedUsers for subsequent connectionStatus command" ) ,
120132 } ;
121133
122- assert_eq ! ( authed_users. len ( ) , 1 ) ;
134+ let bson_user = doc_vec_find ( & authed_users, "user" , "test-auth-mod-successful_login-saghm" ) . unwrap ( ) ;
123135
124- let user = match authed_users [ 0 ] {
136+ let user = match bson_user {
125137 Bson :: Document ( ref doc) => doc. clone ( ) ,
126138 _ => panic ! ( "Invalid auth'd user in subsequent connectionStatus response" ) ,
127139 } ;
128140
129141 match user. get ( "user" ) {
130- Some ( & Bson :: String ( ref s) ) => assert_eq ! ( s, "saghm" ) ,
142+ Some ( & Bson :: String ( ref s) ) => assert_eq ! ( s, "test-auth-mod-successful_login- saghm" ) ,
131143 _ => panic ! ( "Invalid `user` field of auth'd user" ) ,
132144 } ;
133145
134146 match user. get ( "db" ) {
135- Some ( & Bson :: String ( ref s) ) => assert_eq ! ( s, "auth" ) ,
147+ Some ( & Bson :: String ( ref s) ) => assert_eq ! ( s, "test- auth-mod-successful_login " ) ,
136148 _ => panic ! ( "Invalid `db` field of auth'd user" ) ,
137149 } ;
138150}
0 commit comments