From cf69e59a5ffd4e23977aa01404c44c2022929306 Mon Sep 17 00:00:00 2001 From: smavros Date: Tue, 30 Jul 2019 20:31:43 +0200 Subject: [PATCH] Modifies testdata setup: - Decorates User model with json tags for marshaling. - Introduces Model type same as gorm.Model with additional json tags for marshaling. - Modifies ResponseMsgUsers type. - Modifies the testdata for the users by including initializer for the Mode.ID field. - The scenario test fails for those changes --- common/models.go | 23 +++++++++++++++++------ common/responses.go | 2 +- common/testdata.go | 25 +++++++++++++++++++------ routes/user/user_test.go | 5 +---- 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/common/models.go b/common/models.go index d2b283a..9d7be84 100644 --- a/common/models.go +++ b/common/models.go @@ -1,23 +1,34 @@ package common import ( + "time" + "github.com/jinzhu/gorm" "github.com/jinzhu/gorm/dialects/postgres" ) +// The type Model is exactly the same with gorm.Model (see jinzhu/gorm) +// except the json tags that are needed for serializing the models +type Model struct { + ID uint `json:"id",gorm:"primary_key"` + CreatedAt time.Time `json:"-"` + UpdatedAt time.Time `json:"-"` + DeletedAt *time.Time `json:"-",sql:"index"` +} + // User data model type User struct { - gorm.Model + Model // Username of user - Username string `gorm:"unique;not null"` + Username string `json:"username",gorm:"unique;not null"` // Password of user - Password string `gorm:"not null"` + Password string `json:"-",gorm:"not null"` // Mail of user - Mail string `gorm:"default:''"` + Mail string `json:"mail",gorm:"default:''"` // Role of user - Role string `gorm:"default:'user'"` + Role string `json:"role",gorm:"default:'user'"` // Scenarios to which user has access - Scenarios []*Scenario `gorm:"many2many:user_scenarios"` + Scenarios []*Scenario `json:"-",gorm:"many2many:user_scenarios"` } // Scenario data model diff --git a/common/responses.go b/common/responses.go index 06840dd..390a3fd 100644 --- a/common/responses.go +++ b/common/responses.go @@ -88,7 +88,7 @@ type ResponseMsg struct { } type ResponseMsgUsers struct { - Users []UserResponse `json:"users"` + Users []User `json:"users"` } type ResponseMsgUser struct { diff --git a/common/testdata.go b/common/testdata.go index 85eb230..c75f2a0 100644 --- a/common/testdata.go +++ b/common/testdata.go @@ -20,12 +20,25 @@ var bcryptCost = 10 var pw0, _ = bcrypt.GenerateFromPassword([]byte("xyz789"), bcryptCost) var pwA, _ = bcrypt.GenerateFromPassword([]byte("abc123"), bcryptCost) var pwB, _ = bcrypt.GenerateFromPassword([]byte("bcd234"), bcryptCost) -var User0 = User{Username: "User_0", Password: string(pw0), Role: "Admin", Mail: "User_0@example.com"} -var User0_response = UserResponse{Username: User0.Username, Role: User0.Role, ID: 1, Mail: User0.Mail} -var UserA = User{Username: "User_A", Password: string(pwA), Role: "User", Mail: "User_A@example.com"} -var UserA_response = UserResponse{Username: UserA.Username, Role: UserA.Role, ID: 2, Mail: UserA.Mail} -var UserB = User{Username: "User_B", Password: string(pwB), Role: "User", Mail: "User_B@example.com"} -var UserB_response = UserResponse{Username: UserB.Username, Role: UserB.Role, ID: 3, Mail: UserB.Mail} + +// XXX: The IDs should NOT be hardcoded. In the future the expected +// responses must be produced by the request send to the API. This will +// also help to test the POST methods on the endpoints. + +var User0 = User{Username: "User_0", Password: string(pw0), + Role: "Admin", Mail: "User_0@example.com", Model: Model{ID: 1}} +var User0_response = UserResponse{Username: User0.Username, + Role: User0.Role, Mail: User0.Mail, ID: User0.ID} + +var UserA = User{Username: "User_A", Password: string(pwA), + Role: "User", Mail: "User_A@example.com", Model: Model{ID: 2}} +var UserA_response = UserResponse{Username: UserA.Username, + Role: UserA.Role, Mail: UserA.Mail, ID: UserA.ID} + +var UserB = User{Username: "User_B", Password: string(pwB), + Role: "User", Mail: "User_B@example.com", Model: Model{ID: 3}} +var UserB_response = UserResponse{Username: UserB.Username, + Role: UserB.Role, Mail: UserB.Mail, ID: UserB.ID} // Credentials diff --git a/routes/user/user_test.go b/routes/user/user_test.go index 7f53b4a..a976c15 100644 --- a/routes/user/user_test.go +++ b/routes/user/user_test.go @@ -11,10 +11,7 @@ import ( func TestUserEndpoints(t *testing.T) { - myUsers := []common.UserResponse{ - common.User0_response, - common.UserA_response, - common.UserB_response} + myUsers := []common.User{common.User0, common.UserA, common.UserB} msgUsers := common.ResponseMsgUsers{Users: myUsers} db := common.DummyInitDB()