Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
FemiNoviaLina committed Oct 1, 2024
1 parent 180d2ab commit 98158f2
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
32 changes: 32 additions & 0 deletions internal/store/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
100 changes: 100 additions & 0 deletions internal/store/postgres/rule_repository_test.go
Original file line number Diff line number Diff line change
@@ -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{}},

Check failure on line 59 in internal/store/postgres/rule_repository_test.go

View workflow job for this annotation

GitHub Actions / golangci

File is not `gofmt`-ed with `-s` (gofmt)
},
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))
}
23 changes: 23 additions & 0 deletions internal/store/postgres/testdata/mock-rule-config.json
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}
}
]

0 comments on commit 98158f2

Please sign in to comment.