REFACTOR: make helper function for webserver tests with active user

This commit is contained in:
Alessio 2024-12-02 15:08:58 -08:00
parent 854cfb6d7a
commit 222f3d7ab5
5 changed files with 22 additions and 82 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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
// --------