add another tests

This commit is contained in:
Pavel 2024-01-26 18:37:07 +03:00
parent 82c3cf2679
commit bfc332fd77
3 changed files with 318 additions and 45 deletions

@ -148,6 +148,10 @@ func (p *PromoCodeController) CreateFastLink(c *fiber.Ctx) error {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
}
if req.PromoCodeID == "" {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "PromoCode ID is required"})
}
fastLink, err := p.promoCodeService.CreateFastLink(c.Context(), req.PromoCodeID)
if err != nil {
p.logger.Error("Failed to create fastlink", zap.Error(err))

@ -5,19 +5,23 @@ import (
"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) {
jsonString := `{
for i := 0; i < 10; i++ {
jsonString := `{
"codeword": "example",
"description": "Example description",
"greetings": "Example greetings",
@ -40,26 +44,31 @@ func TestCreatePromoCode(t *testing.T) {
"delete": false
}`
var reqBody models.PromoCode
err := json.Unmarshal([]byte(jsonString), &reqBody)
assert.NoError(t, err)
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)
reqJSON, _ := json.Marshal(reqBody)
req := client.Post(BaseUrl+"/promocode/create").Set("Content-Type", "application/json").Body(reqJSON)
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])
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)
}
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 := `{
@ -300,3 +309,260 @@ func toInt64(i int64) *int64 {
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"])
})
}
// 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"])
})
}

@ -7,11 +7,14 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
// todo добавить другие константы такие как exampleUserID
const (
BaseUrl = "http://localhost:8080"
ValidSign = "GSiyv5zBITGshqnvYLHKtXE3e4yZjKGvruOVFWuUuj9Nvaps28-Zt6RDq9n47eaNUlay1-nUVld61I3xoAAgCA==65b286c2f13095d96792079d"
BaseUrl = "http://localhost:8080"
ValidSign = "GSiyv5zBITGshqnvYLHKtXE3e4yZjKGvruOVFWuUuj9Nvaps28-Zt6RDq9n47eaNUlay1-nUVld61I3xoAAgCA==65b286c2f13095d96792079d"
ExampleUserID = "6597babdd1ba7e2dbd32d7e3"
)
// post handler
@ -40,9 +43,9 @@ func TestRecoveryHandler(t *testing.T) {
fmt.Println(responseMap)
})
t.Run("HandleRecoveryRequest", func(t *testing.T) {
t.Run("HandleRecoveryRequest-AlreadyReported", func(t *testing.T) {
reqBody := models.RecoveryRequest{
Email: "admin",
Email: "adminSOLO",
RedirectionURL: "http://redirect.com",
}
reqJSON, _ := json.Marshal(reqBody)
@ -92,28 +95,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")
})
}