Add more non-happy-path HTTP tests

This commit is contained in:
Alessio 2024-12-02 20:30:56 -08:00
parent 9ce4f84a82
commit 6e271bccc0
4 changed files with 67 additions and 0 deletions

View File

@ -102,6 +102,10 @@ func (app *Application) message_detail(w http.ResponseWriter, r *http.Request) {
// Handle reactions // Handle reactions
if len(parts) == 1 && parts[0] == "reacc" { if len(parts) == 1 && parts[0] == "reacc" {
if app.IsScrapingDisabled {
app.error_401(w, r)
return
}
var data struct { var data struct {
MessageID scraper.DMMessageID `json:"message_id,string"` MessageID scraper.DMMessageID `json:"message_id,string"`
Reacc string `json:"reacc"` Reacc string `json:"reacc"`

View File

@ -185,3 +185,39 @@ func TestMessagesSend(t *testing.T) {
)) ))
require.Equal(401, resp.StatusCode) require.Equal(401, resp.StatusCode)
} }
// When scraping is disabled, sending a reacc should 401
func TestMessagesSendReacc(t *testing.T) {
require := require.New(t)
resp := do_request_with_active_user(httptest.NewRequest("GET",
"/messages/1488963321701171204-1178839081222115328/reacc",
strings.NewReader(`{"message_id": "1", "reacc": ":)"}`),
))
require.Equal(401, resp.StatusCode)
}
func TestMessagesRefreshConversationsList(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
// No active chat
req := httptest.NewRequest("GET", "/messages/refresh-list", nil)
req.Header.Set("HX-Request", "true")
resp := do_request_with_active_user(req)
require.Equal(200, resp.StatusCode)
root, err := html.Parse(resp.Body)
require.NoError(err)
assert.Len(cascadia.QueryAll(root, selector(".chat-list-entry")), 2)
assert.Len(cascadia.QueryAll(root, selector(".chat-list-entry.chat-list-entry--active-chat")), 0)
// With an active chat
req1 := httptest.NewRequest("GET", "/messages/refresh-list?active-chat=1488963321701171204-1178839081222115328", nil)
req1.Header.Set("HX-Request", "true")
resp1 := do_request_with_active_user(req1)
require.Equal(200, resp1.StatusCode)
root1, err := html.Parse(resp1.Body)
require.NoError(err)
assert.Len(cascadia.QueryAll(root1, selector(".chat-list-entry")), 2)
assert.Len(cascadia.QueryAll(root1, selector(".chat-list-entry.chat-list-entry--active-chat")), 1)
}

View File

@ -8,6 +8,12 @@ import (
func (app *Application) Notifications(w http.ResponseWriter, r *http.Request) { func (app *Application) Notifications(w http.ResponseWriter, r *http.Request) {
app.traceLog.Printf("'Notifications' handler (path: %q)", r.URL.Path) app.traceLog.Printf("'Notifications' handler (path: %q)", r.URL.Path)
if app.ActiveUser.ID == 0 {
app.error_401(w, r)
return
}
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/") parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
if parts[0] == "mark-all-as-read" { if parts[0] == "mark-all-as-read" {
app.NotificationsMarkAsRead(w, r) app.NotificationsMarkAsRead(w, r)
@ -36,6 +42,10 @@ func (app *Application) Notifications(w http.ResponseWriter, r *http.Request) {
} }
func (app *Application) NotificationsMarkAsRead(w http.ResponseWriter, r *http.Request) { func (app *Application) NotificationsMarkAsRead(w http.ResponseWriter, r *http.Request) {
if app.IsScrapingDisabled {
app.error_401(w, r)
return
}
err := app.API.MarkNotificationsAsRead() err := app.API.MarkNotificationsAsRead()
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -11,6 +11,14 @@ import (
"golang.org/x/net/html" "golang.org/x/net/html"
) )
func TestNotificationsRequiresActiveSession(t *testing.T) {
require := require.New(t)
req := httptest.NewRequest("GET", "/notifications", nil)
resp := do_request(req)
require.Equal(401, resp.StatusCode)
}
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)
@ -18,6 +26,7 @@ func TestNotifications(t *testing.T) {
// Notifications page // Notifications page
req := httptest.NewRequest("GET", "/notifications", nil) req := httptest.NewRequest("GET", "/notifications", nil)
resp := do_request_with_active_user(req) resp := do_request_with_active_user(req)
require.Equal(200, resp.StatusCode)
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)
@ -30,3 +39,11 @@ func TestNotifications(t *testing.T) {
require.NoError(err) require.NoError(err)
assert.Len(cascadia.QueryAll(root, selector(".notification")), 5) assert.Len(cascadia.QueryAll(root, selector(".notification")), 5)
} }
// When scraping is disabled, marking notifs as read should 401
func TestNotificationsMarkAsRead(t *testing.T) {
require := require.New(t)
resp := do_request_with_active_user(httptest.NewRequest("GET", "/notifications/mark-all-as-read", nil))
require.Equal(401, resp.StatusCode)
}