2022-08-03 14:55:48 +00:00
|
|
|
package owl_test
|
2022-07-21 17:44:07 +00:00
|
|
|
|
2022-07-24 14:19:21 +00:00
|
|
|
import (
|
2022-08-03 14:55:48 +00:00
|
|
|
"h4kor/owl-blogs"
|
2022-09-06 18:32:21 +00:00
|
|
|
"io"
|
2022-07-24 14:19:21 +00:00
|
|
|
"math/rand"
|
2022-09-06 18:32:21 +00:00
|
|
|
"net/http"
|
2022-09-04 15:10:40 +00:00
|
|
|
"net/url"
|
2022-07-24 14:19:21 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-09-06 18:32:21 +00:00
|
|
|
type MockHtmlParser struct{}
|
2022-08-31 18:01:58 +00:00
|
|
|
|
2022-09-06 18:32:21 +00:00
|
|
|
func (*MockHtmlParser) ParseHEntry(resp *http.Response) (owl.ParsedHEntry, error) {
|
2022-08-31 18:01:58 +00:00
|
|
|
return owl.ParsedHEntry{Title: "Mock Title"}, nil
|
|
|
|
|
2022-09-06 18:32:21 +00:00
|
|
|
}
|
|
|
|
func (*MockHtmlParser) ParseLinks(resp *http.Response) ([]string, error) {
|
2022-09-04 13:32:37 +00:00
|
|
|
return []string{"http://example.com"}, nil
|
2022-09-06 18:32:21 +00:00
|
|
|
|
2022-09-04 13:32:37 +00:00
|
|
|
}
|
2022-09-06 18:32:21 +00:00
|
|
|
func (*MockHtmlParser) ParseLinksFromString(string) ([]string, error) {
|
|
|
|
return []string{"http://example.com"}, nil
|
2022-09-04 13:32:37 +00:00
|
|
|
|
2022-09-06 18:32:21 +00:00
|
|
|
}
|
|
|
|
func (*MockHtmlParser) GetWebmentionEndpoint(resp *http.Response) (string, error) {
|
2022-09-04 15:10:40 +00:00
|
|
|
return "http://example.com/webmention", nil
|
2022-09-06 18:32:21 +00:00
|
|
|
|
2022-09-04 15:10:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 11:44:25 +00:00
|
|
|
type MockParseLinksHtmlParser struct {
|
|
|
|
Links []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*MockParseLinksHtmlParser) ParseHEntry(resp *http.Response) (owl.ParsedHEntry, error) {
|
|
|
|
return owl.ParsedHEntry{Title: "Mock Title"}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
func (parser *MockParseLinksHtmlParser) ParseLinks(resp *http.Response) ([]string, error) {
|
|
|
|
return parser.Links, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
func (parser *MockParseLinksHtmlParser) ParseLinksFromString(string) ([]string, error) {
|
|
|
|
return parser.Links, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
func (*MockParseLinksHtmlParser) GetWebmentionEndpoint(resp *http.Response) (string, error) {
|
|
|
|
return "http://example.com/webmention", nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-06 18:32:21 +00:00
|
|
|
type MockHttpClient struct{}
|
|
|
|
|
|
|
|
func (*MockHttpClient) Get(url string) (resp *http.Response, err error) {
|
|
|
|
return &http.Response{}, nil
|
|
|
|
}
|
|
|
|
func (*MockHttpClient) Post(url, contentType string, body io.Reader) (resp *http.Response, err error) {
|
2022-08-31 18:01:58 +00:00
|
|
|
|
2022-09-06 18:32:21 +00:00
|
|
|
return &http.Response{}, nil
|
2022-08-31 18:01:58 +00:00
|
|
|
}
|
2022-09-06 18:32:21 +00:00
|
|
|
func (*MockHttpClient) PostForm(url string, data url.Values) (resp *http.Response, err error) {
|
2022-08-31 18:01:58 +00:00
|
|
|
|
2022-09-06 18:32:21 +00:00
|
|
|
return &http.Response{}, nil
|
2022-09-04 15:10:40 +00:00
|
|
|
}
|
|
|
|
|
2022-07-24 14:19:21 +00:00
|
|
|
func randomName() string {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
var letters = []rune("abcdefghijklmnopqrstuvwxyz")
|
|
|
|
b := make([]rune, 8)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = letters[rand.Intn(len(letters))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRepoName() string {
|
|
|
|
return "/tmp/" + randomName()
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomUserName() string {
|
|
|
|
return randomName()
|
|
|
|
}
|
2022-07-21 17:44:07 +00:00
|
|
|
|
2022-08-03 14:55:48 +00:00
|
|
|
func getTestUser() owl.User {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo, _ := owl.CreateRepository(testRepoName(), owl.RepoConfig{})
|
2022-07-21 17:44:07 +00:00
|
|
|
user, _ := repo.CreateUser(randomUserName())
|
|
|
|
return user
|
|
|
|
}
|
2022-07-23 15:19:47 +00:00
|
|
|
|
2022-09-05 18:50:46 +00:00
|
|
|
func getTestRepo(config owl.RepoConfig) owl.Repository {
|
|
|
|
repo, _ := owl.CreateRepository(testRepoName(), config)
|
2022-07-24 18:29:31 +00:00
|
|
|
return repo
|
|
|
|
}
|
|
|
|
|
2022-07-23 15:19:47 +00:00
|
|
|
func contains(s []string, e string) bool {
|
|
|
|
for _, a := range s {
|
|
|
|
if a == e {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|