Skip to content

Commit f2f10a5

Browse files
committed
Minor cleanups in tests ::util
1 parent 6ee5de1 commit f2f10a5

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/tests/util.rs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
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+
1920
use std::{self, rc::Rc, sync::Arc};
2021

2122
use {conduit, conduit_middleware, diesel, serde};
@@ -45,7 +46,7 @@ impl TestApp {
4546
let (_bomb, app, middle) = app();
4647
let inner = Rc::new(TestAppInner { app, _bomb, middle });
4748
let anon = MockAnonymousUser {
48-
inner: TestApp(Rc::clone(&inner)),
49+
app: TestApp(Rc::clone(&inner)),
4950
};
5051
(TestApp(inner), anon)
5152
}
@@ -76,10 +77,12 @@ impl TestApp {
7677
}
7778

7879
/// Create a new user in the database and return a mock user session
80+
///
81+
/// This method updates the database directly
7982
pub fn db_new_user(&self, user: &str) -> MockCookieUser {
8083
let user = self.db(|conn| ::new_user(user).create_or_update(conn).unwrap());
8184
MockCookieUser {
82-
inner: TestApp(Rc::clone(&self.0)),
85+
app: TestApp(Rc::clone(&self.0)),
8386
user,
8487
}
8588
}
@@ -140,7 +143,7 @@ pub trait RequestHelper {
140143
self.get_with_query("/api/v1/crates", query).good()
141144
}
142145

143-
/// Get the crates owned by the specified user.
146+
/// Search for crates owned by the specified user.
144147
fn search_by_user_id(&self, id: i32) -> CrateList {
145148
self.search(&format!("user_id={}", id))
146149
}
@@ -162,7 +165,7 @@ pub trait RequestHelper {
162165

163166
/// A type that can generate unauthenticated requests
164167
pub struct MockAnonymousUser {
165-
inner: TestApp,
168+
app: TestApp,
166169
}
167170

168171
impl RequestHelper for MockAnonymousUser {
@@ -171,7 +174,7 @@ impl RequestHelper for MockAnonymousUser {
171174
}
172175

173176
fn app(&self) -> &TestApp {
174-
&self.inner
177+
&self.app
175178
}
176179
}
177180

@@ -180,7 +183,7 @@ impl RequestHelper for MockAnonymousUser {
180183
/// The `User` is directly injected into middleware extensions and thus the cookie logic is not
181184
/// exercised.
182185
pub struct MockCookieUser {
183-
inner: TestApp,
186+
app: TestApp,
184187
user: User,
185188
}
186189

@@ -195,7 +198,7 @@ impl RequestHelper for MockCookieUser {
195198
}
196199

197200
fn app(&self) -> &TestApp {
198-
&self.inner
201+
&self.app
199202
}
200203
}
201204

@@ -210,18 +213,18 @@ impl MockCookieUser {
210213
/// This method updates the database directly
211214
pub fn db_new_token(&self, name: &str) -> MockTokenUser {
212215
let token = self
213-
.inner
216+
.app
214217
.db(|conn| ApiToken::insert(conn, self.user.id, name).unwrap());
215218
MockTokenUser {
216-
inner: TestApp(Rc::clone(&self.inner.0)),
219+
app: TestApp(Rc::clone(&self.app.0)),
217220
token,
218221
}
219222
}
220223
}
221224

222225
/// A type that can generate token authenticated requests
223226
pub struct MockTokenUser {
224-
inner: TestApp,
227+
app: TestApp,
225228
token: ApiToken,
226229
}
227230

@@ -233,7 +236,7 @@ impl RequestHelper for MockTokenUser {
233236
}
234237

235238
fn app(&self) -> &TestApp {
236-
&self.inner
239+
&self.app
237240
}
238241
}
239242

0 commit comments

Comments
 (0)