entities_test.go 4.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
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"
  "encoding/xml"
)

func TestEntitiesUnmarshalXML(t *testing.T) {
  var entity Entity

  var retractionRaw = []byte(`<retraction></retraction>`)
  var profileRaw = []byte(`<profile></profile>`)
  var statusMessageRaw = []byte(`<status_message></status_message>`)
31
  var reshareRaw = []byte(`<reshare></reshare>`)
32 33 34 35 36 37 38 39 40 41 42 43
  var commentRaw = []byte(`<comment></comment>`)
  var likeRaw = []byte(`<like></like>`)
  var contactRaw = []byte(`<contact></contact>`)
  var notSupportedRaw = []byte(`<notsupported></notsupported>`)

  err := xml.Unmarshal(retractionRaw, &entity)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }
  if data, ok := entity.Data.(EntityRetraction); !ok {
    t.Errorf("Expected to be 'like', got %v", data)
  }
44 45 46 47
  err = xml.Unmarshal(retractionRaw[:len(retractionRaw)-1], &entity)
  if err == nil {
    t.Errorf("Expected an error, got nil")
  }
48 49 50 51 52 53 54 55

  err = xml.Unmarshal(profileRaw, &entity)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }
  if data, ok := entity.Data.(EntityProfile); !ok {
    t.Errorf("Expected to be 'profile', got %v", data)
  }
56 57 58 59
  err = xml.Unmarshal(profileRaw[:len(profileRaw)-1], &entity)
  if err == nil {
    t.Errorf("Expected an error, got nil")
  }
60 61 62 63 64 65 66 67

  err = xml.Unmarshal(statusMessageRaw, &entity)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }
  if data, ok := entity.Data.(EntityStatusMessage); !ok {
    t.Errorf("Expected to be 'status_message', got %v", data)
  }
68 69 70 71
  err = xml.Unmarshal(statusMessageRaw[:len(statusMessageRaw)-1], &entity)
  if err == nil {
    t.Errorf("Expected an error, got nil")
  }
72

73 74 75 76 77 78 79 80 81 82 83
  err = xml.Unmarshal(reshareRaw, &entity)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }
  if data, ok := entity.Data.(EntityReshare); !ok {
    t.Errorf("Expected to be 'reshare', got %v", data)
  }
  err = xml.Unmarshal(reshareRaw[:len(reshareRaw)-1], &entity)
  if err == nil {
    t.Errorf("Expected an error, got nil")
  }
84 85 86 87 88 89 90 91

  err = xml.Unmarshal(commentRaw, &entity)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }
  if data, ok := entity.Data.(EntityComment); !ok {
    t.Errorf("Expected to be 'comment', got %v", data)
  }
92 93 94 95
  err = xml.Unmarshal(commentRaw[:len(commentRaw)-1], &entity)
  if err == nil {
    t.Errorf("Expected an error, got nil")
  }
96 97 98 99 100 101 102 103

  err = xml.Unmarshal(likeRaw, &entity)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }
  if data, ok := entity.Data.(EntityLike); !ok {
    t.Errorf("Expected to be 'like', got %v", data)
  }
104 105 106 107
  err = xml.Unmarshal(likeRaw[:len(likeRaw)-1], &entity)
  if err == nil {
    t.Errorf("Expected an error, got nil")
  }
108 109 110 111 112 113 114 115

  err = xml.Unmarshal(contactRaw, &entity)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }
  if data, ok := entity.Data.(EntityContact); !ok {
    t.Errorf("Expected to be 'contact', got %v", data)
  }
116 117 118 119
  err = xml.Unmarshal(contactRaw[:len(contactRaw)-1], &entity)
  if err == nil {
    t.Errorf("Expected an error, got nil")
  }
120 121 122 123 124 125

  err = xml.Unmarshal(notSupportedRaw, &entity)
  if err == nil {
    t.Errorf("Expected an error, got nil")
  }
}
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

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)
  }
  if origTime.CreatedAt.Time.Format(TIME_FORMAT) != time {
    t.Errorf("Expected to be '%s', got '%s'",
      origTime.CreatedAt.Time.Format(TIME_FORMAT))
  }

  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)
  }

  err = xml.Unmarshal([]byte("<time><CreatedAt></CreatedAt></time>"), &origTime)
  if err == nil {
    t.Errorf("Expected an error, got nil")
  }
}