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.7 KiB
Go
Raw Permalink 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"
2019-06-28 03:03:38 +02:00
"gosrc.io/xmpp/stanza"
2019-06-01 15:21:49 +02:00
"dev.sum7.eu/genofire/golang-lib/database"
2019-08-08 15:48:43 +02:00
"dev.sum7.eu/sum7/thrempp/models"
2019-06-01 15:21:49 +02:00
)
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
2019-06-28 03:03:38 +02:00
out := make(chan stanza.Packet)
2019-06-01 15:21:49 +02:00
tr := Threema{
out: out,
accountJID: make(map[string]*Account),
bot: make(map[string]*Bot),
2019-06-01 15:21:49 +02:00
}
go func() {
2019-06-28 03:03:38 +02:00
tr.Send(stanza.Message{
Attrs: stanza.Attrs{From: "a@example.org"},
2019-06-01 15:21:49 +02:00
})
}()
p := <-out
assert.NotNil(p)
// no account. generate one
2019-06-28 03:03:38 +02:00
msg := p.(stanza.Message)
2019-06-01 15:21:49 +02:00
assert.Contains(msg.Body, "generate")
// test no answer
2019-06-28 03:03:38 +02:00
p = tr.send(stanza.IQ{})
2019-06-01 15:21:49 +02:00
assert.Nil(p)
// chat with bot without sender
2019-06-28 03:03:38 +02:00
p = tr.send(stanza.Message{
Attrs: stanza.Attrs{
To: "example.org",
},
})
assert.Nil(p)
2019-06-01 15:21:49 +02:00
// chat with bot
2019-06-28 03:03:38 +02:00
p = tr.send(stanza.Message{
Attrs: stanza.Attrs{
From: "a@example.com",
To: "example.org",
},
2019-06-01 15:21:49 +02:00
})
assert.NotNil(p)
2019-06-28 03:03:38 +02:00
msg = p.(stanza.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)
2019-06-28 03:03:38 +02:00
p = tr.send(stanza.Message{
Attrs: stanza.Attrs{
2019-06-01 15:21:49 +02:00
From: "a@example.org",
To: "12345678@threema.example.org",
},
})
assert.NotNil(p)
2019-06-28 03:03:38 +02:00
msg = p.(stanza.Message)
2019-06-01 15:21:49 +02:00
assert.Equal("command not supported", msg.Body)
*/
}