Test coverage for the added functionalities

This commit is contained in:
Winni Neessen 2023-02-13 11:09:19 +01:00
parent f7e1345f3d
commit 900280a6cd
Signed by: wneessen
GPG key ID: 385AC9889632126E
3 changed files with 114 additions and 0 deletions

View file

@ -179,6 +179,36 @@ func TestNewMsgWithBoundary(t *testing.T) {
} }
} }
// TestNewMsg_WithPGPType tests WithPGPType option
func TestNewMsg_WithPGPType(t *testing.T) {
tests := []struct {
name string
pt PGPType
hpt bool
}{
{"Not a PGP encoded message", NoPGP, false},
{"PGP encrypted message", PGPEncrypt, true},
{"PGP signed message", PGPSignature, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := NewMsg(WithPGPType(tt.pt))
if m.pgptype != tt.pt {
t.Errorf("WithPGPType() failed. Expected: %d, got: %d", tt.pt, m.pgptype)
}
m.pgptype = 99
m.SetPGPType(tt.pt)
if m.pgptype != tt.pt {
t.Errorf("SetPGPType() failed. Expected: %d, got: %d", tt.pt, m.pgptype)
}
if m.hasPGPType() != tt.hpt {
t.Errorf("hasPGPType() failed. Expected %t, got: %t", tt.hpt, m.hasPGPType())
}
})
}
}
type uppercaseMiddleware struct{} type uppercaseMiddleware struct{}
func (mw uppercaseMiddleware) Handle(m *Msg) *Msg { func (mw uppercaseMiddleware) Handle(m *Msg) *Msg {

View file

@ -104,3 +104,32 @@ func TestMsgWriter_writeMsg(t *testing.T) {
t.Errorf(em) t.Errorf(em)
} }
} }
// TestMsgWriter_writeMsg_PGP tests the writeMsg method of the msgWriter with PGP types set
func TestMsgWriter_writeMsg_PGP(t *testing.T) {
m := NewMsg(WithPGPType(PGPEncrypt))
_ = m.From(`"Toni Tester" <test@example.com>`)
_ = m.To(`"Toni Receiver" <receiver@example.com>`)
m.Subject("This is a subject")
m.SetBodyString(TypeTextPlain, "This is the body")
buf := bytes.Buffer{}
mw := &msgWriter{w: &buf, c: CharsetUTF8, en: mime.QEncoding}
mw.writeMsg(m)
ms := buf.String()
if !strings.Contains(ms, `encrypted; protocol="application/pgp-encrypted"`) {
t.Errorf("writeMsg failed. Expected PGP encoding header but didn't find it in message output")
}
m = NewMsg(WithPGPType(PGPSignature))
_ = m.From(`"Toni Tester" <test@example.com>`)
_ = m.To(`"Toni Receiver" <receiver@example.com>`)
m.Subject("This is a subject")
m.SetBodyString(TypeTextPlain, "This is the body")
buf = bytes.Buffer{}
mw = &msgWriter{w: &buf, c: CharsetUTF8, en: mime.QEncoding}
mw.writeMsg(m)
ms = buf.String()
if !strings.Contains(ms, `signed; protocol="application/pgp-signature"`) {
t.Errorf("writeMsg failed. Expected PGP encoding header but didn't find it in message output")
}
}

View file

@ -45,6 +45,36 @@ func TestPartEncoding(t *testing.T) {
} }
} }
// TestPart_WithPartContentDescription tests the WithPartContentDescription method
func TestPart_WithPartContentDescription(t *testing.T) {
tests := []struct {
name string
desc string
}{
{"Part description: test", "test"},
{"Part description: empty", ""},
}
for _, tt := range tests {
m := NewMsg()
t.Run(tt.name, func(t *testing.T) {
part := m.newPart(TypeTextPlain, WithPartContentDescription(tt.desc), nil)
if part == nil {
t.Errorf("newPart() WithPartContentDescription() failed: no part returned")
return
}
if part.desc != tt.desc {
t.Errorf("newPart() WithPartContentDescription() failed: expected: %s, got: %s", tt.desc,
part.desc)
}
part.desc = ""
part.SetDescription(tt.desc)
if part.desc != tt.desc {
t.Errorf("newPart() SetDescription() failed: expected: %s, got: %s", tt.desc, part.desc)
}
})
}
}
// TestPartContentType tests Part.SetContentType // TestPartContentType tests Part.SetContentType
func TestPart_SetContentType(t *testing.T) { func TestPart_SetContentType(t *testing.T) {
tests := []struct { tests := []struct {
@ -241,6 +271,31 @@ func TestPart_SetContent(t *testing.T) {
} }
} }
// TestPart_SetDescription tests Part.SetDescription
func TestPart_SetDescription(t *testing.T) {
c := "This is a test"
d := "test-description"
m := NewMsg()
m.SetBodyString(TypeTextPlain, c)
pl, err := getPartList(m)
if err != nil {
t.Errorf("failed: %s", err)
return
}
pd := pl[0].GetDescription()
if pd != "" {
t.Errorf("Part.GetDescription failed. Expected empty description but got: %s", pd)
}
pl[0].SetDescription(d)
if pl[0].desc != d {
t.Errorf("Part.SetDescription failed. Expected desc to be: %s, got: %s", d, pd)
}
pd = pl[0].GetDescription()
if pd != d {
t.Errorf("Part.GetDescription failed. Expected: %s, got: %s", d, pd)
}
}
// TestPart_Delete tests Part.Delete // TestPart_Delete tests Part.Delete
func TestPart_Delete(t *testing.T) { func TestPart_Delete(t *testing.T) {
c := "This is a test with ümläutß" c := "This is a test with ümläutß"