Skip to content

Commit 900280a

Browse files
committed
Test coverage for the added functionalities
1 parent f7e1345 commit 900280a

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

msg_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,36 @@ func TestNewMsgWithBoundary(t *testing.T) {
179179
}
180180
}
181181

182+
// TestNewMsg_WithPGPType tests WithPGPType option
183+
func TestNewMsg_WithPGPType(t *testing.T) {
184+
tests := []struct {
185+
name string
186+
pt PGPType
187+
hpt bool
188+
}{
189+
{"Not a PGP encoded message", NoPGP, false},
190+
{"PGP encrypted message", PGPEncrypt, true},
191+
{"PGP signed message", PGPSignature, true},
192+
}
193+
194+
for _, tt := range tests {
195+
t.Run(tt.name, func(t *testing.T) {
196+
m := NewMsg(WithPGPType(tt.pt))
197+
if m.pgptype != tt.pt {
198+
t.Errorf("WithPGPType() failed. Expected: %d, got: %d", tt.pt, m.pgptype)
199+
}
200+
m.pgptype = 99
201+
m.SetPGPType(tt.pt)
202+
if m.pgptype != tt.pt {
203+
t.Errorf("SetPGPType() failed. Expected: %d, got: %d", tt.pt, m.pgptype)
204+
}
205+
if m.hasPGPType() != tt.hpt {
206+
t.Errorf("hasPGPType() failed. Expected %t, got: %t", tt.hpt, m.hasPGPType())
207+
}
208+
})
209+
}
210+
}
211+
182212
type uppercaseMiddleware struct{}
183213

184214
func (mw uppercaseMiddleware) Handle(m *Msg) *Msg {

msgwriter_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,32 @@ func TestMsgWriter_writeMsg(t *testing.T) {
104104
t.Errorf(em)
105105
}
106106
}
107+
108+
// TestMsgWriter_writeMsg_PGP tests the writeMsg method of the msgWriter with PGP types set
109+
func TestMsgWriter_writeMsg_PGP(t *testing.T) {
110+
m := NewMsg(WithPGPType(PGPEncrypt))
111+
_ = m.From(`"Toni Tester" <test@example.com>`)
112+
_ = m.To(`"Toni Receiver" <receiver@example.com>`)
113+
m.Subject("This is a subject")
114+
m.SetBodyString(TypeTextPlain, "This is the body")
115+
buf := bytes.Buffer{}
116+
mw := &msgWriter{w: &buf, c: CharsetUTF8, en: mime.QEncoding}
117+
mw.writeMsg(m)
118+
ms := buf.String()
119+
if !strings.Contains(ms, `encrypted; protocol="application/pgp-encrypted"`) {
120+
t.Errorf("writeMsg failed. Expected PGP encoding header but didn't find it in message output")
121+
}
122+
123+
m = NewMsg(WithPGPType(PGPSignature))
124+
_ = m.From(`"Toni Tester" <test@example.com>`)
125+
_ = m.To(`"Toni Receiver" <receiver@example.com>`)
126+
m.Subject("This is a subject")
127+
m.SetBodyString(TypeTextPlain, "This is the body")
128+
buf = bytes.Buffer{}
129+
mw = &msgWriter{w: &buf, c: CharsetUTF8, en: mime.QEncoding}
130+
mw.writeMsg(m)
131+
ms = buf.String()
132+
if !strings.Contains(ms, `signed; protocol="application/pgp-signature"`) {
133+
t.Errorf("writeMsg failed. Expected PGP encoding header but didn't find it in message output")
134+
}
135+
}

part_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,36 @@ func TestPartEncoding(t *testing.T) {
4545
}
4646
}
4747

48+
// TestPart_WithPartContentDescription tests the WithPartContentDescription method
49+
func TestPart_WithPartContentDescription(t *testing.T) {
50+
tests := []struct {
51+
name string
52+
desc string
53+
}{
54+
{"Part description: test", "test"},
55+
{"Part description: empty", ""},
56+
}
57+
for _, tt := range tests {
58+
m := NewMsg()
59+
t.Run(tt.name, func(t *testing.T) {
60+
part := m.newPart(TypeTextPlain, WithPartContentDescription(tt.desc), nil)
61+
if part == nil {
62+
t.Errorf("newPart() WithPartContentDescription() failed: no part returned")
63+
return
64+
}
65+
if part.desc != tt.desc {
66+
t.Errorf("newPart() WithPartContentDescription() failed: expected: %s, got: %s", tt.desc,
67+
part.desc)
68+
}
69+
part.desc = ""
70+
part.SetDescription(tt.desc)
71+
if part.desc != tt.desc {
72+
t.Errorf("newPart() SetDescription() failed: expected: %s, got: %s", tt.desc, part.desc)
73+
}
74+
})
75+
}
76+
}
77+
4878
// TestPartContentType tests Part.SetContentType
4979
func TestPart_SetContentType(t *testing.T) {
5080
tests := []struct {
@@ -241,6 +271,31 @@ func TestPart_SetContent(t *testing.T) {
241271
}
242272
}
243273

274+
// TestPart_SetDescription tests Part.SetDescription
275+
func TestPart_SetDescription(t *testing.T) {
276+
c := "This is a test"
277+
d := "test-description"
278+
m := NewMsg()
279+
m.SetBodyString(TypeTextPlain, c)
280+
pl, err := getPartList(m)
281+
if err != nil {
282+
t.Errorf("failed: %s", err)
283+
return
284+
}
285+
pd := pl[0].GetDescription()
286+
if pd != "" {
287+
t.Errorf("Part.GetDescription failed. Expected empty description but got: %s", pd)
288+
}
289+
pl[0].SetDescription(d)
290+
if pl[0].desc != d {
291+
t.Errorf("Part.SetDescription failed. Expected desc to be: %s, got: %s", d, pd)
292+
}
293+
pd = pl[0].GetDescription()
294+
if pd != d {
295+
t.Errorf("Part.GetDescription failed. Expected: %s, got: %s", d, pd)
296+
}
297+
}
298+
244299
// TestPart_Delete tests Part.Delete
245300
func TestPart_Delete(t *testing.T) {
246301
c := "This is a test with ümläutß"

0 commit comments

Comments
 (0)