| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | use std::{collections::HashMap, time::SystemTime}; |
| 16 | |
| 17 | use crate::domain::{ |
| 18 | Actor, DomainError, Issue, IssueComment, IssueCommentId, IssueId, IssueNumber, IssueState, |
| 19 | OrgName, RepoName, Repository, UserId, |
| 20 | repository::{ |
| 21 | IssueRepository, MembershipRepository, OrgRepository, RepoRepository, UserRepository, |
| 22 | }, |
| 23 | }; |
| 24 | |
| 25 | use super::{ |
| 26 | authz::{is_org_member, is_org_owner}, |
| 27 | error::{Error, Result}, |
| 28 | }; |
| 29 | |
| 30 | |
| 31 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 32 | pub struct NewIssue { |
| 33 | pub title: String, |
| 34 | pub body: String, |
| 35 | } |
| 36 | |
| 37 | |
| 38 | |
| 39 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 40 | pub struct IssueEdit { |
| 41 | pub title: String, |
| 42 | pub body: String, |
| 43 | } |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 50 | pub struct IssueSummary { |
| 51 | pub number: IssueNumber, |
| 52 | pub title: String, |
| 53 | pub state: IssueState, |
| 54 | |
| 55 | |
| 56 | pub author: Option<OrgName>, |
| 57 | pub created_at: SystemTime, |
| 58 | pub comments: u64, |
| 59 | } |
| 60 | |
| 61 | |
| 62 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 63 | pub struct IssueList { |
| 64 | |
| 65 | |
| 66 | pub state: IssueState, |
| 67 | pub open: u64, |
| 68 | pub closed: u64, |
| 69 | |
| 70 | |
| 71 | pub issues: Vec<IssueSummary>, |
| 72 | pub viewer_is_owner: bool, |
| 73 | } |
| 74 | |
| 75 | |
| 76 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 77 | pub struct CommentView { |
| 78 | pub author: Option<OrgName>, |
| 79 | pub body: String, |
| 80 | pub created_at: SystemTime, |
| 81 | } |
| 82 | |
| 83 | |
| 84 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 85 | pub struct IssueDetail { |
| 86 | pub number: IssueNumber, |
| 87 | pub title: String, |
| 88 | pub body: String, |
| 89 | pub state: IssueState, |
| 90 | pub author: Option<OrgName>, |
| 91 | pub created_at: SystemTime, |
| 92 | pub closed_at: Option<SystemTime>, |
| 93 | |
| 94 | pub comments: Vec<CommentView>, |
| 95 | pub viewer_is_owner: bool, |
| 96 | } |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | #[allow(clippy::too_many_arguments)] |
| 105 | pub async fn list_issues( |
| 106 | actor: &Actor, |
| 107 | handle: &OrgName, |
| 108 | name: &RepoName, |
| 109 | state: IssueState, |
| 110 | orgs: &impl OrgRepository, |
| 111 | memberships: &impl MembershipRepository, |
| 112 | repos: &impl RepoRepository, |
| 113 | issues: &impl IssueRepository, |
| 114 | users: &impl UserRepository, |
| 115 | ) -> Result<Option<IssueList>> { |
| 116 | let Some((repo, viewer_is_owner)) = |
| 117 | readable_repo(actor, handle, name, orgs, memberships, repos).await? |
| 118 | else { |
| 119 | return Ok(None); |
| 120 | }; |
| 121 | |
| 122 | let counts = issues.counts(&repo.id).await?; |
| 123 | let listed = issues.list_by_repo(&repo.id, state).await?; |
| 124 | |
| 125 | |
| 126 | let comments: HashMap<IssueId, u64> = |
| 127 | issues.comment_counts(&repo.id).await?.into_iter().collect(); |
| 128 | |
| 129 | let mut handles = Handles::default(); |
| 130 | let mut summaries = Vec::with_capacity(listed.len()); |
| 131 | |
| 132 | for issue in listed { |
| 133 | summaries.push(IssueSummary { |
| 134 | number: issue.number, |
| 135 | title: issue.title, |
| 136 | state: issue.state, |
| 137 | author: handles.of(&issue.author_id, orgs, users).await?, |
| 138 | created_at: issue.created_at, |
| 139 | comments: comments.get(&issue.id).copied().unwrap_or(0), |
| 140 | }); |
| 141 | } |
| 142 | |
| 143 | Ok(Some(IssueList { |
| 144 | state, |
| 145 | open: counts.open, |
| 146 | closed: counts.closed, |
| 147 | issues: summaries, |
| 148 | viewer_is_owner, |
| 149 | })) |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | #[allow(clippy::too_many_arguments)] |
| 159 | pub async fn view_issue( |
| 160 | actor: &Actor, |
| 161 | handle: &OrgName, |
| 162 | name: &RepoName, |
| 163 | number: IssueNumber, |
| 164 | orgs: &impl OrgRepository, |
| 165 | memberships: &impl MembershipRepository, |
| 166 | repos: &impl RepoRepository, |
| 167 | issues: &impl IssueRepository, |
| 168 | users: &impl UserRepository, |
| 169 | ) -> Result<Option<IssueDetail>> { |
| 170 | let Some((repo, viewer_is_owner)) = |
| 171 | readable_repo(actor, handle, name, orgs, memberships, repos).await? |
| 172 | else { |
| 173 | return Ok(None); |
| 174 | }; |
| 175 | |
| 176 | let Some(issue) = issues.find_by_number(&repo.id, number).await? else { |
| 177 | return Ok(None); |
| 178 | }; |
| 179 | |
| 180 | let mut handles = Handles::default(); |
| 181 | let author = handles.of(&issue.author_id, orgs, users).await?; |
| 182 | |
| 183 | let mut comments = Vec::new(); |
| 184 | for comment in issues.list_comments(&issue.id).await? { |
| 185 | comments.push(CommentView { |
| 186 | author: handles.of(&comment.author_id, orgs, users).await?, |
| 187 | body: comment.body, |
| 188 | created_at: comment.created_at, |
| 189 | }); |
| 190 | } |
| 191 | |
| 192 | Ok(Some(IssueDetail { |
| 193 | number: issue.number, |
| 194 | title: issue.title, |
| 195 | body: issue.body, |
| 196 | state: issue.state, |
| 197 | author, |
| 198 | created_at: issue.created_at, |
| 199 | closed_at: issue.closed_at, |
| 200 | comments, |
| 201 | viewer_is_owner, |
| 202 | })) |
| 203 | } |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | #[allow(clippy::too_many_arguments)] |
| 216 | pub async fn create_issue( |
| 217 | actor: &Actor, |
| 218 | handle: &OrgName, |
| 219 | name: &RepoName, |
| 220 | spec: &NewIssue, |
| 221 | now: SystemTime, |
| 222 | orgs: &impl OrgRepository, |
| 223 | memberships: &impl MembershipRepository, |
| 224 | repos: &impl RepoRepository, |
| 225 | issues: &impl IssueRepository, |
| 226 | ) -> Result<Issue> { |
| 227 | let repo = writable_repo(actor, handle, name, orgs, memberships, repos).await?; |
| 228 | |
| 229 | |
| 230 | |
| 231 | let author_id = actor.user_id().ok_or(DomainError::Forbidden)?.clone(); |
| 232 | |
| 233 | let number = issues.next_number(&repo.id).await?; |
| 234 | let issue = Issue::new( |
| 235 | IssueId::generate(), |
| 236 | repo.id.clone(), |
| 237 | number, |
| 238 | spec.title.clone(), |
| 239 | spec.body.clone(), |
| 240 | author_id, |
| 241 | now, |
| 242 | )?; |
| 243 | |
| 244 | issues.save(&issue).await?; |
| 245 | |
| 246 | Ok(issue) |
| 247 | } |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | #[allow(clippy::too_many_arguments)] |
| 254 | pub async fn comment_on_issue( |
| 255 | actor: &Actor, |
| 256 | handle: &OrgName, |
| 257 | name: &RepoName, |
| 258 | number: IssueNumber, |
| 259 | body: &str, |
| 260 | now: SystemTime, |
| 261 | orgs: &impl OrgRepository, |
| 262 | memberships: &impl MembershipRepository, |
| 263 | repos: &impl RepoRepository, |
| 264 | issues: &impl IssueRepository, |
| 265 | ) -> Result<()> { |
| 266 | let repo = writable_repo(actor, handle, name, orgs, memberships, repos).await?; |
| 267 | let mut issue = existing_issue(&repo, number, issues).await?; |
| 268 | let author_id = actor.user_id().ok_or(DomainError::Forbidden)?.clone(); |
| 269 | |
| 270 | let comment = IssueComment::new( |
| 271 | IssueCommentId::generate(), |
| 272 | issue.id.clone(), |
| 273 | author_id, |
| 274 | body, |
| 275 | now, |
| 276 | )?; |
| 277 | |
| 278 | issues.add_comment(&comment).await?; |
| 279 | |
| 280 | issue.touch(now); |
| 281 | issues.save(&issue).await?; |
| 282 | |
| 283 | Ok(()) |
| 284 | } |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | #[allow(clippy::too_many_arguments)] |
| 291 | pub async fn set_issue_state( |
| 292 | actor: &Actor, |
| 293 | handle: &OrgName, |
| 294 | name: &RepoName, |
| 295 | number: IssueNumber, |
| 296 | state: IssueState, |
| 297 | now: SystemTime, |
| 298 | orgs: &impl OrgRepository, |
| 299 | memberships: &impl MembershipRepository, |
| 300 | repos: &impl RepoRepository, |
| 301 | issues: &impl IssueRepository, |
| 302 | ) -> Result<()> { |
| 303 | let repo = writable_repo(actor, handle, name, orgs, memberships, repos).await?; |
| 304 | let mut issue = existing_issue(&repo, number, issues).await?; |
| 305 | |
| 306 | match state { |
| 307 | IssueState::Closed => issue.close(now), |
| 308 | IssueState::Open => issue.reopen(now), |
| 309 | } |
| 310 | |
| 311 | issues.save(&issue).await?; |
| 312 | |
| 313 | Ok(()) |
| 314 | } |
| 315 | |
| 316 | |
| 317 | #[allow(clippy::too_many_arguments)] |
| 318 | pub async fn edit_issue( |
| 319 | actor: &Actor, |
| 320 | handle: &OrgName, |
| 321 | name: &RepoName, |
| 322 | number: IssueNumber, |
| 323 | edit: &IssueEdit, |
| 324 | now: SystemTime, |
| 325 | orgs: &impl OrgRepository, |
| 326 | memberships: &impl MembershipRepository, |
| 327 | repos: &impl RepoRepository, |
| 328 | issues: &impl IssueRepository, |
| 329 | ) -> Result<()> { |
| 330 | let repo = writable_repo(actor, handle, name, orgs, memberships, repos).await?; |
| 331 | let mut issue = existing_issue(&repo, number, issues).await?; |
| 332 | |
| 333 | |
| 334 | |
| 335 | issue.edit(edit.title.clone(), edit.body.clone(), now)?; |
| 336 | issues.save(&issue).await?; |
| 337 | |
| 338 | Ok(()) |
| 339 | } |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | async fn readable_repo( |
| 349 | actor: &Actor, |
| 350 | handle: &OrgName, |
| 351 | name: &RepoName, |
| 352 | orgs: &impl OrgRepository, |
| 353 | memberships: &impl MembershipRepository, |
| 354 | repos: &impl RepoRepository, |
| 355 | ) -> Result<Option<(Repository, bool)>> { |
| 356 | let Some(org) = orgs.find_by_name(handle).await? else { |
| 357 | return Ok(None); |
| 358 | }; |
| 359 | |
| 360 | let Some(repo) = repos.find_by_org_and_name(&org.id, name).await? else { |
| 361 | return Ok(None); |
| 362 | }; |
| 363 | |
| 364 | if !repo.visibility.is_public() && !is_org_member(&org, actor, memberships).await? { |
| 365 | return Ok(None); |
| 366 | } |
| 367 | |
| 368 | Ok(Some((repo, is_org_owner(&org, actor, memberships).await?))) |
| 369 | } |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | async fn writable_repo( |
| 377 | actor: &Actor, |
| 378 | handle: &OrgName, |
| 379 | name: &RepoName, |
| 380 | orgs: &impl OrgRepository, |
| 381 | memberships: &impl MembershipRepository, |
| 382 | repos: &impl RepoRepository, |
| 383 | ) -> Result<Repository> { |
| 384 | let Some((repo, is_owner)) = |
| 385 | readable_repo(actor, handle, name, orgs, memberships, repos).await? |
| 386 | else { |
| 387 | return Err(Error::Domain(DomainError::NotFound { |
| 388 | entity: "repository", |
| 389 | })); |
| 390 | }; |
| 391 | |
| 392 | if !is_owner { |
| 393 | return Err(Error::Domain(DomainError::Forbidden)); |
| 394 | } |
| 395 | |
| 396 | Ok(repo) |
| 397 | } |
| 398 | |
| 399 | |
| 400 | async fn existing_issue( |
| 401 | repo: &Repository, |
| 402 | number: IssueNumber, |
| 403 | issues: &impl IssueRepository, |
| 404 | ) -> Result<Issue> { |
| 405 | issues |
| 406 | .find_by_number(&repo.id, number) |
| 407 | .await? |
| 408 | .ok_or(Error::Domain(DomainError::NotFound { entity: "issue" })) |
| 409 | } |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | #[derive(Debug, Default)] |
| 417 | struct Handles { |
| 418 | known: HashMap<UserId, Option<OrgName>>, |
| 419 | } |
| 420 | |
| 421 | impl Handles { |
| 422 | async fn of( |
| 423 | &mut self, |
| 424 | user_id: &UserId, |
| 425 | orgs: &impl OrgRepository, |
| 426 | users: &impl UserRepository, |
| 427 | ) -> Result<Option<OrgName>> { |
| 428 | if let Some(known) = self.known.get(user_id) { |
| 429 | return Ok(known.clone()); |
| 430 | } |
| 431 | |
| 432 | |
| 433 | |
| 434 | let handle = match users.find_by_id(user_id).await? { |
| 435 | Some(user) => orgs |
| 436 | .find_by_id(&user.personal_org_id) |
| 437 | .await? |
| 438 | .map(|org| org.name), |
| 439 | None => None, |
| 440 | }; |
| 441 | |
| 442 | self.known.insert(user_id.clone(), handle.clone()); |
| 443 | |
| 444 | Ok(handle) |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | #[cfg(test)] |
| 449 | mod tests { |
| 450 | use std::time::Duration; |
| 451 | |
| 452 | use super::*; |
| 453 | use crate::{ |
| 454 | domain::{ |
| 455 | Email, Membership, MembershipId, OrgId, Organization, PasswordHash, RepoId, Role, User, |
| 456 | Visibility, |
| 457 | repository::{MembershipRepository, OrgRepository}, |
| 458 | }, |
| 459 | infrastructure::repository::{ |
| 460 | InMemoryIssueRepo, InMemoryMembershipRepo, InMemoryOrgRepo, InMemoryRepoRepo, |
| 461 | InMemoryUserRepo, |
| 462 | }, |
| 463 | }; |
| 464 | |
| 465 | fn at(seconds: u64) -> SystemTime { |
| 466 | SystemTime::UNIX_EPOCH + Duration::from_secs(seconds) |
| 467 | } |
| 468 | |
| 469 | struct Fixture { |
| 470 | orgs: InMemoryOrgRepo, |
| 471 | memberships: InMemoryMembershipRepo, |
| 472 | repos: InMemoryRepoRepo, |
| 473 | issues: InMemoryIssueRepo, |
| 474 | users: InMemoryUserRepo, |
| 475 | handle: OrgName, |
| 476 | owner: Actor, |
| 477 | member: Actor, |
| 478 | stranger: Actor, |
| 479 | } |
| 480 | |
| 481 | |
| 482 | |
| 483 | async fn fixture() -> Fixture { |
| 484 | let orgs = InMemoryOrgRepo::new(); |
| 485 | let memberships = InMemoryMembershipRepo::new(); |
| 486 | let repos = InMemoryRepoRepo::new(); |
| 487 | let users = InMemoryUserRepo::new(); |
| 488 | |
| 489 | let org = Organization::new(OrgId::generate(), "acme", None).expect("valid org"); |
| 490 | orgs.save(&org).await.expect("save org"); |
| 491 | |
| 492 | let actor_for = |email: &str, role: Role| { |
| 493 | let user = User::new( |
| 494 | UserId::generate(), |
| 495 | Email::new(email).expect("valid email"), |
| 496 | PasswordHash::from_trusted("$argon2id$test"), |
| 497 | org.id.clone(), |
| 498 | ); |
| 499 | (user, role) |
| 500 | }; |
| 501 | |
| 502 | let (owner, _) = actor_for("owner@example.com", Role::Owner); |
| 503 | let (member, _) = actor_for("member@example.com", Role::Member); |
| 504 | |
| 505 | for (user, role) in [(&owner, Role::Owner), (&member, Role::Member)] { |
| 506 | users.save(user).await.expect("save user"); |
| 507 | memberships |
| 508 | .save(&Membership::new( |
| 509 | MembershipId::generate(), |
| 510 | org.id.clone(), |
| 511 | user.id.clone(), |
| 512 | role, |
| 513 | )) |
| 514 | .await |
| 515 | .expect("save membership"); |
| 516 | } |
| 517 | |
| 518 | for (name, visibility) in [ |
| 519 | ("steid", Visibility::Public), |
| 520 | ("secret", Visibility::Private), |
| 521 | ] { |
| 522 | repos |
| 523 | .save( |
| 524 | &Repository::new( |
| 525 | RepoId::generate(), |
| 526 | org.id.clone(), |
| 527 | name, |
| 528 | None, |
| 529 | visibility, |
| 530 | at(1_000), |
| 531 | ) |
| 532 | .expect("valid repo"), |
| 533 | ) |
| 534 | .await |
| 535 | .expect("save repo"); |
| 536 | } |
| 537 | |
| 538 | Fixture { |
| 539 | orgs, |
| 540 | memberships, |
| 541 | repos, |
| 542 | issues: InMemoryIssueRepo::new(), |
| 543 | users, |
| 544 | handle: org.name.clone(), |
| 545 | owner: Actor::User(owner.id), |
| 546 | member: Actor::User(member.id), |
| 547 | stranger: Actor::Anonymous, |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | fn repo(name: &str) -> RepoName { |
| 552 | RepoName::new(name).expect("valid name") |
| 553 | } |
| 554 | |
| 555 | async fn open(f: &Fixture, name: &str, title: &str) -> Issue { |
| 556 | create_issue( |
| 557 | &f.owner, |
| 558 | &f.handle, |
| 559 | &repo(name), |
| 560 | &NewIssue { |
| 561 | title: title.to_owned(), |
| 562 | body: "the body".to_owned(), |
| 563 | }, |
| 564 | at(2_000), |
| 565 | &f.orgs, |
| 566 | &f.memberships, |
| 567 | &f.repos, |
| 568 | &f.issues, |
| 569 | ) |
| 570 | .await |
| 571 | .expect("should open") |
| 572 | } |
| 573 | |
| 574 | async fn listed( |
| 575 | f: &Fixture, |
| 576 | actor: &Actor, |
| 577 | name: &str, |
| 578 | state: IssueState, |
| 579 | ) -> Option<IssueList> { |
| 580 | list_issues( |
| 581 | actor, |
| 582 | &f.handle, |
| 583 | &repo(name), |
| 584 | state, |
| 585 | &f.orgs, |
| 586 | &f.memberships, |
| 587 | &f.repos, |
| 588 | &f.issues, |
| 589 | &f.users, |
| 590 | ) |
| 591 | .await |
| 592 | .expect("should list") |
| 593 | } |
| 594 | |
| 595 | async fn detail(f: &Fixture, actor: &Actor, name: &str, number: u64) -> Option<IssueDetail> { |
| 596 | view_issue( |
| 597 | actor, |
| 598 | &f.handle, |
| 599 | &repo(name), |
| 600 | IssueNumber::from_trusted(number), |
| 601 | &f.orgs, |
| 602 | &f.memberships, |
| 603 | &f.repos, |
| 604 | &f.issues, |
| 605 | &f.users, |
| 606 | ) |
| 607 | .await |
| 608 | .expect("should read") |
| 609 | } |
| 610 | |
| 611 | fn is_forbidden(error: &Error) -> bool { |
| 612 | matches!(error, Error::Domain(DomainError::Forbidden)) |
| 613 | } |
| 614 | |
| 615 | fn is_not_found(error: &Error) -> bool { |
| 616 | matches!(error, Error::Domain(DomainError::NotFound { .. })) |
| 617 | } |
| 618 | |
| 619 | |
| 620 | |
| 621 | #[tokio::test] |
| 622 | async fn issues_are_numbered_from_one_per_repository() { |
| 623 | |
| 624 | |
| 625 | let f = fixture().await; |
| 626 | |
| 627 | assert_eq!(open(&f, "steid", "first").await.number.get(), 1); |
| 628 | assert_eq!(open(&f, "steid", "second").await.number.get(), 2); |
| 629 | assert_eq!(open(&f, "secret", "elsewhere").await.number.get(), 1); |
| 630 | assert_eq!(open(&f, "steid", "third").await.number.get(), 3); |
| 631 | } |
| 632 | |
| 633 | |
| 634 | |
| 635 | #[tokio::test] |
| 636 | async fn a_listing_is_newest_first_with_counts_for_both_states() { |
| 637 | let f = fixture().await; |
| 638 | open(&f, "steid", "first").await; |
| 639 | open(&f, "steid", "second").await; |
| 640 | set_state(&f, 1, IssueState::Closed).await.expect("close"); |
| 641 | |
| 642 | let open_list = listed(&f, &f.owner, "steid", IssueState::Open) |
| 643 | .await |
| 644 | .expect("visible"); |
| 645 | |
| 646 | assert_eq!( |
| 647 | open_list |
| 648 | .issues |
| 649 | .iter() |
| 650 | .map(|issue| issue.number.get()) |
| 651 | .collect::<Vec<_>>(), |
| 652 | vec![2] |
| 653 | ); |
| 654 | assert_eq!((open_list.open, open_list.closed), (1, 1)); |
| 655 | assert_eq!(open_list.state, IssueState::Open); |
| 656 | |
| 657 | let closed_list = listed(&f, &f.owner, "steid", IssueState::Closed) |
| 658 | .await |
| 659 | .expect("visible"); |
| 660 | |
| 661 | assert_eq!( |
| 662 | closed_list |
| 663 | .issues |
| 664 | .iter() |
| 665 | .map(|issue| issue.title.as_str()) |
| 666 | .collect::<Vec<_>>(), |
| 667 | vec!["first"] |
| 668 | ); |
| 669 | } |
| 670 | |
| 671 | #[tokio::test] |
| 672 | async fn a_listing_names_the_author_and_counts_the_comments() { |
| 673 | let f = fixture().await; |
| 674 | open(&f, "steid", "Blame is slow").await; |
| 675 | comment(&f, &f.owner, 1, "still slow") |
| 676 | .await |
| 677 | .expect("comment"); |
| 678 | |
| 679 | let list = listed(&f, &f.stranger, "steid", IssueState::Open) |
| 680 | .await |
| 681 | .expect("visible"); |
| 682 | let row = list.issues.first().expect("one row"); |
| 683 | |
| 684 | assert_eq!(row.author.as_ref().map(OrgName::as_str), Some("acme")); |
| 685 | assert_eq!(row.comments, 1); |
| 686 | assert!( |
| 687 | !list.viewer_is_owner, |
| 688 | "an anonymous visitor is offered no controls" |
| 689 | ); |
| 690 | } |
| 691 | |
| 692 | #[tokio::test] |
| 693 | async fn an_anonymous_visitor_reads_a_public_repositorys_issues() { |
| 694 | let f = fixture().await; |
| 695 | open(&f, "steid", "Blame is slow").await; |
| 696 | |
| 697 | assert!( |
| 698 | listed(&f, &f.stranger, "steid", IssueState::Open) |
| 699 | .await |
| 700 | .is_some() |
| 701 | ); |
| 702 | assert!(detail(&f, &f.stranger, "steid", 1).await.is_some()); |
| 703 | } |
| 704 | |
| 705 | #[tokio::test] |
| 706 | async fn a_private_repositorys_issues_are_absent_rather_than_refused() { |
| 707 | |
| 708 | let f = fixture().await; |
| 709 | open(&f, "secret", "Blame is slow").await; |
| 710 | |
| 711 | assert!( |
| 712 | listed(&f, &f.stranger, "secret", IssueState::Open) |
| 713 | .await |
| 714 | .is_none() |
| 715 | ); |
| 716 | assert!(detail(&f, &f.stranger, "secret", 1).await.is_none()); |
| 717 | assert!( |
| 718 | listed(&f, &f.member, "secret", IssueState::Open) |
| 719 | .await |
| 720 | .is_some(), |
| 721 | "a member of the organisation may still read them" |
| 722 | ); |
| 723 | } |
| 724 | |
| 725 | #[tokio::test] |
| 726 | async fn a_number_that_does_not_exist_reads_as_absent() { |
| 727 | let f = fixture().await; |
| 728 | open(&f, "steid", "Blame is slow").await; |
| 729 | |
| 730 | assert!(detail(&f, &f.owner, "steid", 2).await.is_none()); |
| 731 | assert!( |
| 732 | detail(&f, &f.owner, "steid", 1).await.is_some(), |
| 733 | "the one that does exist still reads" |
| 734 | ); |
| 735 | } |
| 736 | |
| 737 | #[tokio::test] |
| 738 | async fn an_issue_carries_its_body_and_its_conversation_forwards() { |
| 739 | let f = fixture().await; |
| 740 | open(&f, "steid", "Blame is slow").await; |
| 741 | comment(&f, &f.owner, 1, "first").await.expect("comment"); |
| 742 | comment(&f, &f.owner, 1, "second").await.expect("comment"); |
| 743 | |
| 744 | let issue = detail(&f, &f.owner, "steid", 1).await.expect("visible"); |
| 745 | |
| 746 | assert_eq!(issue.body, "the body"); |
| 747 | assert_eq!( |
| 748 | issue |
| 749 | .comments |
| 750 | .iter() |
| 751 | .map(|comment| comment.body.as_str()) |
| 752 | .collect::<Vec<_>>(), |
| 753 | vec!["first", "second"] |
| 754 | ); |
| 755 | assert_eq!( |
| 756 | issue.comments[0].author.as_ref().map(OrgName::as_str), |
| 757 | Some("acme") |
| 758 | ); |
| 759 | } |
| 760 | |
| 761 | |
| 762 | |
| 763 | async fn set_state(f: &Fixture, number: u64, state: IssueState) -> Result<()> { |
| 764 | set_issue_state( |
| 765 | &f.owner, |
| 766 | &f.handle, |
| 767 | &repo("steid"), |
| 768 | IssueNumber::from_trusted(number), |
| 769 | state, |
| 770 | at(3_000), |
| 771 | &f.orgs, |
| 772 | &f.memberships, |
| 773 | &f.repos, |
| 774 | &f.issues, |
| 775 | ) |
| 776 | .await |
| 777 | } |
| 778 | |
| 779 | async fn comment(f: &Fixture, actor: &Actor, number: u64, body: &str) -> Result<()> { |
| 780 | comment_on_issue( |
| 781 | actor, |
| 782 | &f.handle, |
| 783 | &repo("steid"), |
| 784 | IssueNumber::from_trusted(number), |
| 785 | body, |
| 786 | at(4_000), |
| 787 | &f.orgs, |
| 788 | &f.memberships, |
| 789 | &f.repos, |
| 790 | &f.issues, |
| 791 | ) |
| 792 | .await |
| 793 | } |
| 794 | |
| 795 | #[tokio::test] |
| 796 | async fn closing_and_reopening_move_an_issue_between_the_lists() { |
| 797 | let f = fixture().await; |
| 798 | open(&f, "steid", "Blame is slow").await; |
| 799 | |
| 800 | set_state(&f, 1, IssueState::Closed).await.expect("close"); |
| 801 | let closed = detail(&f, &f.owner, "steid", 1).await.expect("visible"); |
| 802 | assert_eq!(closed.state, IssueState::Closed); |
| 803 | assert_eq!(closed.closed_at, Some(at(3_000))); |
| 804 | |
| 805 | set_state(&f, 1, IssueState::Open).await.expect("reopen"); |
| 806 | let reopened = detail(&f, &f.owner, "steid", 1).await.expect("visible"); |
| 807 | assert_eq!(reopened.state, IssueState::Open); |
| 808 | assert_eq!(reopened.closed_at, None); |
| 809 | } |
| 810 | |
| 811 | #[tokio::test] |
| 812 | async fn editing_replaces_the_title_and_body() { |
| 813 | let f = fixture().await; |
| 814 | open(&f, "steid", "Blame is slow").await; |
| 815 | |
| 816 | edit_issue( |
| 817 | &f.owner, |
| 818 | &f.handle, |
| 819 | &repo("steid"), |
| 820 | IssueNumber::from_trusted(1), |
| 821 | &IssueEdit { |
| 822 | title: "Blame is very slow".to_owned(), |
| 823 | body: "with numbers".to_owned(), |
| 824 | }, |
| 825 | at(5_000), |
| 826 | &f.orgs, |
| 827 | &f.memberships, |
| 828 | &f.repos, |
| 829 | &f.issues, |
| 830 | ) |
| 831 | .await |
| 832 | .expect("should edit"); |
| 833 | |
| 834 | let issue = detail(&f, &f.owner, "steid", 1).await.expect("visible"); |
| 835 | |
| 836 | assert_eq!(issue.title, "Blame is very slow"); |
| 837 | assert_eq!(issue.body, "with numbers"); |
| 838 | } |
| 839 | |
| 840 | #[tokio::test] |
| 841 | async fn an_issue_needs_a_title_and_a_comment_needs_a_body() { |
| 842 | let f = fixture().await; |
| 843 | |
| 844 | let error = create_issue( |
| 845 | &f.owner, |
| 846 | &f.handle, |
| 847 | &repo("steid"), |
| 848 | &NewIssue { |
| 849 | title: " ".to_owned(), |
| 850 | body: "body".to_owned(), |
| 851 | }, |
| 852 | at(2_000), |
| 853 | &f.orgs, |
| 854 | &f.memberships, |
| 855 | &f.repos, |
| 856 | &f.issues, |
| 857 | ) |
| 858 | .await |
| 859 | .expect_err("should refuse"); |
| 860 | |
| 861 | assert!(matches!( |
| 862 | error, |
| 863 | Error::Domain(DomainError::Validation { .. }) |
| 864 | )); |
| 865 | assert!( |
| 866 | listed(&f, &f.owner, "steid", IssueState::Open) |
| 867 | .await |
| 868 | .expect("visible") |
| 869 | .issues |
| 870 | .is_empty() |
| 871 | ); |
| 872 | |
| 873 | open(&f, "steid", "Blame is slow").await; |
| 874 | assert!(comment(&f, &f.owner, 1, " ").await.is_err()); |
| 875 | } |
| 876 | |
| 877 | #[tokio::test] |
| 878 | async fn a_write_aimed_at_a_number_that_is_not_there_is_not_found() { |
| 879 | let f = fixture().await; |
| 880 | |
| 881 | assert!(is_not_found( |
| 882 | &set_state(&f, 9, IssueState::Closed) |
| 883 | .await |
| 884 | .expect_err("refuse") |
| 885 | )); |
| 886 | assert!(is_not_found( |
| 887 | &comment(&f, &f.owner, 9, "hello").await.expect_err("refuse") |
| 888 | )); |
| 889 | } |
| 890 | |
| 891 | |
| 892 | |
| 893 | #[tokio::test] |
| 894 | async fn only_the_owner_may_open_an_issue() { |
| 895 | |
| 896 | let f = fixture().await; |
| 897 | |
| 898 | for actor in [&f.member, &f.stranger] { |
| 899 | let error = create_issue( |
| 900 | actor, |
| 901 | &f.handle, |
| 902 | &repo("steid"), |
| 903 | &NewIssue { |
| 904 | title: "not mine".to_owned(), |
| 905 | body: String::new(), |
| 906 | }, |
| 907 | at(2_000), |
| 908 | &f.orgs, |
| 909 | &f.memberships, |
| 910 | &f.repos, |
| 911 | &f.issues, |
| 912 | ) |
| 913 | .await |
| 914 | .expect_err("should refuse"); |
| 915 | |
| 916 | assert!(is_forbidden(&error), "{actor:?} should be forbidden"); |
| 917 | } |
| 918 | |
| 919 | assert!( |
| 920 | listed(&f, &f.owner, "steid", IssueState::Open) |
| 921 | .await |
| 922 | .expect("visible") |
| 923 | .issues |
| 924 | .is_empty() |
| 925 | ); |
| 926 | } |
| 927 | |
| 928 | #[tokio::test] |
| 929 | async fn only_the_owner_may_comment_close_or_edit() { |
| 930 | let f = fixture().await; |
| 931 | open(&f, "steid", "Blame is slow").await; |
| 932 | |
| 933 | for actor in [&f.member, &f.stranger] { |
| 934 | assert!(is_forbidden( |
| 935 | &comment(&f, actor, 1, "hello").await.expect_err("refuse") |
| 936 | )); |
| 937 | |
| 938 | let error = set_issue_state( |
| 939 | actor, |
| 940 | &f.handle, |
| 941 | &repo("steid"), |
| 942 | IssueNumber::from_trusted(1), |
| 943 | IssueState::Closed, |
| 944 | at(3_000), |
| 945 | &f.orgs, |
| 946 | &f.memberships, |
| 947 | &f.repos, |
| 948 | &f.issues, |
| 949 | ) |
| 950 | .await |
| 951 | .expect_err("should refuse"); |
| 952 | assert!(is_forbidden(&error)); |
| 953 | |
| 954 | let error = edit_issue( |
| 955 | actor, |
| 956 | &f.handle, |
| 957 | &repo("steid"), |
| 958 | IssueNumber::from_trusted(1), |
| 959 | &IssueEdit { |
| 960 | title: "hijacked".to_owned(), |
| 961 | body: String::new(), |
| 962 | }, |
| 963 | at(3_000), |
| 964 | &f.orgs, |
| 965 | &f.memberships, |
| 966 | &f.repos, |
| 967 | &f.issues, |
| 968 | ) |
| 969 | .await |
| 970 | .expect_err("should refuse"); |
| 971 | assert!(is_forbidden(&error)); |
| 972 | } |
| 973 | |
| 974 | let issue = detail(&f, &f.owner, "steid", 1).await.expect("visible"); |
| 975 | assert_eq!(issue.title, "Blame is slow"); |
| 976 | assert_eq!(issue.state, IssueState::Open); |
| 977 | assert!(issue.comments.is_empty()); |
| 978 | } |
| 979 | |
| 980 | #[tokio::test] |
| 981 | async fn a_write_on_an_invisible_repository_is_not_found_rather_than_forbidden() { |
| 982 | |
| 983 | let f = fixture().await; |
| 984 | |
| 985 | let error = create_issue( |
| 986 | &f.stranger, |
| 987 | &f.handle, |
| 988 | &repo("secret"), |
| 989 | &NewIssue { |
| 990 | title: "prying".to_owned(), |
| 991 | body: String::new(), |
| 992 | }, |
| 993 | at(2_000), |
| 994 | &f.orgs, |
| 995 | &f.memberships, |
| 996 | &f.repos, |
| 997 | &f.issues, |
| 998 | ) |
| 999 | .await |
| 1000 | .expect_err("should refuse"); |
| 1001 | |
| 1002 | assert!(is_not_found(&error)); |
| 1003 | } |
| 1004 | |
| 1005 | #[tokio::test] |
| 1006 | async fn a_repository_that_does_not_exist_answers_the_same_way_throughout() { |
| 1007 | let f = fixture().await; |
| 1008 | |
| 1009 | assert!( |
| 1010 | listed(&f, &f.owner, "nothing", IssueState::Open) |
| 1011 | .await |
| 1012 | .is_none() |
| 1013 | ); |
| 1014 | assert!(detail(&f, &f.owner, "nothing", 1).await.is_none()); |
| 1015 | |
| 1016 | let error = create_issue( |
| 1017 | &f.owner, |
| 1018 | &f.handle, |
| 1019 | &repo("nothing"), |
| 1020 | &NewIssue { |
| 1021 | title: "nowhere".to_owned(), |
| 1022 | body: String::new(), |
| 1023 | }, |
| 1024 | at(2_000), |
| 1025 | &f.orgs, |
| 1026 | &f.memberships, |
| 1027 | &f.repos, |
| 1028 | &f.issues, |
| 1029 | ) |
| 1030 | .await |
| 1031 | .expect_err("should refuse"); |
| 1032 | |
| 1033 | assert!(is_not_found(&error)); |
| 1034 | } |
| 1035 | } |