From 98158f229f6e5ef0c610f29aa1aa3591d3b6eec5 Mon Sep 17 00:00:00 2001 From: Femi Novia Lina Date: Tue, 1 Oct 2024 17:31:32 +0700 Subject: [PATCH] test: add test --- internal/store/postgres/postgres_test.go | 32 ++++++ .../store/postgres/rule_repository_test.go | 100 ++++++++++++++++++ .../postgres/testdata/mock-rule-config.json | 23 ++++ 3 files changed, 155 insertions(+) create mode 100644 internal/store/postgres/rule_repository_test.go create mode 100644 internal/store/postgres/testdata/mock-rule-config.json diff --git a/internal/store/postgres/postgres_test.go b/internal/store/postgres/postgres_test.go index e24df0055..2ea71b43b 100644 --- a/internal/store/postgres/postgres_test.go +++ b/internal/store/postgres/postgres_test.go @@ -23,6 +23,7 @@ import ( "github.com/goto/shield/core/relation" "github.com/goto/shield/core/resource" "github.com/goto/shield/core/role" + "github.com/goto/shield/core/rule" "github.com/goto/shield/core/servicedata" "github.com/goto/shield/core/user" "github.com/goto/shield/internal/schema" @@ -601,3 +602,34 @@ func bootstrapResourceConfig(client *db.Client) ([]resource.ResourceConfig, erro return insertedData, nil } + +func bootstrapRuleConfig(client *db.Client) ([]rule.RuleConfig, error) { + ruleRepository := postgres.NewRuleRepository(client) + + testFixtureJSON, err := os.ReadFile("./testdata/mock-rule-config.json") + if err != nil { + return nil, err + } + + type ruleConfig struct { + Name string `json:"name"` + Config rule.Ruleset `json:"config"` + } + + var data []ruleConfig + if err = json.Unmarshal(testFixtureJSON, &data); err != nil { + return nil, err + } + + var insertedData []rule.RuleConfig + for _, d := range data { + data, err := ruleRepository.Upsert(context.Background(), d.Name, d.Config) + if err != nil { + return nil, err + } + + insertedData = append(insertedData, data) + } + + return insertedData, nil +} diff --git a/internal/store/postgres/rule_repository_test.go b/internal/store/postgres/rule_repository_test.go new file mode 100644 index 000000000..bcf4696e3 --- /dev/null +++ b/internal/store/postgres/rule_repository_test.go @@ -0,0 +1,100 @@ +package postgres_test + +import ( + "context" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/goto/salt/log" + "github.com/goto/shield/core/rule" + "github.com/goto/shield/internal/store/postgres" + "github.com/goto/shield/pkg/db" + "github.com/ory/dockertest" + "github.com/stretchr/testify/suite" +) + +type RuleRepositoryTestSuite struct { + suite.Suite + ctx context.Context + client *db.Client + pool *dockertest.Pool + resource *dockertest.Resource + repository *postgres.RuleRepository + ruleConfig []rule.RuleConfig +} + +func (s *RuleRepositoryTestSuite) SetupSuite() { + var err error + + logger := log.NewZap() + s.client, s.pool, s.resource, err = newTestClient(logger) + if err != nil { + s.T().Fatal(err) + } + + s.ctx = context.TODO() + s.repository = postgres.NewRuleRepository(s.client) + + s.ruleConfig, err = bootstrapRuleConfig(s.client) + if err != nil { + s.T().Fatal(err) + } +} + +func (s *RuleRepositoryTestSuite) TestUpsert() { + type testCase struct { + Description string + Name string + Config rule.Ruleset + Expected rule.RuleConfig + ErrString string + } + + testCases := []testCase{ + { + Description: "should create a resource config", + Name: "test", + Config: rule.Ruleset{ + Rules: []rule.Rule{rule.Rule{}}, + }, + Expected: rule.RuleConfig{ + ID: 2, + Name: "test", + Config: "{\"Rules\": [{\"Hooks\": null, \"Backend\": {\"URL\": \"\", \"Prefix\": \"\", \"Namespace\": \"\"}, \"Frontend\": {\"URL\": \"\", \"URLRx\": null, \"Method\": \"\"}, \"Middlewares\": null}]}", + }, + }, + { + Description: "should update a resource config", + Name: s.ruleConfig[0].Name, + Config: rule.Ruleset{ + Rules: []rule.Rule{rule.Rule{}}, + }, + Expected: rule.RuleConfig{ + ID: s.ruleConfig[0].ID, + Name: s.ruleConfig[0].Name, + Config: "{\"Rules\": [{\"Hooks\": null, \"Backend\": {\"URL\": \"\", \"Prefix\": \"\", \"Namespace\": \"\"}, \"Frontend\": {\"URL\": \"\", \"URLRx\": null, \"Method\": \"\"}, \"Middlewares\": null}]}", + }, + }, + } + + for _, tc := range testCases { + s.Run(tc.Description, func() { + got, err := s.repository.Upsert(s.ctx, tc.Name, tc.Config) + if tc.ErrString != "" { + if err.Error() != tc.ErrString { + s.T().Fatalf("got error %s, expected was %s", err.Error(), tc.ErrString) + } + } + if !cmp.Equal(got, tc.Expected, cmpopts.IgnoreFields(rule.RuleConfig{}, + "CreatedAt", + "UpdatedAt")) { + s.T().Fatalf("got result %+v, expected was %+v", got, tc.Expected) + } + }) + } +} + +func TestRuleRepository(t *testing.T) { + suite.Run(t, new(RuleRepositoryTestSuite)) +} diff --git a/internal/store/postgres/testdata/mock-rule-config.json b/internal/store/postgres/testdata/mock-rule-config.json new file mode 100644 index 000000000..8d3f09503 --- /dev/null +++ b/internal/store/postgres/testdata/mock-rule-config.json @@ -0,0 +1,23 @@ +[ + { + "name": "entropy", + "config": { + "Rules": [ + { + "Hooks": [], + "Backend": { + "URL": "http://localhost:8080", + "Prefix": "", + "Namespace": "entropy" + }, + "Frontend": { + "URL": "/api/ping", + "URLRx": null, + "Method": "GET" + }, + "Middlewares": [] + } + ] + } + } +] \ No newline at end of file