Skip to content

Commit

Permalink
add unit tests for oidc.Userinfo
Browse files Browse the repository at this point in the history
- Add get methods for Address fields to handle nil pointers as we used to
  • Loading branch information
muhlemmer committed Mar 2, 2023
1 parent 72a108a commit d41f4b5
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/gorilla/schema v1.2.0
github.com/gorilla/securecookie v1.1.1
github.com/jeremija/gosubmit v0.2.7
github.com/muhlemmer/gu v0.2.0
github.com/muhlemmer/gu v0.3.0
github.com/rs/cors v1.8.3
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/muhlemmer/gu v0.2.0 h1:vnIYZccxWVSPXbCPTuB1J7HWimMeZLlkfDody5qLUEI=
github.com/muhlemmer/gu v0.2.0/go.mod h1:YHtHR+gxM+bKEIIs7Hmi9sPT3ZDUvTN/i88wQpZkrdM=
github.com/muhlemmer/gu v0.3.0 h1:UwNv9xXGp1WDgHKgk7ljjh3duh1w4ZAY1k1NsWBYl3Y=
github.com/muhlemmer/gu v0.3.0/go.mod h1:YHtHR+gxM+bKEIIs7Hmi9sPT3ZDUvTN/i88wQpZkrdM=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
13 changes: 11 additions & 2 deletions pkg/oidc/introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type IntrospectionResponse struct {
// GetUserInfo copies all user related fields into a new UserInfo.
func (i *IntrospectionResponse) GetUserInfo() *UserInfo {
return &UserInfo{
Address: i.Address,
Address: gu.PtrCopy(i.Address),
Subject: i.Subject,
UserInfoProfile: i.UserInfoProfile,
UserInfoEmail: i.UserInfoEmail,
Expand All @@ -49,7 +49,7 @@ func (i *IntrospectionResponse) GetUserInfo() *UserInfo {
func (i *IntrospectionResponse) SetUserInfo(u *UserInfo) {
i.Subject = u.Subject
i.Username = u.PreferredUsername
i.Address = u.Address
i.Address = gu.PtrCopy(u.Address)
i.UserInfoProfile = u.UserInfoProfile
i.UserInfoEmail = u.UserInfoEmail
i.UserInfoPhone = u.UserInfoPhone
Expand All @@ -60,6 +60,15 @@ func (i *IntrospectionResponse) SetUserInfo(u *UserInfo) {
}
}

// GetAddress is a safe getter that takes
// care of a possible nil value.
func (i *IntrospectionResponse) GetAddress() *UserInfoAddress {
if i.Address == nil {
return new(UserInfoAddress)
}
return i.Address
}

// introspectionResponseAlias prevents loops on the JSON methods
type introspectionResponseAlias IntrospectionResponse

Expand Down
9 changes: 9 additions & 0 deletions pkg/oidc/introspection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ func TestIntrospectionResponse_SetUserInfo(t *testing.T) {
}
}

func TestIntrospectionResponse_GetAddress(t *testing.T) {
// nil address
i := new(IntrospectionResponse)
assert.Equal(t, &UserInfoAddress{}, i.GetAddress())

i.Address = &UserInfoAddress{PostalCode: "1234"}
assert.Equal(t, i.Address, i.GetAddress())
}

func TestIntrospectionResponse_MarshalJSON(t *testing.T) {
got, err := json.Marshal(&IntrospectionResponse{
UserInfoProfile: UserInfoProfile{
Expand Down
9 changes: 9 additions & 0 deletions pkg/oidc/userinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ func (u *UserInfo) AppendClaims(k string, v any) {
u.Claims[k] = v
}

// GetAddress is a safe getter that takes
// care of a possible nil value.
func (u *UserInfo) GetAddress() *UserInfoAddress {
if u.Address == nil {
return new(UserInfoAddress)
}
return u.Address
}

type uiAlias UserInfo

func (u *UserInfo) MarshalJSON() ([]byte, error) {
Expand Down
20 changes: 20 additions & 0 deletions pkg/oidc/userinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ import (
"github.com/stretchr/testify/assert"
)

func TestUserInfo_AppendClaims(t *testing.T) {
u := new(UserInfo)
u.AppendClaims("a", "b")
want := map[string]any{"a": "b"}
assert.Equal(t, want, u.Claims)

u.AppendClaims("d", "e")
want["d"] = "e"
assert.Equal(t, want, u.Claims)
}

func TestUserInfo_GetAddress(t *testing.T) {
// nil address
u := new(UserInfo)
assert.Equal(t, &UserInfoAddress{}, u.GetAddress())

u.Address = &UserInfoAddress{PostalCode: "1234"}
assert.Equal(t, u.Address, u.GetAddress())
}

func TestUserInfoMarshal(t *testing.T) {
userinfo := &UserInfo{
Subject: "test",
Expand Down

0 comments on commit d41f4b5

Please sign in to comment.