List Detail 404s if list ID doesn't exist
This commit is contained in:
parent
3f66daef68
commit
333e4bb553
@ -130,9 +130,11 @@ func (app *Application) Lists(w http.ResponseWriter, r *http.Request) {
|
|||||||
app.error_400_with_message(w, "List ID must be a number")
|
app.error_400_with_message(w, "List ID must be a number")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// XXX: Check that the list exists
|
list, err := app.Profile.GetListById(ListID(_list_id))
|
||||||
// Need to modify signature to return an error, because it might be ErrNoRows
|
if err != nil {
|
||||||
list := app.Profile.GetListById(ListID(_list_id))
|
app.error_404(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
req_with_ctx := r.WithContext(add_list_to_context(r.Context(), list))
|
req_with_ctx := r.WithContext(add_list_to_context(r.Context(), list))
|
||||||
http.StripPrefix(fmt.Sprintf("/%d", list.ID), http.HandlerFunc(app.ListDetail)).ServeHTTP(w, req_with_ctx)
|
http.StripPrefix(fmt.Sprintf("/%d", list.ID), http.HandlerFunc(app.ListDetail)).ServeHTTP(w, req_with_ctx)
|
||||||
return
|
return
|
||||||
|
@ -597,8 +597,8 @@ func TestListsIndex(t *testing.T) {
|
|||||||
root, err := html.Parse(resp.Body)
|
root, err := html.Parse(resp.Body)
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
|
|
||||||
// Check that there's 2 Lists
|
// Check that there's at least 2 Lists
|
||||||
assert.Len(t, cascadia.QueryAll(root, selector(".users-list-preview")), 2)
|
assert.True(t, len(cascadia.QueryAll(root, selector(".users-list-preview"))) >= 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestListDetail(t *testing.T) {
|
func TestListDetail(t *testing.T) {
|
||||||
@ -620,6 +620,11 @@ func TestListDetail(t *testing.T) {
|
|||||||
assert.Len(cascadia.QueryAll(root1, selector(".timeline > .tweet")), 3)
|
assert.Len(cascadia.QueryAll(root1, selector(".timeline > .tweet")), 3)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestListDetailDoesntExist(t *testing.T) {
|
||||||
|
resp := do_request(httptest.NewRequest("GET", "/lists/2523478", nil))
|
||||||
|
require.Equal(t, resp.StatusCode, 404)
|
||||||
|
}
|
||||||
|
|
||||||
func TestListDetailInvalidId(t *testing.T) {
|
func TestListDetailInvalidId(t *testing.T) {
|
||||||
resp := do_request(httptest.NewRequest("GET", "/lists/asd", nil))
|
resp := do_request(httptest.NewRequest("GET", "/lists/asd", nil))
|
||||||
require.Equal(t, resp.StatusCode, 400)
|
require.Equal(t, resp.StatusCode, 400)
|
||||||
|
@ -73,13 +73,15 @@ func (p Profile) DeleteListUser(list_id ListID, user_id UserID) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Profile) GetListById(list_id ListID) List {
|
func (p Profile) GetListById(list_id ListID) (List, error) {
|
||||||
var ret List
|
var ret List
|
||||||
err := p.DB.Get(&ret, `select rowid, is_online, online_list_id, name from lists where rowid = ?`, list_id)
|
err := p.DB.Get(&ret, `select rowid, is_online, online_list_id, name from lists where rowid = ?`, list_id)
|
||||||
if err != nil {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return List{}, ErrNotInDatabase{"List", list_id}
|
||||||
|
} else if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return ret
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Profile) GetListUsers(list_id ListID) []User {
|
func (p Profile) GetListUsers(list_id ListID) []User {
|
||||||
|
@ -27,7 +27,8 @@ func TestSaveAndLoadOfflineList(t *testing.T) {
|
|||||||
require.NotEqual(l.ID, ListID(0)) // ID should be assigned when it's saved
|
require.NotEqual(l.ID, ListID(0)) // ID should be assigned when it's saved
|
||||||
|
|
||||||
// Check it comes back the same
|
// Check it comes back the same
|
||||||
new_l := profile.GetListById(l.ID)
|
new_l, err := profile.GetListById(l.ID)
|
||||||
|
require.NoError(err)
|
||||||
assert.Equal(l.ID, new_l.ID)
|
assert.Equal(l.ID, new_l.ID)
|
||||||
assert.Equal(l.IsOnline, new_l.IsOnline)
|
assert.Equal(l.IsOnline, new_l.IsOnline)
|
||||||
assert.Equal(l.Name, new_l.Name)
|
assert.Equal(l.Name, new_l.Name)
|
||||||
@ -50,7 +51,8 @@ func TestRenameOfflineList(t *testing.T) {
|
|||||||
profile.SaveList(&l)
|
profile.SaveList(&l)
|
||||||
|
|
||||||
// Rename should be effective
|
// Rename should be effective
|
||||||
new_l := profile.GetListById(l.ID)
|
new_l, err := profile.GetListById(l.ID)
|
||||||
|
require.NoError(err)
|
||||||
assert.Equal(l.ID, new_l.ID)
|
assert.Equal(l.ID, new_l.ID)
|
||||||
assert.Equal(l.IsOnline, new_l.IsOnline)
|
assert.Equal(l.IsOnline, new_l.IsOnline)
|
||||||
assert.Equal(l.Name, new_l.Name)
|
assert.Equal(l.Name, new_l.Name)
|
||||||
@ -70,7 +72,8 @@ func TestSaveAndLoadOnlineList(t *testing.T) {
|
|||||||
require.NotEqual(l.ID, ListID(0)) // ID should be assigned when it's saved
|
require.NotEqual(l.ID, ListID(0)) // ID should be assigned when it's saved
|
||||||
|
|
||||||
// Check it comes back the same
|
// Check it comes back the same
|
||||||
new_l := profile.GetListById(l.ID)
|
new_l, err := profile.GetListById(l.ID)
|
||||||
|
require.NoError(err)
|
||||||
assert.Equal(l.ID, new_l.ID)
|
assert.Equal(l.ID, new_l.ID)
|
||||||
assert.Equal(l.IsOnline, new_l.IsOnline)
|
assert.Equal(l.IsOnline, new_l.IsOnline)
|
||||||
assert.Equal(l.OnlineID, new_l.OnlineID) // Check OnlineID for online lists
|
assert.Equal(l.OnlineID, new_l.OnlineID) // Check OnlineID for online lists
|
||||||
@ -94,7 +97,8 @@ func TestRenameOnlineList(t *testing.T) {
|
|||||||
profile.SaveList(&l)
|
profile.SaveList(&l)
|
||||||
|
|
||||||
// Rename should be effective
|
// Rename should be effective
|
||||||
new_l := profile.GetListById(l.ID)
|
new_l, err := profile.GetListById(l.ID)
|
||||||
|
require.NoError(err)
|
||||||
assert.Equal(l.ID, new_l.ID)
|
assert.Equal(l.ID, new_l.ID)
|
||||||
assert.Equal(l.IsOnline, new_l.IsOnline)
|
assert.Equal(l.IsOnline, new_l.IsOnline)
|
||||||
assert.Equal(l.OnlineID, new_l.OnlineID) // Check OnlineID for online lists
|
assert.Equal(l.OnlineID, new_l.OnlineID) // Check OnlineID for online lists
|
||||||
|
Loading…
x
Reference in New Issue
Block a user