gosrc.io/xmpp@v0.5.1/stanza/pubsub_owner_test.go (about) 1 package stanza_test 2 3 import ( 4 "encoding/xml" 5 "errors" 6 "gosrc.io/xmpp/stanza" 7 "testing" 8 ) 9 10 // ****************************** 11 // * 8.2 Configure a Node 12 // ****************************** 13 func TestNewConfigureNode(t *testing.T) { 14 expectedReq := "<iq type=\"get\" id=\"config1\" to=\"pubsub.shakespeare.lit\" > " + 15 "<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> <configure node=\"princely_musings\"></configure> " + 16 "</pubsub> </iq>" 17 18 subR, err := stanza.NewConfigureNode("pubsub.shakespeare.lit", "princely_musings") 19 if err != nil { 20 t.Fatalf("failed to create a configure node request: %v", err) 21 } 22 subR.Id = "config1" 23 24 if _, e := checkMarshalling(t, subR); e != nil { 25 t.Fatalf("Failed to check marshalling for generated sub request : %s", e) 26 } 27 28 pubsub, ok := subR.Payload.(*stanza.PubSubOwner) 29 if !ok { 30 t.Fatalf("payload is not a pubsub !") 31 } 32 33 if pubsub.OwnerUseCase == nil { 34 t.Fatalf("owner use case is nil") 35 } 36 37 ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.ConfigureOwner) 38 if !ok { 39 t.Fatalf("owner use case is not a configure tag") 40 } 41 42 if ownrUsecase.Node == "" { 43 t.Fatalf("could not parse node from config tag") 44 } 45 46 data, err := xml.Marshal(subR) 47 if err := compareMarshal(expectedReq, string(data)); err != nil { 48 t.Fatalf(err.Error()) 49 } 50 } 51 52 func TestNewConfigureNodeResp(t *testing.T) { 53 response := ` 54 <iq from="pubsub.shakespeare.lit" id="config1" to="hamlet@denmark.lit/elsinore" type="result"> 55 <pubsub xmlns="http://jabber.org/protocol/pubsub#owner"> 56 <configure node="princely_musings"> 57 <x type="form" xmlns="jabber:x:data"> 58 <field type="hidden" var="FORM_TYPE"> 59 <value>http://jabber.org/protocol/pubsub#node_config</value> 60 </field> 61 <field label="Purge all items when the relevant publisher goes offline?" type="boolean" var="pubsub#purge_offline"> 62 <value>0</value> 63 </field> 64 <field label="Max Payload size in bytes" type="text-single" var="pubsub#max_payload_size"> 65 <value>1028</value> 66 </field> 67 <field label="When to send the last published item" type="list-single" var="pubsub#send_last_published_item"> 68 <option label="Never"> 69 <value>never</value> 70 </option> 71 <option label="When a new subscription is processed"> 72 <value>on_sub</value> 73 </option> 74 <option label="When a new subscription is processed and whenever a subscriber comes online"> 75 <value>on_sub_and_presence</value> 76 </option> 77 <value>never</value> 78 </field> 79 <field label="Deliver event notifications only to available users" type="boolean" var="pubsub#presence_based_delivery"> 80 <value>0</value> 81 </field> 82 <field label="Specify the delivery style for event notifications" type="list-single" var="pubsub#notification_type"> 83 <option> 84 <value>normal</value> 85 </option> 86 <option> 87 <value>headline</value> 88 </option> 89 <value>headline</value> 90 </field> 91 <field label="Specify the type of payload data to be provided at this node" type="text-single" var="pubsub#type"> 92 <value>http://www.w3.org/2005/Atom</value> 93 </field> 94 <field label="Payload XSLT" type="text-single" var="pubsub#dataform_xslt"/> 95 </x> 96 </configure> 97 </pubsub> 98 </iq> 99 ` 100 101 pubsub, err := getPubSubOwnerPayload(response) 102 if err != nil { 103 t.Fatalf(err.Error()) 104 } 105 if pubsub.OwnerUseCase == nil { 106 t.Fatalf("owner use case is nil") 107 } 108 109 ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.ConfigureOwner) 110 if !ok { 111 t.Fatalf("owner use case is not a configure tag") 112 } 113 114 if ownrUsecase.Form == nil { 115 t.Fatalf("form is nil in the parsed config tag") 116 } 117 118 if len(ownrUsecase.Form.Fields) != 8 { 119 t.Fatalf("one or more fields in the response form could not be parsed correctly") 120 } 121 } 122 123 // ************************************************* 124 // * 8.3 Request Default Node Configuration Options 125 // ************************************************* 126 127 func TestNewRequestDefaultConfig(t *testing.T) { 128 expectedReq := "<iq type=\"get\" id=\"def1\" to=\"pubsub.shakespeare.lit\"> " + 129 "<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> <default></default> </pubsub> </iq>" 130 131 subR, err := stanza.NewRequestDefaultConfig("pubsub.shakespeare.lit") 132 if err != nil { 133 t.Fatalf("failed to create a default config request: %v", err) 134 } 135 subR.Id = "def1" 136 137 if _, e := checkMarshalling(t, subR); e != nil { 138 t.Fatalf("Failed to check marshalling for generated sub request : %s", e) 139 } 140 141 pubsub, ok := subR.Payload.(*stanza.PubSubOwner) 142 if !ok { 143 t.Fatalf("payload is not a pubsub !") 144 } 145 146 if pubsub.OwnerUseCase == nil { 147 t.Fatalf("owner use case is nil") 148 } 149 150 _, ok = pubsub.OwnerUseCase.(*stanza.DefaultOwner) 151 if !ok { 152 t.Fatalf("owner use case is not a default tag") 153 } 154 155 data, err := xml.Marshal(subR) 156 if err := compareMarshal(expectedReq, string(data)); err != nil { 157 t.Fatalf(err.Error()) 158 } 159 } 160 161 func TestNewRequestDefaultConfigResp(t *testing.T) { 162 response := ` 163 <iq from="pubsub.shakespeare.lit" id="config1" to="hamlet@denmark.lit/elsinore" type="result"> 164 <pubsub xmlns="http://jabber.org/protocol/pubsub#owner"> 165 <configure node="princely_musings"> 166 <x type="form" xmlns="jabber:x:data"> 167 <field type="hidden" var="FORM_TYPE"> 168 <value>http://jabber.org/protocol/pubsub#node_config</value> 169 </field> 170 <field label="Purge all items when the relevant publisher goes offline?" type="boolean" var="pubsub#purge_offline"> 171 <value>0</value> 172 </field> 173 <field label="Max Payload size in bytes" type="text-single" var="pubsub#max_payload_size"> 174 <value>1028</value> 175 </field> 176 <field label="When to send the last published item" type="list-single" var="pubsub#send_last_published_item"> 177 <option label="Never"> 178 <value>never</value> 179 </option> 180 <option label="When a new subscription is processed"> 181 <value>on_sub</value> 182 </option> 183 <option label="When a new subscription is processed and whenever a subscriber comes online"> 184 <value>on_sub_and_presence</value> 185 </option> 186 <value>never</value> 187 </field> 188 <field label="Deliver event notifications only to available users" type="boolean" var="pubsub#presence_based_delivery"> 189 <value>0</value> 190 </field> 191 <field label="Specify the delivery style for event notifications" type="list-single" var="pubsub#notification_type"> 192 <option> 193 <value>normal</value> 194 </option> 195 <option> 196 <value>headline</value> 197 </option> 198 <value>headline</value> 199 </field> 200 <field label="Specify the type of payload data to be provided at this node" type="text-single" var="pubsub#type"> 201 <value>http://www.w3.org/2005/Atom</value> 202 </field> 203 <field label="Payload XSLT" type="text-single" var="pubsub#dataform_xslt"/> 204 </x> 205 </configure> 206 </pubsub> 207 </iq> 208 ` 209 210 pubsub, err := getPubSubOwnerPayload(response) 211 if err != nil { 212 t.Fatalf(err.Error()) 213 } 214 if pubsub.OwnerUseCase == nil { 215 t.Fatalf("owner use case is nil") 216 } 217 218 ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.ConfigureOwner) 219 if !ok { 220 t.Fatalf("owner use case is not a configure tag") 221 } 222 223 if ownrUsecase.Form == nil { 224 t.Fatalf("form is nil in the parsed config tag") 225 } 226 227 if len(ownrUsecase.Form.Fields) != 8 { 228 t.Fatalf("one or more fields in the response form could not be parsed correctly") 229 } 230 } 231 232 // *********************** 233 // * 8.4 Delete a Node 234 // *********************** 235 236 func TestNewDelNode(t *testing.T) { 237 expectedReq := "<iq type=\"set\" id=\"delete1\" to=\"pubsub.shakespeare.lit\" >" + 238 " <pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> " + 239 "<delete node=\"princely_musings\"></delete> </pubsub> </iq>" 240 241 subR, err := stanza.NewDelNode("pubsub.shakespeare.lit", "princely_musings") 242 if err != nil { 243 t.Fatalf("failed to create a node delete request: %v", err) 244 } 245 subR.Id = "delete1" 246 247 if _, e := checkMarshalling(t, subR); e != nil { 248 t.Fatalf("Failed to check marshalling for generated sub request : %s", e) 249 } 250 251 pubsub, ok := subR.Payload.(*stanza.PubSubOwner) 252 if !ok { 253 t.Fatalf("payload is not a pubsub !") 254 } 255 256 if pubsub.OwnerUseCase == nil { 257 t.Fatalf("owner use case is nil") 258 } 259 260 _, ok = pubsub.OwnerUseCase.(*stanza.DeleteOwner) 261 if !ok { 262 t.Fatalf("owner use case is not a delete tag") 263 } 264 265 data, err := xml.Marshal(subR) 266 if err := compareMarshal(expectedReq, string(data)); err != nil { 267 t.Fatalf(err.Error()) 268 } 269 } 270 271 func TestNewDelNodeResp(t *testing.T) { 272 response := ` 273 <iq id="delete1" to="pubsub.shakespeare.lit" type="set"> 274 <pubsub xmlns="http://jabber.org/protocol/pubsub#owner"> 275 <delete node="princely_musings"> 276 <redirect uri="xmpp:hamlet@denmark.lit"/> 277 </delete> 278 </pubsub> 279 </iq> 280 ` 281 282 pubsub, err := getPubSubOwnerPayload(response) 283 if err != nil { 284 t.Fatalf(err.Error()) 285 } 286 if pubsub.OwnerUseCase == nil { 287 t.Fatalf("owner use case is nil") 288 } 289 290 ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.DeleteOwner) 291 if !ok { 292 t.Fatalf("owner use case is not a configure tag") 293 } 294 295 if ownrUsecase.RedirectOwner == nil { 296 t.Fatalf("redirect is nil in the delete tag") 297 } 298 299 if ownrUsecase.RedirectOwner.URI == "" { 300 t.Fatalf("could not parse redirect uri") 301 } 302 } 303 304 // **************************** 305 // * 8.5 Purge All Node Items 306 // **************************** 307 308 func TestNewPurgeAllItems(t *testing.T) { 309 expectedReq := "<iq type=\"set\" id=\"purge1\" to=\"pubsub.shakespeare.lit\"> " + 310 "<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> " + 311 "<purge node=\"princely_musings\"></purge> </pubsub> </iq>" 312 313 subR, err := stanza.NewPurgeAllItems("pubsub.shakespeare.lit", "princely_musings") 314 if err != nil { 315 t.Fatalf("failed to create a purge all items request: %v", err) 316 } 317 subR.Id = "purge1" 318 319 if _, e := checkMarshalling(t, subR); e != nil { 320 t.Fatalf("Failed to check marshalling for generated sub request : %s", e) 321 } 322 323 pubsub, ok := subR.Payload.(*stanza.PubSubOwner) 324 if !ok { 325 t.Fatalf("payload is not a pubsub !") 326 } 327 328 if pubsub.OwnerUseCase == nil { 329 t.Fatalf("owner use case is nil") 330 } 331 332 purge, ok := pubsub.OwnerUseCase.(*stanza.PurgeOwner) 333 if !ok { 334 t.Fatalf("owner use case is not a delete tag") 335 } 336 337 if purge.Node == "" { 338 t.Fatalf("could not parse purge targer node") 339 } 340 341 data, err := xml.Marshal(subR) 342 if err := compareMarshal(expectedReq, string(data)); err != nil { 343 t.Fatalf(err.Error()) 344 } 345 } 346 347 // ************************************ 348 // * 8.6 Manage Subscription Requests 349 // ************************************ 350 func TestNewApproveSubRequest(t *testing.T) { 351 expectedReq := "<message id=\"approve1\" to=\"pubsub.shakespeare.lit\"> " + 352 "<x xmlns=\"jabber:x:data\" type=\"submit\"> <field var=\"FORM_TYPE\" type=\"hidden\"> " + 353 "<value>http://jabber.org/protocol/pubsub#subscribe_authorization</value> </field> <field var=\"pubsub#subid\">" + 354 " <value>123-abc</value> </field> <field var=\"pubsub#node\"> <value>princely_musings</value> </field> " + 355 "<field var=\"pubsub#subscriber_jid\"> <value>horatio@denmark.lit</value> </field> <field var=\"pubsub#allow\"> " + 356 "<value>true</value> </field> </x> </message>" 357 358 apprForm := &stanza.Form{ 359 Type: stanza.FormTypeSubmit, 360 Fields: []*stanza.Field{ 361 {Var: "FORM_TYPE", Type: stanza.FieldTypeHidden, ValuesList: []string{"http://jabber.org/protocol/pubsub#subscribe_authorization"}}, 362 {Var: "pubsub#subid", ValuesList: []string{"123-abc"}}, 363 {Var: "pubsub#node", ValuesList: []string{"princely_musings"}}, 364 {Var: "pubsub#subscriber_jid", ValuesList: []string{"horatio@denmark.lit"}}, 365 {Var: "pubsub#allow", ValuesList: []string{"true"}}, 366 }, 367 } 368 369 subR, err := stanza.NewApproveSubRequest("pubsub.shakespeare.lit", "approve1", apprForm) 370 if err != nil { 371 t.Fatalf("failed to create a sub approval request: %v", err) 372 } 373 subR.Id = "approve1" 374 375 frm, ok := subR.Extensions[0].(*stanza.Form) 376 if !ok { 377 t.Fatalf("extension is not a from !") 378 } 379 380 var allowField *stanza.Field 381 382 for _, f := range frm.Fields { 383 if f.Var == "pubsub#allow" { 384 allowField = f 385 } 386 } 387 if allowField == nil || allowField.ValuesList[0] != "true" { 388 t.Fatalf("could not correctly parse the allow field in the response from") 389 } 390 391 data, err := xml.Marshal(subR) 392 if err := compareMarshal(expectedReq, string(data)); err != nil { 393 t.Fatalf(err.Error()) 394 } 395 } 396 397 // ******************************************** 398 // * 8.7 Process Pending Subscription Requests 399 // ******************************************** 400 401 func TestNewGetPendingSubRequests(t *testing.T) { 402 expectedReq := "<iq type=\"set\" id=\"pending1\" to=\"pubsub.shakespeare.lit\" > " + 403 "<command xmlns=\"http://jabber.org/protocol/commands\" action=\"execute\" node=\"http://jabber.org/protocol/pubsub#get-pending\" >" + 404 "</command> </iq>" 405 406 subR, err := stanza.NewGetPendingSubRequests("pubsub.shakespeare.lit") 407 if err != nil { 408 t.Fatalf("failed to create a get pending subs request: %v", err) 409 } 410 subR.Id = "pending1" 411 412 if _, e := checkMarshalling(t, subR); e != nil { 413 t.Fatalf("Failed to check marshalling for generated sub request : %s", e) 414 } 415 416 command, ok := subR.Payload.(*stanza.Command) 417 if !ok { 418 t.Fatalf("payload is not a command !") 419 } 420 421 if command.Action != stanza.CommandActionExecute { 422 t.Fatalf("command should be execute !") 423 } 424 425 if command.Node != "http://jabber.org/protocol/pubsub#get-pending" { 426 t.Fatalf("command node should be http://jabber.org/protocol/pubsub#get-pending !") 427 } 428 429 data, err := xml.Marshal(subR) 430 if err := compareMarshal(expectedReq, string(data)); err != nil { 431 t.Fatalf(err.Error()) 432 } 433 } 434 435 func TestNewGetPendingSubRequestsResp(t *testing.T) { 436 response := ` 437 <iq from="pubsub.shakespeare.lit" id="pending1" to="hamlet@denmark.lit/elsinore" type="result"> 438 <command action="execute" node="http://jabber.org/protocol/pubsub#get-pending" sessionid="pubsub-get-pending:20031021T150901Z-600" status="executing" xmlns="http://jabber.org/protocol/commands"> 439 <x type="form" xmlns="jabber:x:data"> 440 <field type="hidden" var="FORM_TYPE"> 441 <value>http://jabber.org/protocol/pubsub#subscribe_authorization</value> 442 </field> 443 <field type="list-single" var="pubsub#node"> 444 <option> 445 <value>princely_musings</value> 446 </option> 447 <option> 448 <value>news_from_elsinore</value> 449 </option> 450 </field> 451 </x> 452 </command> 453 </iq> 454 ` 455 456 var respIQ stanza.IQ 457 err := xml.Unmarshal([]byte(response), &respIQ) 458 if err != nil { 459 t.Fatalf("could not parse iq") 460 } 461 462 _, ok := respIQ.Payload.(*stanza.Command) 463 if !ok { 464 t.Fatal("this iq payload is not a command") 465 } 466 467 fMap, err := respIQ.GetFormFields() 468 if err != nil || len(fMap) != 2 { 469 t.Fatal("could not parse command form fields") 470 } 471 472 } 473 474 // ******************************************** 475 // * 8.7 Process Pending Subscription Requests 476 // ******************************************** 477 478 func TestNewApprovePendingSubRequest(t *testing.T) { 479 expectedReq := "<iq type=\"set\" id=\"pending2\" to=\"pubsub.shakespeare.lit\"> " + 480 "<command xmlns=\"http://jabber.org/protocol/commands\" action=\"execute\"" + 481 "node=\"http://jabber.org/protocol/pubsub#get-pending\"sessionid=\"pubsub-get-pending:20031021T150901Z-600\"> " + 482 "<x xmlns=\"jabber:x:data\" type=\"submit\"> <field xmlns=\"jabber:x:data\" var=\"pubsub#node\"> " + 483 "<value xmlns=\"jabber:x:data\">princely_musings</value> </field> </x> </command> </iq>" 484 485 subR, err := stanza.NewApprovePendingSubRequest("pubsub.shakespeare.lit", 486 "pubsub-get-pending:20031021T150901Z-600", 487 "princely_musings") 488 if err != nil { 489 t.Fatalf("failed to create a approve pending sub request: %v", err) 490 } 491 subR.Id = "pending2" 492 493 if _, e := checkMarshalling(t, subR); e != nil { 494 t.Fatalf("Failed to check marshalling for generated sub request : %s", e) 495 } 496 497 command, ok := subR.Payload.(*stanza.Command) 498 if !ok { 499 t.Fatalf("payload is not a command !") 500 } 501 502 if command.Action != stanza.CommandActionExecute { 503 t.Fatalf("command should be execute !") 504 } 505 506 //if command.Node != "http://jabber.org/protocol/pubsub#get-pending"{ 507 // t.Fatalf("command node should be http://jabber.org/protocol/pubsub#get-pending !") 508 //} 509 // 510 511 data, err := xml.Marshal(subR) 512 if err := compareMarshal(expectedReq, string(data)); err != nil { 513 t.Fatalf(err.Error()) 514 } 515 } 516 517 // ******************************************** 518 // * 8.8.1 Retrieve Subscriptions List 519 // ******************************************** 520 521 func TestNewSubListRqPl(t *testing.T) { 522 expectedReq := "<iq type=\"get\" id=\"subman1\" to=\"pubsub.shakespeare.lit\" > " + 523 "<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> " + 524 "<subscriptions node=\"princely_musings\"></subscriptions> </pubsub> </iq>" 525 526 subR, err := stanza.NewSubListRqPl("pubsub.shakespeare.lit", "princely_musings") 527 if err != nil { 528 t.Fatalf("failed to create a sub list request: %v", err) 529 } 530 subR.Id = "subman1" 531 532 if _, e := checkMarshalling(t, subR); e != nil { 533 t.Fatalf("Failed to check marshalling for generated sub request : %s", e) 534 } 535 536 pubsub, ok := subR.Payload.(*stanza.PubSubOwner) 537 if !ok { 538 t.Fatalf("payload is not a pubsub in namespace owner !") 539 } 540 541 subs, ok := pubsub.OwnerUseCase.(*stanza.SubscriptionsOwner) 542 if !ok { 543 t.Fatalf("pubsub doesn not contain a subscriptions node !") 544 } 545 546 if subs.Node != "princely_musings" { 547 t.Fatalf("subs node attribute should be princely_musings. Found %s", subs.Node) 548 } 549 550 data, err := xml.Marshal(subR) 551 if err := compareMarshal(expectedReq, string(data)); err != nil { 552 t.Fatalf(err.Error()) 553 } 554 } 555 556 func TestNewSubListRqPlResp(t *testing.T) { 557 response := ` 558 <iq from="pubsub.shakespeare.lit" id="subman1" to="hamlet@denmark.lit/elsinore" type="result"> 559 <pubsub xmlns="http://jabber.org/protocol/pubsub#owner"> 560 <subscriptions node="princely_musings"> 561 <subscription jid="hamlet@denmark.lit" subscription="subscribed"></subscription> 562 <subscription jid="polonius@denmark.lit" subscription="unconfigured"></subscription> 563 <subscription jid="bernardo@denmark.lit" subid="123-abc" subscription="subscribed"></subscription> 564 <subscription jid="bernardo@denmark.lit" subid="004-yyy" subscription="subscribed"></subscription> 565 </subscriptions> 566 </pubsub> 567 </iq> 568 ` 569 570 var respIQ stanza.IQ 571 err := xml.Unmarshal([]byte(response), &respIQ) 572 if err != nil { 573 t.Fatalf("could not parse iq") 574 } 575 576 pubsub, ok := respIQ.Payload.(*stanza.PubSubOwner) 577 if !ok { 578 t.Fatal("this iq payload is not a command") 579 } 580 581 subs, ok := pubsub.OwnerUseCase.(*stanza.SubscriptionsOwner) 582 if !ok { 583 t.Fatalf("pubsub doesn not contain a subscriptions node !") 584 } 585 586 if len(subs.Subscriptions) != 4 { 587 t.Fatalf("expected to find 4 subscriptions but got %d", len(subs.Subscriptions)) 588 } 589 590 } 591 592 // ******************************************** 593 // * 8.9.1 Retrieve Affiliations List 594 // ******************************************** 595 596 func TestNewAffiliationListRequest(t *testing.T) { 597 expectedReq := "<iq type=\"get\" id=\"ent1\" to=\"pubsub.shakespeare.lit\" > " + 598 "<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> " + 599 "<affiliations node=\"princely_musings\"></affiliations> </pubsub> </iq>" 600 601 subR, err := stanza.NewAffiliationListRequest("pubsub.shakespeare.lit", "princely_musings") 602 if err != nil { 603 t.Fatalf("failed to create an affiliations list request: %v", err) 604 } 605 subR.Id = "ent1" 606 607 if _, e := checkMarshalling(t, subR); e != nil { 608 t.Fatalf("Failed to check marshalling for generated sub request : %s", e) 609 } 610 611 pubsub, ok := subR.Payload.(*stanza.PubSubOwner) 612 if !ok { 613 t.Fatalf("payload is not a pubsub in namespace owner !") 614 } 615 616 affils, ok := pubsub.OwnerUseCase.(*stanza.AffiliationsOwner) 617 if !ok { 618 t.Fatalf("pubsub doesn not contain an affiliations node !") 619 } 620 621 if affils.Node != "princely_musings" { 622 t.Fatalf("affils node attribute should be princely_musings. Found %s", affils.Node) 623 } 624 625 data, err := xml.Marshal(subR) 626 if err := compareMarshal(expectedReq, string(data)); err != nil { 627 t.Fatalf(err.Error()) 628 } 629 } 630 631 func TestNewAffiliationListRequestResp(t *testing.T) { 632 response := ` 633 <iq from="pubsub.shakespeare.lit" id="ent1" to="hamlet@denmark.lit/elsinore" type="result"> 634 <pubsub xmlns="http://jabber.org/protocol/pubsub#owner"> 635 <affiliations node="princely_musings"> 636 <affiliation affiliation="owner" jid="hamlet@denmark.lit"/> 637 <affiliation affiliation="outcast" jid="polonius@denmark.lit"/> 638 </affiliations> 639 </pubsub> 640 </iq> 641 ` 642 643 var respIQ stanza.IQ 644 err := xml.Unmarshal([]byte(response), &respIQ) 645 if err != nil { 646 t.Fatalf("could not parse iq") 647 } 648 649 pubsub, ok := respIQ.Payload.(*stanza.PubSubOwner) 650 if !ok { 651 t.Fatal("this iq payload is not a command") 652 } 653 654 affils, ok := pubsub.OwnerUseCase.(*stanza.AffiliationsOwner) 655 if !ok { 656 t.Fatalf("pubsub doesn not contain an affiliations node !") 657 } 658 659 if len(affils.Affiliations) != 2 { 660 t.Fatalf("expected to find 2 subscriptions but got %d", len(affils.Affiliations)) 661 } 662 663 } 664 665 // ******************************************** 666 // * 8.9.2 Modify Affiliation 667 // ******************************************** 668 669 func TestNewModifAffiliationRequest(t *testing.T) { 670 expectedReq := "<iq type=\"set\" id=\"ent3\" to=\"pubsub.shakespeare.lit\" > " + 671 "<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> <affiliations node=\"princely_musings\"> " + 672 "<affiliation affiliation=\"none\" jid=\"hamlet@denmark.lit\"></affiliation> " + 673 "<affiliation affiliation=\"none\" jid=\"polonius@denmark.lit\"></affiliation> " + 674 "<affiliation affiliation=\"publisher\" jid=\"bard@shakespeare.lit\"></affiliation> </affiliations> </pubsub> " + 675 "</iq>" 676 677 affils := []stanza.AffiliationOwner{ 678 { 679 AffiliationStatus: stanza.AffiliationStatusNone, 680 Jid: "hamlet@denmark.lit", 681 }, 682 { 683 AffiliationStatus: stanza.AffiliationStatusNone, 684 Jid: "polonius@denmark.lit", 685 }, 686 { 687 AffiliationStatus: stanza.AffiliationStatusPublisher, 688 Jid: "bard@shakespeare.lit", 689 }, 690 } 691 692 subR, err := stanza.NewModifAffiliationRequest("pubsub.shakespeare.lit", "princely_musings", affils) 693 if err != nil { 694 t.Fatalf("failed to create a modif affiliation request: %v", err) 695 } 696 subR.Id = "ent3" 697 698 if _, e := checkMarshalling(t, subR); e != nil { 699 t.Fatalf("Failed to check marshalling for generated sub request : %s", e) 700 } 701 702 pubsub, ok := subR.Payload.(*stanza.PubSubOwner) 703 if !ok { 704 t.Fatalf("payload is not a pubsub in namespace owner !") 705 } 706 707 as, ok := pubsub.OwnerUseCase.(*stanza.AffiliationsOwner) 708 if !ok { 709 t.Fatalf("pubsub doesn not contain an affiliations node !") 710 } 711 712 if as.Node != "princely_musings" { 713 t.Fatalf("affils node attribute should be princely_musings. Found %s", as.Node) 714 } 715 if len(as.Affiliations) != 3 { 716 t.Fatalf("expected 3 affiliations, found %d", len(as.Affiliations)) 717 } 718 719 data, err := xml.Marshal(subR) 720 if err := compareMarshal(expectedReq, string(data)); err != nil { 721 t.Fatalf(err.Error()) 722 } 723 } 724 725 func TestGetFormFields(t *testing.T) { 726 response := ` 727 <iq from="pubsub.shakespeare.lit" id="config1" to="hamlet@denmark.lit/elsinore" type="result"> 728 <pubsub xmlns="http://jabber.org/protocol/pubsub#owner"> 729 <configure node="princely_musings"> 730 <x type="form" xmlns="jabber:x:data"> 731 <field type="hidden" var="FORM_TYPE"> 732 <value>http://jabber.org/protocol/pubsub#node_config</value> 733 </field> 734 <field label="Purge all items when the relevant publisher goes offline?" type="boolean" var="pubsub#purge_offline"> 735 <value>0</value> 736 </field> 737 <field label="Max Payload size in bytes" type="text-single" var="pubsub#max_payload_size"> 738 <value>1028</value> 739 </field> 740 <field label="When to send the last published item" type="list-single" var="pubsub#send_last_published_item"> 741 <option label="Never"> 742 <value>never</value> 743 </option> 744 <option label="When a new subscription is processed"> 745 <value>on_sub</value> 746 </option> 747 <option label="When a new subscription is processed and whenever a subscriber comes online"> 748 <value>on_sub_and_presence</value> 749 </option> 750 <value>never</value> 751 </field> 752 <field label="Deliver event notifications only to available users" type="boolean" var="pubsub#presence_based_delivery"> 753 <value>0</value> 754 </field> 755 <field label="Specify the delivery style for event notifications" type="list-single" var="pubsub#notification_type"> 756 <option> 757 <value>normal</value> 758 </option> 759 <option> 760 <value>headline</value> 761 </option> 762 <value>headline</value> 763 </field> 764 <field label="Specify the type of payload data to be provided at this node" type="text-single" var="pubsub#type"> 765 <value>http://www.w3.org/2005/Atom</value> 766 </field> 767 <field label="Payload XSLT" type="text-single" var="pubsub#dataform_xslt"/> 768 </x> 769 </configure> 770 </pubsub> 771 </iq> 772 ` 773 var iq stanza.IQ 774 err := xml.Unmarshal([]byte(response), &iq) 775 if err != nil { 776 t.Fatalf("could not parse IQ") 777 } 778 779 fields, err := iq.GetFormFields() 780 if len(fields) != 8 { 781 t.Fatalf("could not correctly parse fields. Expected 8, found : %v", len(fields)) 782 } 783 784 } 785 786 func TestGetFormFieldsCmd(t *testing.T) { 787 response := ` 788 <iq from="pubsub.shakespeare.lit" id="pending1" to="hamlet@denmark.lit/elsinore" type="result"> 789 <command action="execute" node="http://jabber.org/protocol/pubsub#get-pending" sessionid="pubsub-get-pending:20031021T150901Z-600" status="executing" xmlns="http://jabber.org/protocol/commands"> 790 <x type="form" xmlns="jabber:x:data"> 791 <field type="hidden" var="FORM_TYPE"> 792 <value>http://jabber.org/protocol/pubsub#subscribe_authorization</value> 793 </field> 794 <field type="list-single" var="pubsub#node"> 795 <option> 796 <value>princely_musings</value> 797 </option> 798 <option> 799 <value>news_from_elsinore</value> 800 </option> 801 </field> 802 </x> 803 </command> 804 </iq> 805 ` 806 var iq stanza.IQ 807 err := xml.Unmarshal([]byte(response), &iq) 808 if err != nil { 809 t.Fatalf("could not parse IQ") 810 } 811 812 fields, err := iq.GetFormFields() 813 if len(fields) != 2 { 814 t.Fatalf("could not correctly parse fields. Expected 2, found : %v", len(fields)) 815 } 816 817 } 818 819 func TestNewFormSubmissionOwner(t *testing.T) { 820 expectedReq := "<iq type=\"set\" id=\"config2\" to=\"pubsub.shakespeare.lit\">" + 821 "<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> <configure node=\"princely_musings\"> " + 822 "<x xmlns=\"jabber:x:data\" type=\"submit\" > <field var=\"FORM_TYPE\" type=\"hidden\"> " + 823 "<value>http://jabber.org/protocol/pubsub#node_config</value> </field> <field var=\"pubsub#item_expire\"> " + 824 "<value>604800</value> </field> <field var=\"pubsub#access_model\"> <value>roster</value> </field> " + 825 "<field var=\"pubsub#roster_groups_allowed\"> <value>friends</value> <value>servants</value> " + 826 "<value>courtiers</value> </field> </x> </configure> </pubsub> </iq>" 827 828 subR, err := stanza.NewFormSubmissionOwner("pubsub.shakespeare.lit", 829 "princely_musings", 830 []*stanza.Field{ 831 {Var: "FORM_TYPE", Type: stanza.FieldTypeHidden, ValuesList: []string{"http://jabber.org/protocol/pubsub#node_config"}}, 832 {Var: "pubsub#item_expire", ValuesList: []string{"604800"}}, 833 {Var: "pubsub#access_model", ValuesList: []string{"roster"}}, 834 {Var: "pubsub#roster_groups_allowed", ValuesList: []string{"friends", "servants", "courtiers"}}, 835 }) 836 if err != nil { 837 t.Fatalf("failed to create a form submission request: %v", err) 838 } 839 subR.Id = "config2" 840 841 if _, e := checkMarshalling(t, subR); e != nil { 842 t.Fatalf("Failed to check marshalling for generated sub request : %s", e) 843 } 844 845 pubsub, ok := subR.Payload.(*stanza.PubSubOwner) 846 if !ok { 847 t.Fatalf("payload is not a pubsub in namespace owner !") 848 } 849 850 conf, ok := pubsub.OwnerUseCase.(*stanza.ConfigureOwner) 851 if !ok { 852 t.Fatalf("pubsub does not contain a configure node !") 853 } 854 855 if conf.Form == nil { 856 t.Fatalf("the form is absent from the configuration submission !") 857 } 858 if len(conf.Form.Fields) != 4 { 859 t.Fatalf("expected 4 fields, found %d", len(conf.Form.Fields)) 860 } 861 if len(conf.Form.Fields[3].ValuesList) != 3 { 862 t.Fatalf("expected 3 values in fourth field, found %d", len(conf.Form.Fields[3].ValuesList)) 863 } 864 865 data, err := xml.Marshal(subR) 866 if err := compareMarshal(expectedReq, string(data)); err != nil { 867 t.Fatalf(err.Error()) 868 } 869 } 870 871 func getPubSubOwnerPayload(response string) (*stanza.PubSubOwner, error) { 872 var respIQ stanza.IQ 873 err := xml.Unmarshal([]byte(response), &respIQ) 874 875 if err != nil { 876 return &stanza.PubSubOwner{}, err 877 } 878 879 pubsub, ok := respIQ.Payload.(*stanza.PubSubOwner) 880 if !ok { 881 return nil, errors.New("this iq payload is not a pubsub of the owner namespace") 882 } 883 884 return pubsub, nil 885 }