Skip to content

Commit bfa3c78

Browse files
committed
WIP
1 parent 41b217c commit bfa3c78

File tree

2 files changed

+59
-88
lines changed

2 files changed

+59
-88
lines changed

src/auth.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ impl AuthCheck {
9797
}
9898
}
9999

100+
// FIXME: better admin detection through chemistry^Wstate.
100101
if self.require_admin && auth.user().admin().is_err() {
101102
let error_message = "User is unauthorized";
102103
return Err(internal(error_message).chain(forbidden()));
@@ -268,7 +269,9 @@ fn ensure_not_locked(user: &User) -> AppResult<()> {
268269

269270
#[cfg(test)]
270271
mod tests {
271-
use crate::models::user::tests::MockUserGenerator;
272+
use std::{collections::HashSet, iter::Cycle};
273+
274+
use crate::models::helpers;
272275

273276
use super::*;
274277

@@ -393,4 +396,58 @@ mod tests {
393396
user,
394397
})
395398
}
399+
400+
// TODO: cleanup what isn't used in this set of tests.
401+
pub struct MockUserGenerator {
402+
authorized_uids: HashSet<i32>,
403+
authorized_uid_iter: Cycle<std::vec::IntoIter<i32>>,
404+
last_unauthorized_uid: i32,
405+
}
406+
407+
impl Default for MockUserGenerator {
408+
fn default() -> Self {
409+
let authorized_uids = helpers::admin::authorized_user_ids().clone();
410+
let authorized_uid_iter = authorized_uids
411+
.iter()
412+
.copied()
413+
.collect::<Vec<i32>>()
414+
.into_iter()
415+
.cycle();
416+
417+
Self {
418+
authorized_uids,
419+
authorized_uid_iter,
420+
last_unauthorized_uid: 0,
421+
}
422+
}
423+
}
424+
425+
impl MockUserGenerator {
426+
pub fn admin(&mut self) -> User {
427+
Self::mock_user(self.authorized_uid_iter.next().unwrap())
428+
}
429+
430+
pub fn regular(&mut self) -> User {
431+
let mut uid = self.last_unauthorized_uid + 1;
432+
while self.authorized_uids.contains(&uid) {
433+
uid += 1;
434+
}
435+
436+
self.last_unauthorized_uid = uid;
437+
Self::mock_user(uid)
438+
}
439+
440+
fn mock_user(gh_id: i32) -> User {
441+
User {
442+
id: 3,
443+
gh_access_token: "arbitrary".into(),
444+
gh_login: "literally_anything".into(),
445+
name: None,
446+
gh_avatar: None,
447+
gh_id,
448+
account_lock_reason: None,
449+
account_lock_until: None,
450+
}
451+
}
452+
}
396453
}

src/models/user.rs

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use crate::app::App;
66
use crate::email::Emails;
77
use crate::util::errors::AppResult;
88

9-
use crate::models::{
10-
helpers::admin, ApiToken, Crate, CrateOwner, Email, NewEmail, Owner, OwnerKind, Rights,
11-
};
9+
use crate::models::{ApiToken, Crate, CrateOwner, Email, NewEmail, Owner, OwnerKind, Rights};
1210
use crate::schema::{crate_owners, emails, users};
1311

1412
/// The model representing a row in the `users` database table.
@@ -179,88 +177,4 @@ impl User {
179177
.first(conn)
180178
.optional()
181179
}
182-
183-
/// Attempt to turn this user into an AdminUser
184-
pub fn admin(&self) -> AppResult<AdminUser> {
185-
AdminUser::new(self)
186-
}
187-
}
188-
189-
#[derive(Debug)]
190-
pub struct AdminUser(User);
191-
192-
impl AdminUser {
193-
pub fn new(user: &User) -> AppResult<Self> {
194-
admin::is_authorized_admin(user.gh_id).map(|_| Self(user.clone()))
195-
}
196-
}
197-
198-
#[cfg(test)]
199-
pub mod tests {
200-
use std::{collections::HashSet, iter::Cycle};
201-
202-
use crate::models::helpers;
203-
204-
use super::*;
205-
206-
#[test]
207-
fn admin_user_instantiation() {
208-
let mut gen = MockUserGenerator::default();
209-
210-
assert!(gen.admin().admin().is_ok());
211-
assert!(gen.regular().admin().is_err());
212-
}
213-
214-
pub struct MockUserGenerator {
215-
authorized_uids: HashSet<i32>,
216-
authorized_uid_iter: Cycle<std::vec::IntoIter<i32>>,
217-
last_unauthorized_uid: i32,
218-
}
219-
220-
impl Default for MockUserGenerator {
221-
fn default() -> Self {
222-
let authorized_uids = helpers::admin::authorized_user_ids().clone();
223-
let authorized_uid_iter = authorized_uids
224-
.iter()
225-
.copied()
226-
.collect::<Vec<i32>>()
227-
.into_iter()
228-
.cycle();
229-
230-
Self {
231-
authorized_uids,
232-
authorized_uid_iter,
233-
last_unauthorized_uid: 0,
234-
}
235-
}
236-
}
237-
238-
impl MockUserGenerator {
239-
pub fn admin(&mut self) -> User {
240-
Self::mock_user(self.authorized_uid_iter.next().unwrap())
241-
}
242-
243-
pub fn regular(&mut self) -> User {
244-
let mut uid = self.last_unauthorized_uid + 1;
245-
while self.authorized_uids.contains(&uid) {
246-
uid += 1;
247-
}
248-
249-
self.last_unauthorized_uid = uid;
250-
Self::mock_user(uid)
251-
}
252-
253-
fn mock_user(gh_id: i32) -> User {
254-
User {
255-
id: 3,
256-
gh_access_token: "arbitrary".into(),
257-
gh_login: "literally_anything".into(),
258-
name: None,
259-
gh_avatar: None,
260-
gh_id,
261-
account_lock_reason: None,
262-
account_lock_until: None,
263-
}
264-
}
265-
}
266180
}

0 commit comments

Comments
 (0)