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
This commit is contained in:
smavros 2019-07-30 20:31:43 +02:00
parent a283d32dff
commit cf69e59a5f
4 changed files with 38 additions and 17 deletions

View file

@ -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

View file

@ -88,7 +88,7 @@ type ResponseMsg struct {
}
type ResponseMsgUsers struct {
Users []UserResponse `json:"users"`
Users []User `json:"users"`
}
type ResponseMsgUser struct {

View file

@ -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

View file

@ -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()