diff --git a/part.go b/part.go index 7c76b7d9..035b3426 100644 --- a/part.go +++ b/part.go @@ -20,6 +20,7 @@ type Part struct { encoding Encoding isDeleted bool writeFunc func(io.Writer) (int64, error) + smime bool } // GetContent executes the WriteFunc of the Part and returns the content as byte slice @@ -56,6 +57,11 @@ func (p *Part) GetDescription() string { return p.description } +// IsSMimeSigned returns true if the Part should be singed with S/MIME +func (p *Part) IsSMimeSigned() bool { + return p.smime +} + // SetContent overrides the content of the Part with the given string func (p *Part) SetContent(content string) { buffer := bytes.NewBufferString(content) @@ -82,6 +88,11 @@ func (p *Part) SetDescription(description string) { p.description = description } +// SetIsSMimeSigned sets the flag for signing the Part with S/MIME +func (p *Part) SetIsSMimeSigned(smime bool) { + p.smime = smime +} + // SetWriteFunc overrides the WriteFunc of the Part func (p *Part) SetWriteFunc(writeFunc func(io.Writer) (int64, error)) { p.writeFunc = writeFunc @@ -113,3 +124,10 @@ func WithPartContentDescription(description string) PartOption { p.description = description } } + +// WithSMimeSinging overrides the flag for signing the Part with S/MIME +func WithSMimeSinging() PartOption { + return func(p *Part) { + p.smime = true + } +} diff --git a/part_test.go b/part_test.go index a1969862..2b9725f1 100644 --- a/part_test.go +++ b/part_test.go @@ -102,6 +102,24 @@ func TestPart_WithPartContentDescription(t *testing.T) { } } +// TestPart_WithSMimeSinging tests the WithSMimeSinging method +func TestPart_WithSMimeSinging(t *testing.T) { + m := NewMsg() + part := m.newPart(TypeTextPlain, WithSMimeSinging()) + if part == nil { + t.Errorf("newPart() WithSMimeSinging() failed: no part returned") + return + } + if part.smime != true { + t.Errorf("newPart() WithSMimeSinging() failed: expected: %v, got: %v", true, part.smime) + } + part.smime = true + part.SetIsSMimeSigned(false) + if part.smime != false { + t.Errorf("newPart() SetIsSMimeSigned() failed: expected: %v, got: %v", false, part.smime) + } +} + // TestPartContentType tests Part.SetContentType func TestPart_SetContentType(t *testing.T) { tests := []struct { @@ -245,6 +263,32 @@ func TestPart_GetContentBroken(t *testing.T) { } } +// TestPart_IsSMimeSigned tests Part.IsSMimeSigned +func TestPart_IsSMimeSigned(t *testing.T) { + tests := []struct { + name string + want bool + }{ + {"smime:", true}, + {"smime:", false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := NewMsg() + pl, err := getPartList(m) + if err != nil { + t.Errorf("failed: %s", err) + return + } + pl[0].SetIsSMimeSigned(tt.want) + smime := pl[0].IsSMimeSigned() + if smime != tt.want { + t.Errorf("SetContentType failed. Got: %v, expected: %v", smime, tt.want) + } + }) + } +} + // TestPart_SetWriteFunc tests Part.SetWriteFunc func TestPart_SetWriteFunc(t *testing.T) { c := "This is a test with ümläutß"