package stanza_test
import (
"encoding/xml"
"errors"
"gosrc.io/xmpp/stanza"
"testing"
)
// ******************************
// * 8.2 Configure a Node
// ******************************
func TestNewConfigureNode(t *testing.T) {
expectedReq := " " +
" " +
" "
subR, err := stanza.NewConfigureNode("pubsub.shakespeare.lit", "princely_musings")
subR.Id = "config1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if pubsub.OwnerUseCase == nil {
t.Fatalf("owner use case is nil")
}
ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.ConfigureOwner)
if !ok {
t.Fatalf("owner use case is not a configure tag")
}
if ownrUsecase.Node == "" {
t.Fatalf("could not parse node from config tag")
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
func TestNewConfigureNodeResp(t *testing.T) {
response := `
http://jabber.org/protocol/pubsub#node_config
0
1028
never
0
headline
http://www.w3.org/2005/Atom
`
pubsub, err := getPubSubOwnerPayload(response)
if err != nil {
t.Fatalf(err.Error())
}
if pubsub.OwnerUseCase == nil {
t.Fatalf("owner use case is nil")
}
ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.ConfigureOwner)
if !ok {
t.Fatalf("owner use case is not a configure tag")
}
if ownrUsecase.Form == nil {
t.Fatalf("form is nil in the parsed config tag")
}
if len(ownrUsecase.Form.Fields) != 8 {
t.Fatalf("one or more fields in the response form could not be parsed correctly")
}
}
// *************************************************
// * 8.3 Request Default Node Configuration Options
// *************************************************
func TestNewRequestDefaultConfig(t *testing.T) {
expectedReq := " " +
" "
subR, err := stanza.NewRequestDefaultConfig("pubsub.shakespeare.lit")
subR.Id = "def1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if pubsub.OwnerUseCase == nil {
t.Fatalf("owner use case is nil")
}
_, ok = pubsub.OwnerUseCase.(*stanza.DefaultOwner)
if !ok {
t.Fatalf("owner use case is not a default tag")
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
func TestNewRequestDefaultConfigResp(t *testing.T) {
response := `
http://jabber.org/protocol/pubsub#node_config
0
1028
never
0
headline
http://www.w3.org/2005/Atom
`
pubsub, err := getPubSubOwnerPayload(response)
if err != nil {
t.Fatalf(err.Error())
}
if pubsub.OwnerUseCase == nil {
t.Fatalf("owner use case is nil")
}
ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.ConfigureOwner)
if !ok {
t.Fatalf("owner use case is not a configure tag")
}
if ownrUsecase.Form == nil {
t.Fatalf("form is nil in the parsed config tag")
}
if len(ownrUsecase.Form.Fields) != 8 {
t.Fatalf("one or more fields in the response form could not be parsed correctly")
}
}
// ***********************
// * 8.4 Delete a Node
// ***********************
func TestNewDelNode(t *testing.T) {
expectedReq := "" +
" " +
" "
subR, err := stanza.NewDelNode("pubsub.shakespeare.lit", "princely_musings")
subR.Id = "delete1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if pubsub.OwnerUseCase == nil {
t.Fatalf("owner use case is nil")
}
_, ok = pubsub.OwnerUseCase.(*stanza.DeleteOwner)
if !ok {
t.Fatalf("owner use case is not a delete tag")
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
func TestNewDelNodeResp(t *testing.T) {
response := `
`
pubsub, err := getPubSubOwnerPayload(response)
if err != nil {
t.Fatalf(err.Error())
}
if pubsub.OwnerUseCase == nil {
t.Fatalf("owner use case is nil")
}
ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.DeleteOwner)
if !ok {
t.Fatalf("owner use case is not a configure tag")
}
if ownrUsecase.RedirectOwner == nil {
t.Fatalf("redirect is nil in the delete tag")
}
if ownrUsecase.RedirectOwner.URI == "" {
t.Fatalf("could not parse redirect uri")
}
}
// ****************************
// * 8.5 Purge All Node Items
// ****************************
func TestNewPurgeAllItems(t *testing.T) {
expectedReq := " " +
" " +
" "
subR, err := stanza.NewPurgeAllItems("pubsub.shakespeare.lit", "princely_musings")
subR.Id = "purge1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if pubsub.OwnerUseCase == nil {
t.Fatalf("owner use case is nil")
}
purge, ok := pubsub.OwnerUseCase.(*stanza.PurgeOwner)
if !ok {
t.Fatalf("owner use case is not a delete tag")
}
if purge.Node == "" {
t.Fatalf("could not parse purge targer node")
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
// ************************************
// * 8.6 Manage Subscription Requests
// ************************************
func TestNewApproveSubRequest(t *testing.T) {
expectedReq := " " +
" " +
"http://jabber.org/protocol/pubsub#subscribe_authorization " +
" 123-abc princely_musings " +
" horatio@denmark.lit " +
"true "
apprForm := &stanza.Form{
Type: stanza.FormTypeSubmit,
Fields: []*stanza.Field{
{Var: "FORM_TYPE", Type: stanza.FieldTypeHidden, ValuesList: []string{"http://jabber.org/protocol/pubsub#subscribe_authorization"}},
{Var: "pubsub#subid", ValuesList: []string{"123-abc"}},
{Var: "pubsub#node", ValuesList: []string{"princely_musings"}},
{Var: "pubsub#subscriber_jid", ValuesList: []string{"horatio@denmark.lit"}},
{Var: "pubsub#allow", ValuesList: []string{"true"}},
},
}
subR, err := stanza.NewApproveSubRequest("pubsub.shakespeare.lit", "approve1", apprForm)
subR.Id = "approve1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
frm, ok := subR.Extensions[0].(*stanza.Form)
if !ok {
t.Fatalf("extension is not a from !")
}
var allowField *stanza.Field
for _, f := range frm.Fields {
if f.Var == "pubsub#allow" {
allowField = f
}
}
if allowField == nil || allowField.ValuesList[0] != "true" {
t.Fatalf("could not correctly parse the allow field in the response from")
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
// ********************************************
// * 8.7 Process Pending Subscription Requests
// ********************************************
func TestNewGetPendingSubRequests(t *testing.T) {
expectedReq := " " +
"" +
" "
subR, err := stanza.NewGetPendingSubRequests("pubsub.shakespeare.lit")
subR.Id = "pending1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
command, ok := subR.Payload.(*stanza.Command)
if !ok {
t.Fatalf("payload is not a command !")
}
if command.Action != stanza.CommandActionExecute {
t.Fatalf("command should be execute !")
}
if command.Node != "http://jabber.org/protocol/pubsub#get-pending" {
t.Fatalf("command node should be http://jabber.org/protocol/pubsub#get-pending !")
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
func TestNewGetPendingSubRequestsResp(t *testing.T) {
response := `
http://jabber.org/protocol/pubsub#subscribe_authorization
`
var respIQ stanza.IQ
err := xml.Unmarshal([]byte(response), &respIQ)
if err != nil {
t.Fatalf("could not parse iq")
}
_, ok := respIQ.Payload.(*stanza.Command)
if !ok {
errors.New("this iq payload is not a command")
}
fMap, err := respIQ.GetFormFields()
if err != nil || len(fMap) != 2 {
errors.New("could not parse command form fields")
}
}
// ********************************************
// * 8.7 Process Pending Subscription Requests
// ********************************************
func TestNewApprovePendingSubRequest(t *testing.T) {
expectedReq := " " +
" " +
" " +
"princely_musings "
subR, err := stanza.NewApprovePendingSubRequest("pubsub.shakespeare.lit",
"pubsub-get-pending:20031021T150901Z-600",
"princely_musings")
subR.Id = "pending2"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
command, ok := subR.Payload.(*stanza.Command)
if !ok {
t.Fatalf("payload is not a command !")
}
if command.Action != stanza.CommandActionExecute {
t.Fatalf("command should be execute !")
}
//if command.Node != "http://jabber.org/protocol/pubsub#get-pending"{
// t.Fatalf("command node should be http://jabber.org/protocol/pubsub#get-pending !")
//}
//
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
// ********************************************
// * 8.8.1 Retrieve Subscriptions List
// ********************************************
func TestNewSubListRqPl(t *testing.T) {
expectedReq := " " +
" " +
" "
subR, err := stanza.NewSubListRqPl("pubsub.shakespeare.lit", "princely_musings")
subR.Id = "subman1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
if !ok {
t.Fatalf("payload is not a pubsub in namespace owner !")
}
subs, ok := pubsub.OwnerUseCase.(*stanza.SubscriptionsOwner)
if !ok {
t.Fatalf("pubsub doesn not contain a subscriptions node !")
}
if subs.Node != "princely_musings" {
t.Fatalf("subs node attribute should be princely_musings. Found %s", subs.Node)
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
func TestNewSubListRqPlResp(t *testing.T) {
response := `
`
var respIQ stanza.IQ
err := xml.Unmarshal([]byte(response), &respIQ)
if err != nil {
t.Fatalf("could not parse iq")
}
pubsub, ok := respIQ.Payload.(*stanza.PubSubOwner)
if !ok {
errors.New("this iq payload is not a command")
}
subs, ok := pubsub.OwnerUseCase.(*stanza.SubscriptionsOwner)
if !ok {
t.Fatalf("pubsub doesn not contain a subscriptions node !")
}
if len(subs.Subscriptions) != 4 {
t.Fatalf("expected to find 4 subscriptions but got %d", len(subs.Subscriptions))
}
}
// ********************************************
// * 8.9.1 Retrieve Affiliations List
// ********************************************
func TestNewAffiliationListRequest(t *testing.T) {
expectedReq := " " +
" " +
" "
subR, err := stanza.NewAffiliationListRequest("pubsub.shakespeare.lit", "princely_musings")
subR.Id = "ent1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
if !ok {
t.Fatalf("payload is not a pubsub in namespace owner !")
}
affils, ok := pubsub.OwnerUseCase.(*stanza.AffiliationsOwner)
if !ok {
t.Fatalf("pubsub doesn not contain an affiliations node !")
}
if affils.Node != "princely_musings" {
t.Fatalf("affils node attribute should be princely_musings. Found %s", affils.Node)
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
func TestNewAffiliationListRequestResp(t *testing.T) {
response := `
`
var respIQ stanza.IQ
err := xml.Unmarshal([]byte(response), &respIQ)
if err != nil {
t.Fatalf("could not parse iq")
}
pubsub, ok := respIQ.Payload.(*stanza.PubSubOwner)
if !ok {
errors.New("this iq payload is not a command")
}
affils, ok := pubsub.OwnerUseCase.(*stanza.AffiliationsOwner)
if !ok {
t.Fatalf("pubsub doesn not contain an affiliations node !")
}
if len(affils.Affiliations) != 2 {
t.Fatalf("expected to find 2 subscriptions but got %d", len(affils.Affiliations))
}
}
// ********************************************
// * 8.9.2 Modify Affiliation
// ********************************************
func TestNewModifAffiliationRequest(t *testing.T) {
expectedReq := " " +
" " +
" " +
" " +
" " +
""
affils := []stanza.AffiliationOwner{
{
AffiliationStatus: stanza.AffiliationStatusNone,
Jid: "hamlet@denmark.lit",
},
{
AffiliationStatus: stanza.AffiliationStatusNone,
Jid: "polonius@denmark.lit",
},
{
AffiliationStatus: stanza.AffiliationStatusPublisher,
Jid: "bard@shakespeare.lit",
},
}
subR, err := stanza.NewModifAffiliationRequest("pubsub.shakespeare.lit", "princely_musings", affils)
subR.Id = "ent3"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
if !ok {
t.Fatalf("payload is not a pubsub in namespace owner !")
}
as, ok := pubsub.OwnerUseCase.(*stanza.AffiliationsOwner)
if !ok {
t.Fatalf("pubsub doesn not contain an affiliations node !")
}
if as.Node != "princely_musings" {
t.Fatalf("affils node attribute should be princely_musings. Found %s", as.Node)
}
if len(as.Affiliations) != 3 {
t.Fatalf("expected 3 affiliations, found %d", len(as.Affiliations))
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
func TestGetFormFields(t *testing.T) {
response := `
http://jabber.org/protocol/pubsub#node_config
0
1028
never
0
headline
http://www.w3.org/2005/Atom
`
var iq stanza.IQ
err := xml.Unmarshal([]byte(response), &iq)
if err != nil {
t.Fatalf("could not parse IQ")
}
fields, err := iq.GetFormFields()
if len(fields) != 8 {
t.Fatalf("could not correctly parse fields. Expected 8, found : %v", len(fields))
}
}
func TestGetFormFieldsCmd(t *testing.T) {
response := `
http://jabber.org/protocol/pubsub#subscribe_authorization
`
var iq stanza.IQ
err := xml.Unmarshal([]byte(response), &iq)
if err != nil {
t.Fatalf("could not parse IQ")
}
fields, err := iq.GetFormFields()
if len(fields) != 2 {
t.Fatalf("could not correctly parse fields. Expected 2, found : %v", len(fields))
}
}
func getPubSubOwnerPayload(response string) (*stanza.PubSubOwner, error) {
var respIQ stanza.IQ
err := xml.Unmarshal([]byte(response), &respIQ)
if err != nil {
return &stanza.PubSubOwner{}, err
}
pubsub, ok := respIQ.Payload.(*stanza.PubSubOwner)
if !ok {
errors.New("this iq payload is not a pubsub of the owner namespace")
}
return pubsub, nil
}