Skip to content

Commit

Permalink
Merge pull request #66 from darkrockmountain/hotfix/msgraph-sdk-update
Browse files Browse the repository at this point in the history
Hotfix/msgraph sdk update
  • Loading branch information
DarkRockMountain-admin authored Jul 17, 2024
2 parents 1a9e069 + a1881cb commit 20312cc
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 85 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ require (
github.com/aws/aws-sdk-go v1.54.11
github.com/mailgun/mailgun-go/v4 v4.12.0
github.com/microcosm-cc/bluemonday v1.0.26
github.com/microsoft/kiota-abstractions-go v1.6.0
github.com/microsoft/kiota-abstractions-go v1.6.1
github.com/microsoft/kiota-authentication-azure-go v1.0.2
github.com/microsoft/kiota-http-go v1.4.1
github.com/microsoft/kiota-serialization-form-go v1.0.0
github.com/microsoft/kiota-serialization-json-go v1.0.7
github.com/microsoft/kiota-serialization-multipart-go v1.0.0
github.com/microsoft/kiota-serialization-text-go v1.0.0
github.com/microsoftgraph/msgraph-sdk-go v1.45.0
github.com/microsoftgraph/msgraph-sdk-go v1.46.0
github.com/pkg/errors v0.9.1
github.com/sendgrid/rest v2.6.9+incompatible
github.com/sendgrid/sendgrid-go v3.14.0+incompatible
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ github.com/mailgun/mailgun-go/v4 v4.12.0/go.mod h1:L9s941Lgk7iB3TgywTPz074pK2Ekk
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58=
github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs=
github.com/microsoft/kiota-abstractions-go v1.6.0 h1:qbGBNMU0/o5myKbikCBXJFohVCFrrpx2cO15Rta2WyA=
github.com/microsoft/kiota-abstractions-go v1.6.0/go.mod h1:7YH20ZbRWXGfHSSvdHkdztzgCB9mRdtFx13+hrYIEpo=
github.com/microsoft/kiota-abstractions-go v1.6.1 h1:NXK50S3BwJn9Wj6bO0YFuAig7y2WVgdQ/ie1ktMl2J4=
github.com/microsoft/kiota-abstractions-go v1.6.1/go.mod h1:FI1I2OHg0E7bK5t8DPnw+9C/CHVyLP6XeqDBT+95pTE=
github.com/microsoft/kiota-authentication-azure-go v1.0.2 h1:tClGeyFZJ+4Bakf8u0euPM4wqy4ethycdOgx3jyH3pI=
github.com/microsoft/kiota-authentication-azure-go v1.0.2/go.mod h1:aTcti0bUJEcq7kBfQG4Sr4ElvRNuaalXcFEu4iEyQ6M=
github.com/microsoft/kiota-http-go v1.4.1 h1:zR54JahUOcu8h9C5z00fcQChzX8d01+BwhkTS8H16Ro=
Expand All @@ -123,8 +123,8 @@ github.com/microsoft/kiota-serialization-multipart-go v1.0.0 h1:3O5sb5Zj+moLBiJy
github.com/microsoft/kiota-serialization-multipart-go v1.0.0/go.mod h1:yauLeBTpANk4L03XD985akNysG24SnRJGaveZf+p4so=
github.com/microsoft/kiota-serialization-text-go v1.0.0 h1:XOaRhAXy+g8ZVpcq7x7a0jlETWnWrEum0RhmbYrTFnA=
github.com/microsoft/kiota-serialization-text-go v1.0.0/go.mod h1:sM1/C6ecnQ7IquQOGUrUldaO5wj+9+v7G2W3sQ3fy6M=
github.com/microsoftgraph/msgraph-sdk-go v1.45.0 h1:PRE3nsGDlASfoM1h6QNCBXmU34LTiReg5LMBjRKOL3k=
github.com/microsoftgraph/msgraph-sdk-go v1.45.0/go.mod h1:MSMgjuMPKAsIz8XfH5l+e781fkWjUxc1XXhb2eoSdc0=
github.com/microsoftgraph/msgraph-sdk-go v1.46.0 h1:5AT0FZkPFTiLZ9nZym1TLUFlcHFVhAAvqNb9B52Xvn0=
github.com/microsoftgraph/msgraph-sdk-go v1.46.0/go.mod h1:Rf2aiCdmTvY325XfhxnUiRIuIyj66SYdszaovW091S0=
github.com/microsoftgraph/msgraph-sdk-go-core v1.1.0 h1:NB7c/n4Knj+TLaLfjsahhSqoUqoN/CtyNB0XIe/nJnM=
github.com/microsoftgraph/msgraph-sdk-go-core v1.1.0/go.mod h1:M3w/5IFJ1u/DpwOyjsjNSVEA43y1rLOeX58suyfBhGk=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
Expand Down
7 changes: 3 additions & 4 deletions providers/microsoft365/microsoft365_email_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/darkrockmountain/gomail"
"github.com/darkrockmountain/gomail/common"
azureAuth "github.com/microsoft/kiota-authentication-azure-go"
msgraph "github.com/microsoftgraph/msgraph-sdk-go"
Expand All @@ -26,14 +25,14 @@ type mSGraphEmailSender struct {
//
// Returns:
// - error: An error if the email sending fails, otherwise nil.
func (s *mSGraphEmailSender) SendEmail(message *gomail.EmailMessage) error {
func (s *mSGraphEmailSender) SendEmail(message *common.EmailMessage) error {
if s.userRequestBuilder == nil {
return fmt.Errorf("error: no user request builder available")
}

msMessage := composeMsMessage(message)
// Send the message
sendMailPostRequestBody := graphusers.NewItemSendmailSendMailPostRequestBody()
sendMailPostRequestBody := graphusers.NewItemSendMailPostRequestBody()
sendMailPostRequestBody.SetMessage(msMessage)

err := s.userRequestBuilder.SendMail().Post(context.Background(), sendMailPostRequestBody, nil)
Expand All @@ -52,7 +51,7 @@ func (s *mSGraphEmailSender) SendEmail(message *gomail.EmailMessage) error {
//
// Returns:
// - *models.Message: A pointer to the composed Message object ready to be sent via the Microsoft Graph API.
func composeMsMessage(message *gomail.EmailMessage) *models.Message {
func composeMsMessage(message *common.EmailMessage) *models.Message {

// Create the message
body := models.NewItemBody()
Expand Down
154 changes: 79 additions & 75 deletions providers/microsoft365/microsoft365_email_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,81 +25,6 @@ func TestEmailSenderImplementation(t *testing.T) {
var _ gomail.EmailSender = (*mSGraphEmailSender)(nil)
}

type MockMSAuthenticationProvider struct {
}

func (m *MockMSAuthenticationProvider) AuthenticateRequest(context context.Context, request *abstractions.RequestInformation, additionalAuthenticationContext map[string]interface{}) error {
return nil
}

// MSMockRoundTripper implements the http.RoundTripper interface
type MSMockRoundTripper struct {
Response *http.Response
Err error
}

func (m *MSMockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if m.Err != nil {
return nil, m.Err
}

return &http.Response{
StatusCode: http.StatusOK,
Header: make(http.Header),
}, nil
}

func mockItemSendMailRequestBuild(err error) *users.UserItemRequestBuilder {

defaultClient := &http.Client{
Transport: &MSMockRoundTripper{
Err: err,
},
}

adapter, err := khttp.NewNetHttpRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient(&MockMSAuthenticationProvider{}, nil, nil, defaultClient)
if err != nil {
panic(err)
}

abstractions.RegisterDefaultSerializer(func() serialization.SerializationWriterFactory { return ksJson.NewJsonSerializationWriterFactory() })
abstractions.RegisterDefaultSerializer(func() serialization.SerializationWriterFactory { return ksText.NewTextSerializationWriterFactory() })
abstractions.RegisterDefaultSerializer(func() serialization.SerializationWriterFactory { return ksForm.NewFormSerializationWriterFactory() })
abstractions.RegisterDefaultSerializer(func() serialization.SerializationWriterFactory { return ksMP.NewMultipartSerializationWriterFactory() })
abstractions.RegisterDefaultDeserializer(func() serialization.ParseNodeFactory { return ksJson.NewJsonParseNodeFactory() })
abstractions.RegisterDefaultDeserializer(func() serialization.ParseNodeFactory { return ksText.NewTextParseNodeFactory() })
abstractions.RegisterDefaultDeserializer(func() serialization.ParseNodeFactory { return ksForm.NewFormParseNodeFactory() })

sendMail := users.UserItemRequestBuilder{
BaseRequestBuilder: abstractions.BaseRequestBuilder{
RequestAdapter: adapter,
UrlTemplate: "{+baseurl}/users{?%24count,%24expand,%24filter,%24orderby,%24search,%24select,%24top}",
PathParameters: map[string]string{"user%2Did": "testUserID"},
},
}
return &sendMail
}

func TestSendEmail(t *testing.T) {
attachment := *gomail.NewAttachment("test.txt", []byte("test_attachment_content"))

message := gomail.NewEmailMessage(
"[email protected]",
[]string{"[email protected]"},
"Test Subject",
"Test Body",
).AddAttachment(attachment)

reqBuild := mockItemSendMailRequestBuild(nil)
sender := &mSGraphEmailSender{
userRequestBuilder: reqBuild,
}

err := sender.SendEmail(message)
assert.NoError(t, err)

}

func TestComposeMessage_PlainTextEmail(t *testing.T) {
message := gomail.NewEmailMessage(
"[email protected]",
Expand Down Expand Up @@ -327,6 +252,85 @@ func TestComposeMessage_MissingFields(t *testing.T) {
assert.Nil(t, msMessage.GetAttachments())
}

type MockMSAuthenticationProvider struct{}

func (m *MockMSAuthenticationProvider) AuthenticateRequest(context context.Context, request *abstractions.RequestInformation, additionalAuthenticationContext map[string]interface{}) error {
return nil
}

type MSMockRoundTripper struct {
Response *http.Response
Err error
}

func (m *MSMockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if m.Err != nil {
return nil, m.Err
}

return &http.Response{
StatusCode: http.StatusOK,
Header: make(http.Header),
}, nil
}

// Initialize the HTTP client and serializers once and reuse them:
var defaultClient *http.Client
var adapter abstractions.RequestAdapter

func init() {
defaultClient = &http.Client{
Transport: &MSMockRoundTripper{},
}

var err error
adapter, err = khttp.NewNetHttpRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient(&MockMSAuthenticationProvider{}, nil, nil, defaultClient)
if err != nil {
panic(err)
}

abstractions.RegisterDefaultSerializer(func() serialization.SerializationWriterFactory { return ksJson.NewJsonSerializationWriterFactory() })
abstractions.RegisterDefaultSerializer(func() serialization.SerializationWriterFactory { return ksText.NewTextSerializationWriterFactory() })
abstractions.RegisterDefaultSerializer(func() serialization.SerializationWriterFactory { return ksForm.NewFormSerializationWriterFactory() })
abstractions.RegisterDefaultSerializer(func() serialization.SerializationWriterFactory { return ksMP.NewMultipartSerializationWriterFactory() })
abstractions.RegisterDefaultDeserializer(func() serialization.ParseNodeFactory { return ksJson.NewJsonParseNodeFactory() })
abstractions.RegisterDefaultDeserializer(func() serialization.ParseNodeFactory { return ksText.NewTextParseNodeFactory() })
abstractions.RegisterDefaultDeserializer(func() serialization.ParseNodeFactory { return ksForm.NewFormParseNodeFactory() })
}

func mockItemSendMailRequestBuild(err error) *users.UserItemRequestBuilder {
defaultClient.Transport.(*MSMockRoundTripper).Err = err

sendMail := users.UserItemRequestBuilder{
BaseRequestBuilder: abstractions.BaseRequestBuilder{
RequestAdapter: adapter,
UrlTemplate: "{+baseurl}/users{?%24count,%24expand,%24filter,%24orderby,%24search,%24select,%24top}",
PathParameters: map[string]string{"user%2Did": "testUserID"},
},
}
return &sendMail
}

func TestSendEmail(t *testing.T) {
attachment := *gomail.NewAttachment("test.txt", []byte("test_attachment_content"))

message := gomail.NewEmailMessage(
"[email protected]",
[]string{"[email protected]"},
"Test Subject",
"Test Body",
).AddAttachment(attachment)

reqBuild := mockItemSendMailRequestBuild(nil)
sender := &mSGraphEmailSender{
userRequestBuilder: reqBuild,
}

err := sender.SendEmail(message)
assert.NoError(t, err)

}

func TestSendEmail_FailedSend(t *testing.T) {
attachment := *gomail.NewAttachment("test.txt", []byte("test_attachment_content"))

Expand Down

0 comments on commit 20312cc

Please sign in to comment.