589 lines
15 KiB
Go
589 lines
15 KiB
Go
package e2e
|
|
|
|
import (
|
|
"codeword/internal/models"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/pioz/faker"
|
|
"github.com/stretchr/testify/assert"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"strconv"
|
|
"testing"
|
|
)
|
|
|
|
var promoID string
|
|
var fastLink string
|
|
|
|
// CreatePromoCode
|
|
func TestCreatePromoCode(t *testing.T) {
|
|
client := fiber.AcquireClient()
|
|
|
|
t.Run("CreatePromoCode-success", func(t *testing.T) {
|
|
for i := 0; i < 10; i++ {
|
|
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)
|
|
if i != 0 {
|
|
reqBody.Codeword = reqBody.Codeword + faker.String() + strconv.Itoa(i)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// CreateFastLink
|
|
func TestCreateFastLink(t *testing.T) {
|
|
client := fiber.AcquireClient()
|
|
|
|
t.Run("CreateFastLink-success", func(t *testing.T) {
|
|
reqBody := map[string]string{"id": promoID}
|
|
|
|
reqJSON, _ := json.Marshal(reqBody)
|
|
|
|
req := client.Post(BaseUrl+"/promocode/fastlink").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 map[string]string
|
|
err := json.Unmarshal(resBody, &response)
|
|
assert.NoError(t, err)
|
|
fastLink = response["fastlink"]
|
|
fmt.Println(response["fastlink"])
|
|
})
|
|
|
|
t.Run("CreateFastLink-missing promoCodeID", func(t *testing.T) {
|
|
req := client.Post(BaseUrl+"/promocode/fastlink").Set("Content-Type", "application/json").Body([]byte(`{}`))
|
|
|
|
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("CreateFastLink-promocode not found", func(t *testing.T) {
|
|
reqBody := map[string]string{"id": primitive.NewObjectID().Hex()}
|
|
|
|
reqJSON, _ := json.Marshal(reqBody)
|
|
|
|
req := client.Post(BaseUrl+"/promocode/fastlink").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.StatusNotFound, statusCode)
|
|
|
|
var response map[string]interface{}
|
|
err := json.Unmarshal(resBody, &response)
|
|
assert.NoError(t, err)
|
|
fmt.Println(response["error"])
|
|
})
|
|
}
|
|
|
|
// GetPromoCodesList
|
|
func TestGetPromoCodesList(t *testing.T) {
|
|
client := fiber.AcquireClient()
|
|
|
|
t.Run("GetPromoCodesList-success", func(t *testing.T) {
|
|
reqBody := models.GetPromoCodesListReq{
|
|
Page: 0,
|
|
Limit: 10,
|
|
Filter: models.GetPromoCodesListReqFilter{
|
|
Text: "example",
|
|
Active: true,
|
|
},
|
|
}
|
|
|
|
reqJSON, _ := json.Marshal(reqBody)
|
|
req := client.Post(BaseUrl+"/promocode/getList").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.GetPromoCodesListResp
|
|
err := json.Unmarshal(resBody, &response)
|
|
assert.NoError(t, err)
|
|
fmt.Println(response)
|
|
})
|
|
|
|
t.Run("GetPromoCodesList-invalid request payload", func(t *testing.T) {
|
|
req := client.Post(BaseUrl+"/promocode/getList").Set("Content-Type", "application/json").Body([]byte("invalid json"))
|
|
|
|
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"])
|
|
})
|
|
}
|
|
|
|
// ActivatePromoCode
|
|
func TestActivatePromoCode(t *testing.T) {
|
|
client := fiber.AcquireClient()
|
|
|
|
t.Run("ActivatePromoCode-success codeword", func(t *testing.T) {
|
|
reqBody := models.ActivateReq{
|
|
UserID: ExampleUserID,
|
|
Codeword: "example",
|
|
}
|
|
|
|
reqJSON, _ := json.Marshal(reqBody)
|
|
|
|
req := client.Post(BaseUrl+"/promocode/activate").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.ActivateResp
|
|
err := json.Unmarshal(resBody, &response)
|
|
assert.NoError(t, err)
|
|
fmt.Println(response)
|
|
})
|
|
|
|
t.Run("ActivatePromoCode-success fastLink", func(t *testing.T) {
|
|
reqBody := models.ActivateReq{
|
|
UserID: ExampleUserID,
|
|
FastLink: fastLink,
|
|
}
|
|
|
|
reqJSON, _ := json.Marshal(reqBody)
|
|
|
|
req := client.Post(BaseUrl+"/promocode/activate").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.ActivateResp
|
|
err := json.Unmarshal(resBody, &response)
|
|
assert.NoError(t, err)
|
|
fmt.Println(response)
|
|
})
|
|
|
|
t.Run("ActivatePromoCode-missing userid", func(t *testing.T) {
|
|
reqBody := models.ActivateReq{
|
|
Codeword: "example",
|
|
}
|
|
reqJSON, _ := json.Marshal(reqBody)
|
|
|
|
req := client.Post(BaseUrl+"/promocode/activate").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("ActivatePromoCode-missing codeword and fastlink", func(t *testing.T) {
|
|
reqBody := models.ActivateReq{
|
|
UserID: ExampleUserID,
|
|
}
|
|
|
|
reqJSON, _ := json.Marshal(reqBody)
|
|
|
|
req := client.Post(BaseUrl+"/promocode/activate").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("ActivatePromoCode-promocode not found", func(t *testing.T) {
|
|
reqBody := models.ActivateReq{
|
|
UserID: ExampleUserID,
|
|
Codeword: "none",
|
|
}
|
|
|
|
reqJSON, _ := json.Marshal(reqBody)
|
|
|
|
req := client.Post(BaseUrl+"/promocode/activate").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.StatusNotFound, statusCode)
|
|
|
|
var response map[string]interface{}
|
|
err := json.Unmarshal(resBody, &response)
|
|
assert.NoError(t, err)
|
|
fmt.Println(response["error"])
|
|
})
|
|
}
|
|
|
|
// GetPromoStats
|
|
func TestGetPromoStats(t *testing.T) {
|
|
client := fiber.AcquireClient()
|
|
|
|
t.Run("GetAllStats", func(t *testing.T) {
|
|
req := client.Get(BaseUrl + "/promocode/stats")
|
|
|
|
statusCode, resBody, errs := req.Bytes()
|
|
if len(errs) != 0 {
|
|
assert.NoError(t, errs[0])
|
|
}
|
|
assert.Equal(t, fiber.StatusOK, statusCode)
|
|
|
|
var response []models.PromoCodeStats
|
|
err := json.Unmarshal(resBody, &response)
|
|
assert.NoError(t, err)
|
|
fmt.Println(response)
|
|
})
|
|
}
|
|
|
|
// DeletePromoCode
|
|
func TestDeletePromoCode(t *testing.T) {
|
|
client := fiber.AcquireClient()
|
|
|
|
t.Run("DeletePromoCode-success", func(t *testing.T) {
|
|
|
|
req := client.Delete(BaseUrl + "/promocode/" + promoID)
|
|
|
|
statusCode, _, errs := req.Bytes()
|
|
if len(errs) != 0 {
|
|
assert.NoError(t, errs[0])
|
|
}
|
|
|
|
assert.Equal(t, fiber.StatusOK, statusCode)
|
|
})
|
|
|
|
t.Run("DeletePromoCode-promocode not found", func(t *testing.T) {
|
|
|
|
req := client.Delete(BaseUrl + "/promocode/" + primitive.NewObjectID().Hex())
|
|
|
|
statusCode, resBody, errs := req.Bytes()
|
|
if len(errs) != 0 {
|
|
assert.Error(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"])
|
|
})
|
|
}
|