|
| 1 | +package postgres_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "log/slog" |
| 8 | + "slices" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/ory/dockertest" |
| 12 | + "github.com/raystack/frontier/core/aggregates/userprojects" |
| 13 | + "github.com/raystack/frontier/internal/store/postgres" |
| 14 | + "github.com/raystack/frontier/pkg/db" |
| 15 | + "github.com/raystack/salt/rql" |
| 16 | + "github.com/stretchr/testify/suite" |
| 17 | +) |
| 18 | + |
| 19 | +// Runs the search against a real postgres to check that soft-deleted projects, |
| 20 | +// policies, and users stay out of the listing and out of the member arrays. |
| 21 | +type UserProjectsRepositoryPGTestSuite struct { |
| 22 | + suite.Suite |
| 23 | + ctx context.Context |
| 24 | + client *db.Client |
| 25 | + pool *dockertest.Pool |
| 26 | + resource *dockertest.Resource |
| 27 | + repository *postgres.UserProjectsRepository |
| 28 | + orgID string |
| 29 | +} |
| 30 | + |
| 31 | +func (s *UserProjectsRepositoryPGTestSuite) SetupSuite() { |
| 32 | + var err error |
| 33 | + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 34 | + s.client, s.pool, s.resource, err = newTestClient(logger) |
| 35 | + if err != nil { |
| 36 | + s.T().Fatal(err) |
| 37 | + } |
| 38 | + s.ctx = context.TODO() |
| 39 | + s.repository = postgres.NewUserProjectsRepository(s.client) |
| 40 | +} |
| 41 | + |
| 42 | +func (s *UserProjectsRepositoryPGTestSuite) TearDownSuite() { |
| 43 | + if err := purgeDocker(s.pool, s.resource); err != nil { |
| 44 | + s.T().Fatal(err) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func (s *UserProjectsRepositoryPGTestSuite) SetupTest() { |
| 49 | + s.exec(`INSERT INTO namespaces (name) VALUES ('app/project'), ('app/user') ON CONFLICT (name) DO NOTHING`) |
| 50 | + |
| 51 | + s.exec(`INSERT INTO organizations (name, title) VALUES ('up-org', 'User Projects Org')`) |
| 52 | + s.orgID = s.scalar(`SELECT id FROM organizations WHERE name = 'up-org'`) |
| 53 | + |
| 54 | + s.exec(`INSERT INTO projects (name, title, org_id) VALUES |
| 55 | + ('up-atlas', 'Atlas', $1), |
| 56 | + ('up-beacon', 'Beacon', $1), |
| 57 | + ('up-cargo', 'Cargo', $1)`, s.orgID) |
| 58 | + s.exec(`UPDATE projects SET deleted_at = now() WHERE name = 'up-beacon'`) |
| 59 | + |
| 60 | + s.exec(`INSERT INTO users (name, email, title, avatar) VALUES |
| 61 | + ('up-dana', 'up-dana@example.com', 'Dana', 'dana.png'), |
| 62 | + ('up-evan', 'up-evan@example.com', 'Evan', 'evan.png'), |
| 63 | + ('up-finn', 'up-finn@example.com', 'Finn', 'finn.png'), |
| 64 | + ('up-gale', 'up-gale@example.com', 'Gale', 'gale.png')`) |
| 65 | + s.exec(`UPDATE users SET deleted_at = now() WHERE name = 'up-finn'`) |
| 66 | + |
| 67 | + s.exec(`INSERT INTO roles (name, org_id) VALUES ('up-role', $1)`, s.orgID) |
| 68 | + |
| 69 | + for _, project := range []string{"up-atlas", "up-beacon", "up-cargo"} { |
| 70 | + s.policy(project, "up-dana") |
| 71 | + } |
| 72 | + s.policy("up-atlas", "up-evan") |
| 73 | + s.policy("up-atlas", "up-finn") |
| 74 | + s.policy("up-atlas", "up-gale") |
| 75 | + |
| 76 | + s.softDeletePolicy("up-cargo", "up-dana") |
| 77 | + s.softDeletePolicy("up-atlas", "up-gale") |
| 78 | +} |
| 79 | + |
| 80 | +func (s *UserProjectsRepositoryPGTestSuite) TearDownTest() { |
| 81 | + queries := []string{} |
| 82 | + for _, table := range []string{postgres.TABLE_POLICIES, postgres.TABLE_ROLES, postgres.TABLE_USERS, |
| 83 | + postgres.TABLE_PROJECTS, postgres.TABLE_ORGANIZATIONS, postgres.TABLE_NAMESPACES} { |
| 84 | + queries = append(queries, fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE", table)) |
| 85 | + } |
| 86 | + if err := execQueries(s.ctx, s.client, queries); err != nil { |
| 87 | + s.T().Fatal(err) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func (s *UserProjectsRepositoryPGTestSuite) exec(query string, args ...any) { |
| 92 | + s.T().Helper() |
| 93 | + execSQL(s.T(), s.ctx, s.client, query, args...) |
| 94 | +} |
| 95 | + |
| 96 | +func (s *UserProjectsRepositoryPGTestSuite) scalar(query string, args ...any) string { |
| 97 | + s.T().Helper() |
| 98 | + return scalarSQL(s.T(), s.ctx, s.client, query, args...) |
| 99 | +} |
| 100 | + |
| 101 | +func (s *UserProjectsRepositoryPGTestSuite) projectID(name string) string { |
| 102 | + s.T().Helper() |
| 103 | + return s.scalar(`SELECT id FROM projects WHERE name = $1`, name) |
| 104 | +} |
| 105 | + |
| 106 | +func (s *UserProjectsRepositoryPGTestSuite) userID(name string) string { |
| 107 | + s.T().Helper() |
| 108 | + return s.scalar(`SELECT id FROM users WHERE name = $1`, name) |
| 109 | +} |
| 110 | + |
| 111 | +func (s *UserProjectsRepositoryPGTestSuite) policy(projectName, userName string) { |
| 112 | + s.T().Helper() |
| 113 | + s.exec(`INSERT INTO policies (role_id, resource_id, resource_type, principal_id, principal_type) |
| 114 | + VALUES ((SELECT id FROM roles WHERE name = 'up-role'), $1, 'app/project', $2, 'app/user')`, |
| 115 | + s.projectID(projectName), s.userID(userName)) |
| 116 | +} |
| 117 | + |
| 118 | +func (s *UserProjectsRepositoryPGTestSuite) softDeletePolicy(projectName, userName string) { |
| 119 | + s.T().Helper() |
| 120 | + res := execSQL(s.T(), s.ctx, s.client, `UPDATE policies SET deleted_at = now() |
| 121 | + WHERE resource_id = $1 AND principal_id = $2`, s.projectID(projectName), s.userID(userName)) |
| 122 | + n, err := res.RowsAffected() |
| 123 | + s.Require().NoError(err) |
| 124 | + s.Require().EqualValues(1, n, "soft delete of %s for %s", projectName, userName) |
| 125 | +} |
| 126 | + |
| 127 | +func (s *UserProjectsRepositoryPGTestSuite) search(text string) []userprojects.AggregatedProject { |
| 128 | + s.T().Helper() |
| 129 | + res, err := s.repository.Search(s.ctx, s.userID("up-dana"), s.orgID, &rql.Query{Limit: 50, Search: text}) |
| 130 | + s.Require().NoError(err) |
| 131 | + return res.Projects |
| 132 | +} |
| 133 | + |
| 134 | +func (s *UserProjectsRepositoryPGTestSuite) names(projects []userprojects.AggregatedProject) []string { |
| 135 | + s.T().Helper() |
| 136 | + out := make([]string, 0, len(projects)) |
| 137 | + for _, p := range projects { |
| 138 | + out = append(out, p.ProjectName) |
| 139 | + } |
| 140 | + return out |
| 141 | +} |
| 142 | + |
| 143 | +func (s *UserProjectsRepositoryPGTestSuite) TestSkipsSoftDeletedProjectsAndPolicies() { |
| 144 | + s.Equal([]string{"up-atlas"}, s.names(s.search(""))) |
| 145 | +} |
| 146 | + |
| 147 | +func (s *UserProjectsRepositoryPGTestSuite) TestMembersComeOnlyFromLiveRows() { |
| 148 | + projects := s.search("") |
| 149 | + s.Require().Len(projects, 1) |
| 150 | + s.Equal(sortedIDs([]string{s.userID("up-dana"), s.userID("up-evan")}), projects[0].UserIDs) |
| 151 | + s.Equal([]string{"up-dana", "up-evan"}, projects[0].UserNames) |
| 152 | + s.Equal([]string{"Dana", "Evan"}, projects[0].UserTitles) |
| 153 | + s.Equal([]string{"dana.png", "evan.png"}, projects[0].UserAvatars) |
| 154 | +} |
| 155 | + |
| 156 | +func (s *UserProjectsRepositoryPGTestSuite) TestSearchMatchesLiveProjectsOnly() { |
| 157 | + s.Equal([]string{"up-atlas"}, s.names(s.search("atlas"))) |
| 158 | + s.Empty(s.names(s.search("beacon")), "the project is soft-deleted") |
| 159 | + s.Empty(s.names(s.search("cargo")), "the grant on it is soft-deleted") |
| 160 | +} |
| 161 | + |
| 162 | +func sortedIDs(ids []string) []string { |
| 163 | + out := append([]string(nil), ids...) |
| 164 | + slices.Sort(out) |
| 165 | + return out |
| 166 | +} |
| 167 | + |
| 168 | +func TestUserProjectsRepositoryPG(t *testing.T) { |
| 169 | + suite.Run(t, new(UserProjectsRepositoryPGTestSuite)) |
| 170 | +} |
0 commit comments