2022-09-05 18:34:24 +00:00
|
|
|
package web_test
|
2022-08-23 15:59:17 +00:00
|
|
|
|
|
|
|
import (
|
2022-08-23 19:06:29 +00:00
|
|
|
"h4kor/owl-blogs"
|
2022-09-05 18:34:24 +00:00
|
|
|
main "h4kor/owl-blogs/cmd/owl/web"
|
2022-11-06 13:17:14 +00:00
|
|
|
"h4kor/owl-blogs/test/assertions"
|
2022-08-23 15:59:17 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2022-09-08 18:52:04 +00:00
|
|
|
"os"
|
2022-08-23 15:59:17 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2022-08-23 19:06:29 +00:00
|
|
|
func setupWebmentionTest(repo owl.Repository, user owl.User, target string, source string) (*httptest.ResponseRecorder, error) {
|
2022-08-23 15:59:17 +00:00
|
|
|
|
|
|
|
data := url.Values{}
|
|
|
|
data.Set("target", target)
|
|
|
|
data.Set("source", source)
|
|
|
|
|
|
|
|
// Create Request and Response
|
|
|
|
req, err := http.NewRequest("POST", user.UrlPath()+"webmention/", strings.NewReader(data.Encode()))
|
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
|
|
|
|
|
|
|
|
if err != nil {
|
2022-08-23 19:06:29 +00:00
|
|
|
return nil, err
|
2022-08-23 15:59:17 +00:00
|
|
|
}
|
2022-08-23 19:06:29 +00:00
|
|
|
|
2022-08-23 15:59:17 +00:00
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
router := main.Router(&repo)
|
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
|
2022-08-23 19:06:29 +00:00
|
|
|
return rr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebmentionHandleAccepts(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-08-23 19:06:29 +00:00
|
|
|
user, _ := repo.CreateUser("test-1")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("post-1", false)
|
2022-08-23 19:06:29 +00:00
|
|
|
|
|
|
|
target := post.FullUrl()
|
|
|
|
source := "https://example.com"
|
|
|
|
|
|
|
|
rr, err := setupWebmentionTest(repo, user, target, source)
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error setting up webmention test")
|
2022-08-23 15:59:17 +00:00
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertStatus(t, rr, http.StatusAccepted)
|
2022-08-23 19:06:29 +00:00
|
|
|
|
2022-08-23 15:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebmentionWrittenToPost(t *testing.T) {
|
2022-08-23 19:06:29 +00:00
|
|
|
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-08-23 15:59:17 +00:00
|
|
|
user, _ := repo.CreateUser("test-1")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("post-1", false)
|
2022-08-23 15:59:17 +00:00
|
|
|
|
|
|
|
target := post.FullUrl()
|
|
|
|
source := "https://example.com"
|
|
|
|
|
2022-08-23 19:06:29 +00:00
|
|
|
rr, err := setupWebmentionTest(repo, user, target, source)
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error setting up webmention test")
|
2022-08-23 15:59:17 +00:00
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertStatus(t, rr, http.StatusAccepted)
|
|
|
|
assertions.AssertLen(t, post.IncomingWebmentions(), 1)
|
2022-08-23 19:06:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// https://www.w3.org/TR/webmention/#h-request-verification
|
|
|
|
//
|
|
|
|
|
|
|
|
// The receiver MUST check that source and target are valid URLs [URL]
|
|
|
|
// and are of schemes that are supported by the receiver.
|
|
|
|
// (Most commonly this means checking that the source and target schemes are http or https).
|
|
|
|
func TestWebmentionSourceValidation(t *testing.T) {
|
|
|
|
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-08-23 19:06:29 +00:00
|
|
|
user, _ := repo.CreateUser("test-1")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("post-1", false)
|
2022-08-23 19:06:29 +00:00
|
|
|
|
|
|
|
target := post.FullUrl()
|
|
|
|
source := "ftp://example.com"
|
|
|
|
|
|
|
|
rr, err := setupWebmentionTest(repo, user, target, source)
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error setting up webmention test")
|
2022-08-23 19:06:29 +00:00
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertStatus(t, rr, http.StatusBadRequest)
|
2022-08-23 19:06:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebmentionTargetValidation(t *testing.T) {
|
|
|
|
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-08-23 19:06:29 +00:00
|
|
|
user, _ := repo.CreateUser("test-1")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("post-1", false)
|
2022-08-23 19:06:29 +00:00
|
|
|
|
|
|
|
target := "ftp://example.com"
|
|
|
|
source := post.FullUrl()
|
|
|
|
|
|
|
|
rr, err := setupWebmentionTest(repo, user, target, source)
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error setting up webmention test")
|
2022-08-23 19:06:29 +00:00
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertStatus(t, rr, http.StatusBadRequest)
|
2022-08-23 19:06:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The receiver MUST reject the request if the source URL is the same as the target URL.
|
|
|
|
|
|
|
|
func TestWebmentionSameTargetAndSource(t *testing.T) {
|
|
|
|
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-08-23 19:06:29 +00:00
|
|
|
user, _ := repo.CreateUser("test-1")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("post-1", false)
|
2022-08-23 19:06:29 +00:00
|
|
|
|
|
|
|
target := post.FullUrl()
|
|
|
|
source := post.FullUrl()
|
|
|
|
|
|
|
|
rr, err := setupWebmentionTest(repo, user, target, source)
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error setting up webmention test")
|
2022-08-23 15:59:17 +00:00
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertStatus(t, rr, http.StatusBadRequest)
|
2022-08-23 15:59:17 +00:00
|
|
|
}
|
2022-08-23 19:06:29 +00:00
|
|
|
|
|
|
|
// The receiver SHOULD check that target is a valid resource for which it can accept Webmentions.
|
|
|
|
// This check SHOULD happen synchronously to reject invalid Webmentions before more in-depth verification begins.
|
|
|
|
// What a "valid resource" means is up to the receiver.
|
|
|
|
func TestValidationOfTarget(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-08-23 19:06:29 +00:00
|
|
|
user, _ := repo.CreateUser("test-1")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("post-1", false)
|
2022-08-23 19:06:29 +00:00
|
|
|
|
|
|
|
target := post.FullUrl()
|
|
|
|
target = target[:len(target)-1] + "invalid"
|
|
|
|
source := post.FullUrl()
|
|
|
|
|
|
|
|
rr, err := setupWebmentionTest(repo, user, target, source)
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error setting up webmention test")
|
2022-08-23 19:06:29 +00:00
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertStatus(t, rr, http.StatusBadRequest)
|
2022-08-23 19:06:29 +00:00
|
|
|
}
|
2022-09-08 18:52:04 +00:00
|
|
|
|
|
|
|
func TestAcceptWebmentionForAlias(t *testing.T) {
|
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
|
|
|
user, _ := repo.CreateUser("test-1")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("post-1", false)
|
2022-09-08 18:52:04 +00:00
|
|
|
|
|
|
|
content := "---\n"
|
|
|
|
content += "title: Test\n"
|
|
|
|
content += "aliases: \n"
|
|
|
|
content += " - /foo/bar\n"
|
|
|
|
content += " - /foo/baz\n"
|
|
|
|
content += "---\n"
|
|
|
|
content += "This is a test"
|
|
|
|
os.WriteFile(post.ContentFile(), []byte(content), 0644)
|
|
|
|
|
|
|
|
target := "https://example.com/foo/bar"
|
|
|
|
source := "https://example.com"
|
|
|
|
|
|
|
|
rr, err := setupWebmentionTest(repo, user, target, source)
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error setting up webmention test")
|
2022-09-08 18:52:04 +00:00
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertStatus(t, rr, http.StatusAccepted)
|
2022-09-08 18:52:04 +00:00
|
|
|
}
|