[TEST] refactory component threema sending

This commit is contained in:
Martin/Geno 2019-06-02 10:41:19 +02:00
parent ee53a32ace
commit 211e0abed0
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
10 changed files with 142 additions and 49 deletions

View File

@ -12,7 +12,7 @@ func (c *Config) receiver() {
log.WithField("type", c.Type).Panicf("connection closed%s", err)
return
}
p, back := c.receive(packet)
p, back := c.receiving(packet)
if p == nil {
continue
}
@ -24,7 +24,7 @@ func (c *Config) receiver() {
}
}
func (c *Config) receive(packet xmpp.Packet) (xmpp.Packet, bool) {
func (c *Config) receiving(packet xmpp.Packet) (xmpp.Packet, bool) {
logger := log.WithField("type", c.Type)
switch p := packet.(type) {

View File

@ -13,20 +13,20 @@ func TestReceive(t *testing.T) {
c := Config{Host: "example.org", Type: "monkeyservice"}
// ignoring packet
p, back := c.receive(xmpp.Handshake{})
p, _ := c.receiving(xmpp.Handshake{})
assert.Nil(p)
// receive presence
p, back = c.receive(xmpp.Presence{})
p, _ = c.receiving(xmpp.Presence{})
assert.Nil(p)
// message
p, back = c.receive(xmpp.Message{})
p, back := c.receiving(xmpp.Message{})
assert.False(back)
assert.NotNil(p)
// unsupported iq
p, back = c.receive(xmpp.IQ{Payload: []xmpp.IQPayload{
p, back = c.receiving(xmpp.IQ{Payload: []xmpp.IQPayload{
&xmpp.Err{},
}})
assert.True(back)
@ -36,7 +36,7 @@ func TestReceive(t *testing.T) {
assert.Equal("feature-not-implemented", iq.Error.Reason)
// iq disco info
p, back = c.receive(xmpp.IQ{
p, back = c.receiving(xmpp.IQ{
PacketAttrs: xmpp.PacketAttrs{Type: "get"},
Payload: []xmpp.IQPayload{
&xmpp.DiscoInfo{},
@ -50,7 +50,7 @@ func TestReceive(t *testing.T) {
assert.Equal("monkeyservice", dinfo.Identity.Name)
// iq disco items
p, back = c.receive(xmpp.IQ{
p, back = c.receiving(xmpp.IQ{
PacketAttrs: xmpp.PacketAttrs{Type: "get"},
Payload: []xmpp.IQPayload{
&xmpp.DiscoItems{},

View File

@ -7,13 +7,13 @@ import (
func (c *Config) sender(packets chan xmpp.Packet) {
for packet := range packets {
if p := c.send(packet); p != nil {
if p := c.sending(packet); p != nil {
c.xmpp.Send(p)
}
}
}
func (c *Config) send(packet xmpp.Packet) xmpp.Packet {
func (c *Config) sending(packet xmpp.Packet) xmpp.Packet {
logger := log.WithField("type", c.Type)
switch p := packet.(type) {
case xmpp.Message:

View File

@ -13,17 +13,17 @@ func TestSend(t *testing.T) {
c := Config{Host: "example.org"}
// ignoring packet
p := c.send(xmpp.IQ{})
p := c.sending(xmpp.IQ{})
assert.Nil(p)
// send by component host
p = c.send(xmpp.Message{})
p = c.sending(xmpp.Message{})
assert.NotNil(p)
msg := p.(xmpp.Message)
assert.Equal("example.org", msg.PacketAttrs.From)
// send by a user of component
p = c.send(xmpp.Message{PacketAttrs: xmpp.PacketAttrs{From: "threemaid"}})
p = c.sending(xmpp.Message{PacketAttrs: xmpp.PacketAttrs{From: "threemaid"}})
assert.NotNil(p)
msg = p.(xmpp.Message)
assert.Equal("threemaid@example.org", msg.PacketAttrs.From)

View File

@ -14,7 +14,7 @@ type Account struct {
models.AccountThreema
Session o3.SessionContext
send chan<- o3.Message
recieve <-chan o3.ReceivedMsg
receive <-chan o3.ReceivedMsg
deliveredMSG map[uint64]string
readedMSG map[uint64]string
}
@ -45,7 +45,7 @@ func (t *Threema) getAccount(jid *models.JID) (*Account, error) {
a := &Account{AccountThreema: account}
a.Session = o3.NewSessionContext(tid)
a.send, a.recieve, err = a.Session.Run()
a.send, a.receive, err = a.Session.Run()
if err != nil {
return nil, err
@ -55,7 +55,7 @@ func (t *Threema) getAccount(jid *models.JID) (*Account, error) {
a.deliveredMSG = make(map[uint64]string)
a.readedMSG = make(map[uint64]string)
go a.reciever(t.out)
go a.receiver(t.out)
t.accountJID[jid.String()] = a
return a, nil

View File

@ -55,7 +55,7 @@ func (t *Threema) send(packet xmpp.Packet) xmpp.Packet {
if to.IsDomain() {
if from == nil {
log.Warn("recieve message without sender")
log.Warn("receive message without sender")
return nil
}
msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
@ -66,7 +66,7 @@ func (t *Threema) send(packet xmpp.Packet) xmpp.Packet {
account, err := t.getAccount(from)
if err != nil {
msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
msg.Body = "It was not possible to send, becouse we have no account for you.\nPlease generate one, by sending `generate` to this gateway"
msg.Body = "It was not possible to send, because we have no account for you.\nPlease generate one, by sending `generate` to this gateway"
return msg
}
@ -77,7 +77,7 @@ func (t *Threema) send(packet xmpp.Packet) xmpp.Packet {
return msg
}
default:
log.Warnf("unkown package: %v", p)
log.Warnf("unknown package: %v", p)
}
return nil
}

View File

@ -8,14 +8,14 @@ import (
"gosrc.io/xmpp"
)
func (a *Account) reciever(out chan<- xmpp.Packet) {
for receivedMessage := range a.recieve {
if p := a.handle(receivedMessage); p != nil {
func (a *Account) receiver(out chan<- xmpp.Packet) {
for receivedMessage := range a.receive {
if p := a.receiving(receivedMessage); p != nil {
out <- p
}
}
}
func (a *Account) handle(receivedMessage o3.ReceivedMsg) xmpp.Packet {
func (a *Account) receiving(receivedMessage o3.ReceivedMsg) xmpp.Packet {
if receivedMessage.Err != nil {
log.Warnf("Error Receiving Message: %s\n", receivedMessage.Err)
return nil

View File

@ -28,28 +28,28 @@ func createDummyAccount() Account {
return a
}
func TestRecieve(t *testing.T) {
func TestReceive(t *testing.T) {
assert := assert.New(t)
a := createDummyAccount()
// handle/skip error
p := a.handle(o3.ReceivedMsg{
// receiving/skip error
p := a.receiving(o3.ReceivedMsg{
Err: errors.New("dummy"),
})
assert.Nil(p)
// nothing to handle
p = a.handle(o3.ReceivedMsg{})
// nothing to receiving
p = a.receiving(o3.ReceivedMsg{})
assert.Nil(p)
}
func TestRecieveText(t *testing.T) {
func TestReceiveText(t *testing.T) {
assert := assert.New(t)
a := createDummyAccount()
// handle text
// receiving text
session := o3.SessionContext{
ID: o3.ThreemaID{
ID: o3.NewIDString("12345678"),
@ -58,14 +58,14 @@ func TestRecieveText(t *testing.T) {
}
txtMsg, err := o3.NewTextMessage(&session, threemaID, "Oojoh0Ah")
assert.NoError(err)
p := a.handle(o3.ReceivedMsg{
p := a.receiving(o3.ReceivedMsg{
Msg: txtMsg,
})
xMSG, ok := p.(xmpp.Message)
assert.True(ok)
assert.Equal("Oojoh0Ah", xMSG.Body)
// handle/skip text to own id
// receiving/skip text to own id
session = o3.SessionContext{
ID: o3.ThreemaID{
ID: threemaIDByte,
@ -74,18 +74,18 @@ func TestRecieveText(t *testing.T) {
}
txtMsg, err = o3.NewTextMessage(&session, threemaID, "Aesh8shu")
assert.NoError(err)
p = a.handle(o3.ReceivedMsg{
p = a.receiving(o3.ReceivedMsg{
Msg: txtMsg,
})
assert.Nil(p)
}
func TestRecieveDeliveryReceipt(t *testing.T) {
func TestReceiveDeliveryReceipt(t *testing.T) {
assert := assert.New(t)
a := createDummyAccount()
// handle delivered
// receiving delivered
session := o3.SessionContext{
ID: o3.ThreemaID{
ID: o3.NewIDString("12345678"),
@ -98,7 +98,7 @@ func TestRecieveDeliveryReceipt(t *testing.T) {
drm, err := o3.NewDeliveryReceiptMessage(&session, threemaID, msgID, o3.MSGDELIVERED)
assert.NoError(err)
p := a.handle(o3.ReceivedMsg{
p := a.receiving(o3.ReceivedMsg{
Msg: drm,
})
xMSG, ok := p.(xmpp.Message)
@ -106,16 +106,16 @@ func TestRecieveDeliveryReceipt(t *testing.T) {
rr := xMSG.Extensions[0].(xmpp.ReceiptReceived)
assert.Equal("im4aeseeh1IbaQui", rr.Id)
// handle delivered -> not in cache
p = a.handle(o3.ReceivedMsg{
// receiving delivered -> not in cache
p = a.receiving(o3.ReceivedMsg{
Msg: drm,
})
assert.Nil(p)
// handle readed
// receiving readed
drm, err = o3.NewDeliveryReceiptMessage(&session, threemaID, msgID, o3.MSGREAD)
assert.NoError(err)
p = a.handle(o3.ReceivedMsg{
p = a.receiving(o3.ReceivedMsg{
Msg: drm,
})
xMSG, ok = p.(xmpp.Message)
@ -123,8 +123,8 @@ func TestRecieveDeliveryReceipt(t *testing.T) {
cmdd := xMSG.Extensions[0].(xmpp.ChatMarkerDisplayed)
assert.Equal("im4aeseeh1IbaQui", cmdd.Id)
// handle delivered -> not in cache
p = a.handle(o3.ReceivedMsg{
// receiving delivered -> not in cache
p = a.receiving(o3.ReceivedMsg{
Msg: drm,
})
assert.Nil(p)

View File

@ -9,6 +9,17 @@ import (
)
func (a *Account) Send(to string, msg xmpp.Message) error {
m, err := a.sending(to, msg)
if err != nil {
return err
}
if m != nil {
a.send <- m
}
return nil
}
func (a *Account) sending(to string, msg xmpp.Message) (o3.Message, error) {
// handle delivered / readed
msgID := ""
readed := false
for _, el := range msg.Extensions {
@ -25,7 +36,7 @@ func (a *Account) Send(to string, msg xmpp.Message) error {
if msgID != "" {
id, err := strconv.ParseUint(msgID, 10, 64)
if err != nil {
return err
return nil, err
}
msgType := o3.MSGDELIVERED
if readed {
@ -33,23 +44,22 @@ func (a *Account) Send(to string, msg xmpp.Message) error {
}
drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType)
if err != nil {
return err
return nil, err
}
a.send <- drm
log.WithFields(map[string]interface{}{
"tid": to,
"msg_id": id,
"type": msgType,
}).Debug("update status of threema message")
return nil
return drm, nil
}
// send text message
msg3, err := o3.NewTextMessage(&a.Session, to, msg.Body)
if err != nil {
return err
return nil, err
}
a.deliveredMSG[msg3.ID()] = msg.Id
a.readedMSG[msg3.ID()] = msg.Id
a.send <- msg3
return nil
return msg3, nil
}

View File

@ -0,0 +1,83 @@
package threema
import (
"testing"
"github.com/o3ma/o3"
"github.com/stretchr/testify/assert"
"gosrc.io/xmpp"
)
func TestAccountSend(t *testing.T) {
assert := assert.New(t)
send := make(chan o3.Message)
a := Account{
Session: o3.NewSessionContext(o3.ThreemaID{ID: o3.NewIDString("43218765")}),
send: send,
deliveredMSG: make(map[uint64]string),
readedMSG: make(map[uint64]string),
}
go func() {
a.Send("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Body: "ohz8kai0ohNgohth",
})
}()
p := <-send
assert.NotNil(p)
msg := p.(o3.TextMessage)
assert.Contains(msg.Text(), "ohz8kai0ohNgohth")
// test error
err := a.Send("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{
&xmpp.ReceiptReceived{Id: "blub"},
},
})
assert.Error(err)
}
func TestAccountSendingDeliviery(t *testing.T) {
assert := assert.New(t)
a := Account{
Session: o3.NewSessionContext(o3.ThreemaID{ID: o3.NewIDString("43218765")}),
}
// test error - threema send only int ids
msg, err := a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{
&xmpp.ReceiptReceived{Id: "blub"},
},
})
assert.Error(err)
assert.Nil(msg)
// test delivered
msg, err = a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{
&xmpp.ChatMarkerReceived{Id: "3"},
},
})
assert.NoError(err)
drm, ok := msg.(o3.DeliveryReceiptMessage)
assert.True(ok)
assert.Equal(o3.MSGDELIVERED, drm.Status())
// test read
msg, err = a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{
&xmpp.ChatMarkerDisplayed{Id: "5"},
},
})
assert.NoError(err)
drm, ok = msg.(o3.DeliveryReceiptMessage)
assert.True(ok)
assert.Equal(o3.MSGREAD, drm.Status())
}