entities_test.go 3.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
package federation
//
// GangGo Diaspora Federation Library
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// 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"
22
  "reflect"
zauberstuhl's avatar
zauberstuhl committed
23
  "github.com/Zauberstuhl/go-xml"
24 25 26 27
)

func TestEntitiesUnmarshalXML(t *testing.T) {
  var entity Entity
28 29 30 31 32 33 34 35 36 37 38
  var tests = []struct{
    Type string
    Raw []byte
  }{
    {Type: "EntityRetraction", Raw: []byte(`<retraction></retraction>`)},
    {Type: "EntityProfile", Raw: []byte(`<profile></profile>`)},
    {Type: "EntityStatusMessage", Raw: []byte(`<status_message></status_message>`)},
    {Type: "EntityReshare", Raw: []byte(`<reshare></reshare>`)},
    {Type: "EntityComment", Raw: []byte(`<comment></comment>`)},
    {Type: "EntityLike", Raw: []byte(`<like></like>`)},
    {Type: "EntityContact", Raw: []byte(`<contact></contact>`)},
39
  }
40

41 42 43 44 45 46 47 48 49 50 51 52 53
  for i, test := range tests {
    err := xml.Unmarshal(test.Raw, &entity)
    if err != nil {
      t.Errorf("#%d: Some error occured while parsing: %v", i, err)
    }
    name := reflect.TypeOf(entity.Data).Name()
    if test.Type != name {
      t.Errorf("#%d: Expected to be '%s', got '%s'", i, test.Type, name)
    }
    err = xml.Unmarshal(test.Raw[:len(test.Raw)-1], &entity)
    if err == nil {
      t.Errorf("#%d: Expected an error, got nil", i)
    }
54
  }
55

56
  err := xml.Unmarshal([]byte(`<not-supported></not-supported>`), &entity)
57 58 59 60
  if err == nil {
    t.Errorf("Expected an error, got nil")
  }
}
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

func TestEntitiesTimeMarshalAndUnmarshal(t *testing.T) {
  // federation time format
  // 2006-01-02T15:04:05Z
  var time = "2018-01-19T01:32:23Z"
  var rawXml = "<time><CreatedAt>"+time+"</CreatedAt></time>";
  var origTime = struct {
    XMLName xml.Name `xml:"time"`
    CreatedAt Time
  }{}

  err := xml.Unmarshal([]byte(rawXml), &origTime)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }
76 77
  if origTime.CreatedAt.String() != time {
    t.Errorf("Expected to be '%s', got '%s'", origTime.CreatedAt.String())
78 79 80 81 82 83 84 85 86 87
  }

  result, err := xml.Marshal(origTime)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }
  if string(result) != rawXml {
    t.Errorf("Expected to be '%s', got '%s'", result, rawXml)
  }

88 89 90 91 92 93 94 95 96
  timeTime, err := origTime.CreatedAt.Time()
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }
  if timeTime.Format(TIME_FORMAT) != time {
    t.Errorf("Expected to be '%s', got '%s'",
      time, timeTime.Format(TIME_FORMAT))
  }

97 98 99 100 101 102
  // XXX the application server uses time.Now if this happens
  // we should change that and let the library decide what is best
  //err = xml.Unmarshal([]byte("<time><CreatedAt></CreatedAt></time>"), &origTime)
  //if err == nil {
  //  t.Errorf("Expected an error, got nil")
  //}
103
}