codeword/tests/e2e/recover_test.go

123 lines
3.3 KiB
Go
Raw Normal View History

2024-01-22 15:09:36 +00:00
package e2e
import (
"codeword/internal/models"
"encoding/json"
2024-01-22 19:36:19 +00:00
"fmt"
2024-01-22 15:09:36 +00:00
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
2024-01-22 19:36:19 +00:00
"testing"
2024-01-26 15:37:07 +00:00
"time"
2024-01-22 15:09:36 +00:00
)
2024-01-26 15:37:07 +00:00
// todo добавить другие константы такие как exampleUserID
2024-01-25 16:20:41 +00:00
const (
2024-01-26 15:37:07 +00:00
BaseUrl = "http://localhost:8080"
ValidSign = "GSiyv5zBITGshqnvYLHKtXE3e4yZjKGvruOVFWuUuj9Nvaps28-Zt6RDq9n47eaNUlay1-nUVld61I3xoAAgCA==65b286c2f13095d96792079d"
ExampleUserID = "6597babdd1ba7e2dbd32d7e3"
2024-01-25 16:20:41 +00:00
)
2024-01-22 19:36:19 +00:00
// post handler
2024-01-22 15:09:36 +00:00
func TestRecoveryHandler(t *testing.T) {
client := fiber.AcquireClient()
t.Run("HandleRecoveryRequest", func(t *testing.T) {
reqBody := models.RecoveryRequest{
2024-01-26 12:24:53 +00:00
Email: "adminSOLO",
2024-01-22 15:09:36 +00:00
RedirectionURL: "http://redirect.com",
}
reqJSON, _ := json.Marshal(reqBody)
2024-01-22 19:36:19 +00:00
req := client.Post(BaseUrl+"/recover").Set("Content-Type", "application/json").Body(reqJSON)
2024-01-22 15:09:36 +00:00
statusCode, resBody, errs := req.Bytes()
2024-01-22 19:36:19 +00:00
if len(errs) != 0 {
assert.NoError(t, errs[0])
}
2024-01-22 15:09:36 +00:00
assert.Equal(t, fiber.StatusOK, statusCode)
var responseMap map[string]interface{}
err := json.Unmarshal(resBody, &responseMap)
assert.NoError(t, err)
2024-01-22 19:36:19 +00:00
fmt.Println(responseMap)
})
2024-01-22 15:09:36 +00:00
2024-01-26 15:37:07 +00:00
t.Run("HandleRecoveryRequest-AlreadyReported", func(t *testing.T) {
2024-01-22 19:36:19 +00:00
reqBody := models.RecoveryRequest{
2024-01-26 15:37:07 +00:00
Email: "adminSOLO",
2024-01-22 19:36:19 +00:00
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)
2024-01-22 15:09:36 +00:00
})
t.Run("HandleRecoveryRequest_MissingEmail", func(t *testing.T) {
reqBody := models.RecoveryRequest{
RedirectionURL: "http://redirect.com",
}
reqJSON, _ := json.Marshal(reqBody)
2024-01-22 19:36:19 +00:00
req := client.Post(BaseUrl+"/recover").Set("Content-Type", "application/json").Body(reqJSON)
2024-01-22 15:09:36 +00:00
statusCode, _, errs := req.Bytes()
2024-01-22 19:36:19 +00:00
if len(errs) != 0 {
assert.NoError(t, errs[0])
}
2024-01-22 15:09:36 +00:00
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)
2024-01-22 19:36:19 +00:00
req := client.Post(BaseUrl+"/recover").Set("Content-Type", "application/json").Body(reqJSON)
2024-01-22 15:09:36 +00:00
statusCode, _, errs := req.Bytes()
2024-01-22 19:36:19 +00:00
if len(errs) != 0 {
assert.NoError(t, errs[0])
}
2024-01-22 15:09:36 +00:00
assert.Equal(t, fiber.StatusNotFound, statusCode)
})
}
2024-01-22 19:36:19 +00:00
// get handler
2024-01-26 15:37:07 +00:00
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")
})
}