Skip to content
Snippets Groups Projects
Commit 8a28db89 authored by Yordan Kinkov's avatar Yordan Kinkov
Browse files

Merge branch '45-remove-duplicate-types-GetLoginProofInvitation' into 'main'

Remove duplicate types in GetLoginProogInvitation ext function

Closes #45

See merge request !41
parents 8b61235c 00dbbd82
No related branches found
No related tags found
1 merge request!41Remove duplicate types in GetLoginProogInvitation ext function
Pipeline #53428 passed
...@@ -38,12 +38,20 @@ func (of *OcmFuncs) GetLoginProofInvitation() (*rego.Function, rego.Builtin2) { ...@@ -38,12 +38,20 @@ func (of *OcmFuncs) GetLoginProofInvitation() (*rego.Function, rego.Builtin2) {
} }
var credTypes []string var credTypes []string
distinctTypes := make(map[string]bool, len(scopeToType))
for _, scope := range scopes { for _, scope := range scopes {
credType, ok := scopeToType[scope] credType, ok := scopeToType[scope]
if !ok { if !ok {
return nil, fmt.Errorf("scope not found in scope to type map: %s", scope) return nil, fmt.Errorf("scope not found in scope to type map: %s", scope)
} }
credTypes = append(credTypes, credType) if credType != "" && !distinctTypes[credType] {
credTypes = append(credTypes, credType)
}
distinctTypes[credType] = true
}
if len(credTypes) == 0 {
return nil, fmt.Errorf("no credential types found in the scope to type map: %s", scopeToType)
} }
res, err := of.client.GetLoginProofInvitation(bctx.Context, credTypes) res, err := of.client.GetLoginProofInvitation(bctx.Context, credTypes)
......
...@@ -44,6 +44,20 @@ func TestGetLoginProofInvitationSuccess(t *testing.T) { ...@@ -44,6 +44,20 @@ func TestGetLoginProofInvitationSuccess(t *testing.T) {
resultBytes, err := json.Marshal(resultSet[0].Expressions[0].Value) resultBytes, err := json.Marshal(resultSet[0].Expressions[0].Value)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expected, string(resultBytes)) assert.Equal(t, expected, string(resultBytes))
// "scope to credential type" map with duplicate and empty credential types
r = rego.New(
rego.Query(`ocm.getLoginProofInvitation(["openid", "profile", "email"], {"openid": "credType1", "profile": "credType1", "email": ""})`),
rego.Function2(ocmFuncs.GetLoginProofInvitation()),
rego.StrictBuiltinErrors(true),
)
resultSet, err = r.Eval(context.Background())
assert.NoError(t, err)
resultBytes, err = json.Marshal(resultSet[0].Expressions[0].Value)
assert.NoError(t, err)
assert.Equal(t, expected, string(resultBytes))
} }
func TestGetLoginProofInvitationErr(t *testing.T) { func TestGetLoginProofInvitationErr(t *testing.T) {
...@@ -77,6 +91,19 @@ func TestGetLoginProofInvitationErr(t *testing.T) { ...@@ -77,6 +91,19 @@ func TestGetLoginProofInvitationErr(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
assert.Empty(t, resultSet) assert.Empty(t, resultSet)
assert.Contains(t, err.Error(), "invalid scope to credential type map") assert.Contains(t, err.Error(), "invalid scope to credential type map")
// empty types in "scope to credential type" map
r = rego.New(
rego.Query(`ocm.getLoginProofInvitation(["openid", "profile"], {"openid": "", "profile": ""})`),
rego.Function2(ocmFuncs.GetLoginProofInvitation()),
rego.StrictBuiltinErrors(true),
)
resultSet, err = r.Eval(context.Background())
assert.Error(t, err)
assert.Empty(t, resultSet)
assert.Contains(t, err.Error(), "no credential types found in the scope to type map")
} }
func TestGetLoginProofResult(t *testing.T) { func TestGetLoginProofResult(t *testing.T) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment