REFACTOR: make helper function for webserver tests with active user
This commit is contained in:
parent
854cfb6d7a
commit
222f3d7ab5
@ -9,23 +9,13 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"golang.org/x/net/html"
|
"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) {
|
func TestBookmarksTab(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
require := require.New(t)
|
require := require.New(t)
|
||||||
|
|
||||||
// Boilerplate for setting an active user
|
resp := do_request_with_active_user(httptest.NewRequest("GET", "/bookmarks", nil))
|
||||||
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()
|
|
||||||
require.Equal(resp.StatusCode, 200)
|
require.Equal(resp.StatusCode, 200)
|
||||||
|
|
||||||
root, err := html.Parse(resp.Body)
|
root, err := html.Parse(resp.Body)
|
||||||
@ -34,9 +24,7 @@ func TestBookmarksTab(t *testing.T) {
|
|||||||
assert.Len(tweets, 2)
|
assert.Len(tweets, 2)
|
||||||
|
|
||||||
// Double check pagination works properly
|
// Double check pagination works properly
|
||||||
recorder = httptest.NewRecorder()
|
resp = do_request_with_active_user(httptest.NewRequest("GET", "/bookmarks?cursor=1800452344077464795", nil))
|
||||||
app.ServeHTTP(recorder, httptest.NewRequest("GET", "/bookmarks?cursor=1800452344077464795", nil))
|
|
||||||
resp = recorder.Result()
|
|
||||||
require.Equal(resp.StatusCode, 200)
|
require.Equal(resp.StatusCode, 200)
|
||||||
|
|
||||||
root, err = html.Parse(resp.Body)
|
root, err = html.Parse(resp.Body)
|
||||||
|
@ -9,9 +9,6 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"golang.org/x/net/html"
|
"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
|
// Loading the index page should work if you're logged in
|
||||||
@ -19,15 +16,8 @@ func TestMessagesIndexPage(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
require := require.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
|
// Chat list
|
||||||
recorder := httptest.NewRecorder()
|
resp := do_request_with_active_user(httptest.NewRequest("GET", "/messages", nil))
|
||||||
app.ServeHTTP(recorder, httptest.NewRequest("GET", "/messages", nil))
|
|
||||||
resp := recorder.Result()
|
|
||||||
root, err := html.Parse(resp.Body)
|
root, err := html.Parse(resp.Body)
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
assert.Len(cascadia.QueryAll(root, selector(".chat-list .chat-list-entry")), 2)
|
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)
|
assert := assert.New(t)
|
||||||
require := require.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
|
// Chat detail
|
||||||
recorder := httptest.NewRecorder()
|
resp := do_request_with_active_user(httptest.NewRequest("GET", "/messages/1488963321701171204-1178839081222115328", nil))
|
||||||
app.ServeHTTP(recorder, httptest.NewRequest("GET", "/messages/1488963321701171204-1178839081222115328", nil))
|
|
||||||
resp := recorder.Result()
|
|
||||||
root, err := html.Parse(resp.Body)
|
root, err := html.Parse(resp.Body)
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
assert.Len(cascadia.QueryAll(root, selector(".chat-list .chat-list-entry")), 2) // Chat list still renders
|
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)
|
assert := assert.New(t)
|
||||||
require := require.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
|
// Chat detail
|
||||||
recorder := httptest.NewRecorder()
|
|
||||||
req := httptest.NewRequest("GET", "/messages/1488963321701171204-1178839081222115328?poll&latest_timestamp=1686025129141", nil)
|
req := httptest.NewRequest("GET", "/messages/1488963321701171204-1178839081222115328?poll&latest_timestamp=1686025129141", nil)
|
||||||
req.Header.Set("HX-Request", "true")
|
req.Header.Set("HX-Request", "true")
|
||||||
app.ServeHTTP(recorder, req)
|
resp := do_request_with_active_user(req)
|
||||||
resp := recorder.Result()
|
|
||||||
root, err := html.Parse(resp.Body)
|
root, err := html.Parse(resp.Body)
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
assert.Len(cascadia.QueryAll(root, selector(".dm-message")), 3)
|
assert.Len(cascadia.QueryAll(root, selector(".dm-message")), 3)
|
||||||
@ -106,17 +82,10 @@ func TestMessagesRoomPollForUpdatesEmptyResult(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
require := require.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
|
// Chat detail
|
||||||
recorder := httptest.NewRecorder()
|
|
||||||
req := httptest.NewRequest("GET", "/messages/1488963321701171204-1178839081222115328?poll&latest_timestamp=1686025129144", nil)
|
req := httptest.NewRequest("GET", "/messages/1488963321701171204-1178839081222115328?poll&latest_timestamp=1686025129144", nil)
|
||||||
req.Header.Set("HX-Request", "true")
|
req.Header.Set("HX-Request", "true")
|
||||||
app.ServeHTTP(recorder, req)
|
resp := do_request_with_active_user(req)
|
||||||
resp := recorder.Result()
|
|
||||||
root, err := html.Parse(resp.Body)
|
root, err := html.Parse(resp.Body)
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
assert.Len(cascadia.QueryAll(root, selector(".dm-message")), 0)
|
assert.Len(cascadia.QueryAll(root, selector(".dm-message")), 0)
|
||||||
@ -140,17 +109,10 @@ func TestMessagesPaginate(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
require := require.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
|
// Chat detail
|
||||||
recorder := httptest.NewRecorder()
|
|
||||||
req := httptest.NewRequest("GET", "/messages/1488963321701171204-1178839081222115328?cursor=1686025129142", nil)
|
req := httptest.NewRequest("GET", "/messages/1488963321701171204-1178839081222115328?cursor=1686025129142", nil)
|
||||||
req.Header.Set("HX-Request", "true")
|
req.Header.Set("HX-Request", "true")
|
||||||
app.ServeHTTP(recorder, req)
|
resp := do_request_with_active_user(req)
|
||||||
resp := recorder.Result()
|
|
||||||
root, err := html.Parse(resp.Body)
|
root, err := html.Parse(resp.Body)
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
assert.Len(cascadia.QueryAll(root, selector(".dm-message")), 2)
|
assert.Len(cascadia.QueryAll(root, selector(".dm-message")), 2)
|
||||||
|
@ -9,35 +9,23 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"golang.org/x/net/html"
|
"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) {
|
func TestNotifications(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
require := require.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
|
// Notifications page
|
||||||
recorder := httptest.NewRecorder()
|
|
||||||
req := httptest.NewRequest("GET", "/notifications", nil)
|
req := httptest.NewRequest("GET", "/notifications", nil)
|
||||||
app.ServeHTTP(recorder, req)
|
resp := do_request_with_active_user(req)
|
||||||
resp := recorder.Result()
|
|
||||||
root, err := html.Parse(resp.Body)
|
root, err := html.Parse(resp.Body)
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
assert.Len(cascadia.QueryAll(root, selector(".notification")), 6)
|
assert.Len(cascadia.QueryAll(root, selector(".notification")), 6)
|
||||||
|
|
||||||
// Show more
|
// Show more
|
||||||
recorder = httptest.NewRecorder()
|
|
||||||
req = httptest.NewRequest("GET", "/notifications?cursor=1726604756351", nil)
|
req = httptest.NewRequest("GET", "/notifications?cursor=1726604756351", nil)
|
||||||
req.Header.Set("HX-Request", "true")
|
req.Header.Set("HX-Request", "true")
|
||||||
app.ServeHTTP(recorder, req)
|
resp = do_request_with_active_user(req)
|
||||||
resp = recorder.Result()
|
|
||||||
root, err = html.Parse(resp.Body)
|
root, err = html.Parse(resp.Body)
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
assert.Len(cascadia.QueryAll(root, selector(".notification")), 5)
|
assert.Len(cascadia.QueryAll(root, selector(".notification")), 5)
|
||||||
|
@ -9,9 +9,6 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"golang.org/x/net/html"
|
"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) {
|
func TestTimeline(t *testing.T) {
|
||||||
@ -58,15 +55,8 @@ func TestUserFeedTimeline(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
require := require.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
|
// Chat list
|
||||||
recorder := httptest.NewRecorder()
|
resp := do_request_with_active_user(httptest.NewRequest("GET", "/timeline", nil))
|
||||||
app.ServeHTTP(recorder, httptest.NewRequest("GET", "/timeline", nil))
|
|
||||||
resp := recorder.Result()
|
|
||||||
require.Equal(resp.StatusCode, 200)
|
require.Equal(resp.StatusCode, 200)
|
||||||
|
|
||||||
root, err := html.Parse(resp.Body)
|
root, err := html.Parse(resp.Body)
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"gitlab.com/offline-twitter/twitter_offline_engine/internal/webserver"
|
"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/persistence"
|
||||||
|
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CapturingWriter struct {
|
type CapturingWriter struct {
|
||||||
@ -40,6 +41,7 @@ func selector(s string) cascadia.Sel {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run an HTTP request against the app and return the response
|
||||||
func do_request(req *http.Request) *http.Response {
|
func do_request(req *http.Request) *http.Response {
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
app := webserver.NewApp(profile)
|
app := webserver.NewApp(profile)
|
||||||
@ -48,6 +50,16 @@ func do_request(req *http.Request) *http.Response {
|
|||||||
return recorder.Result()
|
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
|
// Homepage
|
||||||
// --------
|
// --------
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user