public void SendEmail_DisposesProperly()
        {
            // Arrange
            var writerDisposeCalled = false;
            var smtpDisposeCalled   = false;
            var sendCalled          = false;

            ShimSmtpClient.AllInstances.SendMailMessage = (_1, _2) =>
            {
                if (smtpDisposeCalled)
                {
                    throw new InvalidOperationException("Smtp Client shouldn't dispose before sending");
                }
                sendCalled = true;
            };
            ShimSmtpClient.AllInstances.Dispose  = _ => smtpDisposeCalled = true;
            ShimMailMessage.AllInstances.Dispose = _ =>
            {
                if (!smtpDisposeCalled ||
                    !sendCalled)
                {
                    throw new InvalidOperationException("Mail Message can only be disposed after client send message and properly disposed");
                }
                writerDisposeCalled = true;
            };

            ShimSPAdministrationWebApplication.LocalGet = () =>
            {
                var spAdministrationWebApplication = new ShimSPAdministrationWebApplication();
                var spWebApplication = new ShimSPWebApplication(spAdministrationWebApplication)
                {
                    OutboundMailServiceInstanceGet = () =>
                    {
                        var spOutboundMailServiceInstance = new ShimSPOutboundMailServiceInstance();
                        var spServiceInstance             = new ShimSPServiceInstance(spOutboundMailServiceInstance)
                        {
                            ServerGet = () =>
                            {
                                var spServer      = new ShimSPServer();
                                var spPersistedOb = new ShimSPPersistedObject(spServer)
                                {
                                    NameGet = () => DummyServerName
                                };
                                return(spServer);
                            }
                        };
                        return(spOutboundMailServiceInstance);
                    }
                };
                return(spAdministrationWebApplication);
            };

            ShimSPSecurity.RunWithElevatedPrivilegesSPSecurityCodeToRunElevated = code => code();

            // Act
            _privateObject.Invoke(
                SendEmailMethodName,
                DummyUserEmail,
                DummyFromEmail,
                DummySubject,
                DummyUserDisplayName);

            // Assert
            this.ShouldSatisfyAllConditions(
                () => writerDisposeCalled.ShouldBeTrue(),
                () => smtpDisposeCalled.ShouldBeTrue(),
                () => sendCalled.ShouldBeTrue());
        }
예제 #2
0
        private void ISendEmail_Called_Disposed(bool hideFrom, string curUserEmail)
        {
            // Arrange
            const string outboundEmail = "*****@*****.**";
            var          siteId        = new Guid("0E2FCE32-4507-452C-A8BF-D28C867DF0E9");
            var          webId         = new Guid("4008FE64-56A8-4DDF-99C7-AF3C45AEBDDA");

            var shimCurrentUser = new ShimSPUser
            {
                NameGet  = () => "CurUserName",
                EmailGet = () => curUserEmail
            };
            var currentUser = (SPUser)shimCurrentUser;
            var shimUser    = new ShimSPUser
            {
                NameGet  = () => "EUserName",
                EmailGet = () => "*****@*****.**"
            };
            var user = (SPUser)shimUser;

            var arguments = new object[]
            {
                1,
                hideFrom,
                siteId,
                webId,
                (SPUser)shimCurrentUser,
                (SPUser)shimUser,
                new Hashtable()
                {
                    { "building", "64" }
                }
            };

            ShimSPAdministrationWebApplication.LocalGet = () =>
            {
                var spAdministrationWebApplication = new ShimSPAdministrationWebApplication();
                var spWebApplication = new ShimSPWebApplication(spAdministrationWebApplication)
                {
                    OutboundMailServiceInstanceGet = () =>
                    {
                        var spOutboundMailServiceInstance = new ShimSPOutboundMailServiceInstance();
                        var spServiceInstance             = new ShimSPServiceInstance(spOutboundMailServiceInstance)
                        {
                            ServerGet = () => new ShimSPServer
                            {
                                AddressGet = () => "15th ave"
                            }
                        };
                        return(spOutboundMailServiceInstance);
                    },
                    OutboundMailSenderAddressGet = () => outboundEmail
                };
                return(spAdministrationWebApplication);
            };

            string      setHost        = null;
            MailMessage sentMessage    = null;
            var         clientDisposed = false;

            ShimSmtpClient.Constructor = client =>
            {
                new ShimSmtpClient(client)
                {
                    HostSetString = host =>
                    {
                        setHost = host;
                    },
                    SendMailMessage = message =>
                    {
                        sentMessage = message;
                    },
                    DisposeBoolean = disposing =>
                    {
                        clientDisposed = true;
                    }
                };
            };

            var messageDisposed = false;

            ShimMailMessage.AllInstances.DisposeBoolean = (message, disposing) =>
            {
                messageDisposed = true;
            };

            // Act
            _apiEmailPrivate.Invoke("iSendEmail", BindingFlags.Static | BindingFlags.NonPublic, arguments);

            // Assert
            AssertConnection();
            if (hideFrom)
            {
                Assert.AreEqual(outboundEmail, sentMessage.From.Address);
                Assert.IsTrue(string.IsNullOrWhiteSpace(sentMessage.From.DisplayName));
            }
            else if (string.IsNullOrWhiteSpace(curUserEmail))
            {
                Assert.AreEqual(outboundEmail, sentMessage.From.Address);
                Assert.AreEqual(currentUser.Name, sentMessage.From.DisplayName);
            }
            else
            {
                Assert.AreEqual(currentUser.Email, sentMessage.From.Address);
                Assert.AreEqual(currentUser.Name, sentMessage.From.DisplayName);
            }
            Assert.AreEqual(user.Email, sentMessage.To[0].Address);
            Assert.AreEqual("64 house", sentMessage.Subject);
            Assert.AreEqual("goose live in 64 house", sentMessage.Body);
            Assert.IsTrue(clientDisposed);
            Assert.IsTrue(messageDisposed);
        }