This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
thrempp/component/threema/receive_test.go

193 lines
3.8 KiB
Go
Raw Normal View History

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-08-30 17:28:21 +02:00
const threemaFromID = "87654321"
2019-08-30 17:28:21 +02:00
var threemaFromIDByte o3.IDString
func init() {
2019-08-30 17:28:21 +02:00
threemaFromIDByte = o3.NewIDString(threemaFromID)
}
func createDummyAccount() Account {
a := Account{
deliveredMSG: make(map[uint64]string),
readedMSG: make(map[uint64]string),
}
2019-08-30 17:28:21 +02:00
a.TID = make([]byte, len(threemaFromIDByte))
copy(a.TID, threemaFromIDByte[:])
return a
}
/*
func TestReceive(t *testing.T) {
assert := assert.New(t)
a := createDummyAccount()
// nothing to receiving
p, err := a.receiving(nil)
assert.Nil(p)
assert.Error(err)
}
*/
func TestReceiveText(t *testing.T) {
assert := assert.New(t)
a := createDummyAccount()
// receiving text
2019-08-30 17:28:21 +02:00
txtMsg := o3.TextMessage{
MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
Recipient: o3.NewIDString("12345678"),
},
2019-08-30 17:28:21 +02:00
Body: "Oojoh0Ah",
}
p, err := a.receiving(txtMsg)
assert.NoError(err)
2019-08-30 17:28:21 +02:00
assert.NotNil(p)
2019-06-28 03:03:38 +02:00
xMSG, ok := p.(stanza.Message)
assert.True(ok)
assert.Equal("Oojoh0Ah", xMSG.Body)
}
2019-08-30 17:28:21 +02:00
/*
func TestReceiveAudio(t *testing.T) {
assert := assert.New(t)
a := createDummyAccount()
a.threema = &Threema{}
2019-08-30 17:28:21 +02:00
dataMsg := o3.ImageMessage{
MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
},
2019-08-30 17:28:21 +02:00
}
_, err := a.receiving(dataMsg)
assert.Error(err)
a.threema.httpUploadPath = "/tmp"
dataMsg = o3.AudioMessage{}
_, err = a.receiving(dataMsg)
assert.Error(err)
}
func TestReceiveImage(t *testing.T) {
assert := assert.New(t)
a := createDummyAccount()
a.threema = &Threema{}
2019-08-30 17:28:21 +02:00
// receiving image
dataMsg := o3.ImageMessage{
MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
},
2019-08-30 17:28:21 +02:00
}
_, err := a.receiving(dataMsg)
assert.Error(err)
a.threema.httpUploadPath = "/tmp"
2019-08-30 17:28:21 +02:00
dataMsg := o3.ImageMessage{
MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
},
}
dataMsg = o3.ImageMessage{}
_, err = a.receiving(dataMsg)
assert.Error(err)
}
2019-08-30 17:28:21 +02:00
*/
func TestReceiveDeliveryReceipt(t *testing.T) {
assert := assert.New(t)
a := createDummyAccount()
// receiving delivered
msgID := o3.NewMsgID()
a.deliveredMSG[msgID] = "im4aeseeh1IbaQui"
a.readedMSG[msgID] = "im4aeseeh1IbaQui"
2019-08-30 17:28:21 +02:00
drm := o3.DeliveryReceiptMessage{
MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
},
Status: o3.MSGDELIVERED,
MessageID: msgID,
}
p, err := a.receiving(drm)
assert.NoError(err)
2019-08-30 17:28:21 +02:00
assert.NotNil(p)
2019-06-28 03:03:38 +02:00
xMSG, ok := p.(stanza.Message)
assert.True(ok)
2019-06-28 03:03:38 +02:00
rr := xMSG.Extensions[0].(stanza.ReceiptReceived)
2019-06-05 04:30:00 +02:00
assert.Equal("im4aeseeh1IbaQui", rr.ID)
// receiving delivered -> not in cache
p, err = a.receiving(drm)
assert.NoError(err)
assert.Nil(p)
// receiving readed
2019-08-30 17:28:21 +02:00
drm = o3.DeliveryReceiptMessage{
MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
},
MessageID: msgID,
Status: o3.MSGREAD,
}
p, err = a.receiving(drm)
assert.NoError(err)
2019-08-30 17:28:21 +02:00
assert.NotNil(p)
2019-06-28 03:03:38 +02:00
xMSG, ok = p.(stanza.Message)
assert.True(ok)
2019-06-28 03:03:38 +02:00
cmdd := xMSG.Extensions[0].(stanza.MarkDisplayed)
2019-06-05 04:30:00 +02:00
assert.Equal("im4aeseeh1IbaQui", cmdd.ID)
// receiving delivered -> not in cache
p, err = a.receiving(drm)
assert.NoError(err)
assert.Nil(p)
}
func TestReceiveTyping(t *testing.T) {
assert := assert.New(t)
a := createDummyAccount()
// receiving inactive
2019-08-30 17:28:21 +02:00
tnm := o3.TypingNotificationMessage{
MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
},
}
p, err := a.receiving(tnm)
2019-08-30 17:28:21 +02:00
assert.NotNil(p)
assert.NoError(err)
2019-06-28 03:03:38 +02:00
xMSG, ok := p.(stanza.Message)
assert.True(ok)
2019-06-28 03:03:38 +02:00
assert.IsType(stanza.StateInactive{}, xMSG.Extensions[0])
// receiving composing
2019-08-30 17:28:21 +02:00
tnm = o3.TypingNotificationMessage{
MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
},
OnOff: 0x1,
}
p, err = a.receiving(tnm)
2019-08-30 17:28:21 +02:00
assert.NotNil(p)
assert.NoError(err)
2019-06-28 03:03:38 +02:00
xMSG, ok = p.(stanza.Message)
assert.True(ok)
2019-06-28 03:03:38 +02:00
assert.IsType(stanza.StateComposing{}, xMSG.Extensions[0])
}