Skip to content

Commit

Permalink
feat: add flag smime for indicating that the part should be marked fo…
Browse files Browse the repository at this point in the history
…r singing with S/MIME
  • Loading branch information
theexiile1305 committed Sep 26, 2024
1 parent 65b9fd0 commit 3154649
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
18 changes: 18 additions & 0 deletions part.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
44 changes: 44 additions & 0 deletions part_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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ß"
Expand Down

0 comments on commit 3154649

Please sign in to comment.