Skip to content

Commit

Permalink
add even more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Sep 27, 2023
1 parent f70521d commit f40aea8
Show file tree
Hide file tree
Showing 11 changed files with 553 additions and 20 deletions.
22 changes: 11 additions & 11 deletions front/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ func edit(w text.Writer, r *request) {
return
}

content, err := url.QueryUnescape(r.URL.RawQuery)
if err != nil {
w.Status(40, "Bad input")
return
}

if len(content) > cfg.MaxPostsLength {
w.Status(40, "Post is too long")
return
}

hash := filepath.Base(r.URL.Path)

var noteString string
Expand Down Expand Up @@ -81,17 +92,6 @@ func edit(w text.Writer, r *request) {
return
}

content, err := url.QueryUnescape(r.URL.RawQuery)
if err != nil {
w.Error()
return
}

if len(content) > cfg.MaxPostsLength {
w.Status(40, "Post is too long")
return
}

if err := fed.Edit(r.Context, r.DB, &note, plain.ToHTML(content)); err != nil {
r.Log.Error("Failed to update post", "note", note.ID, "error", err)
w.Error()
Expand Down
4 changes: 2 additions & 2 deletions front/inbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

func dailyPosts(w text.Writer, r *request, day time.Time) {
if r.User == nil {
w.Status(61, "Peer certificate is required")
w.Redirect("/users")
return
}

Expand Down Expand Up @@ -106,7 +106,7 @@ func dailyPosts(w text.Writer, r *request, day time.Time) {
where
notes.inserted >= $4 and
notes.inserted < $4 + 60*60*24 and
(follows.followed is not null or myposts.id is not null)
(follows.followed is not null or (myposts.id is not null and notes.author != $1))
group by notes.id
order by
notes.inserted / 86400 desc,
Expand Down
89 changes: 89 additions & 0 deletions test/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2023 Dima Krasner
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package test

import (
"crypto/sha256"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

func TestDelete_HappyFlow(t *testing.T) {
server := newTestServer()
defer server.Shutdown()

say := server.Handle("/users/say?Hello%20world", server.Alice)
assert.Regexp(t, "30 /users/view/[0-9a-f]{64}", say)

hash := say[15 : len(say)-2]

view := server.Handle("/users/view/"+hash, server.Bob)
assert.Contains(t, view, "Hello world")

delete := server.Handle("/users/delete/"+hash, server.Alice)
assert.Equal(t, fmt.Sprintf("30 /users/outbox/%x\r\n", sha256.Sum256([]byte(server.Alice.ID))), delete)

view = server.Handle("/users/view/"+hash, server.Alice)
assert.Equal(t, view, "40 Post not found\r\n")
}

func TestDelete_NotAuthor(t *testing.T) {
server := newTestServer()
defer server.Shutdown()

say := server.Handle("/users/say?Hello%20world", server.Alice)
assert.Regexp(t, "30 /users/view/[0-9a-f]{64}", say)

hash := say[15 : len(say)-2]

view := server.Handle("/users/view/"+hash, server.Bob)
assert.Contains(t, view, "Hello world")

delete := server.Handle("/users/delete/"+hash, server.Bob)
assert.Equal(t, delete, "40 Error\r\n")

view = server.Handle("/users/view/"+hash, server.Alice)
assert.Contains(t, view, "Hello world")
}

func TestDelete_NoSuchPost(t *testing.T) {
server := newTestServer()
defer server.Shutdown()

delete := server.Handle("/users/delete/87428fc522803d31065e7bce3cf03fe475096631e5e07bbd7a0fde60c4cf25c7", server.Alice)
assert.Equal(t, delete, "40 Error\r\n")
}

func TestDelete_UnauthenticatedUser(t *testing.T) {
server := newTestServer()
defer server.Shutdown()

say := server.Handle("/users/say?Hello%20world", server.Alice)
assert.Regexp(t, "30 /users/view/[0-9a-f]{64}", say)

hash := say[15 : len(say)-2]

view := server.Handle("/users/view/"+hash, server.Bob)
assert.Contains(t, view, "Hello world")

delete := server.Handle("/users/delete/"+hash, nil)
assert.Equal(t, delete, "30 /users\r\n")

view = server.Handle("/users/view/"+hash, server.Alice)
assert.Contains(t, view, "Hello world")
}
16 changes: 12 additions & 4 deletions test/dm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,22 @@ func TestDM_Loopback(t *testing.T) {
server := newTestServer()
defer server.Shutdown()

resp := server.Handle(fmt.Sprintf("/users/dm/%x?Hello%%20world", sha256.Sum256([]byte(server.Alice.ID))), server.Alice)
assert.Regexp(t, "40 [^\r\n]+\r\n", resp)
dm := server.Handle(fmt.Sprintf("/users/dm/%x?Hello%%20world", sha256.Sum256([]byte(server.Alice.ID))), server.Alice)
assert.Equal(t, "40 Error\r\n", dm)
}

func TestDM_NotFollowed(t *testing.T) {
server := newTestServer()
defer server.Shutdown()

resp := server.Handle(fmt.Sprintf("/users/dm/%x?Hello%%20world", sha256.Sum256([]byte(server.Alice.ID))), server.Bob)
assert.Regexp(t, "40 [^\r\n]+\r\n", resp)
dm := server.Handle(fmt.Sprintf("/users/dm/%x?Hello%%20world", sha256.Sum256([]byte(server.Alice.ID))), server.Bob)
assert.Equal(t, "40 Error\r\n", dm)
}

func TestDM_NoSuchUser(t *testing.T) {
server := newTestServer()
defer server.Shutdown()

dm := server.Handle("/users/dm/87428fc522803d31065e7bce3cf03fe475096631e5e07bbd7a0fde60c4cf25c7?Hello%20world", server.Bob)
assert.Equal(t, "40 User does not exist\r\n", dm)
}
Loading

0 comments on commit f40aea8

Please sign in to comment.