package store import ( "context" "errors" "path/filepath" "testing" ) func tenancyStore(t *testing.T) *DB { t.Helper() s, err := OpenSQLite(context.Background(), filepath.Join(t.TempDir(), "ten.db")) if err != nil { t.Fatal(err) } t.Cleanup(func() { _ = s.Close() }) return s } func TestTwoOrganizationsCanBothHaveAPlatformTeam(t *testing.T) { s := tenancyStore(t) a, err := s.CreateOrg(t.Context(), "Acme") if err != nil { t.Fatal(err) } b, err := s.CreateOrg(t.Context(), "Scania") if err != nil { t.Fatal(err) } ta, err := s.CreateTeam(t.Context(), a, "platform") if err != nil { t.Fatal(err) } tb, err := s.CreateTeam(t.Context(), b, "platform") if err != nil { t.Fatal(err) } // Ids were slugs of the name in one flat namespace, and PutTeam upserts on // id — so the second of these moved the first organization's team into the // second. Nothing failed; the team changed owner. if ta == tb { t.Fatal("both teams got the same id, so one overwrote the other") } teams, err := s.AllTeams(t.Context()) if err != nil { t.Fatal(err) } if len(teams) != 2 { t.Fatalf("expected a team in each organization, got %+v", teams) } for _, team := range teams { if team.OrgName == "" { t.Errorf("a team should come back with its organization: %+v", team) } } } func TestTheSameNameTwiceInOneOrganizationIsRefused(t *testing.T) { s := tenancyStore(t) org, _ := s.CreateOrg(t.Context(), "Acme") if _, err := s.CreateTeam(t.Context(), org, "platform"); err != nil { t.Fatal(err) } // Two identically named teams under one organization is somebody making a // mistake in a form, not a script being re-run. _, err := s.CreateTeam(t.Context(), org, "platform") if !errors.Is(err, ErrDuplicateName) { t.Errorf("expected a duplicate to be refused, got %v", err) } } func TestADuplicateOrganizationIsRefused(t *testing.T) { s := tenancyStore(t) if _, err := s.CreateOrg(t.Context(), "Acme"); err != nil { t.Fatal(err) } if _, err := s.CreateOrg(t.Context(), "Acme"); !errors.Is(err, ErrDuplicateName) { t.Errorf("expected a duplicate organization to be refused, got %v", err) } } func TestAProjectNeedsATeamAndANameThatIsNotBlank(t *testing.T) { s := tenancyStore(t) if _, err := s.CreateProject(t.Context(), "", "gateway"); err == nil { t.Error("a project with no team should be refused") } org, _ := s.CreateOrg(t.Context(), "Acme") team, _ := s.CreateTeam(t.Context(), org, "platform") // Whitespace is not a name; it would produce a row nobody can point at. if _, err := s.CreateProject(t.Context(), team, " "); err == nil { t.Error("a blank name should be refused") } } func TestANewProjectShowsItsWholePath(t *testing.T) { s := tenancyStore(t) org, _ := s.CreateOrg(t.Context(), "Acme") team, _ := s.CreateTeam(t.Context(), org, "platform") if _, err := s.CreateProject(t.Context(), team, "gateway"); err != nil { t.Fatal(err) } paths, err := s.AllProjects(t.Context()) if err != nil { t.Fatal(err) } // The chooser on the approval form shows this string, and an id alone // would make two projects named "gateway" indistinguishable. if len(paths) != 1 || paths[0].Path != "Acme / platform / gateway" { t.Errorf("expected the full path, got %+v", paths) } } func TestIdsAreNotDerivedFromTheName(t *testing.T) { s := tenancyStore(t) id, err := s.CreateOrg(t.Context(), "Acme") if err != nil { t.Fatal(err) } // A renamed organization keeping an id that spells its old name is the // smaller half; the real reason is that a slug of a name is not unique and // these are primary keys. if id == "acme" || id == "Acme" { t.Errorf("the id should not be the name; got %q", id) } if len(id) < 8 { t.Errorf("the id looks too short to be random: %q", id) } }