add unit tests for oidc.Userinfo
- Add get methods for Address fields to handle nil pointers as we used to
This commit is contained in:
parent
72a108a33b
commit
d41f4b5d21
6 changed files with 52 additions and 5 deletions
|
@ -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,
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue