@@ -3,11 +3,24 @@ use mongodb::{CommandType, Client, ThreadedClient};
3
3
use mongodb:: db:: ThreadedDatabase ;
4
4
use mongodb:: error:: Error :: OperationError ;
5
5
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
+
6
19
#[ test]
7
20
fn invalid_user ( ) {
8
21
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 ) ;
11
24
let doc = doc ! { "connectionStatus" => 1 } ;
12
25
let before = db. command ( doc. clone ( ) , CommandType :: Suppressed , None ) . unwrap ( ) ;
13
26
@@ -17,11 +30,11 @@ fn invalid_user() {
17
30
} ;
18
31
19
32
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 ( ) ) ,
21
34
_ => panic ! ( "Invalid array of authenticatedUsers for initial connectionStatus command" ) ,
22
35
} ;
23
36
24
- match db. auth ( "invalid_user" , "some_password" ) {
37
+ match db. auth ( "test-auth-mod- invalid_user-saghm " , "some_password" ) {
25
38
Err ( OperationError ( _) ) => ( ) ,
26
39
Err ( _) => {
27
40
panic ! ( "Expected OperationError for invalid authentication, but got some other error \
@@ -36,19 +49,18 @@ fn invalid_user() {
36
49
Some ( & Bson :: Document ( ref doc) ) => doc. clone ( ) ,
37
50
_ => panic ! ( "Invalid response for subsequent connectionStatus command" ) ,
38
51
} ;
39
-
52
+
40
53
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" ) ,
43
56
} ;
44
57
}
45
58
46
-
47
59
#[ test]
48
60
fn invalid_password ( ) {
49
61
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 ) ;
52
64
let doc = doc ! { "connectionStatus" => 1 } ;
53
65
let before = db. command ( doc. clone ( ) , CommandType :: Suppressed , None ) . unwrap ( ) ;
54
66
@@ -58,13 +70,13 @@ fn invalid_password() {
58
70
} ;
59
71
60
72
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 ( ) ) ,
62
74
_ => panic ! ( "Invalid array of authenticatedUsers for initial connectionStatus command" ) ,
63
75
} ;
64
76
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 ( ) ;
66
78
67
- match db. auth ( "saghm" , "wrong_password" ) {
79
+ match db. auth ( "test-auth-mod-invalid_password- saghm" , "wrong_password" ) {
68
80
Err ( OperationError ( _) ) => ( ) ,
69
81
Err ( _) => {
70
82
panic ! ( "Expected OperationError for invalid authentication, but got some other error \
@@ -81,16 +93,16 @@ fn invalid_password() {
81
93
} ;
82
94
83
95
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 ( ) ) ,
85
97
_ => panic ! ( "Invalid array of authenticatedUsers for subsequent connectionStatus command" ) ,
86
98
} ;
87
99
}
88
100
89
101
#[ test]
90
102
fn successful_login ( ) {
91
103
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 ) ;
94
106
let doc = doc ! { "connectionStatus" => 1 } ;
95
107
let before = db. command ( doc. clone ( ) , CommandType :: Suppressed , None ) . unwrap ( ) ;
96
108
@@ -100,12 +112,12 @@ fn successful_login() {
100
112
} ;
101
113
102
114
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 ( ) ) ,
104
116
_ => panic ! ( "Invalid array of authenticatedUsers for initial connectionStatus command" ) ,
105
117
} ;
106
118
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 ( ) ;
109
121
110
122
let after = db. command ( doc, CommandType :: Suppressed , None ) . unwrap ( ) ;
111
123
@@ -119,20 +131,20 @@ fn successful_login() {
119
131
_ => panic ! ( "Invalid array of authenticatedUsers for subsequent connectionStatus command" ) ,
120
132
} ;
121
133
122
- assert_eq ! ( authed_users. len ( ) , 1 ) ;
134
+ let bson_user = doc_vec_find ( & authed_users, "user" , "test-auth-mod-successful_login-saghm" ) . unwrap ( ) ;
123
135
124
- let user = match authed_users [ 0 ] {
136
+ let user = match bson_user {
125
137
Bson :: Document ( ref doc) => doc. clone ( ) ,
126
138
_ => panic ! ( "Invalid auth'd user in subsequent connectionStatus response" ) ,
127
139
} ;
128
140
129
141
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" ) ,
131
143
_ => panic ! ( "Invalid `user` field of auth'd user" ) ,
132
144
} ;
133
145
134
146
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 " ) ,
136
148
_ => panic ! ( "Invalid `db` field of auth'd user" ) ,
137
149
} ;
138
150
}
0 commit comments