From 222f3d7ab5d3daf24a8c67f7c41d56dc8756993c Mon Sep 17 00:00:00 2001 From: Alessio Date: Mon, 2 Dec 2024 15:08:58 -0800 Subject: [PATCH] REFACTOR: make helper function for webserver tests with active user --- internal/webserver/handler_bookmarks_test.go | 16 +------ internal/webserver/handler_messages_test.go | 48 ++----------------- .../webserver/handler_notifications_test.go | 16 +------ internal/webserver/handler_timeline_test.go | 12 +---- internal/webserver/server_test.go | 12 +++++ 5 files changed, 22 insertions(+), 82 deletions(-) diff --git a/internal/webserver/handler_bookmarks_test.go b/internal/webserver/handler_bookmarks_test.go index 1058b4b..1e84ae9 100644 --- a/internal/webserver/handler_bookmarks_test.go +++ b/internal/webserver/handler_bookmarks_test.go @@ -9,23 +9,13 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "golang.org/x/net/html" - - "gitlab.com/offline-twitter/twitter_offline_engine/internal/webserver" - "gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper" ) func TestBookmarksTab(t *testing.T) { assert := assert.New(t) require := require.New(t) - // Boilerplate for setting an active user - app := webserver.NewApp(profile) - app.IsScrapingDisabled = true - app.ActiveUser = scraper.User{ID: 1488963321701171204, Handle: "Offline_Twatter"} // Simulate a login - - recorder := httptest.NewRecorder() - app.ServeHTTP(recorder, httptest.NewRequest("GET", "/bookmarks", nil)) - resp := recorder.Result() + resp := do_request_with_active_user(httptest.NewRequest("GET", "/bookmarks", nil)) require.Equal(resp.StatusCode, 200) root, err := html.Parse(resp.Body) @@ -34,9 +24,7 @@ func TestBookmarksTab(t *testing.T) { assert.Len(tweets, 2) // Double check pagination works properly - recorder = httptest.NewRecorder() - app.ServeHTTP(recorder, httptest.NewRequest("GET", "/bookmarks?cursor=1800452344077464795", nil)) - resp = recorder.Result() + resp = do_request_with_active_user(httptest.NewRequest("GET", "/bookmarks?cursor=1800452344077464795", nil)) require.Equal(resp.StatusCode, 200) root, err = html.Parse(resp.Body) diff --git a/internal/webserver/handler_messages_test.go b/internal/webserver/handler_messages_test.go index 46cf384..ad2f273 100644 --- a/internal/webserver/handler_messages_test.go +++ b/internal/webserver/handler_messages_test.go @@ -9,9 +9,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "golang.org/x/net/html" - - "gitlab.com/offline-twitter/twitter_offline_engine/internal/webserver" - "gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper" ) // Loading the index page should work if you're logged in @@ -19,15 +16,8 @@ func TestMessagesIndexPage(t *testing.T) { assert := assert.New(t) require := require.New(t) - // Boilerplate for setting an active user - app := webserver.NewApp(profile) - app.IsScrapingDisabled = true - app.ActiveUser = scraper.User{ID: 1488963321701171204, Handle: "Offline_Twatter"} // Simulate a login - // Chat list - recorder := httptest.NewRecorder() - app.ServeHTTP(recorder, httptest.NewRequest("GET", "/messages", nil)) - resp := recorder.Result() + resp := do_request_with_active_user(httptest.NewRequest("GET", "/messages", nil)) root, err := html.Parse(resp.Body) require.NoError(err) assert.Len(cascadia.QueryAll(root, selector(".chat-list .chat-list-entry")), 2) @@ -39,15 +29,8 @@ func TestMessagesRoom(t *testing.T) { assert := assert.New(t) require := require.New(t) - // Boilerplate for setting an active user - app := webserver.NewApp(profile) - app.IsScrapingDisabled = true - app.ActiveUser = scraper.User{ID: 1488963321701171204, Handle: "Offline_Twatter"} // Simulate a login - // Chat detail - recorder := httptest.NewRecorder() - app.ServeHTTP(recorder, httptest.NewRequest("GET", "/messages/1488963321701171204-1178839081222115328", nil)) - resp := recorder.Result() + resp := do_request_with_active_user(httptest.NewRequest("GET", "/messages/1488963321701171204-1178839081222115328", nil)) root, err := html.Parse(resp.Body) require.NoError(err) assert.Len(cascadia.QueryAll(root, selector(".chat-list .chat-list-entry")), 2) // Chat list still renders @@ -72,17 +55,10 @@ func TestMessagesRoomPollForUpdates(t *testing.T) { assert := assert.New(t) require := require.New(t) - // Boilerplate for setting an active user - app := webserver.NewApp(profile) - app.IsScrapingDisabled = true - app.ActiveUser = scraper.User{ID: 1488963321701171204, Handle: "Offline_Twatter"} // Simulate a login - // Chat detail - recorder := httptest.NewRecorder() req := httptest.NewRequest("GET", "/messages/1488963321701171204-1178839081222115328?poll&latest_timestamp=1686025129141", nil) req.Header.Set("HX-Request", "true") - app.ServeHTTP(recorder, req) - resp := recorder.Result() + resp := do_request_with_active_user(req) root, err := html.Parse(resp.Body) require.NoError(err) assert.Len(cascadia.QueryAll(root, selector(".dm-message")), 3) @@ -106,17 +82,10 @@ func TestMessagesRoomPollForUpdatesEmptyResult(t *testing.T) { assert := assert.New(t) require := require.New(t) - // Boilerplate for setting an active user - app := webserver.NewApp(profile) - app.IsScrapingDisabled = true - app.ActiveUser = scraper.User{ID: 1488963321701171204, Handle: "Offline_Twatter"} // Simulate a login - // Chat detail - recorder := httptest.NewRecorder() req := httptest.NewRequest("GET", "/messages/1488963321701171204-1178839081222115328?poll&latest_timestamp=1686025129144", nil) req.Header.Set("HX-Request", "true") - app.ServeHTTP(recorder, req) - resp := recorder.Result() + resp := do_request_with_active_user(req) root, err := html.Parse(resp.Body) require.NoError(err) assert.Len(cascadia.QueryAll(root, selector(".dm-message")), 0) @@ -140,17 +109,10 @@ func TestMessagesPaginate(t *testing.T) { assert := assert.New(t) require := require.New(t) - // Boilerplate for setting an active user - app := webserver.NewApp(profile) - app.IsScrapingDisabled = true - app.ActiveUser = scraper.User{ID: 1488963321701171204, Handle: "Offline_Twatter"} // Simulate a login - // Chat detail - recorder := httptest.NewRecorder() req := httptest.NewRequest("GET", "/messages/1488963321701171204-1178839081222115328?cursor=1686025129142", nil) req.Header.Set("HX-Request", "true") - app.ServeHTTP(recorder, req) - resp := recorder.Result() + resp := do_request_with_active_user(req) root, err := html.Parse(resp.Body) require.NoError(err) assert.Len(cascadia.QueryAll(root, selector(".dm-message")), 2) diff --git a/internal/webserver/handler_notifications_test.go b/internal/webserver/handler_notifications_test.go index 0e1490d..8f99064 100644 --- a/internal/webserver/handler_notifications_test.go +++ b/internal/webserver/handler_notifications_test.go @@ -9,35 +9,23 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "golang.org/x/net/html" - - "gitlab.com/offline-twitter/twitter_offline_engine/internal/webserver" - "gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper" ) func TestNotifications(t *testing.T) { assert := assert.New(t) require := require.New(t) - // Boilerplate for setting an active user - app := webserver.NewApp(profile) - app.IsScrapingDisabled = true - app.ActiveUser = scraper.User{ID: 1488963321701171204, Handle: "Offline_Twatter"} // Simulate a login - // Notifications page - recorder := httptest.NewRecorder() req := httptest.NewRequest("GET", "/notifications", nil) - app.ServeHTTP(recorder, req) - resp := recorder.Result() + resp := do_request_with_active_user(req) root, err := html.Parse(resp.Body) require.NoError(err) assert.Len(cascadia.QueryAll(root, selector(".notification")), 6) // Show more - recorder = httptest.NewRecorder() req = httptest.NewRequest("GET", "/notifications?cursor=1726604756351", nil) req.Header.Set("HX-Request", "true") - app.ServeHTTP(recorder, req) - resp = recorder.Result() + resp = do_request_with_active_user(req) root, err = html.Parse(resp.Body) require.NoError(err) assert.Len(cascadia.QueryAll(root, selector(".notification")), 5) diff --git a/internal/webserver/handler_timeline_test.go b/internal/webserver/handler_timeline_test.go index cc7d1a5..38e6d4f 100644 --- a/internal/webserver/handler_timeline_test.go +++ b/internal/webserver/handler_timeline_test.go @@ -9,9 +9,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "golang.org/x/net/html" - - "gitlab.com/offline-twitter/twitter_offline_engine/internal/webserver" - "gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper" ) func TestTimeline(t *testing.T) { @@ -58,15 +55,8 @@ func TestUserFeedTimeline(t *testing.T) { assert := assert.New(t) require := require.New(t) - // Boilerplate for setting an active user - app := webserver.NewApp(profile) - app.IsScrapingDisabled = true - app.ActiveUser = scraper.User{ID: 1488963321701171204, Handle: "Offline_Twatter"} // Simulate a login - // Chat list - recorder := httptest.NewRecorder() - app.ServeHTTP(recorder, httptest.NewRequest("GET", "/timeline", nil)) - resp := recorder.Result() + resp := do_request_with_active_user(httptest.NewRequest("GET", "/timeline", nil)) require.Equal(resp.StatusCode, 200) root, err := html.Parse(resp.Body) diff --git a/internal/webserver/server_test.go b/internal/webserver/server_test.go index f290d84..fca4d73 100644 --- a/internal/webserver/server_test.go +++ b/internal/webserver/server_test.go @@ -11,6 +11,7 @@ import ( "gitlab.com/offline-twitter/twitter_offline_engine/internal/webserver" "gitlab.com/offline-twitter/twitter_offline_engine/pkg/persistence" + "gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper" ) type CapturingWriter struct { @@ -40,6 +41,7 @@ func selector(s string) cascadia.Sel { return ret } +// Run an HTTP request against the app and return the response func do_request(req *http.Request) *http.Response { recorder := httptest.NewRecorder() app := webserver.NewApp(profile) @@ -48,6 +50,16 @@ func do_request(req *http.Request) *http.Response { return recorder.Result() } +// Run an HTTP request against the app, with an Active User set, and return the response +func do_request_with_active_user(req *http.Request) *http.Response { + recorder := httptest.NewRecorder() + app := webserver.NewApp(profile) + app.IsScrapingDisabled = true + app.ActiveUser = scraper.User{ID: 1488963321701171204, Handle: "Offline_Twatter"} // Simulate a login + app.ServeHTTP(recorder, req) + return recorder.Result() +} + // Homepage // --------