Skip to content

Commit

Permalink
add even more tests again
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Sep 27, 2023
1 parent f40aea8 commit 86a83a2
Show file tree
Hide file tree
Showing 6 changed files with 415 additions and 1 deletion.
2 changes: 1 addition & 1 deletion front/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func search(w text.Writer, r *request) {

hashtag, err := url.QueryUnescape(r.URL.RawQuery)
if err != nil {
r.Log.Info("Failed to decode user name", "url", r.URL, "error", err)
r.Log.Info("Failed to decode query", "url", r.URL, "error", err)
w.Status(40, "Bad input")
return
}
Expand Down
70 changes: 70 additions & 0 deletions test/federated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
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 (
"github.com/stretchr/testify/assert"
"testing"
)

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

federated := server.Handle("/users/federated", server.Alice)
assert.Regexp(t, "^20 text/gemini\r\n", federated)
}

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

federated := server.Handle("/federated", nil)
assert.Regexp(t, "^20 text/gemini\r\n", federated)
}

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

federated := server.Handle("/users/federated?a", server.Alice)
assert.Equal(t, "40 Invalid query\r\n", federated)
}

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

federated := server.Handle("/users/federated?901", server.Alice)
assert.Equal(t, "40 Offset must be <= 900\r\n", federated)
}

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

federated := server.Handle("/users/federated?30", server.Alice)
assert.Regexp(t, "^20 text/gemini\r\n", federated)
}

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

federated := server.Handle("/federated?30", nil)
assert.Regexp(t, "^20 text/gemini\r\n", federated)
}
83 changes: 83 additions & 0 deletions test/follows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
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 TestFollows_NoFollows(t *testing.T) {
server := newTestServer()
defer server.Shutdown()

follows := server.Handle("/users/follows", server.Alice)
assert.Contains(t, follows, "No followed users.")
}

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

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

follows := server.Handle("/users/follows", server.Alice)
assert.Contains(t, follows, server.Bob.PreferredUsername)
assert.NotContains(t, follows, server.Carol.PreferredUsername)

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

follows = server.Handle("/users/follows", server.Alice)
assert.Contains(t, follows, server.Bob.PreferredUsername)
assert.Contains(t, follows, server.Carol.PreferredUsername)
}

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

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

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

follows := server.Handle("/users/follows", server.Alice)
assert.Contains(t, follows, server.Bob.PreferredUsername)
assert.NotContains(t, follows, "1 post")
assert.Contains(t, follows, server.Carol.PreferredUsername)

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

follows = server.Handle("/users/follows", server.Alice)
assert.Contains(t, follows, server.Bob.PreferredUsername)
assert.Contains(t, follows, "1 post")
assert.Contains(t, follows, server.Carol.PreferredUsername)
}

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

follows := server.Handle("/users/follows", nil)
assert.Equal(t, "30 /users\r\n", follows)
}
38 changes: 38 additions & 0 deletions test/home_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
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 (
"github.com/stretchr/testify/assert"
"testing"
)

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

home := server.Handle("/", server.Alice)
assert.Equal(t, "30 /users\r\n", home)
}

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

home := server.Handle("/", nil)
assert.Regexp(t, "^20 text/gemini\r\n", home)
}
145 changes: 145 additions & 0 deletions test/inbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/sha256"
"fmt"
"github.com/stretchr/testify/assert"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -119,3 +120,147 @@ func TestInbox_PostToFollowersYesterday(t *testing.T) {
yesterday := server.Handle("/users/inbox/yesterday", server.Alice)
assert.Contains(t, yesterday, "No posts.")
}

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

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

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

whisper := server.Handle("/users/whisper?Hello%20%40alice%21", server.Bob)
assert.Regexp(t, "30 /users/view/[0-9a-f]{64}", whisper)

whisper = server.Handle("/users/whisper?Hello%20alice%21", server.Carol)
assert.Regexp(t, "30 /users/view/[0-9a-f]{64}", whisper)

today := server.Handle("/users/inbox/today", server.Alice)
postWithMention := strings.Index(today, "Hello @alice!")
postWithoutMention := strings.Index(today, "Hello alice!")
assert.NotEqual(t, postWithMention, -1)
assert.NotEqual(t, postWithoutMention, -1)
assert.True(t, postWithMention < postWithoutMention)
}

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

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

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

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

whisper = server.Handle("/users/whisper?Hello%20alice%21", server.Carol)
assert.Regexp(t, "30 /users/view/[0-9a-f]{64}", whisper)

today := server.Handle("/users/inbox/today", server.Alice)
postWithMention := strings.Index(today, "@alice Hello")
postWithoutMention := strings.Index(today, "Hello alice!")
assert.NotEqual(t, postWithMention, -1)
assert.NotEqual(t, postWithoutMention, -1)
assert.True(t, postWithMention < postWithoutMention)
}

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

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

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

whisper := server.Handle("/users/whisper?%40alice%2c%20Hello", server.Bob)
assert.Regexp(t, "30 /users/view/[0-9a-f]{64}", whisper)

whisper = server.Handle("/users/whisper?Hello%20alice%21", server.Carol)
assert.Regexp(t, "30 /users/view/[0-9a-f]{64}", whisper)

today := server.Handle("/users/inbox/today", server.Alice)
postWithMention := strings.Index(today, "@alice, Hello")
postWithoutMention := strings.Index(today, "Hello alice!")
assert.NotEqual(t, postWithMention, -1)
assert.NotEqual(t, postWithoutMention, -1)
assert.True(t, postWithMention < postWithoutMention)
}

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

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

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

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

whisper = server.Handle("/users/whisper?Hello%20%40alice%21", server.Carol)
assert.Regexp(t, "30 /users/view/[0-9a-f]{64}", whisper)

today := server.Handle("/users/inbox/today", server.Alice)
postWithMention := strings.Index(today, "Hello @alice!")
postWithoutMention := strings.Index(today, "Hello alice!")
assert.NotEqual(t, postWithMention, -1)
assert.NotEqual(t, postWithoutMention, -1)
assert.True(t, postWithMention < postWithoutMention)
}

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

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

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

dm := server.Handle(fmt.Sprintf("/users/dm/%x?Hello%%20Alice", sha256.Sum256([]byte(server.Alice.ID))), server.Bob)
assert.Regexp(t, "^30 /users/view/[0-9a-f]{64}\r\n$", dm)

whisper := server.Handle("/users/whisper?Hello%20%40alice%21", server.Carol)
assert.Regexp(t, "30 /users/view/[0-9a-f]{64}", whisper)

today := server.Handle("/users/inbox/today", server.Alice)
dmWithoutMention := strings.Index(today, "Hello Alice")
postWithMention := strings.Index(today, "Hello @alice!")
assert.NotEqual(t, dmWithoutMention, -1)
assert.NotEqual(t, postWithMention, -1)
assert.True(t, dmWithoutMention < postWithMention)
}

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

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

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

whisper := server.Handle("/users/whisper?Hello%20%40alice%21", server.Bob)
assert.Regexp(t, "30 /users/view/[0-9a-f]{64}", whisper)

dm := server.Handle(fmt.Sprintf("/users/dm/%x?Hello%%20Alice", sha256.Sum256([]byte(server.Alice.ID))), server.Carol)
assert.Regexp(t, "^30 /users/view/[0-9a-f]{64}\r\n$", dm)

today := server.Handle("/users/inbox/today", server.Alice)
dmWithoutMention := strings.Index(today, "Hello Alice")
postWithMention := strings.Index(today, "Hello @alice!")
assert.NotEqual(t, dmWithoutMention, -1)
assert.NotEqual(t, postWithMention, -1)
assert.True(t, dmWithoutMention < postWithMention)
}
Loading

0 comments on commit 86a83a2

Please sign in to comment.