hcard_test.go 2.02 KB
Newer Older
1 2
package federation
//
Ghost User's avatar
Ghost User committed
3 4
// GangGo Federation Library
// Copyright (C) 2017-2018 Lukas Matt <lukas@zauberstuhl.de>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import (
  "testing"
  "regexp"
)

func TestHcard(t *testing.T) {
26 27 28 29 30 31 32 33
  // XXX testing on live systems could break stuff
  // First link is a diaspora production server
  // Second link is a diaspora development server
  // 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",
34
  }
35 36 37 38 39 40 41
  for _, link := range links {
    var hcard Hcard
    err := hcard.Fetch(link)
    if err != nil {
      t.Errorf("Some error occured while parsing: %v", err)
    }
    evaluateHcard(hcard, t)
42
  }
43
}
44

45 46 47 48 49 50 51 52 53 54 55 56
func evaluateHcard(hcard Hcard, t *testing.T) {
  var tests = []struct{
    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},
57 58
  }

59 60 61 62 63 64
  for i, test := range tests {
    matched, err := regexp.MatchString(test.Expected, test.Text)
    if err != nil || !matched {
      t.Errorf("#%d: Expected to be '%s', got '%s' (%v)",
        i, test.Expected, test.Text, err)
    }
65 66
  }
}