[TEST] add some tests

This commit is contained in:
Martin/Geno 2019-06-01 02:59:00 +02:00
parent 291b2106ac
commit 709ce45e93
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
9 changed files with 181 additions and 62 deletions

View File

@ -1 +0,0 @@
package cmd

View File

@ -50,54 +50,6 @@ var serveCmd = &cobra.Command{
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs
log.Infof("received %s", sig)
/*
server := o3.ThreemaRest{}
var thrAccount models.AccountThreema
if err := database.Read.First(&thrAccount).Error; err != nil {
id, _ := server.CreateIdentity()
thrAccount.TID = make([]byte, len(id.ID))
thrAccount.LSK = make([]byte, len(id.LSK))
copy(thrAccount.TID, id.ID[:])
copy(thrAccount.LSK, id.LSK[:])
database.Write.Create(&thrAccount)
}
log.Warnf("%s", thrAccount.TID)
var lsk [32]byte
copy(lsk[:], thrAccount.LSK[:])
tid, err := o3.NewThreemaID(string(thrAccount.TID), lsk, o3.AddressBook{})
tid.Nick = o3.NewPubNick("xmpp:geno@fireorbit.de")
ctx := o3.NewSessionContext(tid)
// let the session begin
log.Info("Starting session")
sendMsgChan, receiveMsgChan, err := ctx.Run()
if err != nil {
log.Fatal(err)
}
// handle incoming messages
for receivedMessage := range receiveMsgChan {
if receivedMessage.Err != nil {
log.Errorf("Error Receiving Message: %s\n", receivedMessage.Err)
continue
}
switch msg := receivedMessage.Msg.(type) {
case o3.TextMessage:
if tid.String() == msg.Sender().String() {
continue
}
qoute := fmt.Sprintf("> %s: %s\n%s", msg.Sender(), msg.Text(), "Exactly!")
err = ctx.SendTextMessage(msg.Sender().String(), qoute, sendMsgChan)
if err != nil {
log.Fatal(err)
}
}
}
*/
},
}

26
cmd/serve_test.go Normal file
View File

@ -0,0 +1,26 @@
package cmd
import (
"testing"
"github.com/stretchr/testify/assert"
)
// correct run on root of this project
func TestServe(t *testing.T) {
assert := assert.New(t)
// fail on open file
RootCmd.SetArgs([]string{"serve", "--config", "a"})
assert.Panics(func() {
Execute()
})
// run
RootCmd.SetArgs([]string{"serve", "--config", "../config_example.toml"})
assert.Panics(func() {
Execute()
})
}

View File

@ -1 +0,0 @@
package all

View File

@ -139,7 +139,7 @@ func (a *Account) Send(to string, msg xmpp.Message) error {
"tid": to,
"msg_id": id,
"type": msgType,
}).Debug("delivered")
}).Debug("update status of threema message")
return nil
}

7
main_test.go Normal file
View File

@ -0,0 +1,7 @@
package main
import "testing"
func TestDummy(t *testing.T) {
main()
}

View File

@ -37,7 +37,7 @@ func (jid *JID) String() string {
return ""
}
str := jid.Domain
if jid.Local != "" {
if str != "" && jid.Local != "" {
str = jid.Local + "@" + str
}
return str
@ -47,15 +47,6 @@ func (jid *JID) IsDomain() bool {
return jid != nil && jid.Local == "" && jid.Domain != ""
}
func GetJID(jidStr string) (jid *JID) {
jidS := ParseJID(jidStr)
err := database.Read.Where(jidS).First(jid).Error
if err != nil {
return nil
}
return
}
var jidRegex *regexp.Regexp
func init() {

146
models/jid_test.go Normal file
View File

@ -0,0 +1,146 @@
package models
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestJIDTableName(t *testing.T) {
assert := assert.New(t)
var jid JID
assert.Equal("jid", jid.TableName())
}
// Test Values for NewJID from RFC 7622
// https://tools.ietf.org/html/rfc7622
func TestParseJID(t *testing.T) {
assert := assert.New(t)
checkList := map[string]*JID{
"juliet@example.com": {
Local: "juliet",
Domain: "example.com",
},
"juliet@example.com/foo": {
Local: "juliet",
Domain: "example.com",
},
"juliet@example.com/foo bar": {
Local: "juliet",
Domain: "example.com",
},
"juliet@example.com/foo@bar": {
Local: "juliet",
Domain: "example.com",
},
"foo\\20bar@example.com": {
Local: "foo\\20bar",
Domain: "example.com",
},
"fussball@example.com": {
Local: "fussball",
Domain: "example.com",
},
"fu&#xDF;ball@example.com": {
Local: "fu&#xDF;ball",
Domain: "example.com",
},
"&#x3C0;@example.com": {
Local: "&#x3C0;",
Domain: "example.com",
},
"&#x3A3;@example.com/foo": {
Local: "&#x3A3;",
Domain: "example.com",
},
"&#x3C3;@example.com/foo": {
Local: "&#x3C3;",
Domain: "example.com",
},
"&#x3C2;@example.com/foo": {
Local: "&#x3C2;",
Domain: "example.com",
},
"king@example.com/&#x265A;": {
Local: "king",
Domain: "example.com",
},
"example.com": {
Domain: "example.com",
},
"example.com/foobar": {
Domain: "example.com",
},
"a.example.com/b@example.net": {
Domain: "a.example.com",
},
"\"juliet\"@example.com": nil,
"foo bar@example.com": nil,
"juliet@example.com/ foo": nil,
"@example.com/": nil,
// "henry&#x2163;@example.com": nil, -- ignore for easier implementation
// "&#x265A;@example.com": nil,
"juliet@": nil,
"/foobar": nil,
}
for jidString, jidValid := range checkList {
jid := ParseJID(jidString)
if jidValid != nil {
assert.NotNil(jid, "this should be a valid JID:"+jidString)
if jid == nil {
continue
}
assert.Equal(jidValid.Local, jid.Local, "the local part was not right detectet:"+jidString)
assert.Equal(jidValid.Domain, jid.Domain, "the domain part was not right detectet:"+jidString)
} else {
assert.Nil(jid, "this should not be a valid JID:"+jidString)
}
}
}
func TestJIDString(t *testing.T) {
assert := assert.New(t)
var jid *JID
assert.Equal("", jid.String())
jid = &JID{
Domain: "example.com",
}
assert.Equal("example.com", jid.String())
jid = &JID{
Local: "romeo",
}
assert.Equal("", jid.String())
jid = &JID{
Local: "romeo",
Domain: "example.com",
}
assert.Equal("romeo@example.com", jid.String())
}
func TestJIDIsDomain(t *testing.T) {
assert := assert.New(t)
var jid *JID
assert.False(jid.IsDomain())
jid = &JID{}
assert.False(jid.IsDomain())
jid = &JID{Local: "a"}
assert.False(jid.IsDomain())
jid = &JID{Domain: "a"}
assert.True(jid.IsDomain())
jid = &JID{Local: "a", Domain: "b"}
assert.False(jid.IsDomain())
}

View File

@ -1 +0,0 @@
package models