Breaks user test into different testing functions

This commit is contained in:
smavros 2019-08-15 18:16:30 +02:00
parent c2429394ed
commit 94ea3c4ebf
2 changed files with 26 additions and 15 deletions

View file

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

View file

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