mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
feat: tests (article)
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"GohCMS2/domain/article"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestArticle(t *testing.T) {
|
||||
StartServerIfNotAlready()
|
||||
WaitForServer()
|
||||
|
||||
t.Run("Create", func(t *testing.T) {
|
||||
t.Run("Should return an article with the given title and body", func(t *testing.T) {
|
||||
jsonBody, err := json.Marshal(map[string]string{
|
||||
"title": "Test Title",
|
||||
"body": "Test Body",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, err := ApiRequest("POST", "/article", bytes.NewBuffer(jsonBody))
|
||||
|
||||
var response article.Article
|
||||
bd, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(bd, &response)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, http.StatusOK, r.StatusCode)
|
||||
assert.Equal(t, "Test Title", response.Title)
|
||||
assert.Equal(t, "Test Body", response.Body)
|
||||
})
|
||||
|
||||
t.Run("Should return an error if the title is missing", func(t *testing.T) {
|
||||
jsonBody, err := json.Marshal(map[string]string{
|
||||
"body": "Test Body",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, err := ApiRequest("POST", "/article", bytes.NewBuffer(jsonBody))
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, r.StatusCode)
|
||||
})
|
||||
|
||||
t.Run("Should return an error if the title is too short", func(t *testing.T) {
|
||||
jsonBody, err := json.Marshal(map[string]string{
|
||||
"title": "Te",
|
||||
"body": "Test Body",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, err := ApiRequest("POST", "/article", bytes.NewBuffer(jsonBody))
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, r.StatusCode)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Get", func(t *testing.T) {
|
||||
t.Run("Should return an article with the given id", func(t *testing.T) {
|
||||
var createdArticle article.Article
|
||||
var articleToCreate = article.Article{
|
||||
Title: "Test Title",
|
||||
Body: "Test Body",
|
||||
}
|
||||
db := GetDb()
|
||||
db.Create(&articleToCreate).Scan(&createdArticle)
|
||||
|
||||
r, err := ApiRequest("GET", "/article/"+strconv.Itoa(int(createdArticle.ID)), nil)
|
||||
|
||||
var response article.Article
|
||||
bd, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(bd, &response)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, http.StatusOK, r.StatusCode)
|
||||
assert.Equal(t, createdArticle.ID, response.ID)
|
||||
assert.Equal(t, createdArticle.Title, response.Title)
|
||||
assert.Equal(t, createdArticle.Body, response.Body)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("GetAll", func(t *testing.T) {
|
||||
t.Run("Should return all articles", func(t *testing.T) {
|
||||
var createdArticle article.Article
|
||||
var articleToCreate = article.Article{
|
||||
Title: "Test Title",
|
||||
Body: "Test Body",
|
||||
}
|
||||
db := GetDb()
|
||||
db.Create(&articleToCreate).Scan(&createdArticle)
|
||||
|
||||
r, err := ApiRequest("GET", "/article", nil)
|
||||
|
||||
var response []article.Article
|
||||
bd, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(bd, &response)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, http.StatusOK, r.StatusCode)
|
||||
assert.Equal(t, createdArticle.ID, response[0].ID)
|
||||
assert.Equal(t, createdArticle.Title, response[0].Title)
|
||||
assert.Equal(t, createdArticle.Body, response[0].Body)
|
||||
})
|
||||
})
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"GohCMS2/adapters/secondary/gateways/models"
|
||||
"GohCMS2/api"
|
||||
"GohCMS2/main/server"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ApiUrl string
|
||||
var AuthorizationCookie *http.Cookie
|
||||
var HttpClient = http.Client{}
|
||||
|
||||
func DeleteTestDb() error {
|
||||
return os.Remove("test.db")
|
||||
}
|
||||
|
||||
func GetDb() *gorm.DB {
|
||||
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_ = db.AutoMigrate(&models.Article{}, &models.User{})
|
||||
return db
|
||||
}
|
||||
|
||||
func StartServerIfNotAlready() {
|
||||
_, err := http.Get(ApiUrl)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
err = DeleteTestDb()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
go func(url *string) {
|
||||
router := server.InitServer()
|
||||
*url = "http://localhost:" + os.Getenv("PORT") + "/v1"
|
||||
err := server.StartServer(router)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}(&ApiUrl)
|
||||
}
|
||||
|
||||
func getAuthorizationCookie(userId uint32) *http.Cookie {
|
||||
_, tokenString, err := api.TokenAuth.Encode(map[string]interface{}{"user_id": userId})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return &http.Cookie{
|
||||
Name: "jwt",
|
||||
Value: tokenString,
|
||||
Expires: time.Now().Add(24 * time.Hour),
|
||||
Secure: false,
|
||||
HttpOnly: true,
|
||||
Path: "/",
|
||||
}
|
||||
}
|
||||
|
||||
func SetAuthorizationCookieIfNotAlready(r *http.Request) {
|
||||
if AuthorizationCookie != nil {
|
||||
r.AddCookie(AuthorizationCookie)
|
||||
return
|
||||
}
|
||||
db := GetDb()
|
||||
user := models.User{
|
||||
Username: "testuser",
|
||||
Password: "testpassword",
|
||||
Email: "testemail@a.com",
|
||||
}
|
||||
var createdUser models.User
|
||||
db.Create(&user).Scan(&createdUser)
|
||||
AuthorizationCookie = getAuthorizationCookie(createdUser.ID)
|
||||
r.AddCookie(AuthorizationCookie)
|
||||
}
|
||||
|
||||
func WaitForServer() {
|
||||
for {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
if ApiUrl == "" {
|
||||
continue
|
||||
}
|
||||
_, err := http.Get(ApiUrl)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ApiRequest(method string, route string, body io.Reader) (*http.Response, error) {
|
||||
request, err := http.NewRequest(method, ApiUrl+route, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
SetAuthorizationCookieIfNotAlready(request)
|
||||
|
||||
response, err := HttpClient.Do(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
Reference in New Issue
Block a user