From 276a9cac08df7eeec4fef25d3af26a7505c1a4f4 Mon Sep 17 00:00:00 2001 From: Ye Chen <127243817+yec-akamai@users.noreply.github.com> Date: Tue, 20 Jun 2023 14:43:41 -0400 Subject: [PATCH] fix password_created type (#338) --- account_users.go | 36 +++++++++++++++++++++----- test/integration/account_users_test.go | 9 +++---- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/account_users.go b/account_users.go index 7ed9b89bc..50bae11c3 100644 --- a/account_users.go +++ b/account_users.go @@ -4,19 +4,21 @@ import ( "context" "encoding/json" "fmt" + "time" "github.com/go-resty/resty/v2" + "github.com/linode/linodego/internal/parseabletime" ) // User represents a User object type User struct { - Username string `json:"username"` - Email string `json:"email"` - Restricted bool `json:"restricted"` - TFAEnabled bool `json:"tfa_enabled"` - SSHKeys []string `json:"ssh_keys"` - PasswordCreated string `json:"password_created"` - VerifiedPhoneNumber string `json:"verified_phone_number"` + Username string `json:"username"` + Email string `json:"email"` + Restricted bool `json:"restricted"` + TFAEnabled bool `json:"tfa_enabled"` + SSHKeys []string `json:"ssh_keys"` + PasswordCreated *time.Time `json:"-"` + VerifiedPhoneNumber string `json:"verified_phone_number"` } // UserCreateOptions fields are those accepted by CreateUser @@ -32,6 +34,26 @@ type UserUpdateOptions struct { Restricted *bool `json:"restricted,omitempty"` } +// UnmarshalJSON implements the json.Unmarshaler interface +func (i *User) UnmarshalJSON(b []byte) error { + type Mask User + + p := struct { + *Mask + PasswordCreated *parseabletime.ParseableTime `json:"password_created"` + }{ + Mask: (*Mask)(i), + } + + if err := json.Unmarshal(b, &p); err != nil { + return err + } + + i.PasswordCreated = (*time.Time)(p.PasswordCreated) + + return nil +} + // GetCreateOptions converts a User to UserCreateOptions for use in CreateUser func (i User) GetCreateOptions() (o UserCreateOptions) { o.Username = i.Username diff --git a/test/integration/account_users_test.go b/test/integration/account_users_test.go index c614a15bc..a6935bf12 100644 --- a/test/integration/account_users_test.go +++ b/test/integration/account_users_test.go @@ -4,12 +4,15 @@ import ( "context" "testing" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/linode/linodego" . "github.com/linode/linodego" ) const usernamePrefix = "linodegotest-" +var ignoreUserTimestampes = cmpopts.IgnoreFields(linodego.User{}, "PasswordCreated") + type userModifier func(*linodego.UserCreateOptions) func TestUser_GetMissing(t *testing.T) { @@ -61,9 +64,6 @@ func TestUser_Get(t *testing.T) { if user.TFAEnabled { t.Error("expected TFA is disabled") } - if user.PasswordCreated != "" { - t.Error("expected password is not set") - } if user.VerifiedPhoneNumber != "" { t.Error("expected phone number is not set") } @@ -147,9 +147,6 @@ func TestUsers_List(t *testing.T) { if newUser.TFAEnabled { t.Error("expected TFA is disabled") } - if newUser.PasswordCreated != "" { - t.Error("expected password is not set") - } if newUser.VerifiedPhoneNumber != "" { t.Error("expected phone number is not set") }