Skip to content

Commit

Permalink
Fix add config test persona to dam failed when current config does no…
Browse files Browse the repository at this point in the history
…t have personas

close #37

PiperOrigin-RevId: 319974355
Change-Id: Ie4b5b0b61fe1892a2a7390d9fb6bcb13e8c4a2f5
  • Loading branch information
chaopeng authored and copybara-github committed Jul 7, 2020
1 parent b5aae7b commit eb19f97
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/dam/dam.go
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,9 @@ func normalizeConfig(cfg *pb.DamConfig) error {
if cfg.Clients == nil {
cfg.Clients = make(map[string]*cpb.Client)
}
if cfg.TestPersonas == nil {
cfg.TestPersonas = make(map[string]*cpb.TestPersona)
}
for _, p := range cfg.TestPersonas {
sort.Strings(p.Access)
}
Expand Down
45 changes: 45 additions & 0 deletions lib/dam/dam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,51 @@ func TestMinConfig(t *testing.T) {
test.HandlerTests(t, s.Handler, tests, hydraPublicURL, server.Config())
}

func TestConfig_Add_NilTestPersonas(t *testing.T) {
store := storage.NewMemoryStorage("dam", "testdata/config")
wh := clouds.NewMockTokenCreator(false)
broker, err := persona.NewBroker(hydraPublicURL, &testkeys.PersonaBrokerKey, "dam", "testdata/config", false)
if err != nil {
t.Fatalf("NewBroker() failed: %v", err)
}
awsClient := aws.NewMockAPIClient("123456", "dam-user-id")
s := NewService(&Options{
HTTPClient: httptestclient.New(broker.Handler),
Domain: "test.org",
ServiceName: "dam",
DefaultBroker: testBroker,
Store: store,
Warehouse: wh,
AWSClient: awsClient,
UseHydra: useHydra,
HydraAdminURL: hydraAdminURL,
HydraPublicURL: hydraPublicURL,
HydraSyncFreq: time.Nanosecond,
Encryption: fakeencryption.New(),
})

cfg, err := s.loadConfig(nil, storage.DefaultRealm)
if err != nil {
t.Fatalf("load config failed: %v", err)
}

copy := proto.Clone(cfg).(*pb.DamConfig)
copy.TestPersonas = nil

// Store invalid config to storage
if err := s.store.Write(storage.ConfigDatatype, storage.DefaultRealm, storage.DefaultUser, storage.DefaultID, storage.LatestRev, copy, nil); err != nil {
t.Fatalf("Write config failed: %v", err)
}

req := &pb.ConfigTestPersonaRequest{Item: cfg.TestPersonas["admin"]}

resp := damSendTestRequest(t, http.MethodPost, configTestPersonaPath, "admin", "test", "admin", test.TestClientID, test.TestClientSecret, req, s, broker)

if resp.StatusCode != http.StatusOK {
t.Errorf("Status = %d, wants %d", resp.StatusCode, http.StatusOK)
}
}

func TestConfig_Add_NilResource(t *testing.T) {
store := storage.NewMemoryStorage("dam", "testdata/config")
wh := clouds.NewMockTokenCreator(false)
Expand Down

0 comments on commit eb19f97

Please sign in to comment.