refactor: clean code and use new request helpers
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package integration
|
package integration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -10,7 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"goyco/internal/middleware"
|
"goyco/internal/database"
|
||||||
"goyco/internal/testutils"
|
"goyco/internal/testutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,46 +19,34 @@ func TestIntegration_EndToEndUserJourneys(t *testing.T) {
|
|||||||
t.Run("Complete_Registration_To_Post_Creation_Journey", func(t *testing.T) {
|
t.Run("Complete_Registration_To_Post_Creation_Journey", func(t *testing.T) {
|
||||||
ctx.Suite.EmailSender.Reset()
|
ctx.Suite.EmailSender.Reset()
|
||||||
|
|
||||||
registerBody := map[string]string{
|
registerRequest := makePostRequestWithJSON(t, ctx.Router, "/api/auth/register", map[string]any{
|
||||||
"username": "journey_user",
|
"username": "journey_user",
|
||||||
"email": "journey@example.com",
|
"email": "journey@example.com",
|
||||||
"password": "SecurePass123!",
|
"password": "SecurePass123!",
|
||||||
}
|
})
|
||||||
body, _ := json.Marshal(registerBody)
|
|
||||||
registerReq := httptest.NewRequest("POST", "/api/auth/register", bytes.NewBuffer(body))
|
|
||||||
registerReq.Header.Set("Content-Type", "application/json")
|
|
||||||
registerRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(registerRec, registerReq)
|
|
||||||
|
|
||||||
assertStatus(t, registerRec, http.StatusCreated)
|
assertStatus(t, registerRequest, http.StatusCreated)
|
||||||
|
|
||||||
verificationToken := ctx.Suite.EmailSender.VerificationToken()
|
verificationToken := ctx.Suite.EmailSender.VerificationToken()
|
||||||
if verificationToken == "" {
|
if verificationToken == "" {
|
||||||
t.Fatal("Verification token not sent")
|
t.Fatal("Verification token not sent")
|
||||||
}
|
}
|
||||||
|
|
||||||
confirmReq := httptest.NewRequest("GET", "/api/auth/confirm?token="+url.QueryEscape(verificationToken), nil)
|
confirmRequest := makeGetRequest(t, ctx.Router, "/api/auth/confirm?token="+url.QueryEscape(verificationToken))
|
||||||
confirmRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(confirmRec, confirmReq)
|
|
||||||
|
|
||||||
assertStatus(t, confirmRec, http.StatusOK)
|
assertStatus(t, confirmRequest, http.StatusOK)
|
||||||
|
|
||||||
loginBody := map[string]string{
|
loginRequest := makePostRequestWithJSON(t, ctx.Router, "/api/auth/login", map[string]any{
|
||||||
"username": "journey_user",
|
"username": "journey_user",
|
||||||
"password": "SecurePass123!",
|
"password": "SecurePass123!",
|
||||||
}
|
})
|
||||||
loginBodyBytes, _ := json.Marshal(loginBody)
|
|
||||||
loginReq := httptest.NewRequest("POST", "/api/auth/login", bytes.NewBuffer(loginBodyBytes))
|
|
||||||
loginReq.Header.Set("Content-Type", "application/json")
|
|
||||||
loginRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(loginRec, loginReq)
|
|
||||||
|
|
||||||
loginResponse := assertJSONResponse(t, loginRec, http.StatusOK)
|
loginResponse := assertJSONResponse(t, loginRequest, http.StatusOK)
|
||||||
if loginResponse == nil {
|
if loginResponse == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data, ok := loginResponse["data"].(map[string]any)
|
data, ok := getDataFromResponse(loginResponse)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("Login response missing data")
|
t.Fatal("Login response missing data")
|
||||||
}
|
}
|
||||||
@@ -67,8 +54,8 @@ func TestIntegration_EndToEndUserJourneys(t *testing.T) {
|
|||||||
var token string
|
var token string
|
||||||
if accessToken, ok := data["access_token"].(string); ok && accessToken != "" {
|
if accessToken, ok := data["access_token"].(string); ok && accessToken != "" {
|
||||||
token = accessToken
|
token = accessToken
|
||||||
} else if tokenVal, ok := data["token"].(string); ok && tokenVal != "" {
|
} else if tokenValue, ok := data["token"].(string); ok && tokenValue != "" {
|
||||||
token = tokenVal
|
token = tokenValue
|
||||||
} else {
|
} else {
|
||||||
t.Fatal("Login response missing access_token or token")
|
t.Fatal("Login response missing access_token or token")
|
||||||
}
|
}
|
||||||
@@ -90,25 +77,19 @@ func TestIntegration_EndToEndUserJourneys(t *testing.T) {
|
|||||||
t.Fatalf("Login response missing user.id. Data: %+v", data)
|
t.Fatalf("Login response missing user.id. Data: %+v", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
postBody := map[string]string{
|
postBodyBytes, _ := json.Marshal(map[string]any{
|
||||||
"title": "Journey Test Post",
|
"title": "Journey Test Post",
|
||||||
"url": "https://example.com/journey",
|
"url": "https://example.com/journey",
|
||||||
"content": "Test content",
|
"content": "Test content",
|
||||||
}
|
})
|
||||||
postBodyBytes, _ := json.Marshal(postBody)
|
postRequest := makeAuthenticatedRequest(t, ctx.Router, "POST", "/api/posts", postBodyBytes, &authenticatedUser{User: &database.User{ID: uint(userID)}, Token: token}, nil)
|
||||||
postReq := httptest.NewRequest("POST", "/api/posts", bytes.NewBuffer(postBodyBytes))
|
|
||||||
postReq.Header.Set("Content-Type", "application/json")
|
|
||||||
postReq.Header.Set("Authorization", "Bearer "+token)
|
|
||||||
postReq = testutils.WithUserContext(postReq, middleware.UserIDKey, uint(userID))
|
|
||||||
postRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(postRec, postReq)
|
|
||||||
|
|
||||||
postResponse := assertJSONResponse(t, postRec, http.StatusCreated)
|
postResponse := assertJSONResponse(t, postRequest, http.StatusCreated)
|
||||||
if postResponse == nil {
|
if postResponse == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
postData, ok := postResponse["data"].(map[string]any)
|
postData, ok := getDataFromResponse(postResponse)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("Post response missing data")
|
t.Fatal("Post response missing data")
|
||||||
}
|
}
|
||||||
@@ -118,56 +99,39 @@ func TestIntegration_EndToEndUserJourneys(t *testing.T) {
|
|||||||
t.Fatal("Post response missing id")
|
t.Fatal("Post response missing id")
|
||||||
}
|
}
|
||||||
|
|
||||||
getPostReq := httptest.NewRequest("GET", fmt.Sprintf("/api/posts/%.0f", postID), nil)
|
getPostRequest := makeGetRequest(t, ctx.Router, fmt.Sprintf("/api/posts/%.0f", postID))
|
||||||
getPostRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(getPostRec, getPostReq)
|
|
||||||
|
|
||||||
assertStatus(t, getPostRec, http.StatusOK)
|
assertStatus(t, getPostRequest, http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Complete_Password_Reset_Journey", func(t *testing.T) {
|
t.Run("Complete_Password_Reset_Journey", func(t *testing.T) {
|
||||||
ctx.Suite.EmailSender.Reset()
|
ctx.Suite.EmailSender.Reset()
|
||||||
createAuthenticatedUser(t, ctx.AuthService, ctx.Suite.UserRepo, "reset_journey_user", "reset_journey@example.com")
|
createAuthenticatedUser(t, ctx.AuthService, ctx.Suite.UserRepo, "reset_journey_user", "reset_journey@example.com")
|
||||||
|
|
||||||
resetBody := map[string]string{
|
resetRequest := makePostRequestWithJSON(t, ctx.Router, "/api/auth/forgot-password", map[string]any{
|
||||||
"username_or_email": "reset_journey@example.com",
|
"username_or_email": "reset_journey@example.com",
|
||||||
}
|
})
|
||||||
resetBodyBytes, _ := json.Marshal(resetBody)
|
|
||||||
resetReq := httptest.NewRequest("POST", "/api/auth/forgot-password", bytes.NewBuffer(resetBodyBytes))
|
|
||||||
resetReq.Header.Set("Content-Type", "application/json")
|
|
||||||
resetRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(resetRec, resetReq)
|
|
||||||
|
|
||||||
assertStatus(t, resetRec, http.StatusOK)
|
assertStatus(t, resetRequest, http.StatusOK)
|
||||||
|
|
||||||
resetToken := ctx.Suite.EmailSender.GetLastPasswordResetToken()
|
resetToken := ctx.Suite.EmailSender.GetLastPasswordResetToken()
|
||||||
if resetToken == "" {
|
if resetToken == "" {
|
||||||
t.Fatal("Password reset token not sent")
|
t.Fatal("Password reset token not sent")
|
||||||
}
|
}
|
||||||
|
|
||||||
newPasswordBody := map[string]string{
|
newPasswordRequest := makePostRequestWithJSON(t, ctx.Router, "/api/auth/reset-password", map[string]any{
|
||||||
"token": resetToken,
|
"token": resetToken,
|
||||||
"new_password": "NewSecurePass123!",
|
"new_password": "NewSecurePass123!",
|
||||||
}
|
})
|
||||||
newPasswordBodyBytes, _ := json.Marshal(newPasswordBody)
|
|
||||||
newPasswordReq := httptest.NewRequest("POST", "/api/auth/reset-password", bytes.NewBuffer(newPasswordBodyBytes))
|
|
||||||
newPasswordReq.Header.Set("Content-Type", "application/json")
|
|
||||||
newPasswordRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(newPasswordRec, newPasswordReq)
|
|
||||||
|
|
||||||
assertStatus(t, newPasswordRec, http.StatusOK)
|
assertStatus(t, newPasswordRequest, http.StatusOK)
|
||||||
|
|
||||||
loginBody := map[string]string{
|
loginRequest := makePostRequestWithJSON(t, ctx.Router, "/api/auth/login", map[string]any{
|
||||||
"username": "reset_journey_user",
|
"username": "reset_journey_user",
|
||||||
"password": "NewSecurePass123!",
|
"password": "NewSecurePass123!",
|
||||||
}
|
})
|
||||||
loginBodyBytes, _ := json.Marshal(loginBody)
|
|
||||||
loginReq := httptest.NewRequest("POST", "/api/auth/login", bytes.NewBuffer(loginBodyBytes))
|
|
||||||
loginReq.Header.Set("Content-Type", "application/json")
|
|
||||||
loginRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(loginRec, loginReq)
|
|
||||||
|
|
||||||
assertStatus(t, loginRec, http.StatusOK)
|
assertStatus(t, loginRequest, http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Complete_Vote_And_Unvote_Journey", func(t *testing.T) {
|
t.Run("Complete_Vote_And_Unvote_Journey", func(t *testing.T) {
|
||||||
@@ -176,38 +140,19 @@ func TestIntegration_EndToEndUserJourneys(t *testing.T) {
|
|||||||
|
|
||||||
post := testutils.CreatePostWithRepo(t, ctx.Suite.PostRepo, user.User.ID, "Vote Journey Post", "https://example.com/vote-journey")
|
post := testutils.CreatePostWithRepo(t, ctx.Suite.PostRepo, user.User.ID, "Vote Journey Post", "https://example.com/vote-journey")
|
||||||
|
|
||||||
voteBody := map[string]string{"type": "up"}
|
voteRequest := makePostRequest(t, ctx.Router, fmt.Sprintf("/api/posts/%d/vote", post.ID), map[string]any{"type": "up"}, user, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
||||||
voteBodyBytes, _ := json.Marshal(voteBody)
|
assertStatus(t, voteRequest, http.StatusOK)
|
||||||
voteReq := httptest.NewRequest("POST", fmt.Sprintf("/api/posts/%d/vote", post.ID), bytes.NewBuffer(voteBodyBytes))
|
|
||||||
voteReq.Header.Set("Content-Type", "application/json")
|
|
||||||
voteReq.Header.Set("Authorization", "Bearer "+user.Token)
|
|
||||||
voteReq = testutils.WithUserContext(voteReq, middleware.UserIDKey, user.User.ID)
|
|
||||||
voteReq = testutils.WithURLParams(voteReq, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
|
||||||
voteRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(voteRec, voteReq)
|
|
||||||
|
|
||||||
assertStatus(t, voteRec, http.StatusOK)
|
getVotesRequest := makeAuthenticatedGetRequest(t, ctx.Router, fmt.Sprintf("/api/posts/%d/votes", post.ID), user, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
||||||
|
|
||||||
getVotesReq := httptest.NewRequest("GET", fmt.Sprintf("/api/posts/%d/votes", post.ID), nil)
|
votesResponse := assertJSONResponse(t, getVotesRequest, http.StatusOK)
|
||||||
getVotesReq.Header.Set("Authorization", "Bearer "+user.Token)
|
|
||||||
getVotesReq = testutils.WithUserContext(getVotesReq, middleware.UserIDKey, user.User.ID)
|
|
||||||
getVotesReq = testutils.WithURLParams(getVotesReq, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
|
||||||
getVotesRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(getVotesRec, getVotesReq)
|
|
||||||
|
|
||||||
votesResponse := assertJSONResponse(t, getVotesRec, http.StatusOK)
|
|
||||||
if votesResponse == nil {
|
if votesResponse == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if data, ok := votesResponse["data"].(map[string]any); ok {
|
if data, ok := getDataFromResponse(votesResponse); ok {
|
||||||
if votes, ok := data["votes"].([]any); ok && len(votes) > 0 {
|
if votes, ok := data["votes"].([]any); ok && len(votes) > 0 {
|
||||||
unvoteReq := httptest.NewRequest("DELETE", fmt.Sprintf("/api/posts/%d/vote", post.ID), nil)
|
unvoteRec := makeDeleteRequest(t, ctx.Router, fmt.Sprintf("/api/posts/%d/vote", post.ID), user, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
||||||
unvoteReq.Header.Set("Authorization", "Bearer "+user.Token)
|
|
||||||
unvoteReq = testutils.WithUserContext(unvoteReq, middleware.UserIDKey, user.User.ID)
|
|
||||||
unvoteReq = testutils.WithURLParams(unvoteReq, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
|
||||||
unvoteRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(unvoteRec, unvoteReq)
|
|
||||||
|
|
||||||
assertStatus(t, unvoteRec, http.StatusOK)
|
assertStatus(t, unvoteRec, http.StatusOK)
|
||||||
}
|
}
|
||||||
@@ -221,31 +166,31 @@ func TestIntegration_EndToEndUserJourneys(t *testing.T) {
|
|||||||
|
|
||||||
csrfToken := getCSRFToken(t, pageRouter, "/register")
|
csrfToken := getCSRFToken(t, pageRouter, "/register")
|
||||||
|
|
||||||
reqBody := url.Values{}
|
requestBody := url.Values{}
|
||||||
reqBody.Set("username", "page_journey_user")
|
requestBody.Set("username", "page_journey_user")
|
||||||
reqBody.Set("email", "page_journey@example.com")
|
requestBody.Set("email", "page_journey@example.com")
|
||||||
reqBody.Set("password", "SecurePass123!")
|
requestBody.Set("password", "SecurePass123!")
|
||||||
reqBody.Set("password_confirm", "SecurePass123!")
|
requestBody.Set("password_confirm", "SecurePass123!")
|
||||||
reqBody.Set("csrf_token", csrfToken)
|
requestBody.Set("csrf_token", csrfToken)
|
||||||
|
|
||||||
req := httptest.NewRequest("POST", "/register", strings.NewReader(reqBody.Encode()))
|
request := httptest.NewRequest("POST", "/register", strings.NewReader(requestBody.Encode()))
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
req.AddCookie(&http.Cookie{Name: "csrf_token", Value: csrfToken})
|
request.AddCookie(&http.Cookie{Name: "csrf_token", Value: csrfToken})
|
||||||
rec := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
pageRouter.ServeHTTP(rec, req)
|
pageRouter.ServeHTTP(recorder, request)
|
||||||
|
|
||||||
assertStatusRange(t, rec, http.StatusOK, http.StatusSeeOther)
|
assertStatusRange(t, recorder, http.StatusOK, http.StatusSeeOther)
|
||||||
|
|
||||||
verificationToken := pageCtx.Suite.EmailSender.VerificationToken()
|
verificationToken := pageCtx.Suite.EmailSender.VerificationToken()
|
||||||
if verificationToken == "" {
|
if verificationToken == "" {
|
||||||
t.Fatal("Verification token not sent")
|
t.Fatal("Verification token not sent")
|
||||||
}
|
}
|
||||||
|
|
||||||
confirmReq := httptest.NewRequest("GET", "/confirm?token="+url.QueryEscape(verificationToken), nil)
|
confirmRequest := httptest.NewRequest("GET", "/confirm?token="+url.QueryEscape(verificationToken), nil)
|
||||||
confirmRec := httptest.NewRecorder()
|
confirmRecorder := httptest.NewRecorder()
|
||||||
pageRouter.ServeHTTP(confirmRec, confirmReq)
|
pageRouter.ServeHTTP(confirmRecorder, confirmRequest)
|
||||||
|
|
||||||
assertStatusRange(t, confirmRec, http.StatusOK, http.StatusSeeOther)
|
assertStatusRange(t, confirmRecorder, http.StatusOK, http.StatusSeeOther)
|
||||||
|
|
||||||
loginCSRFToken := getCSRFToken(t, pageRouter, "/login")
|
loginCSRFToken := getCSRFToken(t, pageRouter, "/login")
|
||||||
|
|
||||||
@@ -254,15 +199,15 @@ func TestIntegration_EndToEndUserJourneys(t *testing.T) {
|
|||||||
loginBody.Set("password", "SecurePass123!")
|
loginBody.Set("password", "SecurePass123!")
|
||||||
loginBody.Set("csrf_token", loginCSRFToken)
|
loginBody.Set("csrf_token", loginCSRFToken)
|
||||||
|
|
||||||
loginReq := httptest.NewRequest("POST", "/login", strings.NewReader(loginBody.Encode()))
|
loginRequest := httptest.NewRequest("POST", "/login", strings.NewReader(loginBody.Encode()))
|
||||||
loginReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
loginRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
loginReq.AddCookie(&http.Cookie{Name: "csrf_token", Value: loginCSRFToken})
|
loginRequest.AddCookie(&http.Cookie{Name: "csrf_token", Value: loginCSRFToken})
|
||||||
loginRec := httptest.NewRecorder()
|
loginRecorder := httptest.NewRecorder()
|
||||||
pageRouter.ServeHTTP(loginRec, loginReq)
|
pageRouter.ServeHTTP(loginRecorder, loginRequest)
|
||||||
|
|
||||||
assertStatus(t, loginRec, http.StatusSeeOther)
|
assertStatus(t, loginRecorder, http.StatusSeeOther)
|
||||||
|
|
||||||
loginCookies := loginRec.Result().Cookies()
|
loginCookies := loginRecorder.Result().Cookies()
|
||||||
var authToken string
|
var authToken string
|
||||||
for _, cookie := range loginCookies {
|
for _, cookie := range loginCookies {
|
||||||
if cookie.Name == "auth_token" {
|
if cookie.Name == "auth_token" {
|
||||||
@@ -275,37 +220,31 @@ func TestIntegration_EndToEndUserJourneys(t *testing.T) {
|
|||||||
t.Fatal("Auth token not set after login")
|
t.Fatal("Auth token not set after login")
|
||||||
}
|
}
|
||||||
|
|
||||||
homeReq := httptest.NewRequest("GET", "/", nil)
|
homeRequest := httptest.NewRequest("GET", "/", nil)
|
||||||
homeReq.AddCookie(&http.Cookie{Name: "auth_token", Value: authToken})
|
homeRequest.AddCookie(&http.Cookie{Name: "auth_token", Value: authToken})
|
||||||
homeRec := httptest.NewRecorder()
|
homeRecorder := httptest.NewRecorder()
|
||||||
pageRouter.ServeHTTP(homeRec, homeReq)
|
pageRouter.ServeHTTP(homeRecorder, homeRequest)
|
||||||
|
|
||||||
assertStatus(t, homeRec, http.StatusOK)
|
assertStatus(t, homeRecorder, http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Complete_Post_Creation_And_Update_Journey", func(t *testing.T) {
|
t.Run("Complete_Post_Creation_And_Update_Journey", func(t *testing.T) {
|
||||||
ctx.Suite.EmailSender.Reset()
|
ctx.Suite.EmailSender.Reset()
|
||||||
user := createAuthenticatedUser(t, ctx.AuthService, ctx.Suite.UserRepo, "post_update_journey_user", "post_update_journey@example.com")
|
user := createAuthenticatedUser(t, ctx.AuthService, ctx.Suite.UserRepo, "post_update_journey_user", "post_update_journey@example.com")
|
||||||
|
|
||||||
postBody := map[string]string{
|
postBodyBytes, _ := json.Marshal(map[string]any{
|
||||||
"title": "Original Title",
|
"title": "Original Title",
|
||||||
"url": "https://example.com/original",
|
"url": "https://example.com/original",
|
||||||
"content": "Original content",
|
"content": "Original content",
|
||||||
}
|
})
|
||||||
postBodyBytes, _ := json.Marshal(postBody)
|
postRequest := makeAuthenticatedRequest(t, ctx.Router, "POST", "/api/posts", postBodyBytes, user, nil)
|
||||||
postReq := httptest.NewRequest("POST", "/api/posts", bytes.NewBuffer(postBodyBytes))
|
|
||||||
postReq.Header.Set("Content-Type", "application/json")
|
|
||||||
postReq.Header.Set("Authorization", "Bearer "+user.Token)
|
|
||||||
postReq = testutils.WithUserContext(postReq, middleware.UserIDKey, user.User.ID)
|
|
||||||
postRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(postRec, postReq)
|
|
||||||
|
|
||||||
postResponse := assertJSONResponse(t, postRec, http.StatusCreated)
|
postResponse := assertJSONResponse(t, postRequest, http.StatusCreated)
|
||||||
if postResponse == nil {
|
if postResponse == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
postData, ok := postResponse["data"].(map[string]any)
|
postData, ok := getDataFromResponse(postResponse)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("Post response missing data")
|
t.Fatal("Post response missing data")
|
||||||
}
|
}
|
||||||
@@ -315,34 +254,25 @@ func TestIntegration_EndToEndUserJourneys(t *testing.T) {
|
|||||||
t.Fatal("Post response missing id")
|
t.Fatal("Post response missing id")
|
||||||
}
|
}
|
||||||
|
|
||||||
updateBody := map[string]string{
|
updateBodyBytes, _ := json.Marshal(map[string]any{
|
||||||
"title": "Updated Title",
|
"title": "Updated Title",
|
||||||
"content": "Updated content",
|
"content": "Updated content",
|
||||||
}
|
})
|
||||||
updateBodyBytes, _ := json.Marshal(updateBody)
|
updateRequest := makeAuthenticatedRequest(t, ctx.Router, "PUT", fmt.Sprintf("/api/posts/%.0f", postID), updateBodyBytes, user, map[string]string{"id": fmt.Sprintf("%.0f", postID)})
|
||||||
updateReq := httptest.NewRequest("PUT", fmt.Sprintf("/api/posts/%.0f", postID), bytes.NewBuffer(updateBodyBytes))
|
|
||||||
updateReq.Header.Set("Content-Type", "application/json")
|
|
||||||
updateReq.Header.Set("Authorization", "Bearer "+user.Token)
|
|
||||||
updateReq = testutils.WithUserContext(updateReq, middleware.UserIDKey, user.User.ID)
|
|
||||||
updateReq = testutils.WithURLParams(updateReq, map[string]string{"id": fmt.Sprintf("%.0f", postID)})
|
|
||||||
updateRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(updateRec, updateReq)
|
|
||||||
|
|
||||||
updateResponse := assertJSONResponse(t, updateRec, http.StatusOK)
|
updateResponse := assertJSONResponse(t, updateRequest, http.StatusOK)
|
||||||
if updateResponse == nil {
|
if updateResponse == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
getPostReq := httptest.NewRequest("GET", fmt.Sprintf("/api/posts/%.0f", postID), nil)
|
getPostRequest := makeGetRequest(t, ctx.Router, fmt.Sprintf("/api/posts/%.0f", postID))
|
||||||
getPostRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(getPostRec, getPostReq)
|
|
||||||
|
|
||||||
getPostResponse := assertJSONResponse(t, getPostRec, http.StatusOK)
|
getPostResponse := assertJSONResponse(t, getPostRequest, http.StatusOK)
|
||||||
if getPostResponse == nil {
|
if getPostResponse == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if data, ok := getPostResponse["data"].(map[string]any); ok {
|
if data, ok := getDataFromResponse(getPostResponse); ok {
|
||||||
if post, ok := data["post"].(map[string]any); ok {
|
if post, ok := data["post"].(map[string]any); ok {
|
||||||
if title, ok := post["title"].(string); ok && title != "Updated Title" {
|
if title, ok := post["title"].(string); ok && title != "Updated Title" {
|
||||||
t.Errorf("Post title not updated: expected 'Updated Title', got '%s'", title)
|
t.Errorf("Post title not updated: expected 'Updated Title', got '%s'", title)
|
||||||
|
|||||||
Reference in New Issue
Block a user