Skip to content

Commit

Permalink
fix password_created type (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
yec-akamai authored Jun 20, 2023
1 parent 00bac95 commit 276a9ca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
36 changes: 29 additions & 7 deletions account_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 3 additions & 6 deletions test/integration/account_users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit 276a9ca

Please sign in to comment.