mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Update handler integration tests
This commit is contained in:
@@ -23,7 +23,7 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
|
||||
t.Run("get current user", func(t *testing.T) {
|
||||
t.Run("successful get", func(t *testing.T) {
|
||||
rr := h.makeRequest(t, http.MethodGet, "/api/v1/auth/me", nil, h.RegularToken, nil)
|
||||
rr := h.makeRequest(t, http.MethodGet, "/api/v1/auth/me", nil, h.RegularSession, nil)
|
||||
require.Equal(t, http.StatusOK, rr.Code)
|
||||
|
||||
var user models.User
|
||||
@@ -38,7 +38,7 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("unauthorized", func(t *testing.T) {
|
||||
rr := h.makeRequest(t, http.MethodGet, "/api/v1/auth/me", nil, "", nil)
|
||||
rr := h.makeRequest(t, http.MethodGet, "/api/v1/auth/me", nil, nil, nil)
|
||||
assert.Equal(t, http.StatusUnauthorized, rr.Code)
|
||||
})
|
||||
})
|
||||
@@ -49,7 +49,7 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
DisplayName: "Updated Name",
|
||||
}
|
||||
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularToken, nil)
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil)
|
||||
require.Equal(t, http.StatusOK, rr.Code)
|
||||
|
||||
var user models.User
|
||||
@@ -64,7 +64,7 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
CurrentPassword: currentPassword,
|
||||
}
|
||||
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularToken, nil)
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil)
|
||||
require.Equal(t, http.StatusOK, rr.Code)
|
||||
|
||||
var user models.User
|
||||
@@ -80,7 +80,7 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
Email: "anotheremail@test.com",
|
||||
}
|
||||
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularToken, nil)
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil)
|
||||
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
||||
})
|
||||
|
||||
@@ -90,7 +90,7 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
CurrentPassword: "wrongpassword",
|
||||
}
|
||||
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularToken, nil)
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil)
|
||||
assert.Equal(t, http.StatusUnauthorized, rr.Code)
|
||||
})
|
||||
|
||||
@@ -100,7 +100,7 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
NewPassword: "newpassword123",
|
||||
}
|
||||
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularToken, nil)
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil)
|
||||
require.Equal(t, http.StatusOK, rr.Code)
|
||||
|
||||
// Verify can login with new password
|
||||
@@ -109,7 +109,7 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
Password: "newpassword123",
|
||||
}
|
||||
|
||||
rr = h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", loginReq, "", nil)
|
||||
rr = h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", loginReq, nil, nil)
|
||||
assert.Equal(t, http.StatusOK, rr.Code)
|
||||
|
||||
currentPassword = updateReq.NewPassword
|
||||
@@ -120,7 +120,7 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
NewPassword: "newpass123",
|
||||
}
|
||||
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularToken, nil)
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil)
|
||||
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
||||
})
|
||||
|
||||
@@ -130,7 +130,7 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
NewPassword: "newpass123",
|
||||
}
|
||||
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularToken, nil)
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil)
|
||||
assert.Equal(t, http.StatusUnauthorized, rr.Code)
|
||||
})
|
||||
|
||||
@@ -140,7 +140,7 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
NewPassword: "short",
|
||||
}
|
||||
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularToken, nil)
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil)
|
||||
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
||||
})
|
||||
|
||||
@@ -150,7 +150,7 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
CurrentPassword: currentPassword,
|
||||
}
|
||||
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularToken, nil)
|
||||
rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil)
|
||||
assert.Equal(t, http.StatusConflict, rr.Code)
|
||||
})
|
||||
})
|
||||
@@ -164,37 +164,44 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
Role: models.RoleEditor,
|
||||
}
|
||||
|
||||
rr := h.makeRequest(t, http.MethodPost, "/api/v1/admin/users", createReq, h.AdminToken, nil)
|
||||
rr := h.makeRequest(t, http.MethodPost, "/api/v1/admin/users", createReq, h.AdminSession, nil)
|
||||
require.Equal(t, http.StatusOK, rr.Code)
|
||||
|
||||
var newUser models.User
|
||||
err := json.NewDecoder(rr.Body).Decode(&newUser)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Get token for new user
|
||||
// Get session for new user
|
||||
loginReq := handlers.LoginRequest{
|
||||
Email: createReq.Email,
|
||||
Password: createReq.Password,
|
||||
}
|
||||
|
||||
rr = h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", loginReq, "", nil)
|
||||
rr = h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", loginReq, nil, nil)
|
||||
require.Equal(t, http.StatusOK, rr.Code)
|
||||
|
||||
var loginResp handlers.LoginResponse
|
||||
err = json.NewDecoder(rr.Body).Decode(&loginResp)
|
||||
require.NoError(t, err)
|
||||
userToken := loginResp.AccessToken
|
||||
|
||||
// Create a session struct for the new user
|
||||
userSession := &models.Session{
|
||||
ID: loginResp.SessionID,
|
||||
UserID: newUser.ID,
|
||||
RefreshToken: "",
|
||||
ExpiresAt: loginResp.ExpiresAt,
|
||||
}
|
||||
|
||||
t.Run("successful delete", func(t *testing.T) {
|
||||
deleteReq := handlers.DeleteAccountRequest{
|
||||
Password: createReq.Password,
|
||||
}
|
||||
|
||||
rr := h.makeRequest(t, http.MethodDelete, "/api/v1/profile", deleteReq, userToken, nil)
|
||||
rr := h.makeRequest(t, http.MethodDelete, "/api/v1/profile", deleteReq, userSession, nil)
|
||||
require.Equal(t, http.StatusNoContent, rr.Code)
|
||||
|
||||
// Verify user is deleted
|
||||
rr = h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", loginReq, "", nil)
|
||||
rr = h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", loginReq, nil, nil)
|
||||
assert.Equal(t, http.StatusUnauthorized, rr.Code)
|
||||
})
|
||||
|
||||
@@ -203,7 +210,7 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
Password: "wrongpassword",
|
||||
}
|
||||
|
||||
rr := h.makeRequest(t, http.MethodDelete, "/api/v1/profile", deleteReq, h.RegularToken, nil)
|
||||
rr := h.makeRequest(t, http.MethodDelete, "/api/v1/profile", deleteReq, h.RegularSession, nil)
|
||||
assert.Equal(t, http.StatusUnauthorized, rr.Code)
|
||||
})
|
||||
|
||||
@@ -212,7 +219,7 @@ func TestUserHandlers_Integration(t *testing.T) {
|
||||
Password: "admin123", // Admin password from test harness
|
||||
}
|
||||
|
||||
rr := h.makeRequest(t, http.MethodDelete, "/api/v1/profile", deleteReq, h.AdminToken, nil)
|
||||
rr := h.makeRequest(t, http.MethodDelete, "/api/v1/profile", deleteReq, h.AdminSession, nil)
|
||||
assert.Equal(t, http.StatusForbidden, rr.Code)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user