1
- /// This module provides utility types and traits for managing a test session
2
- ///
3
- /// Tests start by using one of the `TestApp` constructors. All constrcutors return at least a
4
- /// `TestApp` and `MockAnonymousUser`. The `MockAnonymousUser` can be used to issue requests
5
- /// in an unauthenticated session.
6
- ///
7
- /// A `TestApp` value provides raw access to the database through the `db` function and can
8
- /// construct new users via the `db_new_user` function. This function returns a
9
- /// `MockCookieUser`, which can be used to generate one or more tokens via its `db_new_token`
10
- /// function, which in turn returns a `MockTokenUser`.
11
- ///
12
- /// All three user types implement the `RequestHelper` trait which provides convenience methods for
13
- /// constructing requests. Some of these methods, such as `publish` are expected to fail for an
14
- /// unauthenticated user (or for other reasons) and return a `Response<T>`. The `Response<T>`
15
- /// provides several functions to check the response status and deserialize the JSON response.
16
- ///
17
- /// `MockCookieUser` and `MockTokenUser` provide an `as_model` function which returns a reference
18
- /// to the underlying database model value (`User` and `ApiToken` respectively).
1
+ //! This module provides utility types and traits for managing a test session
2
+ //!
3
+ //! Tests start by using one of the `TestApp` constructors. All constructors return at least a
4
+ //! `TestApp` and `MockAnonymousUser`. The `MockAnonymousUser` can be used to issue requests
5
+ //! in an unauthenticated session.
6
+ //!
7
+ //! A `TestApp` value provides raw access to the database through the `db` function and can
8
+ //! construct new users via the `db_new_user` function. This function returns a
9
+ //! `MockCookieUser`, which can be used to generate one or more tokens via its `db_new_token`
10
+ //! function, which in turn returns a `MockTokenUser`.
11
+ //!
12
+ //! All three user types implement the `RequestHelper` trait which provides convenience methods for
13
+ //! constructing requests. Some of these methods, such as `publish` are expected to fail for an
14
+ //! unauthenticated user (or for other reasons) and return a `Response<T>`. The `Response<T>`
15
+ //! provides several functions to check the response status and deserialize the JSON response.
16
+ //!
17
+ //! `MockCookieUser` and `MockTokenUser` provide an `as_model` function which returns a reference
18
+ //! to the underlying database model value (`User` and `ApiToken` respectively).
19
+
19
20
use std:: { self , rc:: Rc , sync:: Arc } ;
20
21
21
22
use { conduit, conduit_middleware, diesel, serde} ;
@@ -45,7 +46,7 @@ impl TestApp {
45
46
let ( _bomb, app, middle) = app ( ) ;
46
47
let inner = Rc :: new ( TestAppInner { app, _bomb, middle } ) ;
47
48
let anon = MockAnonymousUser {
48
- inner : TestApp ( Rc :: clone ( & inner) ) ,
49
+ app : TestApp ( Rc :: clone ( & inner) ) ,
49
50
} ;
50
51
( TestApp ( inner) , anon)
51
52
}
@@ -76,10 +77,12 @@ impl TestApp {
76
77
}
77
78
78
79
/// Create a new user in the database and return a mock user session
80
+ ///
81
+ /// This method updates the database directly
79
82
pub fn db_new_user ( & self , user : & str ) -> MockCookieUser {
80
83
let user = self . db ( |conn| :: new_user ( user) . create_or_update ( conn) . unwrap ( ) ) ;
81
84
MockCookieUser {
82
- inner : TestApp ( Rc :: clone ( & self . 0 ) ) ,
85
+ app : TestApp ( Rc :: clone ( & self . 0 ) ) ,
83
86
user,
84
87
}
85
88
}
@@ -140,7 +143,7 @@ pub trait RequestHelper {
140
143
self . get_with_query ( "/api/v1/crates" , query) . good ( )
141
144
}
142
145
143
- /// Get the crates owned by the specified user.
146
+ /// Search for crates owned by the specified user.
144
147
fn search_by_user_id ( & self , id : i32 ) -> CrateList {
145
148
self . search ( & format ! ( "user_id={}" , id) )
146
149
}
@@ -162,7 +165,7 @@ pub trait RequestHelper {
162
165
163
166
/// A type that can generate unauthenticated requests
164
167
pub struct MockAnonymousUser {
165
- inner : TestApp ,
168
+ app : TestApp ,
166
169
}
167
170
168
171
impl RequestHelper for MockAnonymousUser {
@@ -171,7 +174,7 @@ impl RequestHelper for MockAnonymousUser {
171
174
}
172
175
173
176
fn app ( & self ) -> & TestApp {
174
- & self . inner
177
+ & self . app
175
178
}
176
179
}
177
180
@@ -180,7 +183,7 @@ impl RequestHelper for MockAnonymousUser {
180
183
/// The `User` is directly injected into middleware extensions and thus the cookie logic is not
181
184
/// exercised.
182
185
pub struct MockCookieUser {
183
- inner : TestApp ,
186
+ app : TestApp ,
184
187
user : User ,
185
188
}
186
189
@@ -195,7 +198,7 @@ impl RequestHelper for MockCookieUser {
195
198
}
196
199
197
200
fn app ( & self ) -> & TestApp {
198
- & self . inner
201
+ & self . app
199
202
}
200
203
}
201
204
@@ -210,18 +213,18 @@ impl MockCookieUser {
210
213
/// This method updates the database directly
211
214
pub fn db_new_token ( & self , name : & str ) -> MockTokenUser {
212
215
let token = self
213
- . inner
216
+ . app
214
217
. db ( |conn| ApiToken :: insert ( conn, self . user . id , name) . unwrap ( ) ) ;
215
218
MockTokenUser {
216
- inner : TestApp ( Rc :: clone ( & self . inner . 0 ) ) ,
219
+ app : TestApp ( Rc :: clone ( & self . app . 0 ) ) ,
217
220
token,
218
221
}
219
222
}
220
223
}
221
224
222
225
/// A type that can generate token authenticated requests
223
226
pub struct MockTokenUser {
224
- inner : TestApp ,
227
+ app : TestApp ,
225
228
token : ApiToken ,
226
229
}
227
230
@@ -233,7 +236,7 @@ impl RequestHelper for MockTokenUser {
233
236
}
234
237
235
238
fn app ( & self ) -> & TestApp {
236
- & self . inner
239
+ & self . app
237
240
}
238
241
}
239
242
0 commit comments