303 lines
7.4 KiB
Go
303 lines
7.4 KiB
Go
package e2e
|
|
|
|
import (
|
|
"codeword/internal/models"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/stretchr/testify/assert"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"testing"
|
|
)
|
|
|
|
var promoID string
|
|
|
|
// CreatePromoCode
|
|
func TestCreatePromoCode(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)
|
|
promoID = response.ID.Hex()
|
|
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"])
|
|
})
|
|
}
|
|
|
|
// 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
|
|
}
|