From ede96920b449836a4a2f781ef720d7a3e801417a Mon Sep 17 00:00:00 2001 From: "Claire.Nicholas" Date: Tue, 5 Dec 2023 13:32:45 -0600 Subject: [PATCH] feat: adding new user changes to server --- api/user/update.go | 5 +++++ api/user/update_current.go | 6 ++++++ database/user/create_test.go | 4 ++-- database/user/get_name_test.go | 4 ++-- database/user/get_test.go | 4 ++-- database/user/list_test.go | 6 +++--- database/user/table.go | 2 ++ database/user/update_test.go | 6 +++--- mock/server/user.go | 9 ++++++--- 9 files changed, 31 insertions(+), 15 deletions(-) diff --git a/api/user/update.go b/api/user/update.go index 0535da1f5..4f776780a 100644 --- a/api/user/update.go +++ b/api/user/update.go @@ -106,6 +106,11 @@ func UpdateUser(c *gin.Context) { u.SetFavorites(input.GetFavorites()) } + if input.Dashboards != nil { + // update dashboards if set + u.SetDashboards(input.GetDashboards()) + } + // send API call to update the user u, err = database.FromContext(c).UpdateUser(ctx, u) if err != nil { diff --git a/api/user/update_current.go b/api/user/update_current.go index ab56023ad..0a4ea8436 100644 --- a/api/user/update_current.go +++ b/api/user/update_current.go @@ -80,6 +80,12 @@ func UpdateCurrentUser(c *gin.Context) { u.SetFavorites(input.GetFavorites()) } + // update user fields if provided + if input.Dashboards != nil { + // update dashboards if set + u.SetDashboards(input.GetDashboards()) + } + // send API call to update the user u, err = database.FromContext(c).UpdateUser(ctx, u) if err != nil { diff --git a/database/user/create_test.go b/database/user/create_test.go index 283bdb443..a661fe8a9 100644 --- a/database/user/create_test.go +++ b/database/user/create_test.go @@ -26,9 +26,9 @@ func TestUser_Engine_CreateUser(t *testing.T) { // ensure the mock expects the query _mock.ExpectQuery(`INSERT INTO "users" -("name","refresh_token","token","hash","favorites","active","admin","id") +("name","refresh_token","token","hash","favorites","active","admin","dashboards","id") VALUES ($1,$2,$3,$4,$5,$6,$7,$8) RETURNING "id"`). - WithArgs("foo", AnyArgument{}, AnyArgument{}, AnyArgument{}, nil, false, false, 1). + WithArgs("foo", AnyArgument{}, AnyArgument{}, AnyArgument{}, nil, false, false, nil, 1). WillReturnRows(_rows) _sqlite := testSqlite(t) diff --git a/database/user/get_name_test.go b/database/user/get_name_test.go index 855fdfaaa..b32d0ac70 100644 --- a/database/user/get_name_test.go +++ b/database/user/get_name_test.go @@ -25,8 +25,8 @@ func TestUser_Engine_GetUserForName(t *testing.T) { // create expected result in mock _rows := sqlmock.NewRows( - []string{"id", "name", "refresh_token", "token", "hash", "favorites", "active", "admin"}). - AddRow(1, "foo", "", "bar", "baz", "{}", false, false) + []string{"id", "name", "refresh_token", "token", "hash", "favorites", "active", "admin", "dashboards"}). + AddRow(1, "foo", "", "bar", "baz", "{}", false, false, nil) // ensure the mock expects the query _mock.ExpectQuery(`SELECT * FROM "users" WHERE name = $1 LIMIT 1`).WithArgs("foo").WillReturnRows(_rows) diff --git a/database/user/get_test.go b/database/user/get_test.go index 8ad1b3c77..c0b2f958d 100644 --- a/database/user/get_test.go +++ b/database/user/get_test.go @@ -25,8 +25,8 @@ func TestUser_Engine_GetUser(t *testing.T) { // create expected result in mock _rows := sqlmock.NewRows( - []string{"id", "name", "refresh_token", "token", "hash", "favorites", "active", "admin"}). - AddRow(1, "foo", "", "bar", "baz", "{}", false, false) + []string{"id", "name", "refresh_token", "token", "hash", "favorites", "active", "admin", "dashboards"}). + AddRow(1, "foo", "", "bar", "baz", "{}", false, false, nil) // ensure the mock expects the query _mock.ExpectQuery(`SELECT * FROM "users" WHERE id = $1 LIMIT 1`).WithArgs(1).WillReturnRows(_rows) diff --git a/database/user/list_test.go b/database/user/list_test.go index da61b5b67..6676b11ee 100644 --- a/database/user/list_test.go +++ b/database/user/list_test.go @@ -38,9 +38,9 @@ func TestUser_Engine_ListUsers(t *testing.T) { // create expected result in mock _rows = sqlmock.NewRows( - []string{"id", "name", "refresh_token", "token", "hash", "favorites", "active", "admin"}). - AddRow(1, "foo", "", "bar", "baz", "{}", false, false). - AddRow(2, "baz", "", "bar", "foo", "{}", false, false) + []string{"id", "name", "refresh_token", "token", "hash", "favorites", "active", "admin", "dashboards"}). + AddRow(1, "foo", "", "bar", "baz", "{}", false, false, nil). + AddRow(2, "baz", "", "bar", "foo", "{}", false, false, nil) // ensure the mock expects the query _mock.ExpectQuery(`SELECT * FROM "users"`).WillReturnRows(_rows) diff --git a/database/user/table.go b/database/user/table.go index 070546435..ce1fd0ea5 100644 --- a/database/user/table.go +++ b/database/user/table.go @@ -22,6 +22,7 @@ users ( favorites VARCHAR(5000), active BOOLEAN, admin BOOLEAN, + dashboards VARCHAR(5000), UNIQUE(name) ); ` @@ -39,6 +40,7 @@ users ( favorites TEXT, active BOOLEAN, admin BOOLEAN, + dashboards TEXT, UNIQUE(name) ); ` diff --git a/database/user/update_test.go b/database/user/update_test.go index 83b455d77..c8c50e590 100644 --- a/database/user/update_test.go +++ b/database/user/update_test.go @@ -23,9 +23,9 @@ func TestUser_Engine_UpdateUser(t *testing.T) { // ensure the mock expects the query _mock.ExpectExec(`UPDATE "users" -SET "name"=$1,"refresh_token"=$2,"token"=$3,"hash"=$4,"favorites"=$5,"active"=$6,"admin"=$7 -WHERE "id" = $8`). - WithArgs("foo", AnyArgument{}, AnyArgument{}, AnyArgument{}, nil, false, false, 1). +SET "name"=$1,"refresh_token"=$2,"token"=$3,"hash"=$4,"favorites"=$5,"active"=$6,"admin"=$7,"dashboards"=$8 +WHERE "id" = $9`). + WithArgs("foo", AnyArgument{}, AnyArgument{}, AnyArgument{}, nil, false, false, nil, 1). WillReturnResult(sqlmock.NewResult(1, 1)) _sqlite := testSqlite(t) diff --git a/mock/server/user.go b/mock/server/user.go index 1ec672b47..c35ac4ac0 100644 --- a/mock/server/user.go +++ b/mock/server/user.go @@ -22,7 +22,8 @@ const ( "token": null, "favorites": ["github/octocat"], "active": true, - "admin": false + "admin": false, + "dashboards": [] }` // UsersResp represents a JSON return for one to many users. @@ -33,7 +34,8 @@ const ( "token": null, "favorites": ["github/octocat"], "active": true, - "admin": false + "admin": false, + "dashboards": [] }, { "id": 1, @@ -41,7 +43,8 @@ const ( "token": null, "favorites": ["github/octocat"], "active": true, - "admin": false + "admin": false, + "dashboards": [] } ]` )