mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
refac: renamed 'post' -> 'article'
This commit is contained in:
@@ -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
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user