add some one
This commit is contained in:
parent
ba4d3d2b01
commit
2508b0fc5e
@ -47,6 +47,10 @@ func (r *RecoveryController) HandleRecoveryRequest(c *fiber.Ctx) error {
|
|||||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Bad Request"})
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Bad Request"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if req.Email == "" {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "email is required"})
|
||||||
|
}
|
||||||
|
|
||||||
referralURL := c.Get("Referrer")
|
referralURL := c.Get("Referrer")
|
||||||
|
|
||||||
if req.RedirectionURL == "" && referralURL != "" {
|
if req.RedirectionURL == "" && referralURL != "" {
|
||||||
@ -113,5 +117,14 @@ func (r *RecoveryController) HandleRecoveryLink(c *fiber.Ctx) error {
|
|||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error"})
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error"})
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(tokens)
|
c.Cookie(&fiber.Cookie{
|
||||||
|
Name: "refreshToken",
|
||||||
|
Value: tokens["refreshToken"],
|
||||||
|
Domain: ".pena.digital",
|
||||||
|
Expires: time.Now().Add(30 * 24 * time.Hour),
|
||||||
|
Secure: true,
|
||||||
|
HTTPOnly: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
return c.Redirect(record.SignUrl + "?auth=" + tokens["accessToken"])
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,34 +3,55 @@ package e2e
|
|||||||
import (
|
import (
|
||||||
"codeword/internal/models"
|
"codeword/internal/models"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"testing"
|
"fmt"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const BaseUrl = "http://localhost:8080"
|
||||||
|
|
||||||
|
// post handler
|
||||||
func TestRecoveryHandler(t *testing.T) {
|
func TestRecoveryHandler(t *testing.T) {
|
||||||
client := fiber.AcquireClient()
|
client := fiber.AcquireClient()
|
||||||
|
|
||||||
t.Run("HandleRecoveryRequest", func(t *testing.T) {
|
t.Run("HandleRecoveryRequest", func(t *testing.T) {
|
||||||
reqBody := models.RecoveryRequest{
|
reqBody := models.RecoveryRequest{
|
||||||
Email: "test@example.com",
|
Email: "admin",
|
||||||
RedirectionURL: "http://redirect.com",
|
RedirectionURL: "http://redirect.com",
|
||||||
}
|
}
|
||||||
reqJSON, _ := json.Marshal(reqBody)
|
reqJSON, _ := json.Marshal(reqBody)
|
||||||
|
|
||||||
req := client.Post("/recover").Set("Content-Type", "application/json").Body(reqJSON)
|
req := client.Post(BaseUrl+"/recover").Set("Content-Type", "application/json").Body(reqJSON)
|
||||||
|
|
||||||
statusCode, resBody, errs := req.Bytes()
|
statusCode, resBody, errs := req.Bytes()
|
||||||
assert.NoError(t, errs[0])
|
if len(errs) != 0 {
|
||||||
|
assert.NoError(t, errs[0])
|
||||||
|
}
|
||||||
|
|
||||||
assert.Equal(t, fiber.StatusOK, statusCode)
|
assert.Equal(t, fiber.StatusOK, statusCode)
|
||||||
|
|
||||||
var responseMap map[string]interface{}
|
var responseMap map[string]interface{}
|
||||||
err := json.Unmarshal(resBody, &responseMap)
|
err := json.Unmarshal(resBody, &responseMap)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
fmt.Println(responseMap)
|
||||||
|
})
|
||||||
|
|
||||||
assert.Equal(t, "Recovery email sent successfully", responseMap["message"])
|
t.Run("HandleRecoveryRequest", func(t *testing.T) {
|
||||||
|
reqBody := models.RecoveryRequest{
|
||||||
|
Email: "admin",
|
||||||
|
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) {
|
t.Run("HandleRecoveryRequest_MissingEmail", func(t *testing.T) {
|
||||||
@ -39,10 +60,12 @@ func TestRecoveryHandler(t *testing.T) {
|
|||||||
}
|
}
|
||||||
reqJSON, _ := json.Marshal(reqBody)
|
reqJSON, _ := json.Marshal(reqBody)
|
||||||
|
|
||||||
req := client.Post("/recover").Set("Content-Type", "application/json").Body(reqJSON)
|
req := client.Post(BaseUrl+"/recover").Set("Content-Type", "application/json").Body(reqJSON)
|
||||||
|
|
||||||
statusCode, _, errs := req.Bytes()
|
statusCode, _, errs := req.Bytes()
|
||||||
assert.NoError(t, errs[0])
|
if len(errs) != 0 {
|
||||||
|
assert.NoError(t, errs[0])
|
||||||
|
}
|
||||||
|
|
||||||
assert.Equal(t, fiber.StatusBadRequest, statusCode)
|
assert.Equal(t, fiber.StatusBadRequest, statusCode)
|
||||||
})
|
})
|
||||||
@ -54,11 +77,15 @@ func TestRecoveryHandler(t *testing.T) {
|
|||||||
}
|
}
|
||||||
reqJSON, _ := json.Marshal(reqBody)
|
reqJSON, _ := json.Marshal(reqBody)
|
||||||
|
|
||||||
req := client.Post("/recover").Set("Content-Type", "application/json").Body(reqJSON)
|
req := client.Post(BaseUrl+"/recover").Set("Content-Type", "application/json").Body(reqJSON)
|
||||||
|
|
||||||
statusCode, _, errs := req.Bytes()
|
statusCode, _, errs := req.Bytes()
|
||||||
assert.NoError(t, errs[0])
|
if len(errs) != 0 {
|
||||||
|
assert.NoError(t, errs[0])
|
||||||
|
}
|
||||||
|
|
||||||
assert.Equal(t, fiber.StatusNotFound, statusCode)
|
assert.Equal(t, fiber.StatusNotFound, statusCode)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get handler
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user