add test edit promo

This commit is contained in:
Pavel 2024-01-26 15:24:53 +03:00
parent a24f601f1f
commit 82c3cf2679
2 changed files with 145 additions and 29 deletions

@ -6,10 +6,14 @@ import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson/primitive"
"testing"
)
func TestPromoCodeControllerE2E(t *testing.T) {
var promoID string
// CreatePromoCode
func TestCreatePromoCode(t *testing.T) {
client := fiber.AcquireClient()
t.Run("CreatePromoCode-success", func(t *testing.T) {
@ -54,6 +58,7 @@ func TestPromoCodeControllerE2E(t *testing.T) {
var response models.PromoCode
err = json.Unmarshal(resBody, &response)
assert.NoError(t, err)
promoID = response.ID.Hex()
fmt.Println(response)
})
t.Run("CreatePromoCode-duplicate", func(t *testing.T) {
@ -181,5 +186,117 @@ func TestPromoCodeControllerE2E(t *testing.T) {
assert.NoError(t, err)
fmt.Println(response["error"])
})
}
// EditPromoCode
func TestEditPromoCode(t *testing.T) {
client := fiber.AcquireClient()
t.Run("EditPromoCode-success", func(t *testing.T) {
reqBody := models.ReqEditPromoCode{
ID: promoID,
Description: toString("Updated description"),
Greetings: toString("Updated greetings"),
DueTo: toInt64(1734429225),
ActivationCount: toInt64(150),
Delete: toBool(false),
}
reqJSON, _ := json.Marshal(reqBody)
req := client.Put(BaseUrl+"/promocode/edit").Set("Content-Type", "application/json").Body(reqJSON)
statusCode, resBody, errs := req.Bytes()
if len(errs) != 0 {
assert.NoError(t, errs[0])
}
assert.Equal(t, fiber.StatusOK, statusCode)
var response models.PromoCode
err := json.Unmarshal(resBody, &response)
assert.NoError(t, err)
fmt.Println(response)
})
t.Run("EditPromoCode-success one column", func(t *testing.T) {
reqBody := models.ReqEditPromoCode{
ID: promoID,
Greetings: toString("Updated greetings one"),
}
reqJSON, _ := json.Marshal(reqBody)
req := client.Put(BaseUrl+"/promocode/edit").Set("Content-Type", "application/json").Body(reqJSON)
statusCode, resBody, errs := req.Bytes()
if len(errs) != 0 {
assert.NoError(t, errs[0])
}
assert.Equal(t, fiber.StatusOK, statusCode)
var response models.PromoCode
err := json.Unmarshal(resBody, &response)
assert.NoError(t, err)
fmt.Println(response)
})
t.Run("EditPromoCode-promocod not found", func(t *testing.T) {
reqBody := models.ReqEditPromoCode{
ID: primitive.NewObjectID().Hex(),
Description: toString("Updated description"),
Greetings: toString("Updated greetings"),
DueTo: toInt64(1734429225),
ActivationCount: toInt64(150),
Delete: toBool(false),
}
reqJSON, _ := json.Marshal(reqBody)
req := client.Put(BaseUrl+"/promocode/edit").Set("Content-Type", "application/json").Body(reqJSON)
statusCode, resBody, errs := req.Bytes()
if len(errs) != 0 {
assert.NoError(t, errs[0])
}
assert.Equal(t, fiber.StatusNotFound, statusCode)
var response map[string]interface{}
err := json.Unmarshal(resBody, &response)
assert.NoError(t, err)
fmt.Println(response["error"])
})
t.Run("EditPromoCode-invalid request payload", func(t *testing.T) {
reqBody := map[string]interface{}{
"invalid_field": "example",
"description": "Updated description",
"greetings": "Updated greetings",
"dueTo": 1734429225,
"activationCount": 150,
"delete": false,
}
reqJSON, _ := json.Marshal(reqBody)
req := client.Put(BaseUrl+"/promocode/edit").Set("Content-Type", "application/json").Body(reqJSON)
statusCode, resBody, errs := req.Bytes()
if len(errs) != 0 {
assert.NoError(t, errs[0])
}
assert.Equal(t, fiber.StatusBadRequest, statusCode)
var response map[string]interface{}
err := json.Unmarshal(resBody, &response)
assert.NoError(t, err)
fmt.Println(response["error"])
})
}
func toString(s string) *string {
return &s
}
func toInt64(i int64) *int64 {
return &i
}
func toBool(b bool) *bool {
return &b
}

@ -7,7 +7,6 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
const (
@ -21,7 +20,7 @@ func TestRecoveryHandler(t *testing.T) {
t.Run("HandleRecoveryRequest", func(t *testing.T) {
reqBody := models.RecoveryRequest{
Email: "admin",
Email: "adminSOLO",
RedirectionURL: "http://redirect.com",
}
reqJSON, _ := json.Marshal(reqBody)
@ -93,28 +92,28 @@ func TestRecoveryHandler(t *testing.T) {
}
// get handler
func TestRecoveryLinkHandler(t *testing.T) {
client := fiber.AcquireClient()
t.Run("HandleRecoveryLink_ValidSign", func(t *testing.T) {
req := client.Get(BaseUrl + "/recover/" + ValidSign)
statusCode, _, errs := req.Bytes()
if len(errs) != 0 {
assert.NoError(t, errs[0])
}
assert.Equal(t, fiber.StatusOK, statusCode)
fmt.Println("Recovery link handled successfully")
})
time.Sleep(15 * time.Minute)
t.Run("HandleRecoveryLink_ExpiredSign", func(t *testing.T) {
req := client.Get(BaseUrl + "/recover/" + ValidSign)
statusCode, _, errs := req.Bytes()
if len(errs) != 0 {
assert.NoError(t, errs[0])
}
assert.Equal(t, fiber.StatusNotAcceptable, statusCode)
fmt.Println("Recovery link with expired sign handled correctly")
})
}
//func TestRecoveryLinkHandler(t *testing.T) {
// client := fiber.AcquireClient()
//
// t.Run("HandleRecoveryLink_ValidSign", func(t *testing.T) {
// req := client.Get(BaseUrl + "/recover/" + ValidSign)
// statusCode, _, errs := req.Bytes()
// if len(errs) != 0 {
// assert.NoError(t, errs[0])
// }
//
// assert.Equal(t, fiber.StatusOK, statusCode)
//
// fmt.Println("Recovery link handled successfully")
// })
// time.Sleep(15 * time.Minute)
// t.Run("HandleRecoveryLink_ExpiredSign", func(t *testing.T) {
// req := client.Get(BaseUrl + "/recover/" + ValidSign)
// statusCode, _, errs := req.Bytes()
// if len(errs) != 0 {
// assert.NoError(t, errs[0])
// }
// assert.Equal(t, fiber.StatusNotAcceptable, statusCode)
// fmt.Println("Recovery link with expired sign handled correctly")
// })
//}