Skip to content

Commit

Permalink
fix: fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Claire.Nicholas authored and Claire.Nicholas committed Dec 5, 2023
1 parent b819668 commit b108ae0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
14 changes: 4 additions & 10 deletions database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"database/sql"
"encoding/base64"
"errors"
"fmt"
"regexp"

"github.com/go-vela/types/constants"
Expand Down Expand Up @@ -50,7 +49,7 @@ type User struct {
Favorites pq.StringArray `sql:"favorites" gorm:"type:varchar(5000)"`
Active sql.NullBool `sql:"active"`
Admin sql.NullBool `sql:"admin"`
Dashboards pq.StringArray `sql:"dashboards"`
Dashboards pq.StringArray `sql:"dashboards" gorm:"type:varchar(5000)"`
}

// Decrypt will manipulate the existing user tokens by
Expand Down Expand Up @@ -201,7 +200,7 @@ func (u *User) Nullify() *User {

// ToLibrary converts the User type
// to a library User type.
func (u *User) ToLibrary(dashboards []library.Dashboard) *library.User {
func (u *User) ToLibrary() *library.User {
user := new(library.User)

user.SetID(u.ID.Int64)
Expand All @@ -212,7 +211,7 @@ func (u *User) ToLibrary(dashboards []library.Dashboard) *library.User {
user.SetActive(u.Active.Bool)
user.SetAdmin(u.Admin.Bool)
user.SetFavorites(u.Favorites)
user.SetDashboards(dashboards)
user.SetDashboards(u.Dashboards)

return user
}
Expand Down Expand Up @@ -265,11 +264,6 @@ func (u *User) Validate() error {
// UserFromLibrary converts the library User type
// to a database User type.
func UserFromLibrary(u *library.User) *User {
dahsboardIDs := []string{}
for _, dashboard := range u.GetDashboards() {
dahsboardIDs = append(dahsboardIDs, fmt.Sprint(dashboard.GetID()))
}

user := &User{
ID: sql.NullInt64{Int64: u.GetID(), Valid: true},
Name: sql.NullString{String: u.GetName(), Valid: true},
Expand All @@ -279,7 +273,7 @@ func UserFromLibrary(u *library.User) *User {
Active: sql.NullBool{Bool: u.GetActive(), Valid: true},
Admin: sql.NullBool{Bool: u.GetAdmin(), Valid: true},
Favorites: pq.StringArray(u.GetFavorites()),
Dashboards: dahsboardIDs,
Dashboards: pq.StringArray(u.GetDashboards()),
}

return user.Nullify()
Expand Down
2 changes: 1 addition & 1 deletion database/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestDatabase_User_ToLibrary(t *testing.T) {
want.SetDashboards(nil)

// run test
got := testUser().ToLibrary(nil)
got := testUser().ToLibrary()

if !reflect.DeepEqual(got, want) {
t.Errorf("ToLibrary is %v, want %v", got, want)
Expand Down
31 changes: 16 additions & 15 deletions library/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package library

import (
"fmt"
"strconv"

"github.com/go-vela/types/constants"
)
Expand All @@ -12,15 +13,15 @@ import (
//
// swagger:model User
type User struct {
ID *int64 `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
RefreshToken *string `json:"-"`
Token *string `json:"-"`
Hash *string `json:"-"` // deprecated
Favorites *[]string `json:"favorites,omitempty"`
Active *bool `json:"active,omitempty"`
Admin *bool `json:"admin,omitempty"`
Dashboards *[]Dashboard `json:"dashboards,omitempty"`
ID *int64 `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
RefreshToken *string `json:"-"`
Token *string `json:"-"`
Hash *string `json:"-"` // deprecated
Favorites *[]string `json:"favorites,omitempty"`
Active *bool `json:"active,omitempty"`
Admin *bool `json:"admin,omitempty"`
Dashboards *[]string `json:"dashboards,omitempty"`
}

// Sanitize creates a duplicate of the User without the token values.
Expand Down Expand Up @@ -162,10 +163,10 @@ func (u *User) GetFavorites() []string {
//
// When the provided User type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (u *User) GetDashboards() []Dashboard {
func (u *User) GetDashboards() []string {
// return zero value if User type or Favorites field is nil
if u == nil || u.Dashboards == nil {
return []Dashboard{}
return []string{}
}

return *u.Dashboards
Expand Down Expand Up @@ -279,7 +280,7 @@ func (u *User) SetFavorites(v []string) {
//
// When the provided User type is nil, it
// will set nothing and immediately return.
func (u *User) SetDashboards(v []Dashboard) {
func (u *User) SetDashboards(v []string) {
// return if User type is nil
if u == nil {
return
Expand All @@ -297,7 +298,7 @@ func (u *User) SetDefaultDashboard(d Dashboard) {
dID := d.GetID()

for a, dashboard := range u.GetDashboards() {
if dashboard.GetID() == dID {
if dashboard == strconv.FormatInt(dID, 10) {
hold := dashboards[0]
dashboards[0] = dashboard
dashboards[a] = hold
Expand All @@ -312,15 +313,15 @@ func (u *User) String() string {
return fmt.Sprintf(`{
Active: %t,
Admin: %t,
Dashboards: %d,
Dashboards: %s,
Favorites: %s,
ID: %d,
Name: %s,
Token: %s,
}`,
u.GetActive(),
u.GetAdmin(),
len(u.GetDashboards()),
u.GetDashboards(),
u.GetFavorites(),
u.GetID(),
u.GetName(),
Expand Down
2 changes: 2 additions & 0 deletions library/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,15 @@ func TestLibrary_User_String(t *testing.T) {
want := fmt.Sprintf(`{
Active: %t,
Admin: %t,
Dashboards: %s,
Favorites: %s,
ID: %d,
Name: %s,
Token: %s,
}`,
u.GetActive(),
u.GetAdmin(),
u.GetDashboards(),
u.GetFavorites(),
u.GetID(),
u.GetName(),
Expand Down

0 comments on commit b108ae0

Please sign in to comment.