refac: renamed 'post' -> 'article'

This commit is contained in:
Florian Sylvain
2025-11-17 19:27:48 +01:00
parent cb9a431488
commit b470bfcc67
44 changed files with 701 additions and 967 deletions
-163
View File
@@ -1,163 +0,0 @@
package test
import (
"GoCMS/domain/post"
"bytes"
"encoding/json"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"strconv"
"testing"
)
var TestCreatePostSuccess = func(t *testing.T) {
jsonBody, err := json.Marshal(map[string]string{
"title": "Test Title",
"body": "Test Body",
})
if err != nil {
t.Fatal(err)
}
r, _ := ApiRequest("POST", "/post", bytes.NewBuffer(jsonBody))
var response post.Post
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)
}
var TestCreatePostFailTitleMissing = func(t *testing.T) {
jsonBody, err := json.Marshal(map[string]string{
"body": "Test Body",
})
if err != nil {
t.Fatal(err)
}
r, _ := ApiRequest("POST", "/post", bytes.NewBuffer(jsonBody))
assert.Equal(t, http.StatusBadRequest, r.StatusCode)
}
var TestCreatePostTitleTooShort = func(t *testing.T) {
jsonBody, err := json.Marshal(map[string]string{
"title": "Te",
"body": "Test Body",
})
if err != nil {
t.Fatal(err)
}
r, _ := ApiRequest("POST", "/post", bytes.NewBuffer(jsonBody))
assert.Equal(t, http.StatusBadRequest, r.StatusCode)
}
var TestGetPostSuccess = func(t *testing.T) {
var createdPost post.Post
var postToCreate = post.Post{
Title: "Test Title",
Body: "Test Body",
}
db := GetDb()
db.Create(&postToCreate).Scan(&createdPost)
r, _ := ApiRequest("GET", "/post/"+strconv.Itoa(int(createdPost.ID)), nil)
var response post.Post
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, createdPost.ID, response.ID)
assert.Equal(t, createdPost.Title, response.Title)
assert.Equal(t, createdPost.Body, response.Body)
}
var TestGetAllPostsSuccess = func(t *testing.T) {
var createdPost post.Post
var postToCreate = post.Post{
Title: "Test Title",
Body: "Test Body",
}
db := GetDb()
db.Create(&postToCreate).Scan(&createdPost)
r, _ := ApiRequest("GET", "/post", nil)
var response []post.Post
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, createdPost.Title, response[0].Title)
assert.Equal(t, createdPost.Body, response[0].Body)
}
var TestDeletePostSuccess = func(t *testing.T) {
var createdPost post.Post
var postToCreate = post.Post{
Title: "Test Title",
Body: "Test Body",
}
db := GetDb()
db.Create(&postToCreate).Scan(&createdPost)
r, _ := ApiRequest("DELETE", "/post/"+strconv.Itoa(int(createdPost.ID)), nil)
assert.Equal(t, http.StatusOK, r.StatusCode)
}
var TestPostCreate = func(t *testing.T) {
t.Run("Should return a post with the given title and body", TestCreatePostSuccess)
t.Run("Should return an error if the title is missing", TestCreatePostFailTitleMissing)
t.Run("Should return an error if the title is too short", TestCreatePostTitleTooShort)
}
var TestPostGet = func(t *testing.T) {
t.Run("Should return a post with the given id", TestGetPostSuccess)
}
var TestPostGetAll = func(t *testing.T) {
t.Run("Should return all posts", TestGetAllPostsSuccess)
}
var TestPostDelete = func(t *testing.T) {
t.Run("Should return success", TestDeletePostSuccess)
}
func TestPost(t *testing.T) {
StartServerIfNotAlready()
WaitForServer()
t.Run("Create", TestPostCreate)
t.Run("Get", TestPostGet)
t.Run("GetAll", TestPostGetAll)
t.Run("Delete", TestPostDelete)
}
-105
View File
@@ -1,105 +0,0 @@
package test
import (
"GoCMS/adapters/secondary/gateways/models"
"GoCMS/api/controllers/auth"
"GoCMS/main/server"
"io"
"net/http"
"os"
"time"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)
const testDbFile = "test.db"
var ApiUrl string
var AuthorizationCookie *http.Cookie
var HttpClient = http.Client{}
func GetDb() *gorm.DB {
db, err := gorm.Open(sqlite.Open(testDbFile), &gorm.Config{})
if err != nil {
panic(err)
}
_ = db.AutoMigrate(&models.Post{}, &models.User{})
return db
}
func StartServerIfNotAlready() {
_, err := http.Get(ApiUrl)
if err == nil {
return
}
_ = os.Remove(testDbFile)
_ = os.Setenv("DB_FILE", testDbFile)
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 := auth.Token.Encode(map[string]any{"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
}