From 211e0abed0ff0c047f31598ce92efe92aea65416 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Sun, 2 Jun 2019 10:41:19 +0200 Subject: [PATCH] [TEST] refactory component threema sending --- component/receiver.go | 4 +- component/receiver_test.go | 12 +-- component/send.go | 4 +- component/send_test.go | 6 +- component/threema/account.go | 6 +- component/threema/main.go | 6 +- component/threema/{recieve.go => receive.go} | 8 +- .../{recieve_test.go => receive_test.go} | 38 ++++----- component/threema/send.go | 24 ++++-- component/threema/send_test.go | 83 +++++++++++++++++++ 10 files changed, 142 insertions(+), 49 deletions(-) rename component/threema/{recieve.go => receive.go} (87%) rename component/threema/{recieve_test.go => receive_test.go} (77%) create mode 100644 component/threema/send_test.go diff --git a/component/receiver.go b/component/receiver.go index 3456bf4..f567108 100644 --- a/component/receiver.go +++ b/component/receiver.go @@ -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) { diff --git a/component/receiver_test.go b/component/receiver_test.go index 3fcd236..5c83681 100644 --- a/component/receiver_test.go +++ b/component/receiver_test.go @@ -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{}, diff --git a/component/send.go b/component/send.go index f3108d9..21a5179 100644 --- a/component/send.go +++ b/component/send.go @@ -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: diff --git a/component/send_test.go b/component/send_test.go index 585e73e..7abbd6a 100644 --- a/component/send_test.go +++ b/component/send_test.go @@ -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) diff --git a/component/threema/account.go b/component/threema/account.go index d749a68..a3ecfdf 100644 --- a/component/threema/account.go +++ b/component/threema/account.go @@ -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 diff --git a/component/threema/main.go b/component/threema/main.go index 5a549ce..3d72762 100644 --- a/component/threema/main.go +++ b/component/threema/main.go @@ -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 } diff --git a/component/threema/recieve.go b/component/threema/receive.go similarity index 87% rename from component/threema/recieve.go rename to component/threema/receive.go index d2fc136..59492be 100644 --- a/component/threema/recieve.go +++ b/component/threema/receive.go @@ -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 diff --git a/component/threema/recieve_test.go b/component/threema/receive_test.go similarity index 77% rename from component/threema/recieve_test.go rename to component/threema/receive_test.go index 16a9566..743e5e2 100644 --- a/component/threema/recieve_test.go +++ b/component/threema/receive_test.go @@ -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) diff --git a/component/threema/send.go b/component/threema/send.go index f6b66aa..0c95c74 100644 --- a/component/threema/send.go +++ b/component/threema/send.go @@ -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 } diff --git a/component/threema/send_test.go b/component/threema/send_test.go new file mode 100644 index 0000000..7e09ca1 --- /dev/null +++ b/component/threema/send_test.go @@ -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()) +}