123 lines
3.3 KiB
Go
123 lines
3.3 KiB
Go
package e2e
|
|
|
|
import (
|
|
"codeword/internal/models"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// todo добавить другие константы такие как exampleUserID
|
|
const (
|
|
BaseUrl = "http://localhost:8080"
|
|
ValidSign = "GSiyv5zBITGshqnvYLHKtXE3e4yZjKGvruOVFWuUuj9Nvaps28-Zt6RDq9n47eaNUlay1-nUVld61I3xoAAgCA==65b286c2f13095d96792079d"
|
|
ExampleUserID = "6597babdd1ba7e2dbd32d7e3"
|
|
)
|
|
|
|
// post handler
|
|
func TestRecoveryHandler(t *testing.T) {
|
|
client := fiber.AcquireClient()
|
|
|
|
t.Run("HandleRecoveryRequest", func(t *testing.T) {
|
|
reqBody := models.RecoveryRequest{
|
|
Email: "adminSOLO",
|
|
RedirectionURL: "http://redirect.com",
|
|
}
|
|
reqJSON, _ := json.Marshal(reqBody)
|
|
|
|
req := client.Post(BaseUrl+"/recover").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 responseMap map[string]interface{}
|
|
err := json.Unmarshal(resBody, &responseMap)
|
|
assert.NoError(t, err)
|
|
fmt.Println(responseMap)
|
|
})
|
|
|
|
t.Run("HandleRecoveryRequest-AlreadyReported", func(t *testing.T) {
|
|
reqBody := models.RecoveryRequest{
|
|
Email: "adminSOLO",
|
|
RedirectionURL: "http://redirect.com",
|
|
}
|
|
reqJSON, _ := json.Marshal(reqBody)
|
|
|
|
req := client.Post(BaseUrl+"/recover").Set("Content-Type", "application/json").Body(reqJSON)
|
|
|
|
statusCode, _, errs := req.Bytes()
|
|
if len(errs) != 0 {
|
|
assert.NoError(t, errs[0])
|
|
}
|
|
|
|
assert.Equal(t, fiber.StatusAlreadyReported, statusCode)
|
|
})
|
|
|
|
t.Run("HandleRecoveryRequest_MissingEmail", func(t *testing.T) {
|
|
reqBody := models.RecoveryRequest{
|
|
RedirectionURL: "http://redirect.com",
|
|
}
|
|
reqJSON, _ := json.Marshal(reqBody)
|
|
|
|
req := client.Post(BaseUrl+"/recover").Set("Content-Type", "application/json").Body(reqJSON)
|
|
|
|
statusCode, _, errs := req.Bytes()
|
|
if len(errs) != 0 {
|
|
assert.NoError(t, errs[0])
|
|
}
|
|
|
|
assert.Equal(t, fiber.StatusBadRequest, statusCode)
|
|
})
|
|
|
|
t.Run("HandleRecoveryRequest_UserNotFound", func(t *testing.T) {
|
|
reqBody := models.RecoveryRequest{
|
|
Email: "nonexistent@example.com",
|
|
RedirectionURL: "http://redirect.com",
|
|
}
|
|
reqJSON, _ := json.Marshal(reqBody)
|
|
|
|
req := client.Post(BaseUrl+"/recover").Set("Content-Type", "application/json").Body(reqJSON)
|
|
|
|
statusCode, _, errs := req.Bytes()
|
|
if len(errs) != 0 {
|
|
assert.NoError(t, errs[0])
|
|
}
|
|
|
|
assert.Equal(t, fiber.StatusNotFound, statusCode)
|
|
})
|
|
}
|
|
|
|
// 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")
|
|
})
|
|
}
|