[TEST] review threema main component
This commit is contained in:
parent
32a646012c
commit
bd87c4ac86
|
@ -21,9 +21,9 @@ type Account struct {
|
||||||
readedMSG map[uint64]string
|
readedMSG map[uint64]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Threema) getAccount(jid *models.JID) *Account {
|
func (t *Threema) getAccount(jid *models.JID) (*Account, error) {
|
||||||
if a, ok := t.accountJID[jid.String()]; ok {
|
if a, ok := t.accountJID[jid.String()]; ok {
|
||||||
return a
|
return a, nil
|
||||||
}
|
}
|
||||||
account := models.AccountThreema{}
|
account := models.AccountThreema{}
|
||||||
|
|
||||||
|
@ -36,28 +36,27 @@ func (t *Threema) getAccount(jid *models.JID) *Account {
|
||||||
var lsk [32]byte
|
var lsk [32]byte
|
||||||
copy(lsk[:], account.LSK[:])
|
copy(lsk[:], account.LSK[:])
|
||||||
tid, err := o3.NewThreemaID(string(account.TID), lsk, o3.AddressBook{})
|
tid, err := o3.NewThreemaID(string(account.TID), lsk, o3.AddressBook{})
|
||||||
// TODO error handling
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil, err
|
||||||
}
|
}
|
||||||
tid.Nick = o3.NewPubNick("xmpp:" + jid.String())
|
tid.Nick = o3.NewPubNick("xmpp:" + jid.String())
|
||||||
|
|
||||||
a := &Account{AccountThreema: account}
|
a := &Account{AccountThreema: account}
|
||||||
a.XMPP = *jid
|
|
||||||
a.Session = o3.NewSessionContext(tid)
|
a.Session = o3.NewSessionContext(tid)
|
||||||
a.send, a.recieve, err = a.Session.Run()
|
a.send, a.recieve, err = a.Session.Run()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
a.XMPP = *jid
|
||||||
a.deliveredMSG = make(map[uint64]string)
|
a.deliveredMSG = make(map[uint64]string)
|
||||||
a.readedMSG = make(map[uint64]string)
|
a.readedMSG = make(map[uint64]string)
|
||||||
|
|
||||||
// TODO error handling
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
go a.reciever(t.out)
|
go a.reciever(t.out)
|
||||||
|
|
||||||
t.accountJID[jid.String()] = a
|
t.accountJID[jid.String()] = a
|
||||||
return a
|
return a, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Account) reciever(out chan<- xmpp.Packet) {
|
func (a *Account) reciever(out chan<- xmpp.Packet) {
|
||||||
|
|
|
@ -22,8 +22,8 @@ func (t *Threema) Bot(from *models.JID, request string) string {
|
||||||
case "generate":
|
case "generate":
|
||||||
|
|
||||||
// test if account already exists
|
// test if account already exists
|
||||||
account := t.getAccount(from)
|
account, err := t.getAccount(from)
|
||||||
if account != nil {
|
if err == nil {
|
||||||
return fmt.Sprintf("you already has the threema account with id: %s", string(account.TID))
|
return fmt.Sprintf("you already has the threema account with id: %s", string(account.TID))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ func (t *Threema) Bot(from *models.JID, request string) string {
|
||||||
database.Write.Create(&a)
|
database.Write.Create(&a)
|
||||||
|
|
||||||
// fetch account and connect
|
// fetch account and connect
|
||||||
account = t.getAccount(from)
|
account, err = t.getAccount(from)
|
||||||
tid := string(account.TID)
|
tid := string(account.TID)
|
||||||
if tid != "" {
|
if tid != "" {
|
||||||
logger.WithField("threema", tid).Info("generate")
|
logger.WithField("threema", tid).Info("generate")
|
||||||
|
|
|
@ -29,15 +29,23 @@ func (t *Threema) Connect() (chan xmpp.Packet, error) {
|
||||||
var jids []*models.JID
|
var jids []*models.JID
|
||||||
database.Read.Find(&jids)
|
database.Read.Find(&jids)
|
||||||
for _, jid := range jids {
|
for _, jid := range jids {
|
||||||
a := t.getAccount(jid)
|
logger := log.WithField("jid", jid.String())
|
||||||
log.WithFields(map[string]interface{}{
|
a, err := t.getAccount(jid)
|
||||||
"jid": jid.String(),
|
if err != nil {
|
||||||
"threema": string(a.TID),
|
logger.Warnf("unable to connect%s", err)
|
||||||
}).Info("connected")
|
continue
|
||||||
|
}
|
||||||
|
logger = logger.WithField("threema", string(a.TID))
|
||||||
|
logger.Info("connected")
|
||||||
}
|
}
|
||||||
return t.out, nil
|
return t.out, nil
|
||||||
}
|
}
|
||||||
func (t *Threema) Send(packet xmpp.Packet) {
|
func (t *Threema) Send(packet xmpp.Packet) {
|
||||||
|
if p := t.send(packet); p != nil {
|
||||||
|
t.out <- p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (t *Threema) send(packet xmpp.Packet) xmpp.Packet {
|
||||||
switch p := packet.(type) {
|
switch p := packet.(type) {
|
||||||
case xmpp.Message:
|
case xmpp.Message:
|
||||||
from := models.ParseJID(p.PacketAttrs.From)
|
from := models.ParseJID(p.PacketAttrs.From)
|
||||||
|
@ -46,27 +54,26 @@ func (t *Threema) Send(packet xmpp.Packet) {
|
||||||
if to.IsDomain() {
|
if to.IsDomain() {
|
||||||
msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
|
msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
|
||||||
msg.Body = t.Bot(from, p.Body)
|
msg.Body = t.Bot(from, p.Body)
|
||||||
t.out <- msg
|
return msg
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
account := t.getAccount(from)
|
account, err := t.getAccount(from)
|
||||||
if account == nil {
|
if err != nil {
|
||||||
msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
|
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, becouse we have no account for you.\nPlease generate one, by sending `generate` to this gateway"
|
||||||
t.out <- msg
|
return msg
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
threemaID := strings.ToUpper(to.Local)
|
threemaID := strings.ToUpper(to.Local)
|
||||||
if err := account.Send(threemaID, p); err != nil {
|
if err := account.Send(threemaID, p); err != nil {
|
||||||
msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
|
msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
|
||||||
msg.Body = err.Error()
|
msg.Body = err.Error()
|
||||||
t.out <- msg
|
return msg
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
log.Warnf("unkown package%v", p)
|
log.Warnf("unkown package%v", p)
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -1 +1,117 @@
|
||||||
package threema
|
package threema
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
c, err := NewThreema(map[string]interface{}{})
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
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
|
||||||
|
p = tr.send(xmpp.Message{
|
||||||
|
PacketAttrs: xmpp.PacketAttrs{To: "example.org"},
|
||||||
|
})
|
||||||
|
assert.NotNil(p)
|
||||||
|
msg = p.(xmpp.Message)
|
||||||
|
assert.Equal("command not supported", msg.Body)
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
Reference in New Issue