refactor testing files
This commit is contained in:
88
book_test.go
88
book_test.go
@@ -1,98 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/config"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func testSetup() *gin.Engine {
|
||||
c := config.LoadConfig("config_test/test.toml")
|
||||
return setup(&c)
|
||||
}
|
||||
|
||||
type loginResponse struct {
|
||||
Message string `json:"message"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
func connectDemoUser(router *gin.Engine) string {
|
||||
loginJson :=
|
||||
`{
|
||||
"username": "demo",
|
||||
"password":"demopw"
|
||||
}`
|
||||
return connectUser(router, loginJson)
|
||||
}
|
||||
|
||||
func connectDemo2User(router *gin.Engine) string {
|
||||
loginJson :=
|
||||
`{
|
||||
"username": "demo2",
|
||||
"password":"demopw"
|
||||
}`
|
||||
return connectUser(router, loginJson)
|
||||
}
|
||||
|
||||
func connectUser(router *gin.Engine, loginJson string) string {
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/auth/login", strings.NewReader(loginJson))
|
||||
router.ServeHTTP(w, req)
|
||||
var parsedResponse loginResponse
|
||||
err := json.Unmarshal(w.Body.Bytes(), &parsedResponse)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return parsedResponse.Token
|
||||
}
|
||||
|
||||
type bookUserGet struct {
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
Rating int `json:"rating" binding:"min=0,max=10"`
|
||||
}
|
||||
|
||||
func TestGetBooksHandler_Demo(t *testing.T) {
|
||||
router := testSetup()
|
||||
|
||||
token := connectDemoUser(router)
|
||||
books := testGetbooksHandler(t, router, token, 200)
|
||||
assert.Equal(t, 26, len(books))
|
||||
}
|
||||
|
||||
func TestGetBooksHandler_Demo2(t *testing.T) {
|
||||
router := testSetup()
|
||||
|
||||
token := connectDemo2User(router)
|
||||
books := testGetbooksHandler(t, router, token, 200)
|
||||
assert.Equal(t, 2, len(books))
|
||||
}
|
||||
|
||||
func testGetbooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int) []bookUserGet {
|
||||
req, _ := http.NewRequest("GET", "/mybooks", nil)
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", userToken))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
var parsedResponse []bookUserGet
|
||||
err := json.Unmarshal(w.Body.Bytes(), &parsedResponse)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, expectedCode, w.Code)
|
||||
return parsedResponse
|
||||
}
|
||||
|
||||
func TestPostBookHandler_Ok(t *testing.T) {
|
||||
bookJson :=
|
||||
`{
|
||||
@@ -137,10 +55,10 @@ func TestPostBookHandler_AuthorTooLong(t *testing.T) {
|
||||
}
|
||||
|
||||
func testPostBookHandler(t *testing.T, bookJson string, expectedCode int) {
|
||||
router := testSetup()
|
||||
router := testutils.TestSetup()
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
token := connectDemoUser(router)
|
||||
token := testutils.ConnectDemoUser(router)
|
||||
req, _ := http.NewRequest("POST", "/book",
|
||||
strings.NewReader(string(bookJson)))
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
|
||||
45
internal/setup/setup.go
Normal file
45
internal/setup/setup.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package setup
|
||||
|
||||
import (
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/api"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/config"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/db"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/jwtauth"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/middleware"
|
||||
)
|
||||
|
||||
func Setup(config *config.Config) *gin.Engine {
|
||||
db := db.Initdb(config.DatabaseFilePath, config.DemoDataPath)
|
||||
err := jwtauth.InitKey(config.JWTKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
r := gin.Default()
|
||||
r.Use(cors.New(configureCors())) // All origins allowed by default
|
||||
r.Use(middleware.Auth())
|
||||
r.GET("/mybooks", func(c *gin.Context) {
|
||||
api.GetMyBooksHanderl(c, db)
|
||||
})
|
||||
r.POST("/book", func(c *gin.Context) {
|
||||
api.PostBookHandler(c, db)
|
||||
})
|
||||
r.POST("/auth/signup", func(c *gin.Context) {
|
||||
api.PostSignupHandler(c, db)
|
||||
})
|
||||
r.POST("/auth/login", func(c *gin.Context) {
|
||||
api.PostLoginHandler(c, db)
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
func configureCors() cors.Config {
|
||||
config := cors.DefaultConfig()
|
||||
config.AllowOrigins = []string{"http://localhost:5173"}
|
||||
config.AllowPrivateNetwork = true
|
||||
config.AllowCredentials = true
|
||||
config.AllowHeaders = []string{"Authorization", "Content-Type"}
|
||||
return config
|
||||
}
|
||||
53
internal/testutils/testutils.go
Normal file
53
internal/testutils/testutils.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package testutils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/config"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/setup"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestSetup() *gin.Engine {
|
||||
c := config.LoadConfig("config_test/test.toml")
|
||||
return setup.Setup(&c)
|
||||
}
|
||||
|
||||
type loginResponse struct {
|
||||
Message string `json:"message"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
func ConnectDemoUser(router *gin.Engine) string {
|
||||
loginJson :=
|
||||
`{
|
||||
"username": "demo",
|
||||
"password":"demopw"
|
||||
}`
|
||||
return connectUser(router, loginJson)
|
||||
}
|
||||
|
||||
func ConnectDemo2User(router *gin.Engine) string {
|
||||
loginJson :=
|
||||
`{
|
||||
"username": "demo2",
|
||||
"password":"demopw"
|
||||
}`
|
||||
return connectUser(router, loginJson)
|
||||
}
|
||||
|
||||
func connectUser(router *gin.Engine, loginJson string) string {
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/auth/login", strings.NewReader(loginJson))
|
||||
router.ServeHTTP(w, req)
|
||||
var parsedResponse loginResponse
|
||||
err := json.Unmarshal(w.Body.Bytes(), &parsedResponse)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return parsedResponse.Token
|
||||
}
|
||||
43
main.go
43
main.go
@@ -1,51 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/api"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/config"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/db"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/jwtauth"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/middleware"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/setup"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c := config.LoadConfig("plm.toml")
|
||||
r := setup(&c)
|
||||
r := setup.Setup(&c)
|
||||
r.Run(":" + c.Port)
|
||||
}
|
||||
|
||||
func setup(config *config.Config) *gin.Engine {
|
||||
db := db.Initdb(config.DatabaseFilePath, config.DemoDataPath)
|
||||
err := jwtauth.InitKey(config.JWTKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
r := gin.Default()
|
||||
r.Use(cors.New(configureCors())) // All origins allowed by default
|
||||
r.Use(middleware.Auth())
|
||||
r.GET("/mybooks", func(c *gin.Context) {
|
||||
api.GetMyBooksHanderl(c, db)
|
||||
})
|
||||
r.POST("/book", func(c *gin.Context) {
|
||||
api.PostBookHandler(c, db)
|
||||
})
|
||||
r.POST("/auth/signup", func(c *gin.Context) {
|
||||
api.PostSignupHandler(c, db)
|
||||
})
|
||||
r.POST("/auth/login", func(c *gin.Context) {
|
||||
api.PostLoginHandler(c, db)
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
func configureCors() cors.Config {
|
||||
config := cors.DefaultConfig()
|
||||
config.AllowOrigins = []string{"http://localhost:5173"}
|
||||
config.AllowPrivateNetwork = true
|
||||
config.AllowCredentials = true
|
||||
config.AllowHeaders = []string{"Authorization", "Content-Type"}
|
||||
return config
|
||||
}
|
||||
|
||||
53
user_book_test.go
Normal file
53
user_book_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type bookUserGet struct {
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
Rating int `json:"rating" binding:"min=0,max=10"`
|
||||
}
|
||||
|
||||
func TestGetBooksHandler_Demo(t *testing.T) {
|
||||
router := testutils.TestSetup()
|
||||
|
||||
token := testutils.ConnectDemoUser(router)
|
||||
books := testGetbooksHandler(t, router, token, 200)
|
||||
assert.Equal(t, 26, len(books))
|
||||
}
|
||||
|
||||
func TestGetBooksHandler_Demo2(t *testing.T) {
|
||||
router := testutils.TestSetup()
|
||||
|
||||
token := testutils.ConnectDemo2User(router)
|
||||
books := testGetbooksHandler(t, router, token, 200)
|
||||
assert.Equal(t, 2, len(books))
|
||||
}
|
||||
|
||||
func testGetbooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int) []bookUserGet {
|
||||
req, _ := http.NewRequest("GET", "/mybooks", nil)
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", userToken))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
var parsedResponse []bookUserGet
|
||||
err := json.Unmarshal(w.Body.Bytes(), &parsedResponse)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, expectedCode, w.Code)
|
||||
return parsedResponse
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -55,7 +56,7 @@ func TestPostUserHandler_PasswordTooBig(t *testing.T) {
|
||||
}
|
||||
|
||||
func testPostUserHandler(t *testing.T, userJson string, expectedCode int) {
|
||||
router := testSetup()
|
||||
router := testutils.TestSetup()
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
req, _ := http.NewRequest("POST", "/auth/signup",
|
||||
|
||||
Reference in New Issue
Block a user