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 with stages
in 1 minute and 27 seconds
......@@ -38,12 +38,20 @@ func (of *OcmFuncs) GetLoginProofInvitation() (*rego.Function, rego.Builtin2) {
}
var credTypes []string
distinctTypes := make(map[string]bool, len(scopeToType))
for _, scope := range scopes {
credType, ok := scopeToType[scope]
if !ok {
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)
......
......@@ -44,6 +44,20 @@ func TestGetLoginProofInvitationSuccess(t *testing.T) {
resultBytes, err := json.Marshal(resultSet[0].Expressions[0].Value)
assert.NoError(t, err)
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) {
......@@ -77,6 +91,19 @@ func TestGetLoginProofInvitationErr(t *testing.T) {
assert.Error(t, err)
assert.Empty(t, resultSet)
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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment