|
| 1 | +package postgres_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/raystack/frontier/internal/store/postgres" |
| 9 | + "github.com/raystack/frontier/pkg/db" |
| 10 | + "github.com/raystack/salt/rql" |
| 11 | + "github.com/stretchr/testify/suite" |
| 12 | +) |
| 13 | + |
| 14 | +type OrgPATsRepositoryPGTestSuite struct { |
| 15 | + suite.Suite |
| 16 | + ctx context.Context |
| 17 | + client *db.Client |
| 18 | + repository *postgres.OrgPATsRepository |
| 19 | + orgID string |
| 20 | +} |
| 21 | + |
| 22 | +func (s *OrgPATsRepositoryPGTestSuite) SetupSuite() { |
| 23 | + var err error |
| 24 | + s.client, err = newTestClient() |
| 25 | + if err != nil { |
| 26 | + s.T().Fatal(err) |
| 27 | + } |
| 28 | + s.ctx = context.TODO() |
| 29 | + s.repository = postgres.NewOrgPATsRepository(s.client) |
| 30 | +} |
| 31 | + |
| 32 | +func (s *OrgPATsRepositoryPGTestSuite) TearDownSuite() { |
| 33 | + if err := closeTestClient(s.client); err != nil { |
| 34 | + s.T().Fatal(err) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func (s *OrgPATsRepositoryPGTestSuite) SetupTest() { |
| 39 | + execSQL(s.T(), s.ctx, s.client, `INSERT INTO organizations (name, title) VALUES ('op-org', 'Org PATs Org')`) |
| 40 | + s.orgID = scalarSQL(s.T(), s.ctx, s.client, `SELECT id FROM organizations WHERE name = 'op-org'`) |
| 41 | + |
| 42 | + execSQL(s.T(), s.ctx, s.client, `INSERT INTO users (name, email, title) VALUES |
| 43 | + ('op-uma', 'op-uma@example.com', 'Uma'), |
| 44 | + ('op-gary', 'op-gary@example.com', 'Gary')`) |
| 45 | + execSQL(s.T(), s.ctx, s.client, `INSERT INTO user_pats (user_id, org_id, title, secret_hash, expires_at) VALUES |
| 46 | + ((SELECT id FROM users WHERE name = 'op-uma'), $1, 'deploy-token', 'op-hash-uma', now() + interval '1 day'), |
| 47 | + ((SELECT id FROM users WHERE name = 'op-gary'), $1, 'backup-token', 'op-hash-gary', now() + interval '1 day')`, |
| 48 | + s.orgID) |
| 49 | + execSQL(s.T(), s.ctx, s.client, `UPDATE users SET deleted_at = now() WHERE name = 'op-gary'`) |
| 50 | +} |
| 51 | + |
| 52 | +func (s *OrgPATsRepositoryPGTestSuite) TearDownTest() { |
| 53 | + queries := []string{} |
| 54 | + for _, table := range []string{postgres.TABLE_USER_PATS, postgres.TABLE_USERS, postgres.TABLE_ORGANIZATIONS} { |
| 55 | + queries = append(queries, fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE", table)) |
| 56 | + } |
| 57 | + if err := execQueries(s.ctx, s.client, queries); err != nil { |
| 58 | + s.T().Fatal(err) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func (s *OrgPATsRepositoryPGTestSuite) search(text string) ([]string, int64) { |
| 63 | + s.T().Helper() |
| 64 | + res, err := s.repository.Search(s.ctx, s.orgID, &rql.Query{Limit: 50, Search: text}) |
| 65 | + s.Require().NoError(err) |
| 66 | + titles := make([]string, 0, len(res.PATs)) |
| 67 | + for _, pat := range res.PATs { |
| 68 | + titles = append(titles, pat.Title) |
| 69 | + } |
| 70 | + return titles, res.Pagination.TotalCount |
| 71 | +} |
| 72 | + |
| 73 | +func (s *OrgPATsRepositoryPGTestSuite) TestSkipsPATsOfSoftDeletedUsers() { |
| 74 | + titles, total := s.search("") |
| 75 | + s.Equal([]string{"deploy-token"}, titles) |
| 76 | + s.Equal(int64(1), total) |
| 77 | +} |
| 78 | + |
| 79 | +func (s *OrgPATsRepositoryPGTestSuite) TestSearchMatchesLiveOwnersOnly() { |
| 80 | + titles, _ := s.search("Uma") |
| 81 | + s.Equal([]string{"deploy-token"}, titles) |
| 82 | + |
| 83 | + titles, total := s.search("Gary") |
| 84 | + s.Empty(titles, "the owner is soft-deleted") |
| 85 | + s.Zero(total) |
| 86 | +} |
| 87 | + |
| 88 | +func TestOrgPATsRepositoryPG(t *testing.T) { |
| 89 | + suite.Run(t, new(OrgPATsRepositoryPGTestSuite)) |
| 90 | +} |
0 commit comments