2019-06-02 10:41:19 +02:00
|
|
|
package threema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/o3ma/o3"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-06-28 03:03:38 +02:00
|
|
|
"gosrc.io/xmpp/stanza"
|
2019-06-02 10:41:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccountSend(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
send := make(chan o3.Message)
|
|
|
|
a := Account{
|
2019-09-06 05:30:56 +02:00
|
|
|
ThreemaID: &o3.ThreemaID{ID: o3.NewIDString("43218765")},
|
2019-06-02 10:41:19 +02:00
|
|
|
send: send,
|
|
|
|
deliveredMSG: make(map[uint64]string),
|
|
|
|
readedMSG: make(map[uint64]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2019-06-28 03:03:38 +02:00
|
|
|
a.Send("a", stanza.Message{
|
|
|
|
Attrs: stanza.Attrs{From: "a@example.org"},
|
|
|
|
Body: "ohz8kai0ohNgohth",
|
2019-06-02 10:41:19 +02:00
|
|
|
})
|
|
|
|
}()
|
|
|
|
p := <-send
|
|
|
|
assert.NotNil(p)
|
2019-09-06 05:30:56 +02:00
|
|
|
msg := p.(*o3.TextMessage)
|
2019-08-30 17:28:21 +02:00
|
|
|
assert.Contains(msg.Body, "ohz8kai0ohNgohth")
|
2019-06-02 10:41:19 +02:00
|
|
|
|
|
|
|
// test error
|
2019-06-28 03:03:38 +02:00
|
|
|
err := a.Send("a", stanza.Message{
|
|
|
|
Attrs: stanza.Attrs{From: "a@example.org"},
|
|
|
|
Extensions: []stanza.MsgExtension{
|
|
|
|
&stanza.ReceiptReceived{ID: "blub"},
|
2019-06-02 10:41:19 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccountSendingDeliviery(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
a := Account{
|
2019-09-06 05:30:56 +02:00
|
|
|
ThreemaID: &o3.ThreemaID{ID: o3.NewIDString("43218765")},
|
2019-06-02 10:41:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// test error - threema send only int ids
|
2019-06-28 03:03:38 +02:00
|
|
|
msg, err := a.sending("a", stanza.Message{
|
|
|
|
Attrs: stanza.Attrs{From: "a@example.org"},
|
|
|
|
Extensions: []stanza.MsgExtension{
|
|
|
|
&stanza.ReceiptReceived{ID: "blub"},
|
2019-06-02 10:41:19 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(err)
|
|
|
|
assert.Nil(msg)
|
|
|
|
|
|
|
|
// test delivered
|
2019-06-28 03:03:38 +02:00
|
|
|
msg, err = a.sending("a", stanza.Message{
|
|
|
|
Attrs: stanza.Attrs{From: "a@example.org"},
|
|
|
|
Extensions: []stanza.MsgExtension{
|
|
|
|
&stanza.MarkReceived{ID: "3"},
|
2019-06-02 10:41:19 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(err)
|
2019-09-06 05:30:56 +02:00
|
|
|
drm, ok := msg.(*o3.DeliveryReceiptMessage)
|
2019-06-02 10:41:19 +02:00
|
|
|
assert.True(ok)
|
2019-08-30 17:28:21 +02:00
|
|
|
assert.Equal(o3.MSGDELIVERED, drm.Status)
|
2019-06-02 10:41:19 +02:00
|
|
|
|
|
|
|
// test read
|
2019-06-28 03:03:38 +02:00
|
|
|
msg, err = a.sending("a", stanza.Message{
|
|
|
|
Attrs: stanza.Attrs{From: "a@example.org"},
|
|
|
|
Extensions: []stanza.MsgExtension{
|
|
|
|
&stanza.MarkDisplayed{ID: "5"},
|
2019-06-02 10:41:19 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(err)
|
2019-09-06 05:30:56 +02:00
|
|
|
drm, ok = msg.(*o3.DeliveryReceiptMessage)
|
2019-06-02 10:41:19 +02:00
|
|
|
assert.True(ok)
|
2019-08-30 17:28:21 +02:00
|
|
|
assert.Equal(o3.MSGREAD, drm.Status)
|
2019-06-02 10:41:19 +02:00
|
|
|
}
|
2019-06-06 22:05:33 +02:00
|
|
|
func TestSendTyping(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
a := Account{
|
2019-09-06 05:30:56 +02:00
|
|
|
ThreemaID: &o3.ThreemaID{ID: o3.NewIDString("43218765")},
|
2019-06-06 22:05:33 +02:00
|
|
|
deliveredMSG: make(map[uint64]string),
|
|
|
|
readedMSG: make(map[uint64]string),
|
|
|
|
}
|
|
|
|
|
2019-09-06 05:30:56 +02:00
|
|
|
// skip typing message
|
2019-06-28 03:03:38 +02:00
|
|
|
msg, err := a.sending("a", stanza.Message{
|
|
|
|
Attrs: stanza.Attrs{From: "a@example.org"},
|
|
|
|
Extensions: []stanza.MsgExtension{
|
|
|
|
&stanza.StateComposing{},
|
2019-06-06 22:05:33 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(err)
|
2019-09-06 05:30:56 +02:00
|
|
|
assert.NotNil(msg)
|
2019-06-06 22:05:33 +02:00
|
|
|
|
|
|
|
// skip gone
|
2019-06-28 03:03:38 +02:00
|
|
|
msg, err = a.sending("a", stanza.Message{
|
|
|
|
Attrs: stanza.Attrs{From: "a@example.org"},
|
|
|
|
Extensions: []stanza.MsgExtension{
|
|
|
|
&stanza.StateActive{},
|
|
|
|
&stanza.StateGone{},
|
|
|
|
&stanza.StateInactive{},
|
|
|
|
&stanza.StatePaused{},
|
2019-06-06 22:05:33 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(err)
|
2019-09-06 05:30:56 +02:00
|
|
|
assert.NotNil(msg)
|
2019-06-06 22:05:33 +02:00
|
|
|
|
|
|
|
// skip gone
|
2019-06-28 03:03:38 +02:00
|
|
|
msg, err = a.sending("a", stanza.Message{
|
|
|
|
Attrs: stanza.Attrs{From: "a@example.org"},
|
|
|
|
Extensions: []stanza.MsgExtension{
|
|
|
|
&stanza.StateActive{},
|
|
|
|
&stanza.StateComposing{},
|
|
|
|
&stanza.StateGone{},
|
|
|
|
&stanza.StateInactive{},
|
|
|
|
&stanza.StatePaused{},
|
2019-06-06 22:05:33 +02:00
|
|
|
},
|
|
|
|
Body: "hi",
|
|
|
|
})
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(msg)
|
2019-09-06 05:30:56 +02:00
|
|
|
o3msg := msg.(*o3.TextMessage)
|
2019-08-30 17:28:21 +02:00
|
|
|
assert.Contains(o3msg.Body, "hi")
|
2019-06-06 22:05:33 +02:00
|
|
|
}
|