65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
|
package e2e
|
||
|
|
||
|
import (
|
||
|
"codeword/internal/models"
|
||
|
"encoding/json"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/gofiber/fiber/v2"
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestRecoveryHandler(t *testing.T) {
|
||
|
client := fiber.AcquireClient()
|
||
|
|
||
|
t.Run("HandleRecoveryRequest", func(t *testing.T) {
|
||
|
reqBody := models.RecoveryRequest{
|
||
|
Email: "test@example.com",
|
||
|
RedirectionURL: "http://redirect.com",
|
||
|
}
|
||
|
reqJSON, _ := json.Marshal(reqBody)
|
||
|
|
||
|
req := client.Post("/recover").Set("Content-Type", "application/json").Body(reqJSON)
|
||
|
|
||
|
statusCode, resBody, errs := req.Bytes()
|
||
|
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)
|
||
|
|
||
|
assert.Equal(t, "Recovery email sent successfully", responseMap["message"])
|
||
|
})
|
||
|
|
||
|
t.Run("HandleRecoveryRequest_MissingEmail", func(t *testing.T) {
|
||
|
reqBody := models.RecoveryRequest{
|
||
|
RedirectionURL: "http://redirect.com",
|
||
|
}
|
||
|
reqJSON, _ := json.Marshal(reqBody)
|
||
|
|
||
|
req := client.Post("/recover").Set("Content-Type", "application/json").Body(reqJSON)
|
||
|
|
||
|
statusCode, _, errs := req.Bytes()
|
||
|
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("/recover").Set("Content-Type", "application/json").Body(reqJSON)
|
||
|
|
||
|
statusCode, _, errs := req.Bytes()
|
||
|
assert.NoError(t, errs[0])
|
||
|
|
||
|
assert.Equal(t, fiber.StatusNotFound, statusCode)
|
||
|
})
|
||
|
}
|