2021-04-09 18:04:04 +02:00
package recv
2021-04-08 12:16:33 +02:00
import (
2021-04-09 21:00:54 +02:00
"brainbaking.com/go-jamming/app/mf"
2021-04-08 12:16:33 +02:00
"errors"
2021-04-09 10:12:14 +02:00
"github.com/stretchr/testify/assert"
"io/ioutil"
2021-04-11 09:50:27 +02:00
"net/http"
2021-04-08 16:37:04 +02:00
"os"
"testing"
2021-04-09 10:12:14 +02:00
"time"
2021-04-08 12:16:33 +02:00
2021-04-09 18:04:04 +02:00
"brainbaking.com/go-jamming/common"
"brainbaking.com/go-jamming/mocks"
2021-04-08 12:16:33 +02:00
)
var conf = & common . Config {
2021-04-09 21:00:54 +02:00
AllowedWebmentionSources : [ ] string {
2021-04-08 12:16:33 +02:00
"jefklakscodex.com" ,
} ,
DataPath : "testdata" ,
}
func TestConvertWebmentionToPath ( t * testing . T ) {
2021-04-09 14:21:25 +02:00
wm := mf . Mention {
2021-04-09 12:40:37 +02:00
Source : "https://brainbaking.com" ,
Target : "https://jefklakscodex.com/articles" ,
2021-04-08 12:16:33 +02:00
}
2021-04-09 14:21:25 +02:00
result := wm . AsPath ( conf )
2021-04-08 12:16:33 +02:00
if result != "testdata/jefklakscodex.com/99be66594fdfcf482545fead8e7e4948.json" {
t . Fatalf ( "md5 hash check failed, got " + result )
}
}
func writeSomethingTo ( filename string ) {
file , _ := os . Create ( filename )
file . WriteString ( "lolz" )
2021-04-09 21:00:54 +02:00
defer file . Close ( )
2021-04-08 12:16:33 +02:00
}
2021-04-09 10:12:14 +02:00
func TestReceive ( t * testing . T ) {
cases := [ ] struct {
label string
2021-04-09 14:21:25 +02:00
wm mf . Mention
2021-04-09 12:40:37 +02:00
json string
2021-04-09 21:00:54 +02:00
} {
2021-04-09 10:12:14 +02:00
{
2021-04-09 12:40:37 +02:00
label : "receive a Webmention bookmark via twitter" ,
2021-04-09 14:21:25 +02:00
wm : mf . Mention {
2021-04-09 12:40:37 +02:00
Source : "https://brainbaking.com/valid-bridgy-twitter-source.html" ,
Target : "https://brainbaking.com/post/2021/03/the-indieweb-mixed-bag" ,
2021-04-09 10:12:14 +02:00
} ,
json : ` { "author": { "name":"Jamie Tanna","picture":"https://www.jvt.me/img/profile.png"},"name":"","content":"Recommended read:\nThe IndieWeb Mixed Bag - Thoughts about the (d)evolution of blog interactions\nhttps://brainbaking.com/post/2021/03/the-indieweb-mixed-bag/","published":"2021-03-15T12:42:00+0000","url":"https://brainbaking.com/mf2/2021/03/1bkre/","type":"bookmark","source":"https://brainbaking.com/valid-bridgy-twitter-source.html","target":"https://brainbaking.com/post/2021/03/the-indieweb-mixed-bag"} ` ,
} ,
{
2021-04-09 12:40:37 +02:00
label : "receive a brid.gy Webmention like" ,
2021-04-09 14:21:25 +02:00
wm : mf . Mention {
2021-04-09 12:40:37 +02:00
Source : "https://brainbaking.com/valid-bridgy-like.html" ,
2021-04-09 10:12:14 +02:00
// wrapped in a a class="u-like-of" tag
2021-04-09 12:40:37 +02:00
Target : "https://brainbaking.com/valid-indieweb-target.html" ,
2021-04-09 10:12:14 +02:00
} ,
// no dates in bridgy-to-mastodon likes...
json : ` { "author": { "name":"Stampeding Longhorn","picture":"https://cdn.social.linux.pizza/v1/AUTH_91eb37814936490c95da7b85993cc2ff/sociallinuxpizza/accounts/avatars/000/185/996/original/9e36da0c093cfc9b.png"},"name":"","content":"","published":"2020-01-01T12:30:00","url":"https://chat.brainbaking.com/notice/A4nx1rFwKUJYSe4TqK#favorited-by-A4nwg4LYyh4WgrJOXg","type":"like","source":"https://brainbaking.com/valid-bridgy-like.html","target":"https://brainbaking.com/valid-indieweb-target.html"} ` ,
} ,
{
2021-04-09 12:40:37 +02:00
label : "receive a brid.gy Webmention that has a url and photo without value" ,
2021-04-09 14:21:25 +02:00
wm : mf . Mention {
2021-04-09 12:40:37 +02:00
Source : "https://brainbaking.com/valid-bridgy-source.html" ,
Target : "https://brainbaking.com/valid-indieweb-target.html" ,
2021-04-09 10:12:14 +02:00
} ,
json : ` { "author": { "name":"Stampeding Longhorn", "picture":"https://cdn.social.linux.pizza/v1/AUTH_91eb37814936490c95da7b85993cc2ff/sociallinuxpizza/accounts/avatars/000/185/996/original/9e36da0c093cfc9b.png"}, "content":"@wouter The cat pictures are awesome. for jest tests!", "name":"@wouter The cat pictures are awesome. for jest tests!", "published":"2021-03-02T16:17:18.000Z", "source":"https://brainbaking.com/valid-bridgy-source.html", "target":"https://brainbaking.com/valid-indieweb-target.html", "type":"mention", "url":"https://social.linux.pizza/@StampedingLonghorn/105821099684887793"} ` ,
} ,
{
label : "receive saves a JSON file of indieweb-metadata if all is valid" ,
2021-04-09 14:21:25 +02:00
wm : mf . Mention {
2021-04-09 12:40:37 +02:00
Source : "https://brainbaking.com/valid-indieweb-source.html" ,
Target : "https://jefklakscodex.com/articles" ,
2021-04-09 10:12:14 +02:00
} ,
json : ` { "author": { "name":"Wouter Groeneveld","picture":"https://brainbaking.com//img/avatar.jpg"},"name":"I just learned about https://www.inklestudios.com/...","content":"This is cool, I just found out about valid indieweb target - so cool","published":"2021-03-06T12:41:00","url":"https://brainbaking.com/notes/2021/03/06h12m41s48/","type":"mention","source":"https://brainbaking.com/valid-indieweb-source.html","target":"https://jefklakscodex.com/articles"} ` ,
} ,
{
label : "receive saves a JSON file of indieweb-metadata with summary as content if present" ,
2021-04-09 14:21:25 +02:00
wm : mf . Mention {
2021-04-09 12:40:37 +02:00
Source : "https://brainbaking.com/valid-indieweb-source-with-summary.html" ,
Target : "https://brainbaking.com/valid-indieweb-target.html" ,
2021-04-09 10:12:14 +02:00
} ,
json : ` { "author": { "name":"Wouter Groeneveld", "picture":"https://brainbaking.com//img/avatar.jpg"}, "content":"This is cool, this is a summary!", "name":"I just learned about https://www.inklestudios.com/...", "published":"2021-03-06T12:41:00", "source":"https://brainbaking.com/valid-indieweb-source-with-summary.html", "target":"https://brainbaking.com/valid-indieweb-target.html", "type":"mention", "url":"https://brainbaking.com/notes/2021/03/06h12m41s48/"} ` ,
} ,
{
label : "receive saves a JSON file of non-indieweb-data such as title if all is valid" ,
2021-04-09 14:21:25 +02:00
wm : mf . Mention {
2021-04-09 12:40:37 +02:00
Source : "https://brainbaking.com/valid-nonindieweb-source.html" ,
Target : "https://brainbaking.com/valid-indieweb-target.html" ,
2021-04-09 10:12:14 +02:00
} ,
json : ` { "author": { "name":"https://brainbaking.com/valid-nonindieweb-source.html", "picture":""}, "content":"Diablo 2 Twenty Years Later: A Retrospective | Jefklaks Codex", "name":"Diablo 2 Twenty Years Later: A Retrospective | Jefklaks Codex", "published":"2020-01-01T12:30:00", "source":"https://brainbaking.com/valid-nonindieweb-source.html", "target":"https://brainbaking.com/valid-indieweb-target.html", "type":"mention", "url":"https://brainbaking.com/valid-nonindieweb-source.html"} ` ,
2021-04-08 16:37:04 +02:00
} ,
}
2021-04-09 10:12:14 +02:00
for _ , tc := range cases {
t . Run ( tc . label , func ( t * testing . T ) {
os . MkdirAll ( "testdata/brainbaking.com" , os . ModePerm )
os . MkdirAll ( "testdata/jefklakscodex.com" , os . ModePerm )
defer os . RemoveAll ( "testdata" )
2021-04-09 14:21:25 +02:00
common . Now = func ( ) time . Time {
2021-04-09 10:12:14 +02:00
return time . Date ( 2020 , time . January , 1 , 12 , 30 , 0 , 0 , time . UTC )
}
2021-04-09 12:40:37 +02:00
receiver := & Receiver {
Conf : conf ,
RestClient : & mocks . RestClientMock {
2021-04-09 14:21:25 +02:00
GetBodyFunc : mocks . RelPathGetBodyFunc ( t , "../../../mocks/" ) ,
2021-04-09 10:12:14 +02:00
} ,
}
2021-04-09 12:40:37 +02:00
receiver . Receive ( tc . wm )
2021-04-09 10:12:14 +02:00
2021-04-09 14:21:25 +02:00
actualJson , _ := ioutil . ReadFile ( tc . wm . AsPath ( conf ) )
2021-04-09 10:12:14 +02:00
assert . JSONEq ( t , tc . json , string ( actualJson ) )
} )
}
2021-04-08 16:37:04 +02:00
}
2021-04-08 12:16:33 +02:00
func TestReceiveTargetDoesNotExistAnymoreDeletesPossiblyOlderWebmention ( t * testing . T ) {
os . MkdirAll ( "testdata/jefklakscodex.com" , os . ModePerm )
defer os . RemoveAll ( "testdata" )
2021-04-09 14:21:25 +02:00
wm := mf . Mention {
2021-04-09 12:40:37 +02:00
Source : "https://brainbaking.com" ,
Target : "https://jefklakscodex.com/articles" ,
2021-04-08 12:16:33 +02:00
}
2021-04-09 14:21:25 +02:00
filename := wm . AsPath ( conf )
2021-04-08 12:16:33 +02:00
writeSomethingTo ( filename )
client := & mocks . RestClientMock {
2021-04-11 09:50:27 +02:00
GetBodyFunc : func ( url string ) ( http . Header , string , error ) {
return nil , "" , errors . New ( "whoops" )
2021-04-08 12:16:33 +02:00
} ,
2021-04-09 21:00:54 +02:00
}
2021-04-09 12:40:37 +02:00
receiver := & Receiver {
Conf : conf ,
RestClient : client ,
2021-04-08 12:16:33 +02:00
}
2021-04-09 12:40:37 +02:00
receiver . Receive ( wm )
2021-04-09 10:12:14 +02:00
assert . NoFileExists ( t , filename )
}
func TestReceiveTargetThatDoesNotPointToTheSourceDoesNothing ( t * testing . T ) {
2021-04-09 14:21:25 +02:00
wm := mf . Mention {
2021-04-09 12:40:37 +02:00
Source : "https://brainbaking.com/valid-indieweb-source.html" ,
Target : "https://brainbaking.com/valid-indieweb-source.html" ,
2021-04-09 10:12:14 +02:00
}
2021-04-09 14:21:25 +02:00
filename := wm . AsPath ( conf )
2021-04-09 10:12:14 +02:00
writeSomethingTo ( filename )
2021-04-09 12:40:37 +02:00
receiver := & Receiver {
Conf : conf ,
RestClient : & mocks . RestClientMock {
2021-04-09 14:21:25 +02:00
GetBodyFunc : mocks . RelPathGetBodyFunc ( t , "../../../mocks/" ) ,
2021-04-09 10:12:14 +02:00
} ,
}
2021-04-09 12:40:37 +02:00
receiver . Receive ( wm )
2021-04-09 10:12:14 +02:00
assert . NoFileExists ( t , filename )
2021-04-08 12:50:15 +02:00
}
func TestProcessSourceBodyAbortsIfNoMentionOfTargetFoundInSourceHtml ( t * testing . T ) {
os . MkdirAll ( "testdata/jefklakscodex.com" , os . ModePerm )
defer os . RemoveAll ( "testdata" )
2021-04-09 14:21:25 +02:00
wm := mf . Mention {
2021-04-09 12:40:37 +02:00
Source : "https://brainbaking.com" ,
Target : "https://jefklakscodex.com/articles" ,
2021-04-08 12:50:15 +02:00
}
2021-04-09 12:40:37 +02:00
receiver := & Receiver {
Conf : conf ,
2021-04-08 12:50:15 +02:00
}
receiver . processSourceBody ( "<html>my nice body</html>" , wm )
2021-04-09 14:21:25 +02:00
assert . NoFileExists ( t , wm . AsPath ( conf ) )
2021-04-08 12:50:15 +02:00
}