add: Login system
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"GoMembership/internal/config"
|
||||
"GoMembership/internal/constants"
|
||||
@@ -57,8 +59,74 @@ func TestUserController(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
testLoginUser(t)
|
||||
}
|
||||
|
||||
func testLoginUser(t *testing.T) {
|
||||
// This test should run after the user registration test
|
||||
t.Run("LoginUser", func(t *testing.T) {
|
||||
// Test cases
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
wantStatusCode int
|
||||
wantToken bool
|
||||
}{
|
||||
{
|
||||
name: "Valid login",
|
||||
input: `{
|
||||
"email": "john.doe@example.com",
|
||||
"password": "password123"
|
||||
}`,
|
||||
wantStatusCode: http.StatusOK,
|
||||
wantToken: true,
|
||||
},
|
||||
{
|
||||
name: "Invalid email",
|
||||
input: `{
|
||||
"email": "nonexistent@example.com",
|
||||
"password": "password123"
|
||||
}`,
|
||||
wantStatusCode: http.StatusNotFound,
|
||||
wantToken: false,
|
||||
},
|
||||
{
|
||||
name: "Invalid password",
|
||||
input: `{
|
||||
"email": "john.doe@example.com",
|
||||
"password": "wrongpassword"
|
||||
}`,
|
||||
wantStatusCode: http.StatusNotAcceptable,
|
||||
wantToken: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Setup
|
||||
c, w, _ := GetMockedJSONContext([]byte(tt.input), "/login")
|
||||
|
||||
// Execute
|
||||
Uc.LoginUser(c)
|
||||
|
||||
// Assert
|
||||
assert.Equal(t, tt.wantStatusCode, w.Code)
|
||||
|
||||
var response map[string]interface{}
|
||||
err := json.Unmarshal(w.Body.Bytes(), &response)
|
||||
assert.NoError(t, err)
|
||||
|
||||
if tt.wantToken {
|
||||
logger.Info.Printf("Response: %#v", response)
|
||||
assert.Contains(t, response, "token")
|
||||
assert.NotEmpty(t, response["token"])
|
||||
} else {
|
||||
assert.NotContains(t, response, "token")
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
func validateUser(assert bool, wantDBData map[string]interface{}) error {
|
||||
users, err := Uc.Service.GetUsers(wantDBData)
|
||||
if err != nil {
|
||||
@@ -223,7 +291,7 @@ func verifyMail(verificationURL string) error {
|
||||
router := gin.New()
|
||||
router.LoadHTMLGlob(filepath.Join(config.Templates.HTMLPath, "*"))
|
||||
|
||||
router.GET("/backend/verify", Uc.VerifyMailHandler)
|
||||
router.GET("/verify", Uc.VerifyMailHandler)
|
||||
wv := httptest.NewRecorder()
|
||||
cv, _ := gin.CreateTestContext(wv)
|
||||
var err error
|
||||
|
||||
Reference in New Issue
Block a user