From d41f4b5d21543a133703fec7eccb2106cde39758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20M=C3=B6hlmann?= Date: Thu, 2 Mar 2023 17:35:06 +0200 Subject: [PATCH] add unit tests for oidc.Userinfo - Add get methods for Address fields to handle nil pointers as we used to --- go.mod | 2 +- go.sum | 4 ++-- pkg/oidc/introspection.go | 13 +++++++++++-- pkg/oidc/introspection_test.go | 9 +++++++++ pkg/oidc/userinfo.go | 9 +++++++++ pkg/oidc/userinfo_test.go | 20 ++++++++++++++++++++ 6 files changed, 52 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index deb30b3..9ed1e8e 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e9a2ca5..1933228 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/oidc/introspection.go b/pkg/oidc/introspection.go index 78c0ad3..9cf8265 100644 --- a/pkg/oidc/introspection.go +++ b/pkg/oidc/introspection.go @@ -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 diff --git a/pkg/oidc/introspection_test.go b/pkg/oidc/introspection_test.go index 6ecd719..6289a2c 100644 --- a/pkg/oidc/introspection_test.go +++ b/pkg/oidc/introspection_test.go @@ -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{ diff --git a/pkg/oidc/userinfo.go b/pkg/oidc/userinfo.go index eef6c89..3df8b6a 100644 --- a/pkg/oidc/userinfo.go +++ b/pkg/oidc/userinfo.go @@ -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) { diff --git a/pkg/oidc/userinfo_test.go b/pkg/oidc/userinfo_test.go index e0a1b39..faab4e3 100644 --- a/pkg/oidc/userinfo_test.go +++ b/pkg/oidc/userinfo_test.go @@ -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",