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/main_test.go

148 lines
2.8 KiB
Go
Raw Normal View History

2019-05-31 10:07:40 +02:00
package threema
2019-06-01 15:21:49 +02:00
import (
"testing"
"github.com/stretchr/testify/assert"
"gosrc.io/xmpp"
"dev.sum7.eu/genofire/golang-lib/database"
"dev.sum7.eu/genofire/thrempp/models"
)
func TestThreema(t *testing.T) {
assert := assert.New(t)
// failed
c, err := NewThreema(map[string]interface{}{
"http_upload_path": 3,
})
assert.Error(err)
assert.Nil(c)
// failed
c, err = NewThreema(map[string]interface{}{
"http_upload_url": 3,
})
assert.Error(err)
assert.Nil(c)
// ---
c, err = NewThreema(map[string]interface{}{
"http_upload_url": "",
"http_upload_path": "",
})
2019-06-01 15:21:49 +02:00
assert.NoError(err)
assert.NotNil(c)
database.Open(database.Config{
Type: "sqlite3",
Connection: "file::memory:?mode=memory",
})
defer database.Close()
jid := models.JID{
Local: "a",
Domain: "example.org",
}
database.Write.Create(&jid)
database.Write.Create(&models.AccountThreema{
TID: []byte("12345678"),
LSK: []byte("b"),
XMPPID: jid.ID,
})
//broken
jid = models.JID{
Local: "b",
Domain: "example.org",
}
database.Write.Create(&jid)
database.Write.Create(&models.AccountThreema{
TID: []byte("123"),
LSK: []byte("b"),
XMPPID: jid.ID,
})
ch, err := c.Connect()
assert.NoError(err)
assert.NotNil(ch)
}
func TestSend(t *testing.T) {
assert := assert.New(t)
// test channel
out := make(chan xmpp.Packet)
tr := Threema{
out: out,
accountJID: make(map[string]*Account),
bot: make(map[string]*Bot),
2019-06-01 15:21:49 +02:00
}
go func() {
tr.Send(xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
})
}()
p := <-out
assert.NotNil(p)
// no account. generate one
msg := p.(xmpp.Message)
assert.Contains(msg.Body, "generate")
// test no answer
p = tr.send(xmpp.IQ{})
assert.Nil(p)
// chat with bot without sender
p = tr.send(xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{
To: "example.org",
},
})
assert.Nil(p)
2019-06-01 15:21:49 +02:00
// chat with bot
p = tr.send(xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{
From: "a@example.com",
To: "example.org",
},
2019-06-01 15:21:49 +02:00
})
assert.NotNil(p)
msg = p.(xmpp.Message)
assert.Contains(msg.Body, "command not found")
2019-06-01 15:21:49 +02:00
// chat with delivier error
database.Open(database.Config{
Type: "sqlite3",
Connection: "file::memory:?mode=memory",
})
defer database.Close()
jid := models.JID{
Local: "a",
Domain: "example.org",
}
database.Write.Create(&jid)
database.Write.Create(&models.AccountThreema{
TID: []byte("12345678"),
LSK: []byte("b"),
XMPPID: jid.ID,
})
/* TODO manipulate account to no sendpipe
_, err := tr.getAccount(&jid)
assert.NoError(err)
p = tr.send(xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{
From: "a@example.org",
To: "12345678@threema.example.org",
},
})
assert.NotNil(p)
msg = p.(xmpp.Message)
assert.Equal("command not supported", msg.Body)
*/
}