Commit 8cd7111c authored by Lukas Matt's avatar Lukas Matt

Fix public key retrieval from friendica via hcard

related to ganggo#61
parent e0406596
...@@ -71,6 +71,10 @@ func (h *Hcard) Fetch(endpoint string) error { ...@@ -71,6 +71,10 @@ func (h *Hcard) Fetch(endpoint string) error {
func(i int, s *goquery.Selection) { func(i int, s *goquery.Selection) {
(*h).PublicKey = s.Find("pre").Text() (*h).PublicKey = s.Find("pre").Text()
}) })
doc.Find(".key.u-key").Each(
func(i int, s *goquery.Selection) {
(*h).PublicKey = s.Text()
})
doc.Find(".entity_url").Each( doc.Find(".entity_url").Each(
func(i int, s *goquery.Selection) { func(i int, s *goquery.Selection) {
(*h).Url = s.Find("a").Text() (*h).Url = s.Find("a").Text()
......
...@@ -23,45 +23,44 @@ import ( ...@@ -23,45 +23,44 @@ import (
) )
func TestHcard(t *testing.T) { func TestHcard(t *testing.T) {
var hcard Hcard // XXX testing on live systems could break stuff
err := hcard.Fetch(TEST_HCARD_LINK) // First link is a diaspora production server
if err != nil { // Second link is a diaspora development server
t.Errorf("Some error occured while parsing: %v", err) // Third link is a friendica production server
links := []string{
"https://joindiaspora.com/hcard/users/db3d5a70c1ee01334cdd5215e5076e52",
"https://sechat.org/hcard/users/18ad211f33c65726",
"https://pirati.ca/hcard/heluecht",
} }
for _, link := range links {
matched, err := regexp.MatchString(`^[\w\d]+?$`, hcard.Guid) var hcard Hcard
if err != nil || !matched { err := hcard.Fetch(link)
t.Errorf("Expected nil and match, got %v and %v", err, matched) if err != nil {
} t.Errorf("Some error occured while parsing: %v", err)
}
nickname := "podmin" evaluateHcard(hcard, t)
if hcard.Nickname != nickname {
t.Errorf("Expected to be %s, got %s", nickname, hcard.Nickname)
}
matched, err = regexp.MatchString(`^-----BEGIN.+?KEY-----`, hcard.PublicKey)
if err != nil || !matched {
t.Errorf("Expected nil and match, got %v and '%s'", err, hcard.PublicKey)
}
url := "https://joindiaspora.com/"
if hcard.Url != url {
t.Errorf("Expected to be %s, got %s", url, hcard.Url)
}
photo := url + "uploads/images/thumb_"
matched, err = regexp.MatchString(photo + `large_[\w\d]+?\.png`, hcard.Photo)
if err != nil || !matched {
t.Errorf("Expected nil and match, got %v and %s", err, hcard.Photo)
} }
}
matched, err = regexp.MatchString(photo + `medium_[\w\d]+?\.png`, hcard.PhotoMedium) func evaluateHcard(hcard Hcard, t *testing.T) {
if err != nil || !matched { var tests = []struct{
t.Errorf("Expected nil and match, got %v and %s", err, hcard.PhotoMedium) Expected string
Text string
}{
{`^[\w\d]+?$`, hcard.Guid},
{`^[\w\d]+?$`, hcard.Nickname},
{`^-----BEGIN.+?KEY-----`, hcard.PublicKey},
{`^[\w\d\.\:\/]+?$`, hcard.Url},
{`^http.*\.\w+\?{0,1}.*$`, hcard.Photo},
{`^http.*\.\w+\?{0,1}.*$`, hcard.PhotoSmall},
{`^http.*\.\w+\?{0,1}.*$`, hcard.PhotoMedium},
} }
matched, err = regexp.MatchString(photo + `small_[\w\d]+?\.png`, hcard.PhotoSmall) for i, test := range tests {
if err != nil || !matched { matched, err := regexp.MatchString(test.Expected, test.Text)
t.Errorf("Expected nil and match, got %v and %s", err, hcard.PhotoSmall) if err != nil || !matched {
t.Errorf("#%d: Expected to be '%s', got '%s' (%v)",
i, test.Expected, test.Text, err)
}
} }
} }
...@@ -25,8 +25,6 @@ import ( ...@@ -25,8 +25,6 @@ import (
var TEST_AUTHOR = `diaspora_2nd@localhost:3001` var TEST_AUTHOR = `diaspora_2nd@localhost:3001`
var TEST_HCARD_LINK = "https://joindiaspora.com/hcard/users/db3d5a70c1ee01334cdd5215e5076e52"
var TEST_PRIV_KEY = []byte(`-----BEGIN PRIVATE KEY----- var TEST_PRIV_KEY = []byte(`-----BEGIN PRIVATE KEY-----
MIIEpAIBAAKCAQEA5qryP/x8Wtht+VLH/TvLNofTF9B4aj78sYlctQxIuzEmuLc3 MIIEpAIBAAKCAQEA5qryP/x8Wtht+VLH/TvLNofTF9B4aj78sYlctQxIuzEmuLc3
nU+EhpeExe9DpJEt6/l5V3NIHymkkNMA7Mu4fWmxdyVMEweY7utTl+wkwdJFFlBV nU+EhpeExe9DpJEt6/l5V3NIHymkkNMA7Mu4fWmxdyVMEweY7utTl+wkwdJFFlBV
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment