Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle instances where scope is returned as array in log entries #194

Merged
merged 3 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion management/log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package management

import (
"encoding/json"
"fmt"
"strings"
"time"
)

Expand Down Expand Up @@ -117,7 +120,7 @@ type Log struct {
Audience *string `json:"audience"`

// Scope permissions applied to the event.
Scope *string `json:"scope"`
Scope *string `json:"-"`

// Name of the strategy involved in the event.
Strategy *string `json:"strategy"`
Expand All @@ -143,6 +146,43 @@ func (l *Log) TypeName() string {
return ""
}

// UnmarshalJSON is a custom deserializer for the Log type.
//
// It is required due to differences in the scope field which cane be a string or array of strings.
func (l *Log) UnmarshalJSON(data []byte) error {
type log Log
type logWrapper struct {
*log
RawScope interface{} `json:"scope"`
}

alias := &logWrapper{(*log)(l), nil}

err := json.Unmarshal(data, alias)

if err != nil {
return err
}

if alias.RawScope != nil {
switch rawScope := alias.RawScope.(type) {
case []interface{}:
scopes := make([]string, len(rawScope))
for i, v := range rawScope {
scopes[i] = v.(string)
}
scope := strings.Join(scopes, " ")
l.Scope = &scope
case string:
l.Scope = &rawScope
default:
return fmt.Errorf("unexpected type for field scope: %T", alias.RawScope)
}
}

return nil
}

// LogManager manages Auth0 Log resources.
type LogManager struct {
*Management
Expand Down
29 changes: 29 additions & 0 deletions management/log_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package management

import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/assert"

"github.com/auth0/go-auth0"
)

const successfulAPIOperation = "sapi"
Expand Down Expand Up @@ -49,3 +52,29 @@ func TestLogManager_CheckpointPagination(t *testing.T) {
assert.Equal(t, log1.GetID(), logs[0].GetID())
assert.Equal(t, log2.GetID(), logs[1].GetID())
}

func TestLogManagerScope_UnmarshalJSON(t *testing.T) {
for expectedAsString, expected := range map[string]*Log{
`{}`: {},
`{"scope": "openid profile email"}`: {
Scope: auth0.String("openid profile email"),
},
`{"scope": ["openid", "profile", "email"]}`: {
Scope: auth0.String("openid profile email"),
},
`{"scope": []}`: {
Scope: auth0.String(""),
},
} {
var actual *Log
err := json.Unmarshal([]byte(expectedAsString), &actual)
assert.NoError(t, err)
assert.Equal(t, actual, expected)
}

t.Run("Throws an unexpected type error", func(t *testing.T) {
var actual *Log
err := json.Unmarshal([]byte(`{"scope": 1}`), &actual)
assert.EqualError(t, err, "unexpected type for field scope: float64")
})
}