Skip to content

Commit

Permalink
Addressing comments pt 3
Browse files Browse the repository at this point in the history
  • Loading branch information
denisonbarbosa committed Aug 29, 2023
1 parent 97cdc33 commit 89241af
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 27 deletions.
6 changes: 3 additions & 3 deletions internal/brokers/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ 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.layoutValidators = generateValidators(ctx, supportedUILayouts)
b.layoutValidators = generateValidators(ctx, sessionID, supportedUILayouts)
authenticationModes, err = b.brokerer.GetAuthenticationModes(ctx, sessionID, supportedUILayouts)
if err != nil {
return nil, err
Expand Down Expand Up @@ -187,11 +187,11 @@ func (b Broker) cancelIsAuthorized(ctx context.Context, sessionID string) {
// }
// }
// }
func generateValidators(ctx context.Context, supportedUILayouts []map[string]string) map[string]map[string]fieldValidator {
func generateValidators(ctx context.Context, sessionID string, supportedUILayouts []map[string]string) map[string]map[string]fieldValidator {
validators := make(map[string]map[string]fieldValidator)
for _, layout := range supportedUILayouts {
if _, exists := layout["type"]; !exists {
log.Errorf(ctx, "layout %v provided with missing type, it will be ignored", layout)
log.Errorf(ctx, "layout %v provided with missing type for session %s, it will be ignored", layout, sessionID)
continue
}

Expand Down
15 changes: 8 additions & 7 deletions internal/brokers/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
var supportedLayouts = map[string]map[string]string{
"required-value": {
"type": "required-value",
"value": "required:value_type, other_value_type",
"value": "required:value_type,other_value_type",
},
"optional-value": {
"type": "optional-value",
"value": "optional:value_type, other_value_type",
"value": "optional:value_type,other_value_type",
},
"missing-type": {
"value": "required:missing_type",
Expand Down Expand Up @@ -95,8 +95,9 @@ func TestGetAuthenticationModes(t *testing.T) {

wantErr bool
}{
"Get authentication modes and generate validators": {sessionID: "success", supportedUILayouts: []string{"required-value", "optional-value"}},
"Get authentication modes and ignores invalid UI layout": {sessionID: "success", supportedUILayouts: []string{"required-value", "missing-type"}},
"Get authentication modes and generate validators": {sessionID: "success", supportedUILayouts: []string{"required-value", "optional-value"}},
"Get authentication modes and ignores invalid UI layout": {sessionID: "success", supportedUILayouts: []string{"required-value", "missing-type"}},
"Get multiple authentication modes and generate validators": {sessionID: "GAM_multiple_modes", supportedUILayouts: []string{"required-value", "optional-value"}},

"Does not error out when no authentication modes are returned": {sessionID: "GAM_empty"},

Expand All @@ -115,7 +116,6 @@ func TestGetAuthenticationModes(t *testing.T) {
tc.supportedUILayouts = []string{"required-value"}
}

// This is normally done in the broker's GetAuthenticationModes method, but we need to do it here to test the SelectAuthenticationMode method.
var supportedUILayouts []map[string]string
for _, layout := range tc.supportedUILayouts {
supportedUILayouts = append(supportedUILayouts, supportedLayouts[layout])
Expand Down Expand Up @@ -156,6 +156,7 @@ func TestSelectAuthenticationMode(t *testing.T) {

/* Layout errors */
"Error when returns no layout": {sessionID: "SAM_no_layout", wantErr: true},
"Error when returns empty layout": {sessionID: "SAM_empty_layout", wantErr: true},
"Error when returns layout with no type": {sessionID: "SAM_no_layout_type", wantErr: true},
"Error when returns layout with invalid type": {sessionID: "SAM_invalid_layout_type", wantErr: true},
"Error when returns layout without required value": {sessionID: "SAM_missing_required_value", wantErr: true},
Expand All @@ -173,12 +174,12 @@ func TestSelectAuthenticationMode(t *testing.T) {
tc.supportedUILayouts = []string{"required-value"}
}

// This is normally done in the broker's GetAuthenticationModes method, but we need to do it here to test the SelectAuthenticationMode method.
var supportedUILayouts []map[string]string
for _, layout := range tc.supportedUILayouts {
supportedUILayouts = append(supportedUILayouts, supportedLayouts[layout])
}
brokers.GenerateLayoutValidators(&b, supportedUILayouts)
// This is normally done in the broker's GetAuthenticationModes method, but we need to do it here to test the SelectAuthenticationMode method.
brokers.GenerateLayoutValidators(&b, tc.sessionID, supportedUILayouts)

gotUI, err := b.SelectAuthenticationMode(context.Background(), tc.sessionID, "mode1")
if tc.wantErr {
Expand Down
32 changes: 26 additions & 6 deletions internal/brokers/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package brokers
import (
"context"
"fmt"
"sort"

"github.com/godbus/dbus/v5"
)
Expand All @@ -29,17 +30,36 @@ func (m *Manager) SetBrokerForSession(b *Broker, sessionID string) {
}

// GenerateLayoutValidators generates the layout validators and assign them to the specified broker.
func GenerateLayoutValidators(b *Broker, supportedUILayouts []map[string]string) {
b.layoutValidators = generateValidators(context.Background(), supportedUILayouts)
func GenerateLayoutValidators(b *Broker, sessionID string, supportedUILayouts []map[string]string) {
b.layoutValidators = generateValidators(context.Background(), sessionID, supportedUILayouts)
}

// LayoutValidatorsString returns a string representation of the layout validators.
func (b *Broker) LayoutValidatorsString() string {
// Gets the map keys and sort them
var keys []string
for k := range b.layoutValidators {
keys = append(keys, k)
}
sort.Strings(keys)

var s string
for t, v := range b.layoutValidators {
layoutStr := fmt.Sprintf("\t%s:\n", t)
for field, validator := range v {
layoutStr += fmt.Sprintf("\t\t%s: { required: %v, supportedValues: %v }\n", field, validator.required, validator.supportedValues)
for _, k := range keys {
layoutStr := fmt.Sprintf("\t%s:\n", k)

validator := b.layoutValidators[k]

// Same thing for sorting the keys of the validator map
var vKeys []string
for v := range validator {
vKeys = append(vKeys, v)
}
sort.Strings(vKeys)

for _, v := range vKeys {
layoutStr += fmt.Sprintf("\t\t%s: { required: %v, supportedValues: %v }\n", v, validator[v].required, validator[v].supportedValues)
}

s += layoutStr
}
return s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ MODES:

VALIDATORS:
required-value:
value: { required: true, supportedValues: [value_type other_value_type] }
value: { required: true, supportedValues: [value_type other_value_type] }
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ MODES:
[{"id":"mode1","label":"Mode 1"}]

VALIDATORS:
required-value:
value: { required: true, supportedValues: [value_type other_value_type] }
optional-value:
value: { required: false, supportedValues: [value_type other_value_type] }
value: { required: false, supportedValues: [value_type other_value_type] }
required-value:
value: { required: true, supportedValues: [value_type other_value_type] }
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ MODES:

VALIDATORS:
required-value:
value: { required: true, supportedValues: [value_type other_value_type] }
value: { required: true, supportedValues: [value_type other_value_type] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
MODES:
[{"id":"mode1","label":"Mode 1"},{"id":"mode2","label":"Mode 2"}]

VALIDATORS:
optional-value:
value: { required: false, supportedValues: [value_type other_value_type] }
required-value:
value: { required: true, supportedValues: [value_type other_value_type] }
17 changes: 11 additions & 6 deletions internal/testutils/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,20 @@ func (b *BrokerBusMock) GetAuthenticationModes(sessionID string, supportedUILayo
return []map[string]string{
{"invalid": "invalid"},
}, nil

case "GAM_empty":
return nil, nil

case "GAM_error":
return nil, dbus.MakeFailedError(fmt.Errorf("Broker %q: GetAuthenticationModes errored out", b.name))
case "GAM_multiple_modes":
return []map[string]string{
{"id": "mode1", "label": "Mode 1"},
{"id": "mode2", "label": "Mode 2"},
}, nil
default:
return []map[string]string{
{"id": "mode1", "label": "Mode 1"},
}, nil
}

return []map[string]string{
{"id": "mode1", "label": "Mode 1"},
}, nil
}

// SelectAuthenticationMode returns default values to be used in tests or an error if requested.
Expand Down Expand Up @@ -171,6 +174,8 @@ func (b *BrokerBusMock) SelectAuthenticationMode(sessionID, authenticationModeNa
return nil, dbus.MakeFailedError(fmt.Errorf("Broker %q: SelectAuthenticationMode errored out", b.name))
case "SAM_no_layout":
return nil, nil
case "SAM_empty_layout":
return map[string]string{}, nil
}
// Should never get here
return map[string]string{}, nil
Expand Down

0 comments on commit 89241af

Please sign in to comment.