Issue 1020 (#1021)

## Description

CI tests are showing a consistent panic when adding attendees
to events with zero content.

## Type of change

- [x] 🐛 Bugfix

## Issue(s)

* #1020

## Test Plan

- [x]  Unit test
- [x] 💚 E2E
This commit is contained in:
Keepers 2022-10-03 11:17:32 -06:00 committed by GitHub
parent 03bb63f52d
commit 7fa86824e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 123 additions and 12 deletions

View File

@ -424,12 +424,16 @@ func ToEventSimplified(orig models.Eventable) models.Eventable {
return orig return orig
} }
type getContenter interface {
GetContent() *string
GetContentType() *models.BodyType
}
// insertStringToBody helper function to insert text into models.bodyable // insertStringToBody helper function to insert text into models.bodyable
// @returns string containing the content string of altered body. // @returns string containing the content string of altered body.
func insertStringToBody(body models.ItemBodyable, newContent string) string { func insertStringToBody(body getContenter, newContent string) string {
var prefix, suffix string
if body.GetContent() == nil || if body.GetContent() == nil ||
len(*body.GetContent()) == 0 ||
body.GetContentType() == nil { body.GetContentType() == nil {
return "" return ""
} }
@ -439,14 +443,28 @@ func insertStringToBody(body models.ItemBodyable, newContent string) string {
switch *body.GetContentType() { switch *body.GetContentType() {
case models.TEXT_BODYTYPE: case models.TEXT_BODYTYPE:
return newContent + content return newContent + content
case models.HTML_BODYTYPE: case models.HTML_BODYTYPE:
array := strings.Split(content, "<body>") arr := strings.Split(content, "<body>")
prefix = array[0] + "<body>" if len(arr) < 2 {
interior := array[1] // malformed html; can't be sure where to insert attendees.
bodyArray := strings.Split(interior, ">") return newContent + content
prefix += bodyArray[0] + ">" }
suffix = strings.Join(bodyArray[1:], ">")
prefix := arr[0] + "<body>"
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
} }

View File

@ -3,6 +3,7 @@ package support
import ( import (
"testing" "testing"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
@ -50,3 +51,97 @@ func (suite *SupportTestSuite) TestToEventSimplified() {
assert.Contains(t, *event.GetBody().GetContent(), *member.GetEmailAddress().GetAddress()) 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("_<body><div>_text</div></body>_", models.HTML_BODYTYPE),
content: "foo",
expect: "_<body><div>foo_text</div></body>_",
},
{
name: "no div html",
input: makeMockContent("_<body>_text</body>_", models.HTML_BODYTYPE),
content: "bar",
expect: "_<body>bar_text</body>_",
},
{
name: "no body html",
input: makeMockContent("_text", models.HTML_BODYTYPE),
content: "baz",
expect: "baz_text",
},
{
name: "empty html",
input: makeMockContent("", models.HTML_BODYTYPE),
content: "fnords",
expect: "",
},
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
result := insertStringToBody(test.input, test.content)
assert.Equal(t, test.expect, result)
})
}
}

View File

@ -257,7 +257,6 @@ func (op *RestoreOperation) persistResults(
ctx, ctx,
events.RestoreEnd, events.RestoreEnd,
map[string]any{ map[string]any{
// TODO: RestoreID
events.BackupID: op.BackupID, events.BackupID: op.BackupID,
events.DataRetrieved: op.Results.BytesRead, events.DataRetrieved: op.Results.BytesRead,
events.Duration: op.Results.CompletedAt.Sub(op.Results.StartedAt), events.Duration: op.Results.CompletedAt.Sub(op.Results.StartedAt),
@ -269,7 +268,6 @@ func (op *RestoreOperation) persistResults(
events.Service: op.Selectors.Service.String(), events.Service: op.Selectors.Service.String(),
events.StartTime: op.Results.StartedAt, events.StartTime: op.Results.StartedAt,
events.Status: op.Status, events.Status: op.Status,
// TODO: events.ExchangeDataObserved: <amount of data retrieved>,
}, },
) )