diff --git a/common/database.go b/common/database.go index 29de39b..25ea578 100644 --- a/common/database.go +++ b/common/database.go @@ -91,11 +91,9 @@ func DummyInitDB() *gorm.DB { return test_db } -func DummyOnlyAdminDB(test_db *gorm.DB) { - - MigrateModels(test_db) - - checkErr(test_db.Create(&User0).Error) +func DummyAddOnlyUserTableWithAdminDB(db *gorm.DB) { + db.AutoMigrate(&User{}) + checkErr(db.Create(&User0).Error) } // Migrates models and populates them with data diff --git a/routes/user/user_test.go b/routes/user/user_test.go index 0734c6b..6d8a68e 100644 --- a/routes/user/user_test.go +++ b/routes/user/user_test.go @@ -1,38 +1,51 @@ package user import ( + "os" "testing" "github.com/gin-gonic/gin" + "github.com/jinzhu/gorm" "github.com/stretchr/testify/assert" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" ) -func TestUserEndpoints(t *testing.T) { +var router *gin.Engine +var db *gorm.DB - db := common.DummyInitDB() +func cleanUserTable() { + db.DropTable(&common.User{}) + db.AutoMigrate(&common.User{}) + db.Create(&common.User0) +} + +func TestMain(m *testing.M) { + + db = common.DummyInitDB() defer db.Close() - common.DummyOnlyAdminDB(db) - router := gin.Default() + common.DummyAddOnlyUserTableWithAdminDB(db) + + router = gin.Default() api := router.Group("/api") RegisterAuthenticate(api.Group("/authenticate")) api.Use(Authentication(true)) RegisterUserEndpoints(api.Group("/users")) + os.Exit(m.Run()) +} + +func TestGetAllUsers(t *testing.T) { + + defer cleanUserTable() + // authenticate as admin token, err := common.NewAuthenticateForTest(router, "/api/authenticate", "POST", common.AdminCredentials, 200) assert.NoError(t, err) - // test GET user/ - err = common.NewTestEndpoint(router, token, - "/api/users", "GET", nil, - 200, common.KeyModels{"users": []common.User{common.User0}}) - assert.NoError(t, err) - // test GET user/1 (the admin) err = common.NewTestEndpoint(router, token, "/api/users/1", "GET", nil,