-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from darkrockmountain/hotfix/msgraph-sdk-update
Hotfix/msgraph sdk update
- Loading branch information
Showing
4 changed files
with
88 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]", | ||
|
@@ -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")) | ||
|
||
|