From 709ce45e933e8fcef3202817ff6fde0d01de5fb4 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Sat, 1 Jun 2019 02:59:00 +0200 Subject: [PATCH] [TEST] add some tests --- cmd/root_test.go | 1 - cmd/serve.go | 48 ------------ cmd/serve_test.go | 26 +++++++ component/all/main_test.go | 1 - component/threema/account.go | 2 +- main_test.go | 7 ++ models/jid.go | 11 +-- models/jid_test.go | 146 +++++++++++++++++++++++++++++++++++ models/main_test.go | 1 - 9 files changed, 181 insertions(+), 62 deletions(-) delete mode 100644 cmd/root_test.go create mode 100644 cmd/serve_test.go delete mode 100644 component/all/main_test.go create mode 100644 main_test.go create mode 100644 models/jid_test.go delete mode 100644 models/main_test.go diff --git a/cmd/root_test.go b/cmd/root_test.go deleted file mode 100644 index 1d619dd..0000000 --- a/cmd/root_test.go +++ /dev/null @@ -1 +0,0 @@ -package cmd diff --git a/cmd/serve.go b/cmd/serve.go index 7903fac..dce70d3 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -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) - } - - } - } - */ }, } diff --git a/cmd/serve_test.go b/cmd/serve_test.go new file mode 100644 index 0000000..291974f --- /dev/null +++ b/cmd/serve_test.go @@ -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() + }) +} diff --git a/component/all/main_test.go b/component/all/main_test.go deleted file mode 100644 index 1a6c647..0000000 --- a/component/all/main_test.go +++ /dev/null @@ -1 +0,0 @@ -package all diff --git a/component/threema/account.go b/component/threema/account.go index 97956a9..bfeaed0 100644 --- a/component/threema/account.go +++ b/component/threema/account.go @@ -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 } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..fd31a28 --- /dev/null +++ b/main_test.go @@ -0,0 +1,7 @@ +package main + +import "testing" + +func TestDummy(t *testing.T) { + main() +} diff --git a/models/jid.go b/models/jid.go index 7ce2304..b5c8454 100644 --- a/models/jid.go +++ b/models/jid.go @@ -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() { diff --git a/models/jid_test.go b/models/jid_test.go new file mode 100644 index 0000000..04f4616 --- /dev/null +++ b/models/jid_test.go @@ -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ßball@example.com": { + Local: "fußball", + Domain: "example.com", + }, + "π@example.com": { + Local: "π", + Domain: "example.com", + }, + "Σ@example.com/foo": { + Local: "Σ", + Domain: "example.com", + }, + "σ@example.com/foo": { + Local: "σ", + Domain: "example.com", + }, + "ς@example.com/foo": { + Local: "ς", + Domain: "example.com", + }, + "king@example.com/♚": { + 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Ⅳ@example.com": nil, -- ignore for easier implementation + // "♚@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()) +} diff --git a/models/main_test.go b/models/main_test.go deleted file mode 100644 index 2640e7f..0000000 --- a/models/main_test.go +++ /dev/null @@ -1 +0,0 @@ -package models