From 900280a6cd626848d6840743d9a6b805fa9042ef Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Mon, 13 Feb 2023 11:09:19 +0100 Subject: [PATCH] Test coverage for the added functionalities --- msg_test.go | 30 ++++++++++++++++++++++++++ msgwriter_test.go | 29 +++++++++++++++++++++++++ part_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) diff --git a/msg_test.go b/msg_test.go index a2c4a8e..5c8bf90 100644 --- a/msg_test.go +++ b/msg_test.go @@ -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{} func (mw uppercaseMiddleware) Handle(m *Msg) *Msg { diff --git a/msgwriter_test.go b/msgwriter_test.go index 857b876..d19d334 100644 --- a/msgwriter_test.go +++ b/msgwriter_test.go @@ -104,3 +104,32 @@ func TestMsgWriter_writeMsg(t *testing.T) { 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" `) + _ = m.To(`"Toni Receiver" `) + 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" `) + _ = m.To(`"Toni Receiver" `) + 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") + } +} diff --git a/part_test.go b/part_test.go index d3ceef2..b0d50f3 100644 --- a/part_test.go +++ b/part_test.go @@ -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 func TestPart_SetContentType(t *testing.T) { 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 func TestPart_Delete(t *testing.T) { c := "This is a test with ümläutß"