Skip to content

Commit

Permalink
Addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
denisonbarbosa committed Aug 28, 2023
1 parent edb9493 commit 8b896ea
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
17 changes: 12 additions & 5 deletions internal/brokers/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ func (b Broker) newSession(ctx context.Context, username, lang string) (sessionI
func (b Broker) GetAuthenticationModes(ctx context.Context, sessionID string, supportedUILayouts []map[string]string) (authenticationModes []map[string]string, err error) {
sessionID = b.parseSessionID(sessionID)

b.generateValidators(supportedUILayouts)
b.layoutValidators, err = generateValidators(supportedUILayouts)
if err != nil {
return nil, fmt.Errorf("could not generate layout validators: %w", err)
}

authenticationModes, err = b.brokerer.GetAuthenticationModes(ctx, sessionID, supportedUILayouts)
if err != nil {
Expand Down Expand Up @@ -188,19 +191,23 @@ func (b Broker) cancelIsAuthorized(ctx context.Context, sessionID string) {
// }
// }
// }
func (b *Broker) generateValidators(supportedUILayouts []map[string]string) {
func generateValidators(supportedUILayouts []map[string]string) (map[string]map[string]fieldValidator, error) {
validators := make(map[string]map[string]fieldValidator)
for _, layout := range supportedUILayouts {
if _, exists := layout["type"]; !exists {
return nil, fmt.Errorf("Supported UI layouts are invalid")
}

layoutValidator := make(map[string]fieldValidator)
for key, value := range layout {
if key == "type" {
continue
}

modifier, supportedValues, _ := strings.Cut(value, ":")
required, supportedValues, _ := strings.Cut(value, ":")
validator := fieldValidator{
supportedValues: nil,
required: (modifier == "required"),
required: (required == "required"),
}
if supportedValues != "" {
validator.supportedValues = strings.Split(supportedValues, ",")
Expand All @@ -209,7 +216,7 @@ func (b *Broker) generateValidators(supportedUILayouts []map[string]string) {
}
validators[layout["type"]] = layoutValidator
}
b.layoutValidators = validators
return validators, nil
}

// validateUILayout validates the layout fields and content according to the broker validators and returns the layout
Expand Down
5 changes: 4 additions & 1 deletion internal/brokers/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ func TestSelectAuthenticationMode(t *testing.T) {
t.Parallel()

b, _ := newBrokerForTests(t)
b.GenerateLayoutValidators(supportedLayouts)

// This is normally done in the broker's GetAuthenticationModes method, but we need to do it here to test the SelectAuthenticationMode method.
err := brokers.GenerateLayoutValidators(&b, supportedLayouts)
require.NoError(t, err, "Setup: could not generate layout validators")

gotUI, err := b.SelectAuthenticationMode(context.Background(), tc.sessionID, "mode1")
if tc.wantErr {
Expand Down
10 changes: 7 additions & 3 deletions internal/brokers/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ func (m *Manager) SetBrokerForSession(b *Broker, sessionID string) {
m.transactionsToBrokerMu.Unlock()
}

// GenerateLayoutValidators exports the private generateValidators function for testing purposes.
func (b *Broker) GenerateLayoutValidators(supportedUILayouts []map[string]string) {
b.generateValidators(supportedUILayouts)
// GenerateLayoutValidators generates the layout validators and assign them to the specified broker.
func GenerateLayoutValidators(b *Broker, supportedUILayouts []map[string]string) (err error) {
b.layoutValidators, err = generateValidators(supportedUILayouts)
if err != nil {
return err
}
return nil
}

0 comments on commit 8b896ea

Please sign in to comment.