codeword/tests/e2e/promo_test.go

186 lines
4.3 KiB
Go
Raw Normal View History

2024-01-25 16:20:41 +00:00
package e2e
2024-01-26 11:47:44 +00:00
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"])
})
}