| 1 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | package orgs_test |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/tenseleyFlow/shithub/internal/orgs" |
| 11 | ) |
| 12 | |
| 13 | func TestCreateTeam_HappyPath(t *testing.T) { |
| 14 | _, deps, alice := setup(t) |
| 15 | ctx := context.Background() |
| 16 | org, _ := orgs.Create(ctx, deps, orgs.CreateParams{Slug: "acme", CreatedByUserID: alice}) |
| 17 | team, err := orgs.CreateTeam(ctx, deps, orgs.CreateTeamParams{ |
| 18 | OrgID: org.ID, Slug: "engineering", DisplayName: "Engineering", |
| 19 | CreatedByUserID: alice, |
| 20 | }) |
| 21 | if err != nil { |
| 22 | t.Fatalf("CreateTeam: %v", err) |
| 23 | } |
| 24 | if string(team.Slug) != "engineering" { |
| 25 | t.Fatalf("slug: want engineering, got %q", team.Slug) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func TestCreateTeam_RejectsInvalidSlug(t *testing.T) { |
| 30 | _, deps, alice := setup(t) |
| 31 | ctx := context.Background() |
| 32 | org, _ := orgs.Create(ctx, deps, orgs.CreateParams{Slug: "acme", CreatedByUserID: alice}) |
| 33 | _, err := orgs.CreateTeam(ctx, deps, orgs.CreateTeamParams{ |
| 34 | OrgID: org.ID, Slug: "BAD SLUG", CreatedByUserID: alice, |
| 35 | }) |
| 36 | if !errors.Is(err, orgs.ErrTeamSlugInvalid) { |
| 37 | t.Fatalf("want ErrTeamSlugInvalid, got %v", err) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestCreateTeam_RejectsDuplicateSlug(t *testing.T) { |
| 42 | _, deps, alice := setup(t) |
| 43 | ctx := context.Background() |
| 44 | org, _ := orgs.Create(ctx, deps, orgs.CreateParams{Slug: "acme", CreatedByUserID: alice}) |
| 45 | if _, err := orgs.CreateTeam(ctx, deps, orgs.CreateTeamParams{ |
| 46 | OrgID: org.ID, Slug: "eng", CreatedByUserID: alice, |
| 47 | }); err != nil { |
| 48 | t.Fatalf("first: %v", err) |
| 49 | } |
| 50 | _, err := orgs.CreateTeam(ctx, deps, orgs.CreateTeamParams{ |
| 51 | OrgID: org.ID, Slug: "eng", CreatedByUserID: alice, |
| 52 | }) |
| 53 | if !errors.Is(err, orgs.ErrTeamSlugTaken) { |
| 54 | t.Fatalf("want ErrTeamSlugTaken, got %v", err) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // One-level-deep nesting: creating a team with a parent that already |
| 59 | // has a parent must be refused at the trigger. |
| 60 | func TestCreateTeam_RejectsTwoLevelNesting(t *testing.T) { |
| 61 | _, deps, alice := setup(t) |
| 62 | ctx := context.Background() |
| 63 | org, _ := orgs.Create(ctx, deps, orgs.CreateParams{Slug: "acme", CreatedByUserID: alice}) |
| 64 | gp, err := orgs.CreateTeam(ctx, deps, orgs.CreateTeamParams{ |
| 65 | OrgID: org.ID, Slug: "g", CreatedByUserID: alice, |
| 66 | }) |
| 67 | if err != nil { |
| 68 | t.Fatalf("create grandparent: %v", err) |
| 69 | } |
| 70 | parent, err := orgs.CreateTeam(ctx, deps, orgs.CreateTeamParams{ |
| 71 | OrgID: org.ID, Slug: "p", ParentTeamID: gp.ID, CreatedByUserID: alice, |
| 72 | }) |
| 73 | if err != nil { |
| 74 | t.Fatalf("create parent: %v", err) |
| 75 | } |
| 76 | _, err = orgs.CreateTeam(ctx, deps, orgs.CreateTeamParams{ |
| 77 | OrgID: org.ID, Slug: "c", ParentTeamID: parent.ID, CreatedByUserID: alice, |
| 78 | }) |
| 79 | if !errors.Is(err, orgs.ErrTeamNestingTooDeep) { |
| 80 | t.Fatalf("want ErrTeamNestingTooDeep, got %v", err) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestSetTeamParent_RejectsSelfParent(t *testing.T) { |
| 85 | _, deps, alice := setup(t) |
| 86 | ctx := context.Background() |
| 87 | org, _ := orgs.Create(ctx, deps, orgs.CreateParams{Slug: "acme", CreatedByUserID: alice}) |
| 88 | team, _ := orgs.CreateTeam(ctx, deps, orgs.CreateTeamParams{ |
| 89 | OrgID: org.ID, Slug: "eng", CreatedByUserID: alice, |
| 90 | }) |
| 91 | err := orgs.SetTeamParent(ctx, deps, team.ID, team.ID) |
| 92 | if !errors.Is(err, orgs.ErrTeamSelfParent) { |
| 93 | t.Fatalf("want ErrTeamSelfParent, got %v", err) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func TestCreateTeam_RejectsCrossOrgParent(t *testing.T) { |
| 98 | pool, deps, alice := setup(t) |
| 99 | ctx := context.Background() |
| 100 | bob := mustUser(t, pool, "bob") |
| 101 | org1, _ := orgs.Create(ctx, deps, orgs.CreateParams{Slug: "acme", CreatedByUserID: alice}) |
| 102 | org2, _ := orgs.Create(ctx, deps, orgs.CreateParams{Slug: "beta", CreatedByUserID: bob}) |
| 103 | parent, _ := orgs.CreateTeam(ctx, deps, orgs.CreateTeamParams{ |
| 104 | OrgID: org2.ID, Slug: "g", CreatedByUserID: bob, |
| 105 | }) |
| 106 | _, err := orgs.CreateTeam(ctx, deps, orgs.CreateTeamParams{ |
| 107 | OrgID: org1.ID, Slug: "c", ParentTeamID: parent.ID, CreatedByUserID: alice, |
| 108 | }) |
| 109 | if !errors.Is(err, orgs.ErrTeamCrossOrgParent) { |
| 110 | t.Fatalf("want ErrTeamCrossOrgParent, got %v", err) |
| 111 | } |
| 112 | } |
| 113 |