Skip to content

Commit a02b8b3

Browse files
authored
Merge pull request #217 from Sakib25800/refactor/prebuilt-test-users
Improve tests regarding permissions
2 parents 1f773b3 + d4f7a66 commit a02b8b3

File tree

4 files changed

+66
-42
lines changed

4 files changed

+66
-42
lines changed

src/bors/handlers/review.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -297,38 +297,33 @@ mod tests {
297297

298298
#[sqlx::test]
299299
async fn insufficient_permission_approve(pool: sqlx::PgPool) {
300-
let world = World::default();
301-
world.default_repo().lock().permissions = Permissions::default();
302-
303-
BorsBuilder::new(pool)
304-
.world(world)
305-
.run_test(|mut tester| async {
306-
tester.post_comment("@bors try").await?;
307-
assert_eq!(
308-
tester.get_comment().await?,
309-
"@default-user: :key: Insufficient privileges: not in try users"
310-
);
311-
Ok(tester)
312-
})
313-
.await;
300+
run_test(pool, |mut tester| async {
301+
tester
302+
.post_comment(Comment::from("@bors try").with_author(User::unprivileged()))
303+
.await?;
304+
assert_eq!(
305+
tester.get_comment().await?,
306+
"@unprivileged-user: :key: Insufficient privileges: not in try users"
307+
);
308+
Ok(tester)
309+
})
310+
.await;
314311
}
315312

316313
#[sqlx::test]
317314
async fn insufficient_permission_set_priority(pool: sqlx::PgPool) {
318-
let world = World::default();
319-
world.default_repo().lock().permissions = Permissions::default();
320-
321-
BorsBuilder::new(pool)
322-
.world(world)
323-
.run_test(|mut tester| async {
324-
tester.post_comment("@bors p=2").await?;
325-
assert_eq!(
326-
tester.get_comment().await?,
327-
"@default-user: :key: Insufficient privileges: not in review users"
328-
);
329-
Ok(tester)
330-
})
331-
.await;
315+
run_test(pool, |mut tester| async {
316+
tester
317+
.post_comment(Comment::from("@bors p=2").with_author(User::unprivileged()))
318+
.await?;
319+
tester.post_comment("@bors p=2").await?;
320+
assert_eq!(
321+
tester.get_comment().await?,
322+
"@unprivileged-user: :key: Insufficient privileges: not in review users"
323+
);
324+
Ok(tester)
325+
})
326+
.await;
332327
}
333328

334329
#[sqlx::test]

src/bors/handlers/trybuild.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ mod tests {
334334
use crate::database::BuildStatus;
335335
use crate::github::CommitSha;
336336
use crate::tests::mocks::{
337-
default_pr_number, default_repo_name, run_test, BorsBuilder, Permissions, Workflow,
337+
default_pr_number, default_repo_name, run_test, BorsBuilder, Comment, User, Workflow,
338338
WorkflowEvent, World,
339339
};
340340

@@ -380,20 +380,32 @@ mod tests {
380380

381381
#[sqlx::test]
382382
async fn try_no_permissions(pool: sqlx::PgPool) {
383-
let world = World::default();
384-
world.get_repo(default_repo_name()).lock().permissions = Permissions::default();
383+
run_test(pool, |mut tester| async {
384+
tester
385+
.post_comment(Comment::from("@bors try").with_author(User::unprivileged()))
386+
.await?;
387+
assert_eq!(
388+
tester.get_comment().await?,
389+
"@unprivileged-user: :key: Insufficient privileges: not in try users"
390+
);
391+
Ok(tester)
392+
})
393+
.await;
394+
}
385395

386-
BorsBuilder::new(pool)
387-
.world(world)
388-
.run_test(|mut tester| async {
389-
tester.post_comment("@bors try").await?;
390-
assert_eq!(
391-
tester.get_comment().await?,
392-
"@default-user: :key: Insufficient privileges: not in try users"
393-
);
394-
Ok(tester)
395-
})
396-
.await;
396+
#[sqlx::test]
397+
async fn try_only_requires_try_permission(pool: sqlx::PgPool) {
398+
run_test(pool, |mut tester| async {
399+
tester
400+
.post_comment(Comment::from("@bors try").with_author(User::try_user()))
401+
.await?;
402+
assert_eq!(
403+
tester.get_comment().await?,
404+
":hourglass: Trying commit pr-1-sha with merge merge-main-sha1-pr-1-sha-0…"
405+
);
406+
Ok(tester)
407+
})
408+
.await;
397409
}
398410

399411
#[sqlx::test]

src/tests/mocks/repository.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ timeout = 3600
116116
User::default(),
117117
vec![PermissionType::Try, PermissionType::Review],
118118
);
119+
users.insert(User::try_user(), vec![PermissionType::Try]);
120+
users.insert(
121+
User::reviewer(),
122+
vec![PermissionType::Try, PermissionType::Review],
123+
);
119124

120125
Self::new(
121126
default_repo_name().owner(),

src/tests/mocks/user.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ impl User {
1616
Self::new(102, "bors-bot")
1717
}
1818

19+
pub fn unprivileged() -> Self {
20+
Self::new(103, "unprivileged-user")
21+
}
22+
23+
pub fn try_user() -> Self {
24+
Self::new(104, "try-user")
25+
}
26+
27+
pub fn reviewer() -> Self {
28+
Self::new(105, "reviewer")
29+
}
30+
1931
pub fn new(id: u64, name: &str) -> Self {
2032
Self {
2133
github_id: id,

0 commit comments

Comments
 (0)