refactor: clean code and use new request helpers
This commit is contained in:
@@ -19,27 +19,18 @@ func TestIntegration_DataConsistency(t *testing.T) {
|
|||||||
ctx.Suite.EmailSender.Reset()
|
ctx.Suite.EmailSender.Reset()
|
||||||
user := createAuthenticatedUser(t, ctx.AuthService, ctx.Suite.UserRepo, "consistency_user", "consistency@example.com")
|
user := createAuthenticatedUser(t, ctx.AuthService, ctx.Suite.UserRepo, "consistency_user", "consistency@example.com")
|
||||||
|
|
||||||
postBody := map[string]string{
|
request := makePostRequest(t, ctx.Router, "/api/posts", map[string]any{
|
||||||
"title": "Consistency Test Post",
|
"title": "Consistency Test Post",
|
||||||
"url": "https://example.com/consistency",
|
"url": "https://example.com/consistency",
|
||||||
"content": "Test content",
|
"content": "Test content",
|
||||||
}
|
}, user, nil)
|
||||||
body, _ := json.Marshal(postBody)
|
|
||||||
|
|
||||||
req := httptest.NewRequest("POST", "/api/posts", bytes.NewBuffer(body))
|
createResponse := assertJSONResponse(t, request, http.StatusCreated)
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("Authorization", "Bearer "+user.Token)
|
|
||||||
req = testutils.WithUserContext(req, middleware.UserIDKey, user.User.ID)
|
|
||||||
rec := httptest.NewRecorder()
|
|
||||||
|
|
||||||
ctx.Router.ServeHTTP(rec, req)
|
|
||||||
|
|
||||||
createResponse := assertJSONResponse(t, rec, http.StatusCreated)
|
|
||||||
if createResponse == nil {
|
if createResponse == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
postData, ok := createResponse["data"].(map[string]any)
|
postData, ok := getDataFromResponse(createResponse)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("Response missing data")
|
t.Fatal("Response missing data")
|
||||||
}
|
}
|
||||||
@@ -53,16 +44,14 @@ func TestIntegration_DataConsistency(t *testing.T) {
|
|||||||
createdURL := postData["url"]
|
createdURL := postData["url"]
|
||||||
createdContent := postData["content"]
|
createdContent := postData["content"]
|
||||||
|
|
||||||
getReq := httptest.NewRequest("GET", fmt.Sprintf("/api/posts/%.0f", postID), nil)
|
getRequest := makeGetRequest(t, ctx.Router, fmt.Sprintf("/api/posts/%.0f", postID))
|
||||||
getRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(getRec, getReq)
|
|
||||||
|
|
||||||
getResponse := assertJSONResponse(t, getRec, http.StatusOK)
|
getResponse := assertJSONResponse(t, getRequest, http.StatusOK)
|
||||||
if getResponse == nil {
|
if getResponse == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
getPostData, ok := getResponse["data"].(map[string]any)
|
getPostData, ok := getDataFromResponse(getResponse)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("Get response missing data")
|
t.Fatal("Get response missing data")
|
||||||
}
|
}
|
||||||
@@ -96,32 +85,17 @@ func TestIntegration_DataConsistency(t *testing.T) {
|
|||||||
|
|
||||||
post := testutils.CreatePostWithRepo(t, ctx.Suite.PostRepo, user.User.ID, "Vote Consistency Post", "https://example.com/vote-consistency")
|
post := testutils.CreatePostWithRepo(t, ctx.Suite.PostRepo, user.User.ID, "Vote Consistency Post", "https://example.com/vote-consistency")
|
||||||
|
|
||||||
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)})
|
||||||
body, _ := json.Marshal(voteBody)
|
assertStatus(t, voteRequest, http.StatusOK)
|
||||||
|
|
||||||
voteReq := httptest.NewRequest("POST", fmt.Sprintf("/api/posts/%d/vote", post.ID), bytes.NewBuffer(body))
|
getVotesRequest := makeAuthenticatedGetRequest(t, ctx.Router, fmt.Sprintf("/api/posts/%d/votes", post.ID), user, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
||||||
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)
|
votesResponse := assertJSONResponse(t, getVotesRequest, http.StatusOK)
|
||||||
|
|
||||||
getVotesReq := httptest.NewRequest("GET", fmt.Sprintf("/api/posts/%d/votes", post.ID), nil)
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
votesData, ok := votesResponse["data"].(map[string]any)
|
votesData, ok := getDataFromResponse(votesResponse)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("Votes response missing data")
|
t.Fatal("Votes response missing data")
|
||||||
}
|
}
|
||||||
@@ -172,32 +146,21 @@ func TestIntegration_DataConsistency(t *testing.T) {
|
|||||||
|
|
||||||
post := testutils.CreatePostWithRepo(t, ctx.Suite.PostRepo, user.User.ID, "Original Title", "https://example.com/original")
|
post := testutils.CreatePostWithRepo(t, ctx.Suite.PostRepo, user.User.ID, "Original Title", "https://example.com/original")
|
||||||
|
|
||||||
updateBody := map[string]string{
|
updateRequest := makePutRequest(t, ctx.Router, fmt.Sprintf("/api/posts/%d", post.ID), map[string]any{
|
||||||
"title": "Updated Title",
|
"title": "Updated Title",
|
||||||
"content": "Updated content",
|
"content": "Updated content",
|
||||||
}
|
}, user, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
||||||
body, _ := json.Marshal(updateBody)
|
|
||||||
|
|
||||||
updateReq := httptest.NewRequest("PUT", fmt.Sprintf("/api/posts/%d", post.ID), bytes.NewBuffer(body))
|
assertStatus(t, updateRequest, http.StatusOK)
|
||||||
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("%d", post.ID)})
|
|
||||||
updateRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(updateRec, updateReq)
|
|
||||||
|
|
||||||
assertStatus(t, updateRec, http.StatusOK)
|
getRequest := makeGetRequest(t, ctx.Router, fmt.Sprintf("/api/posts/%d", post.ID))
|
||||||
|
|
||||||
getReq := httptest.NewRequest("GET", fmt.Sprintf("/api/posts/%d", post.ID), nil)
|
getResponse := assertJSONResponse(t, getRequest, http.StatusOK)
|
||||||
getRec := httptest.NewRecorder()
|
|
||||||
ctx.Router.ServeHTTP(getRec, getReq)
|
|
||||||
|
|
||||||
getResponse := assertJSONResponse(t, getRec, http.StatusOK)
|
|
||||||
if getResponse == nil {
|
if getResponse == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
getPostData, ok := getResponse["data"].(map[string]any)
|
getPostData, ok := getDataFromResponse(getResponse)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("Get response missing data")
|
t.Fatal("Get response missing data")
|
||||||
}
|
}
|
||||||
@@ -215,18 +178,18 @@ func TestIntegration_DataConsistency(t *testing.T) {
|
|||||||
ctx.Suite.EmailSender.Reset()
|
ctx.Suite.EmailSender.Reset()
|
||||||
user := createAuthenticatedUser(t, ctx.AuthService, ctx.Suite.UserRepo, "user_posts_consistency", "user_posts_consistency@example.com")
|
user := createAuthenticatedUser(t, ctx.AuthService, ctx.Suite.UserRepo, "user_posts_consistency", "user_posts_consistency@example.com")
|
||||||
|
|
||||||
post1 := testutils.CreatePostWithRepo(t, ctx.Suite.PostRepo, user.User.ID, "Post 1", "https://example.com/post1")
|
firstPost := testutils.CreatePostWithRepo(t, ctx.Suite.PostRepo, user.User.ID, "Post 1", "https://example.com/post1")
|
||||||
post2 := testutils.CreatePostWithRepo(t, ctx.Suite.PostRepo, user.User.ID, "Post 2", "https://example.com/post2")
|
secondPost := testutils.CreatePostWithRepo(t, ctx.Suite.PostRepo, user.User.ID, "Post 2", "https://example.com/post2")
|
||||||
|
|
||||||
req := httptest.NewRequest("GET", fmt.Sprintf("/api/users/%d/posts", user.User.ID), nil)
|
request := httptest.NewRequest("GET", fmt.Sprintf("/api/users/%d/posts", user.User.ID), nil)
|
||||||
req.Header.Set("Authorization", "Bearer "+user.Token)
|
request.Header.Set("Authorization", "Bearer "+user.Token)
|
||||||
req = testutils.WithUserContext(req, middleware.UserIDKey, user.User.ID)
|
request = testutils.WithUserContext(request, middleware.UserIDKey, user.User.ID)
|
||||||
req = testutils.WithURLParams(req, map[string]string{"id": fmt.Sprintf("%d", user.User.ID)})
|
request = testutils.WithURLParams(request, map[string]string{"id": fmt.Sprintf("%d", user.User.ID)})
|
||||||
rec := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
|
|
||||||
ctx.Router.ServeHTTP(rec, req)
|
ctx.Router.ServeHTTP(recorder, request)
|
||||||
|
|
||||||
response := assertJSONResponse(t, rec, http.StatusOK)
|
response := assertJSONResponse(t, recorder, http.StatusOK)
|
||||||
if response == nil {
|
if response == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -245,26 +208,26 @@ func TestIntegration_DataConsistency(t *testing.T) {
|
|||||||
t.Errorf("Expected at least 2 posts, got %d", len(posts))
|
t.Errorf("Expected at least 2 posts, got %d", len(posts))
|
||||||
}
|
}
|
||||||
|
|
||||||
foundPost1 := false
|
foundFirstPost := false
|
||||||
foundPost2 := false
|
foundSecondPost := false
|
||||||
for _, post := range posts {
|
for _, post := range posts {
|
||||||
if postMap, ok := post.(map[string]any); ok {
|
if postMap, ok := post.(map[string]any); ok {
|
||||||
if postID, ok := postMap["id"].(float64); ok {
|
if postID, ok := postMap["id"].(float64); ok {
|
||||||
if uint(postID) == post1.ID {
|
if uint(postID) == firstPost.ID {
|
||||||
foundPost1 = true
|
foundFirstPost = true
|
||||||
}
|
}
|
||||||
if uint(postID) == post2.ID {
|
if uint(postID) == secondPost.ID {
|
||||||
foundPost2 = true
|
foundSecondPost = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !foundPost1 {
|
if !foundFirstPost {
|
||||||
t.Error("Post 1 not found in user posts")
|
t.Error("Post 1 not found in user posts")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !foundPost2 {
|
if !foundSecondPost {
|
||||||
t.Error("Post 2 not found in user posts")
|
t.Error("Post 2 not found in user posts")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -275,20 +238,20 @@ func TestIntegration_DataConsistency(t *testing.T) {
|
|||||||
|
|
||||||
post := testutils.CreatePostWithRepo(t, ctx.Suite.PostRepo, user.User.ID, "Delete Consistency Post", "https://example.com/delete-consistency")
|
post := testutils.CreatePostWithRepo(t, ctx.Suite.PostRepo, user.User.ID, "Delete Consistency Post", "https://example.com/delete-consistency")
|
||||||
|
|
||||||
deleteReq := httptest.NewRequest("DELETE", fmt.Sprintf("/api/posts/%d", post.ID), nil)
|
deleteRequest := httptest.NewRequest("DELETE", fmt.Sprintf("/api/posts/%d", post.ID), nil)
|
||||||
deleteReq.Header.Set("Authorization", "Bearer "+user.Token)
|
deleteRequest.Header.Set("Authorization", "Bearer "+user.Token)
|
||||||
deleteReq = testutils.WithUserContext(deleteReq, middleware.UserIDKey, user.User.ID)
|
deleteRequest = testutils.WithUserContext(deleteRequest, middleware.UserIDKey, user.User.ID)
|
||||||
deleteReq = testutils.WithURLParams(deleteReq, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
deleteRequest = testutils.WithURLParams(deleteRequest, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
||||||
deleteRec := httptest.NewRecorder()
|
deleteRecorder := httptest.NewRecorder()
|
||||||
ctx.Router.ServeHTTP(deleteRec, deleteReq)
|
ctx.Router.ServeHTTP(deleteRecorder, deleteRequest)
|
||||||
|
|
||||||
assertStatus(t, deleteRec, http.StatusOK)
|
assertStatus(t, deleteRecorder, http.StatusOK)
|
||||||
|
|
||||||
getReq := httptest.NewRequest("GET", fmt.Sprintf("/api/posts/%d", post.ID), nil)
|
getRequest := httptest.NewRequest("GET", fmt.Sprintf("/api/posts/%d", post.ID), nil)
|
||||||
getRec := httptest.NewRecorder()
|
getRecorder := httptest.NewRecorder()
|
||||||
ctx.Router.ServeHTTP(getRec, getReq)
|
ctx.Router.ServeHTTP(getRecorder, getRequest)
|
||||||
|
|
||||||
assertStatus(t, getRec, http.StatusNotFound)
|
assertStatus(t, getRecorder, http.StatusNotFound)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Vote_Removal_Consistency", func(t *testing.T) {
|
t.Run("Vote_Removal_Consistency", func(t *testing.T) {
|
||||||
@@ -300,33 +263,33 @@ func TestIntegration_DataConsistency(t *testing.T) {
|
|||||||
voteBody := map[string]string{"type": "up"}
|
voteBody := map[string]string{"type": "up"}
|
||||||
body, _ := json.Marshal(voteBody)
|
body, _ := json.Marshal(voteBody)
|
||||||
|
|
||||||
voteReq := httptest.NewRequest("POST", fmt.Sprintf("/api/posts/%d/vote", post.ID), bytes.NewBuffer(body))
|
voteRequest := httptest.NewRequest("POST", fmt.Sprintf("/api/posts/%d/vote", post.ID), bytes.NewBuffer(body))
|
||||||
voteReq.Header.Set("Content-Type", "application/json")
|
voteRequest.Header.Set("Content-Type", "application/json")
|
||||||
voteReq.Header.Set("Authorization", "Bearer "+user.Token)
|
voteRequest.Header.Set("Authorization", "Bearer "+user.Token)
|
||||||
voteReq = testutils.WithUserContext(voteReq, middleware.UserIDKey, user.User.ID)
|
voteRequest = testutils.WithUserContext(voteRequest, middleware.UserIDKey, user.User.ID)
|
||||||
voteReq = testutils.WithURLParams(voteReq, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
voteRequest = testutils.WithURLParams(voteRequest, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
||||||
voteRec := httptest.NewRecorder()
|
voteRecorder := httptest.NewRecorder()
|
||||||
ctx.Router.ServeHTTP(voteRec, voteReq)
|
ctx.Router.ServeHTTP(voteRecorder, voteRequest)
|
||||||
|
|
||||||
assertStatus(t, voteRec, http.StatusOK)
|
assertStatus(t, voteRecorder, http.StatusOK)
|
||||||
|
|
||||||
removeVoteReq := httptest.NewRequest("DELETE", fmt.Sprintf("/api/posts/%d/vote", post.ID), nil)
|
removeVoteRequest := httptest.NewRequest("DELETE", fmt.Sprintf("/api/posts/%d/vote", post.ID), nil)
|
||||||
removeVoteReq.Header.Set("Authorization", "Bearer "+user.Token)
|
removeVoteRequest.Header.Set("Authorization", "Bearer "+user.Token)
|
||||||
removeVoteReq = testutils.WithUserContext(removeVoteReq, middleware.UserIDKey, user.User.ID)
|
removeVoteRequest = testutils.WithUserContext(removeVoteRequest, middleware.UserIDKey, user.User.ID)
|
||||||
removeVoteReq = testutils.WithURLParams(removeVoteReq, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
removeVoteRequest = testutils.WithURLParams(removeVoteRequest, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
||||||
removeVoteRec := httptest.NewRecorder()
|
removeVoteRecorder := httptest.NewRecorder()
|
||||||
ctx.Router.ServeHTTP(removeVoteRec, removeVoteReq)
|
ctx.Router.ServeHTTP(removeVoteRecorder, removeVoteRequest)
|
||||||
|
|
||||||
assertStatus(t, removeVoteRec, http.StatusOK)
|
assertStatus(t, removeVoteRecorder, http.StatusOK)
|
||||||
|
|
||||||
getVotesReq := httptest.NewRequest("GET", fmt.Sprintf("/api/posts/%d/votes", post.ID), nil)
|
getVotesRequest := httptest.NewRequest("GET", fmt.Sprintf("/api/posts/%d/votes", post.ID), nil)
|
||||||
getVotesReq.Header.Set("Authorization", "Bearer "+user.Token)
|
getVotesRequest.Header.Set("Authorization", "Bearer "+user.Token)
|
||||||
getVotesReq = testutils.WithUserContext(getVotesReq, middleware.UserIDKey, user.User.ID)
|
getVotesRequest = testutils.WithUserContext(getVotesRequest, middleware.UserIDKey, user.User.ID)
|
||||||
getVotesReq = testutils.WithURLParams(getVotesReq, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
getVotesRequest = testutils.WithURLParams(getVotesRequest, map[string]string{"id": fmt.Sprintf("%d", post.ID)})
|
||||||
getVotesRec := httptest.NewRecorder()
|
getVotesRecorder := httptest.NewRecorder()
|
||||||
ctx.Router.ServeHTTP(getVotesRec, getVotesReq)
|
ctx.Router.ServeHTTP(getVotesRecorder, getVotesRequest)
|
||||||
|
|
||||||
votesResponse := assertJSONResponse(t, getVotesRec, http.StatusOK)
|
votesResponse := assertJSONResponse(t, getVotesRecorder, http.StatusOK)
|
||||||
if votesResponse == nil {
|
if votesResponse == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user