forked from raystack/frontier
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
180d2ab
commit 98158f2
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}}, | ||
}, | ||
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} | ||
] | ||
} | ||
} | ||
] |