diff --git a/src/internal/connector/support/m365Transform.go b/src/internal/connector/support/m365Transform.go index 285bb7946..082626713 100644 --- a/src/internal/connector/support/m365Transform.go +++ b/src/internal/connector/support/m365Transform.go @@ -424,12 +424,16 @@ func ToEventSimplified(orig models.Eventable) models.Eventable { return orig } +type getContenter interface { + GetContent() *string + GetContentType() *models.BodyType +} + // insertStringToBody helper function to insert text into models.bodyable // @returns string containing the content string of altered body. -func insertStringToBody(body models.ItemBodyable, newContent string) string { - var prefix, suffix string - +func insertStringToBody(body getContenter, newContent string) string { if body.GetContent() == nil || + len(*body.GetContent()) == 0 || body.GetContentType() == nil { return "" } @@ -439,14 +443,28 @@ func insertStringToBody(body models.ItemBodyable, newContent string) string { switch *body.GetContentType() { case models.TEXT_BODYTYPE: return newContent + content + case models.HTML_BODYTYPE: - array := strings.Split(content, "
") - prefix = array[0] + "" - interior := array[1] - bodyArray := strings.Split(interior, ">") - prefix += bodyArray[0] + ">" - suffix = strings.Join(bodyArray[1:], ">") + arr := strings.Split(content, "") + if len(arr) < 2 { + // malformed html; can't be sure where to insert attendees. + return newContent + content + } + + prefix := arr[0] + "" + interior := arr[1] + splitOnCloseAngle := strings.Split(interior, ">") + + if len(splitOnCloseAngle) < 3 { + // no inner elements in body, just insert the new content + return prefix + newContent + strings.Join(arr[1:], "") + } + + prefix += splitOnCloseAngle[0] + ">" + suffix := strings.Join(splitOnCloseAngle[1:], ">") + + return prefix + newContent + suffix } - return prefix + newContent + suffix + return newContent + content } diff --git a/src/internal/connector/support/m365Transform_test.go b/src/internal/connector/support/m365Transform_test.go index e38f72d29..90d8e757b 100644 --- a/src/internal/connector/support/m365Transform_test.go +++ b/src/internal/connector/support/m365Transform_test.go @@ -3,6 +3,7 @@ package support import ( "testing" + "github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -50,3 +51,97 @@ func (suite *SupportTestSuite) TestToEventSimplified() { assert.Contains(t, *event.GetBody().GetContent(), *member.GetEmailAddress().GetAddress()) } } + +type mockContenter struct { + content *string + contentType *models.BodyType +} + +func (mc mockContenter) GetContent() *string { + return mc.content +} + +func (mc mockContenter) GetContentType() *models.BodyType { + return mc.contentType +} + +func makeMockContent(c string, ct models.BodyType) mockContenter { + return mockContenter{&c, &ct} +} + +func (suite *SupportTestSuite) TestInsertStringToBody() { + nilTextContent := makeMockContent("", models.TEXT_BODYTYPE) + nilTextContent.content = nil + nilHTMLContent := makeMockContent("", models.HTML_BODYTYPE) + nilHTMLContent.content = nil + nilContentType := makeMockContent("brawnhilda", models.TEXT_BODYTYPE) + nilContentType.contentType = nil + + table := []struct { + name string + input mockContenter + content string + expect string + }{ + { + name: "nil text content", + input: nilTextContent, + content: "nil", + expect: "", + }, + { + name: "nil html content", + input: nilHTMLContent, + content: "nil", + expect: "", + }, + { + name: "nil content type", + input: nilContentType, + content: "nil", + expect: "", + }, + { + name: "text", + input: makeMockContent("_text", models.TEXT_BODYTYPE), + content: "new", + expect: "new_text", + }, + { + name: "empty text", + input: makeMockContent("", models.TEXT_BODYTYPE), + content: "new", + expect: "", + }, + { + name: "expected html", + input: makeMockContent("_