add test create promo
This commit is contained in:
parent
539f88a89d
commit
87b255a356
@ -27,6 +27,10 @@ func (p *PromoCodeController) CreatePromoCode(c *fiber.Ctx) error {
|
|||||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if req.Codeword == "" {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "codeword ID is required"})
|
||||||
|
}
|
||||||
|
|
||||||
createdPromoCode, err := p.promoCodeService.CreatePromoCode(c.Context(), &req)
|
createdPromoCode, err := p.promoCodeService.CreatePromoCode(c.Context(), &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.logger.Error("Failed to create promocode", zap.Error(err))
|
p.logger.Error("Failed to create promocode", zap.Error(err))
|
||||||
|
@ -1 +1,185 @@
|
|||||||
package e2e
|
package e2e
|
||||||
|
|
||||||
|
import (
|
||||||
|
"codeword/internal/models"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPromoCodeControllerE2E(t *testing.T) {
|
||||||
|
client := fiber.AcquireClient()
|
||||||
|
|
||||||
|
t.Run("CreatePromoCode-success", func(t *testing.T) {
|
||||||
|
jsonString := `{
|
||||||
|
"codeword": "example",
|
||||||
|
"description": "Example description",
|
||||||
|
"greetings": "Example greetings",
|
||||||
|
"dueTo": 1734429225,
|
||||||
|
"activationCount": 100,
|
||||||
|
"bonus": {
|
||||||
|
"privilege": {
|
||||||
|
"privilegeID": "examplePrivilegeID",
|
||||||
|
"amount": 50
|
||||||
|
},
|
||||||
|
"discount": {
|
||||||
|
"layer": 1,
|
||||||
|
"factor": 0.2,
|
||||||
|
"target": "exampleTarget",
|
||||||
|
"threshold": 500
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outdated": false,
|
||||||
|
"offLimit": false,
|
||||||
|
"delete": false
|
||||||
|
}`
|
||||||
|
|
||||||
|
var reqBody models.PromoCode
|
||||||
|
err := json.Unmarshal([]byte(jsonString), &reqBody)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
reqJSON, _ := json.Marshal(reqBody)
|
||||||
|
|
||||||
|
req := client.Post(BaseUrl+"/promocode/create").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.StatusCreated, statusCode)
|
||||||
|
|
||||||
|
var response models.PromoCode
|
||||||
|
err = json.Unmarshal(resBody, &response)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
fmt.Println(response)
|
||||||
|
})
|
||||||
|
t.Run("CreatePromoCode-duplicate", func(t *testing.T) {
|
||||||
|
jsonString := `{
|
||||||
|
"codeword": "example",
|
||||||
|
"description": "Example description",
|
||||||
|
"greetings": "Example greetings",
|
||||||
|
"dueTo": 1734429225,
|
||||||
|
"activationCount": 100,
|
||||||
|
"bonus": {
|
||||||
|
"privilege": {
|
||||||
|
"privilegeID": "examplePrivilegeID",
|
||||||
|
"amount": 50
|
||||||
|
},
|
||||||
|
"discount": {
|
||||||
|
"layer": 1,
|
||||||
|
"factor": 0.2,
|
||||||
|
"target": "exampleTarget",
|
||||||
|
"threshold": 500
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outdated": false,
|
||||||
|
"offLimit": false,
|
||||||
|
"delete": false
|
||||||
|
}`
|
||||||
|
|
||||||
|
var reqBody models.PromoCode
|
||||||
|
err := json.Unmarshal([]byte(jsonString), &reqBody)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
reqJSON, _ := json.Marshal(reqBody)
|
||||||
|
|
||||||
|
req := client.Post(BaseUrl+"/promocode/create").Set("Content-Type", "application/json").Body(reqJSON)
|
||||||
|
|
||||||
|
statusCode, resBody, errs := req.Bytes()
|
||||||
|
if len(errs) != 0 {
|
||||||
|
assert.Error(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"])
|
||||||
|
})
|
||||||
|
t.Run("CreatePromoCode-invalid request payload", func(t *testing.T) {
|
||||||
|
jsonString := `{
|
||||||
|
"example": "example",
|
||||||
|
"description": "Example description",
|
||||||
|
"greetings": "Example greetings",
|
||||||
|
"dueTo": 1734429225,
|
||||||
|
"activationCount": 100,
|
||||||
|
"bonus": {
|
||||||
|
"privilege": {
|
||||||
|
"privilegeID": "examplePrivilegeID",
|
||||||
|
"amount": 50
|
||||||
|
},
|
||||||
|
"discount": {
|
||||||
|
"layer": 1,
|
||||||
|
"factor": 0.2,
|
||||||
|
"target": "exampleTarget",
|
||||||
|
"threshold": 500
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outdated": false,
|
||||||
|
"offLimit": false,
|
||||||
|
"delete": false
|
||||||
|
}`
|
||||||
|
|
||||||
|
req := client.Post(BaseUrl+"/promocode/create").Set("Content-Type", "application/json").Body([]byte(jsonString))
|
||||||
|
|
||||||
|
statusCode, resBody, errs := req.Bytes()
|
||||||
|
if len(errs) != 0 {
|
||||||
|
assert.Error(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"])
|
||||||
|
})
|
||||||
|
t.Run("CreatePromoCode-nil codeword", func(t *testing.T) {
|
||||||
|
jsonString := `{
|
||||||
|
"description": "Example description",
|
||||||
|
"greetings": "Example greetings",
|
||||||
|
"dueTo": 1734429225,
|
||||||
|
"activationCount": 100,
|
||||||
|
"bonus": {
|
||||||
|
"privilege": {
|
||||||
|
"privilegeID": "examplePrivilegeID",
|
||||||
|
"amount": 50
|
||||||
|
},
|
||||||
|
"discount": {
|
||||||
|
"layer": 1,
|
||||||
|
"factor": 0.2,
|
||||||
|
"target": "exampleTarget",
|
||||||
|
"threshold": 500
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outdated": false,
|
||||||
|
"offLimit": false,
|
||||||
|
"delete": false
|
||||||
|
}`
|
||||||
|
|
||||||
|
var reqBody models.PromoCode
|
||||||
|
err := json.Unmarshal([]byte(jsonString), &reqBody)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
reqJSON, _ := json.Marshal(reqBody)
|
||||||
|
|
||||||
|
req := client.Post(BaseUrl+"/promocode/create").Set("Content-Type", "application/json").Body(reqJSON)
|
||||||
|
|
||||||
|
statusCode, resBody, errs := req.Bytes()
|
||||||
|
if len(errs) != 0 {
|
||||||
|
assert.Error(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"])
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user