How to use TestSetup method of B.Dummy class

Best Nunit code snippet using B.Dummy.TestSetup

SMTPClientTests.cs

Source:SMTPClientTests.cs Github

copy

Full Screen

...16 {17 _queue = _application.GlobalObjects.DeliveryQueue;18 _status = _application.Status;19 // add an account to send from20 _account = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "test@test.com", "test");21 }22 #endregion23 private DeliveryQueue _queue;24 private Status _status;25 private Account _account;26 internal static Route AddRoutePointingAtLocalhost(int numberOfTries, int port, bool treatSecurityAsLocal)27 {28 // Add a route pointing at localhost29 Settings oSettings = SingletonProvider<TestSetup>.Instance.GetApp().Settings;30 Route route = oSettings.Routes.Add();31 route.DomainName = "dummy-example.com";32 route.TargetSMTPHost = "localhost";33 route.TargetSMTPPort = port;34 route.NumberOfTries = numberOfTries;35 route.MinutesBetweenTry = 5;36 route.TreatRecipientAsLocalDomain = true;37 route.TreatSenderAsLocalDomain = true;38 route.Save();39 return route;40 }41 public static Route AddRoutePointingAtLocalhostMultipleHosts(int numberOfTries, int port)42 {43 // Add a route pointing at localhost44 Route route = AddRoutePointingAtLocalhost(numberOfTries, port, false);45 route.DomainName = "dummy-example.com";46 route.TargetSMTPHost = "localhost|localhost";47 route.TargetSMTPPort = port;48 route.NumberOfTries = numberOfTries;49 route.MinutesBetweenTry = 5;50 route.Save();51 return route;52 }53 [Test]54 [Description("Make sure that the bounce message doesn't include a SMTP auth password")]55 public void TestAuthFailurePasswordInBounce()56 {57 Assert.AreEqual(0, _status.UndeliveredMessages.Length);58 // No valid recipients...59 var deliveryResults = new Dictionary<string, int>();60 deliveryResults["test@dummy-example.com"] = 250;61 int smtpServerPort = TestSetup.GetNextFreePort();62 using (var server = new SMTPServerSimulator(1, smtpServerPort))63 {64 server.AddRecipientResult(deliveryResults);65 server.SimulatedError = SimulatedErrorType.ForceAuthenticationFailure;66 server.StartListen();67 // Add a route so we can connect to localhost.68 Route route = AddRoutePointingAtLocalhost(5, smtpServerPort, false);69 route.RelayerRequiresAuth = true;70 route.RelayerAuthUsername = "user@example.com";71 route.SetRelayerAuthPassword("MySecretPassword");72 // Send message to this route.73 var smtp = new SMTPClientSimulator();74 var recipients = new List<string>();75 recipients.Add("test@dummy-example.com");76 Assert.IsTrue(smtp.Send("test@test.com", recipients, "Test", "Test message"));77 // Wait for the client to disconnect.78 server.WaitForCompletion();79 TestSetup.AssertRecipientsInDeliveryQueue(0);80 string messageText = POP3Simulator.AssertGetFirstMessageText(_account.Address, "test");81 Assert.IsFalse(messageText.Contains("MySecretPassword"));82 Assert.IsTrue(messageText.Contains("<Password removed>"));83 }84 }85 [Test]86 [Description(87 "Test that hMailServer does not try to connect to itself, if a route is set up incorrectly or DNS records are pointing at localhost"88 )]89 public void TestDeliverToMyselfOnLocalPort()90 {91 Assert.AreEqual(0, _status.UndeliveredMessages.Length);92 // Add a route so we can conenct to localhost.93 AddRoutePointingAtLocalhost(1, 25, false);94 // Send message to this route.95 SMTPClientSimulator.StaticSend("test@test.com", "test@dummy-example.com", "subject", "body");96 for (int i = 0; i < 40; i++)97 {98 string s = _status.UndeliveredMessages;99 if (s.Contains("\t\ttest@test.com"))100 break;101 Thread.Sleep(250);102 }103 // Wait for the bounce message to be delivered.104 TestSetup.AssertRecipientsInDeliveryQueue(0, true);105 string message = POP3Simulator.AssertGetFirstMessageText("test@test.com", "test");106 Assert.IsTrue(message.Contains("this would mean connecting to myself."));107 }108 [Test]109 [Description("Issue 227, Restarting server doesn't refresh local address list")]110 public void TestDeliverToMyselfOnLocalPortAfterChangedLocalPort()111 {112 TCPIPPorts oPorts = _application.Settings.TCPIPPorts;113 for (int i = 0; i < oPorts.Count; i++)114 {115 TCPIPPort oTestPort = oPorts[i];116 if (oTestPort.Protocol == eSessionType.eSTIMAP)117 oTestPort.PortNumber = 14300;118 else if (oTestPort.Protocol == eSessionType.eSTSMTP)119 oTestPort.PortNumber = 11000;120 else if (oTestPort.Protocol == eSessionType.eSTPOP3)121 oTestPort.PortNumber = 2500;122 oTestPort.Save();123 }124 _application.Stop();125 _application.Start();126 // Set up a server and start listening.127 var deliveryResults = new Dictionary<string, int>();128 deliveryResults["user@dummy-example.com"] = 250;129 int smtpServerPort = TestSetup.GetNextFreePort();130 using (var server = new SMTPServerSimulator(1, smtpServerPort))131 {132 server.AddRecipientResult(deliveryResults);133 server.StartListen();134 // Add a route so we can connect to localhost.135 AddRoutePointingAtLocalhost(5, smtpServerPort, false);136 // Send message to this route.137 var sim = new SMTPClientSimulator(false, 11000);138 sim.Send("test@test.com", "user@dummy-example.com", "Test", "Test message");139 // Wait for the client to disconnect.140 server.WaitForCompletion();141 TestSetup.AssertRecipientsInDeliveryQueue(0, false);142 Assert.IsTrue(server.MessageData.Contains("Test message"));143 }144 oPorts.SetDefault();145 _application.Stop();146 _application.Start();147 }148 [Test]149 public void TestDeliverySuccess250Recipients()150 {151 Assert.AreEqual(0, _status.UndeliveredMessages.Length);152 // No valid recipients...153 var deliveryResults = new Dictionary<string, int>();154 for (int i = 0; i < 250; i++)155 deliveryResults["user" + i.ToString() + "@dummy-example.com"] = 250;156 int smtpServerPort = TestSetup.GetNextFreePort();157 using (var server = new SMTPServerSimulator(3, smtpServerPort))158 {159 server.AddRecipientResult(deliveryResults);160 server.AddRecipientResult(deliveryResults);161 server.AddRecipientResult(deliveryResults);162 server.StartListen();163 // Add a route so we can connect to localhost.164 AddRoutePointingAtLocalhost(5, smtpServerPort, false);165 // Send message to this route.166 var smtp = new SMTPClientSimulator();167 var recipients = new List<string>();168 for (int i = 0; i < 250; i++)169 recipients.Add("user" + i.ToString() + "@dummy-example.com");170 Assert.IsTrue(smtp.Send("test@test.com", recipients, "Test", "Test message"));171 // Wait for the client to disconnect.172 server.WaitForCompletion();173 TestSetup.AssertRecipientsInDeliveryQueue(0, false);174 Assert.IsTrue(server.MessageData.Contains("Test message"));175 }176 }177 [Test]178 public void TestDeliverySuccess50Recipients()179 {180 Assert.AreEqual(0, _status.UndeliveredMessages.Length);181 // No valid recipients...182 var deliveryResults = new Dictionary<string, int>();183 for (int i = 0; i < 50; i++)184 deliveryResults["user" + i.ToString() + "@dummy-example.com"] = 250;185 int smtpServerPort = TestSetup.GetNextFreePort();186 using (var server = new SMTPServerSimulator(1, smtpServerPort))187 {188 server.AddRecipientResult(deliveryResults);189 server.StartListen();190 // Add a route so we can conenct to localhost.191 AddRoutePointingAtLocalhost(5, smtpServerPort, false);192 // Send message to this route.193 var smtp = new SMTPClientSimulator();194 var recipients = new List<string>();195 for (int i = 0; i < 50; i++)196 recipients.Add("user" + i.ToString() + "@dummy-example.com");197 if (!smtp.Send("test@test.com", recipients, "Test", "Test message"))198 Assert.Fail("Delivery failed");199 // Wait for the client to disconnect.200 server.WaitForCompletion();201 TestSetup.AssertRecipientsInDeliveryQueue(0, false);202 Assert.IsTrue(server.MessageData.Contains("Test message"));203 }204 }205 [Test]206 [Description(207 "Test delivery to a server which accepts the message but then disconnects immediately before we've sent QUIT."208 )]209 public void TestDeliverySuccessDisconnectAfterMessageAccept()210 {211 Assert.AreEqual(0, _status.UndeliveredMessages.Length);212 // No valid recipients...213 var deliveryResults = new Dictionary<string, int>();214 deliveryResults["test@dummy-example.com"] = 250;215 int smtpServerPort = TestSetup.GetNextFreePort();216 using (var server = new SMTPServerSimulator(1, smtpServerPort))217 {218 server.AddRecipientResult(deliveryResults);219 server.StartListen();220 server.SimulatedError = SimulatedErrorType.DisconnectAfterMessageAccept;221 // Add a route so we can connect to localhost.222 AddRoutePointingAtLocalhost(5, smtpServerPort, false);223 // Send message to this route.224 var smtp = new SMTPClientSimulator();225 var recipients = new List<string>();226 recipients.Add("test@dummy-example.com");227 Assert.IsTrue(smtp.Send("test@test.com", recipients, "Test", "DisconnectAfterAcceptBeforeQuit"));228 // Wait for the client to disconnect.229 server.WaitForCompletion();230 TestSetup.AssertRecipientsInDeliveryQueue(0, false);231 Assert.IsTrue(server.MessageData.Contains("DisconnectAfterAcceptBeforeQuit"));232 }233 }234 [Test]235 public void TestDeliverySuccessSingleRecipient()236 {237 Assert.AreEqual(0, _status.UndeliveredMessages.Length);238 // No valid recipients...239 var deliveryResults = new Dictionary<string, int>();240 deliveryResults["test@dummy-example.com"] = 250;241 int smtpServerPort = TestSetup.GetNextFreePort();242 using (var server = new SMTPServerSimulator(1, smtpServerPort))243 {244 server.AddRecipientResult(deliveryResults);245 server.StartListen();246 // Add a route so we can connect to localhost.247 AddRoutePointingAtLocalhost(5, smtpServerPort, false);248 // Send message to this route.249 var smtp = new SMTPClientSimulator();250 var recipients = new List<string>();251 recipients.Add("test@dummy-example.com");252 Assert.IsTrue(smtp.Send("test@test.com", recipients, "Test", "Test message"));253 // Wait for the client to disconnect.254 server.WaitForCompletion();255 Assert.IsTrue(server.Conversation.Contains("\r\nDATA\r\n"));256 TestSetup.AssertRecipientsInDeliveryQueue(0, false);257 Assert.IsTrue(server.MessageData.Contains("Test message"));258 }259 }260 [Test]261 [Description("Test delivery to a server which accepts the message but does not respond to QUIT.")]262 public void TestDeliverySuccessSingleRecipientMissingQuitResponse()263 {264 Assert.AreEqual(0, _status.UndeliveredMessages.Length);265 // No valid recipients...266 var deliveryResults = new Dictionary<string, int>();267 deliveryResults["test@dummy-example.com"] = 250;268 int smtpServerPort = TestSetup.GetNextFreePort();269 using (var server = new SMTPServerSimulator(1, smtpServerPort))270 {271 server.AddRecipientResult(deliveryResults);272 server.StartListen();273 server.SimulatedError = SimulatedErrorType.DisconnectWithoutReplyOnQuit;274 // Add a route so we can connect to localhost.275 AddRoutePointingAtLocalhost(5, smtpServerPort, false);276 // Send message to this route.277 var smtp = new SMTPClientSimulator();278 var recipients = new List<string>();279 recipients.Add("test@dummy-example.com");280 Assert.IsTrue(smtp.Send("test@test.com", recipients, "Test", "DeliverySuccessNoQuitResponse"));281 // Wait for the client to disconnect.282 server.WaitForCompletion();283 TestSetup.AssertRecipientsInDeliveryQueue(0, false);284 Assert.IsTrue(server.MessageData.Contains("DeliverySuccessNoQuitResponse"));285 }286 }287 [Test]288 [Description("In this test, the server will respond with a permanent-error on the MAIL FROM command.")]289 public void TestErrorOnMailFrom()290 {291 Assert.AreEqual(0, _status.UndeliveredMessages.Length);292 // No valid recipients...293 var deliveryResults = new Dictionary<string, int>();294 deliveryResults["test@dummy-example.com"] = 250;295 int smtpServerPort = TestSetup.GetNextFreePort();296 using (var server = new SMTPServerSimulator(1, smtpServerPort))297 {298 server.MailFromResult = 561;299 server.AddRecipientResult(deliveryResults);300 server.StartListen();301 // Add a route so we can conenct to localhost.302 AddRoutePointingAtLocalhost(5, smtpServerPort, false);303 // Send message to this route.304 var smtp = new SMTPClientSimulator();305 var recipients = new List<string>();306 recipients.Add("test@dummy-example.com");307 Assert.IsTrue(smtp.Send("test@test.com", recipients, "Test", "Test message"));308 // Wait for the client to disconnect.309 server.WaitForCompletion();310 TestSetup.AssertRecipientsInDeliveryQueue(0);311 string bounceMessage = POP3Simulator.AssertGetFirstMessageText(_account.Address, "test");312 Assert.IsTrue(bounceMessage.Contains("MAIL FROM:<test@test.com>"));313 Assert.IsTrue(bounceMessage.Contains("Remote server replied: 561"));314 }315 }316 [Test]317 [Description("Test that an error after the message delivery has started causes a retry.")]318 public void TestFailureAfterConnect()319 {320 _application.Settings.SMTPNoOfTries = 3;321 _application.Settings.SMTPMinutesBetweenTry = 60;322 Assert.AreEqual(0, _status.UndeliveredMessages.Length);323 // No valid recipients...324 var deliveryResults = new Dictionary<string, int>();325 deliveryResults["test@dummy-example.com"] = 250;326 int smtpServerPort = TestSetup.GetNextFreePort();327 using (var server = new SMTPServerSimulator(1, smtpServerPort))328 {329 server.AddRecipientResult(deliveryResults);330 server.SimulatedError = SimulatedErrorType.DisconnectAfterSessionStart;331 server.StartListen();332 // Add a route so we can connect to localhost.333 AddRoutePointingAtLocalhost(1, smtpServerPort, false);334 // Send message to this route.335 var smtp = new SMTPClientSimulator();336 var recipients = new List<string>();337 recipients.Add("test@dummy-example.com");338 Assert.IsTrue(smtp.Send(_account.Address, recipients, "Test", "Test message"));339 // Wait for the client to disconnect.340 server.WaitForCompletion();341 // Force the message to be bounced.342 TestSetup.AssertRecipientsInDeliveryQueue(0, true);343 string bounce = POP3Simulator.AssertGetFirstMessageText(_account.Address, "test");344 Assert.IsTrue(bounce.Contains("test@dummy-example.com"));345 Assert.IsTrue(bounce.Contains("Remote server closed connection."));346 Assert.IsTrue(bounce.Contains("Tried 1 time(s)"));347 }348 }349 [Test]350 [Description("Test that an error after the message delivery completion doens't affect the delivery.")]351 public void TestFailureAfterDelivery()352 {353 _application.Settings.SMTPNoOfTries = 3;354 _application.Settings.SMTPMinutesBetweenTry = 60;355 Assert.AreEqual(0, _status.UndeliveredMessages.Length);356 // No valid recipients...357 var deliveryResults = new Dictionary<string, int>();358 deliveryResults["test@dummy-example.com"] = 250;359 int smtpServerPort = TestSetup.GetNextFreePort();360 using (var server = new SMTPServerSimulator(1, smtpServerPort))361 {362 server.AddRecipientResult(deliveryResults);363 server.QuitResult = 421;364 server.StartListen();365 // Add a route so we can connect to localhost.366 AddRoutePointingAtLocalhost(5, smtpServerPort, false);367 // Send message to this route.368 var smtp = new SMTPClientSimulator();369 var recipients = new List<string>();370 recipients.Add("test@dummy-example.com");371 Assert.IsTrue(smtp.Send("test@test.com", recipients, "Test", "test of error after accepted delivery"));372 // Wait for the client to disconnect.373 server.WaitForCompletion();374 TestSetup.AssertRecipientsInDeliveryQueue(0);375 Assert.IsTrue(server.MessageData.Contains("test of error after accepted delivery"));376 }377 }378 [Test]379 [Description("Test that an error after the message delivery has started causes a retry.")]380 public void TestFailureAfterDeliveryStarted()381 {382 _application.Settings.SMTPNoOfTries = 3;383 _application.Settings.SMTPMinutesBetweenTry = 60;384 Assert.AreEqual(0, _status.UndeliveredMessages.Length);385 // No valid recipients...386 var deliveryResults = new Dictionary<string, int>();387 deliveryResults["test@dummy-example.com"] = 250;388 int smtpServerPort = TestSetup.GetNextFreePort();389 using (var server = new SMTPServerSimulator(1, smtpServerPort))390 {391 server.AddRecipientResult(deliveryResults);392 server.SimulatedError = SimulatedErrorType.DisconnectAfterDeliveryStarted;393 server.StartListen();394 // Add a route so we can connect to localhost.395 AddRoutePointingAtLocalhost(1, smtpServerPort, false);396 // Send message to this route.397 var smtp = new SMTPClientSimulator();398 var recipients = new List<string>();399 recipients.Add("test@dummy-example.com");400 Assert.IsTrue(smtp.Send(_account.Address, recipients, "Test", "Test message"));401 // Wait for the client to disconnect.402 server.WaitForCompletion();403 // Force the message to be bounced.404 TestSetup.AssertRecipientsInDeliveryQueue(0, true);405 string bounce = POP3Simulator.AssertGetFirstMessageText(_account.Address, "test");406 Assert.IsTrue(bounce.Contains("test@dummy-example.com"));407 Assert.IsTrue(bounce.Contains("Remote server closed connection."));408 Assert.IsTrue(bounce.Contains("Tried 1 time(s)"));409 }410 }411 [Test]412 [Description("Test that an error after the message delivery has started causes a retry.")]413 public void TestFailureAfterReceivedHelloBanner()414 {415 _application.Settings.SMTPNoOfTries = 3;416 _application.Settings.SMTPMinutesBetweenTry = 60;417 Assert.AreEqual(0, _status.UndeliveredMessages.Length);418 // No valid recipients...419 var deliveryResults = new Dictionary<string, int>();420 deliveryResults["test@dummy-example.com"] = 250;421 int smtpServerPort = TestSetup.GetNextFreePort();422 using (var server = new SMTPServerSimulator(1, smtpServerPort))423 {424 server.AddRecipientResult(deliveryResults);425 server.SimulatedError = SimulatedErrorType.DisconnectAfterSessionStart;426 server.StartListen();427 // Add a route so we can connect to localhost.428 AddRoutePointingAtLocalhost(1, smtpServerPort, false);429 // Send message to this route.430 var smtp = new SMTPClientSimulator();431 var recipients = new List<string>();432 recipients.Add("test@dummy-example.com");433 Assert.IsTrue(smtp.Send(_account.Address, recipients, "Test", "Test message"));434 // Wait for the client to disconnect.435 server.WaitForCompletion();436 // Force the message to be bounced.437 TestSetup.AssertRecipientsInDeliveryQueue(0, true);438 string bounce = POP3Simulator.AssertGetFirstMessageText(_account.Address, "test");439 Assert.IsTrue(bounce.Contains("test@dummy-example.com"));440 Assert.IsTrue(bounce.Contains("Remote server closed connection."));441 Assert.IsTrue(bounce.Contains("Tried 1 time(s)"));442 }443 }444 [Test]445 public void TestFatalDeliveryFailure()446 {447 _application.Settings.Logging.Enabled = true;448 _application.Settings.Logging.LogSMTP = true;449 _application.Settings.Logging.LogTCPIP = true;450 _application.Settings.Logging.LogDebug = true;451 Assert.AreEqual(0, _status.UndeliveredMessages.Length);452 // No valid recipients...453 var deliveryResults = new Dictionary<string, int>();454 deliveryResults["test@dummy-example.com"] = 550;455 int smtpServerPort = TestSetup.GetNextFreePort();456 using (var server = new SMTPServerSimulator(1, smtpServerPort))457 {458 server.AddRecipientResult(deliveryResults);459 server.StartListen();460 // Add a route so we can conenct to localhost.461 AddRoutePointingAtLocalhost(0, smtpServerPort, false);462 // Send message to this route.463 var smtp = new SMTPClientSimulator();464 var recipients = new List<string>();465 recipients.Add("test@dummy-example.com");466 Assert.IsTrue(smtp.Send("test@test.com", recipients, "Test", "Test message"));467 string undeliveredMessages = _status.UndeliveredMessages;468 // Wait for the client to disconnect.469 server.WaitForCompletion();470 // wait for the message to be delivered.471 for (int i = 1; i <= 40; i++)472 {473 Assert.IsFalse(i == 40);474 if (_status.UndeliveredMessages.Contains("\ttest@test.com") || _status.UndeliveredMessages.Length == 0)475 break;476 Thread.Sleep(250);477 }478 TestSetup.AssertRecipientsInDeliveryQueue(0, true);479 string bounceMessage = POP3Simulator.AssertGetFirstMessageText(_account.Address, "test");480 Assert.IsTrue(bounceMessage.Contains("Remote server (127.0.0.1) issued an error."));481 Assert.IsTrue(bounceMessage.Contains("550 test@dummy-example.com"));482 }483 }484 /// <summary>485 /// 3 recipients. On the first server, 2 recipients are successful and one temp fail.486 /// On the second, the 3'rd one fails with a permanent error.487 /// </summary>488 [Test]489 public void TestMultipleHostsHalfDeliveryOnFirstPermanentOnSecond()490 {491 Assert.AreEqual(0, _status.UndeliveredMessages.Length);492 // No valid recipients...493 var deliveryResults = new Dictionary<string, int>();494 deliveryResults["user1@dummy-example.com"] = 250;495 deliveryResults["user2@dummy-example.com"] = 250;496 deliveryResults["user3@dummy-example.com"] = 450;497 var deliveryResultsSecond = new Dictionary<string, int>();498 deliveryResultsSecond["user1@dummy-example.com"] = 550;499 deliveryResultsSecond["user2@dummy-example.com"] = 550;500 deliveryResultsSecond["user3@dummy-example.com"] = 500;501 int smtpServerPort = TestSetup.GetNextFreePort();502 using (var server = new SMTPServerSimulator(2, smtpServerPort))503 {504 server.AddRecipientResult(deliveryResults);505 server.AddRecipientResult(deliveryResultsSecond);506 server.StartListen();507 // Add a route so we can connect to localhost.508 AddRoutePointingAtLocalhostMultipleHosts(1, smtpServerPort);509 // Send message to this route.510 var smtp = new SMTPClientSimulator();511 var recipients = new List<string>();512 recipients.Add("user1@dummy-example.com");513 recipients.Add("user2@dummy-example.com");514 recipients.Add("user3@dummy-example.com");515 Assert.IsTrue(smtp.Send("test@test.com", recipients, "Test", "Accepted message"));516 // Wait for the client to disconnect.517 server.WaitForCompletion();518 // Trigger a sending of the bounce message.519 TestSetup.AssertRecipientsInDeliveryQueue(0, true);520 string bounceMessage = POP3Simulator.AssertGetFirstMessageText(_account.Address, "test");521 Assert.IsFalse(bounceMessage.Contains("RCPT TO:<user1@dummy-example.com>"));522 Assert.IsFalse(bounceMessage.Contains("RCPT TO:<user2@dummy-example.com>"));523 Assert.IsTrue(bounceMessage.Contains("RCPT TO:<user3@dummy-example.com>"));524 Assert.IsTrue(bounceMessage.Contains("500 user3@dummy-example.com"));525 }526 }527 [Test]528 public void TestMultipleHostsTemporaryFailure()529 {530 Assert.AreEqual(0, _status.UndeliveredMessages.Length);531 // No valid recipients...532 var deliveryResults = new Dictionary<string, int>();533 deliveryResults["user1@dummy-example.com"] = 250;534 deliveryResults["user2@dummy-example.com"] = 250;535 deliveryResults["user3@dummy-example.com"] = 499;536 int smtpServerPort = TestSetup.GetNextFreePort();537 using (var server = new SMTPServerSimulator(2, smtpServerPort))538 {539 server.AddRecipientResult(deliveryResults);540 server.AddRecipientResult(deliveryResults);541 server.StartListen();542 // Add a route so we can connect to localhost.543 Route route = AddRoutePointingAtLocalhostMultipleHosts(2, smtpServerPort);544 // Send message to this route.545 var smtp = new SMTPClientSimulator();546 var recipients = new List<string>();547 recipients.Add("user1@dummy-example.com");548 recipients.Add("user2@dummy-example.com");549 recipients.Add("user3@dummy-example.com");550 if (!smtp.Send("test@test.com", recipients, "Test", "Test message"))551 Assert.Fail("Delivery failed");552 // Wait for the client to disconnect.553 server.WaitForCompletion();554 }555 TestSetup.AssertRecipientsInDeliveryQueue(1);556 // Check so that only user 3 remains in the queue.557 Assert.AreEqual(-1, _status.UndeliveredMessages.IndexOf("user1@dummy-example.com"));558 Assert.AreEqual(-1, _status.UndeliveredMessages.IndexOf("user2@dummy-example.com"));559 Assert.AreNotEqual(-1, _status.UndeliveredMessages.IndexOf("user3@dummy-example.com"));560 using (var server = new SMTPServerSimulator(2, smtpServerPort))561 {562 server.AddRecipientResult(deliveryResults);563 server.AddRecipientResult(deliveryResults);564 server.StartListen();565 TestSetup.AssertRecipientsInDeliveryQueue(0, true);566 server.WaitForCompletion();567 string bounceMessage = POP3Simulator.AssertGetFirstMessageText("test@test.com", "test");568 Assert.IsFalse(bounceMessage.Contains("user1@dummy-example.com"));569 Assert.IsFalse(bounceMessage.Contains("user2@dummy-example.com"));570 Assert.IsTrue(bounceMessage.Contains("user3@dummy-example.com"));571 Assert.IsTrue(bounceMessage.Contains("499 user3@dummy-example.com"));572 Assert.IsTrue(bounceMessage.Contains("Tried 2 time(s)"));573 }574 }575 [Test]576 public void TestMultipleHostsTemporaryFailureDeliveryOnSecondServer()577 {578 Assert.AreEqual(0, _status.UndeliveredMessages.Length);579 // No valid recipients...580 var deliveryResults = new Dictionary<string, int>();581 deliveryResults["user1@dummy-example.com"] = 250;582 deliveryResults["user2@dummy-example.com"] = 250;583 deliveryResults["user3@dummy-example.com"] = 450;584 var deliveryResultsSecond = new Dictionary<string, int>();585 deliveryResultsSecond["user1@dummy-example.com"] = 250;586 deliveryResultsSecond["user2@dummy-example.com"] = 250;587 deliveryResultsSecond["user3@dummy-example.com"] = 250;588 int smtpServerPort = TestSetup.GetNextFreePort();589 using (var server = new SMTPServerSimulator(2, smtpServerPort))590 {591 server.AddRecipientResult(deliveryResults);592 server.AddRecipientResult(deliveryResultsSecond);593 server.StartListen();594 // Add a route so we can connect to localhost.595 AddRoutePointingAtLocalhostMultipleHosts(2, smtpServerPort);596 // Send message to this route.597 var smtp = new SMTPClientSimulator();598 var recipients = new List<string>();599 recipients.Add("user1@dummy-example.com");600 recipients.Add("user2@dummy-example.com");601 recipients.Add("user3@dummy-example.com");602 if (!smtp.Send("test@test.com", recipients, "Test", "Accepted message"))603 Assert.Fail("Delivery failed");604 // Wait for the client to disconnect.605 server.WaitForCompletion();606 // Trigger a sending of the bounce message.607 TestSetup.AssertRecipientsInDeliveryQueue(0);608 Assert.IsTrue(server.MessageData.Contains("Accepted message"));609 }610 }611 [Test]612 public void TestPartialTemporaryErrorFailure()613 {614 Assert.AreEqual(0, _status.UndeliveredMessages.Length);615 // No valid recipients...616 var deliveryResults = new Dictionary<string, int>();617 deliveryResults["user1@dummy-example.com"] = 250;618 deliveryResults["user2@dummy-example.com"] = 250;619 deliveryResults["user3@dummy-example.com"] = 400;620 int smtpServerPort = TestSetup.GetNextFreePort();621 using (var server = new SMTPServerSimulator(1, smtpServerPort))622 {623 server.AddRecipientResult(deliveryResults);624 server.StartListen();625 // Add a route so we can connect to localhost.626 AddRoutePointingAtLocalhost(2, smtpServerPort, false);627 // Send message to this route.628 var smtp = new SMTPClientSimulator();629 var recipients = new List<string>();630 recipients.Add("user1@dummy-example.com");631 recipients.Add("user2@dummy-example.com");632 recipients.Add("user3@dummy-example.com");633 if (!smtp.Send("test@test.com", recipients, "Test", "Test message"))634 Assert.Fail("Delivery failed");635 // Wait for the client to disconnect.636 server.WaitForCompletion();637 // Trigger a sending of the bounce message.638 TestSetup.AssertRecipientsInDeliveryQueue(1);639 Assert.AreEqual(-1, _status.UndeliveredMessages.IndexOf("user1@dummy-example.com"));640 Assert.AreEqual(-1, _status.UndeliveredMessages.IndexOf("user2@dummy-example.com"));641 Assert.AreNotEqual(-1, _status.UndeliveredMessages.IndexOf("user3@dummy-example.com"));642 Assert.IsTrue(server.MessageData.Contains("Test message"));643 }644 // Attempt to deliver the message again.645 using (var server = new SMTPServerSimulator(1, smtpServerPort))646 {647 server.AddRecipientResult(deliveryResults);648 server.StartListen();649 TestSetup.SendMessagesInQueue();650 server.WaitForCompletion();651 // 652 TestSetup.AssertRecipientsInDeliveryQueue(0);653 string bounceMessage = POP3Simulator.AssertGetFirstMessageText("test@test.com", "test");654 Assert.IsTrue(bounceMessage.Contains("400 user3@dummy-example.com"));655 Assert.IsTrue(bounceMessage.Contains("Tried 2 time(s)"));656 Assert.IsFalse(bounceMessage.Contains("user2@dummy-example.com"));657 Assert.IsFalse(bounceMessage.Contains("user1@dummy-example.com"));658 }659 }660 [Test]661 public void TestPermanentFailure()662 {663 Assert.AreEqual(0, _status.UndeliveredMessages.Length);664 var deliveryResults = new Dictionary<string, int>();665 deliveryResults["test@dummy-example.com"] = 542;666 int smtpServerPort = TestSetup.GetNextFreePort();667 using (var server = new SMTPServerSimulator(1, smtpServerPort))668 {669 server.AddRecipientResult(deliveryResults);670 server.StartListen();671 // Add a route so we can connect to localhost.672 AddRoutePointingAtLocalhost(5, smtpServerPort, false);673 // Send message to this route.674 var smtp = new SMTPClientSimulator();675 var recipients = new List<string>();676 recipients.Add("test@dummy-example.com");677 Assert.IsTrue(smtp.Send("test@test.com", recipients, "Test", "Test message"));678 // Wait for the client to disconnect.679 server.WaitForCompletion();680 Assert.IsFalse(server.Conversation.Contains("\r\nDATA\r\n"));681 }682 TestSetup.AssertRecipientsInDeliveryQueue(0);683 string bounce = POP3Simulator.AssertGetFirstMessageText(_account.Address, "test");684 Assert.IsTrue(bounce.Contains("Remote server replied: 542 test@dummy-example.com"));685 }686 [Test]687 [Description("Tests that the SMTP client times out after 10 minutes.")]688 [Ignore]689 public void TestSMTPClientTimeout()690 {691 Assert.AreEqual(0, _status.UndeliveredMessages.Length);692 // No valid recipients...693 var deliveryResults = new Dictionary<string, int>();694 deliveryResults["test@dummy-example.com"] = 250;695 int smtpServerPort = TestSetup.GetNextFreePort();696 using (var server = new SMTPServerSimulator(1, smtpServerPort))697 {698 server.AddRecipientResult(deliveryResults);699 server.SimulatedError = SimulatedErrorType.Sleep15MinutesAfterSessionStart;700 server.SecondsToWaitBeforeTerminate = 20*60;701 server.StartListen();702 // Add a route so we can connect to localhost.703 Route route = AddRoutePointingAtLocalhost(5, smtpServerPort, false);704 route.RelayerRequiresAuth = true;705 route.RelayerAuthUsername = "user@example.com";706 route.SetRelayerAuthPassword("MySecretPassword");707 // Send message to this route.708 var smtp = new SMTPClientSimulator();709 var recipients = new List<string>();710 recipients.Add("test@dummy-example.com");711 Assert.IsTrue(smtp.Send("test@test.com", recipients, "Test", "Test message"));712 // Wait for the client to disconnect.713 server.WaitForCompletion();714 }715 TestSetup.AssertRecipientsInDeliveryQueue(0);716 }717 [Test]718 public void TestTemporaryFailure()719 {720 Assert.AreEqual(0, _status.UndeliveredMessages.Length);721 var deliveryResults = new Dictionary<string, int>();722 deliveryResults["test@dummy-example.com"] = 452;723 int smtpServerPort = TestSetup.GetNextFreePort();724 using (var server = new SMTPServerSimulator(1, smtpServerPort))725 {726 server.AddRecipientResult(deliveryResults);727 server.StartListen();728 // Add a route so we can connect to localhost.729 AddRoutePointingAtLocalhost(2, smtpServerPort, false);730 // Send message to this route.731 var smtp = new SMTPClientSimulator();732 var recipients = new List<string>();733 recipients.Add("test@dummy-example.com");734 Assert.IsTrue(smtp.Send("test@test.com", recipients, "Test", "Test message"));735 // Wait for the client to disconnect.736 server.WaitForCompletion();737 Assert.AreNotEqual(0, _status.UndeliveredMessages.Length);738 }739 740 using (var server = new SMTPServerSimulator(1, smtpServerPort))// Start to listen again.741 {742 server.AddRecipientResult(deliveryResults);743 server.StartListen();744 // Deliver the message to the server and then deliver the bounce message.745 TestSetup.AssertRecipientsInDeliveryQueue(0, true);746 // Wait for the client to disconnect.747 server.WaitForCompletion();748 Assert.IsFalse(server.Conversation.Contains("\r\nDATA\r\n"));749 }750 // Now the message has bounced.751 string message = POP3Simulator.AssertGetFirstMessageText(_account.Address, "test");752 Assert.IsTrue(message.Contains("452 test@dummy-example.com"));753 Assert.IsTrue(message.Contains("Tried 2 time(s)"));754 }755 }756}...

Full Screen

Full Screen

SMTPAuthentication.cs

Source:SMTPAuthentication.cs Github

copy

Full Screen

...21 [Description("Local to local")]22 public void TestBlockingDeliveries()23 {24 SecurityRange range =25 SingletonProvider<TestSetup>.Instance.GetApp().Settings.SecurityRanges.get_ItemByName("My computer");26 range.RequireSMTPAuthLocalToLocal = false;27 range.RequireSMTPAuthLocalToExternal = false;28 range.RequireSMTPAuthExternalToLocal = false;29 range.RequireSMTPAuthExternalToExternal = false;30 range.AllowDeliveryFromLocalToLocal = false;31 range.AllowDeliveryFromLocalToRemote = false;32 range.AllowDeliveryFromRemoteToLocal = false;33 range.AllowDeliveryFromRemoteToRemote = false;34 range.Save();35 Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "test@test.com", "test");36 var oSMTP = new SMTPClientSimulator();37 string result1, result2, result3, result4;38 Assert.IsFalse(oSMTP.Send(account1.Address, account1.Address, "Mail 1", "Mail 1", out result1));39 Assert.IsFalse(oSMTP.Send(account1.Address, "externaladdress@gmail.com", "Mail 1", "Mail 1", out result2));40 Assert.IsFalse(oSMTP.Send("externaladdress@gmail.com", account1.Address, "Mail 1", "Mail 1", out result3));41 Assert.IsFalse(oSMTP.Send("externaladdress@gmail.com", "externaladdress@gmail.com", "Mail 1", "Mail 1",42 out result4));43 Assert.IsTrue(result1.Contains("550 Delivery is not allowed to this address."));44 Assert.IsTrue(result2.Contains("550 Delivery is not allowed to this address."));45 Assert.IsTrue(result3.Contains("550 Delivery is not allowed to this address."));46 Assert.IsTrue(result4.Contains("550 Delivery is not allowed to this address."));47 }48 [Test]49 [Description("Test require SMTP auth is not happening if we're trying to deliver from external to external.")]50 public void TestSMTPAuthExternalToExternal()51 {52 SecurityRange range =53 SingletonProvider<TestSetup>.Instance.GetApp().Settings.SecurityRanges.get_ItemByName("My computer");54 range.RequireSMTPAuthExternalToExternal = true;55 range.Save();56 var oSMTP = new SMTPClientSimulator();57 string result;58 Assert.IsFalse(oSMTP.Send("externaladdress@example.com", "someexternaladdress@example.com", "Mail 1",59 "Mail 1", out result));60 Assert.IsTrue(result.Contains("SMTP authentication is required."));61 range.RequireSMTPAuthExternalToExternal = false;62 range.AllowDeliveryFromRemoteToRemote = false;63 range.Save();64 Assert.IsFalse(oSMTP.Send("externaladdress@example.com", "someexternaladdress@example.com", "Mail 1",65 "Mail 1", out result));66 Assert.IsTrue(result.Contains("550 Delivery is not allowed to this address."));67 }68 [Test]69 [Description("External to local")]70 public void TestSMTPAuthExternalToLocal()71 {72 SecurityRange range =73 SingletonProvider<TestSetup>.Instance.GetApp().Settings.SecurityRanges.get_ItemByName("My computer");74 range.RequireSMTPAuthExternalToLocal = true;75 range.Save();76 Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "test@test.com", "test");77 var oSMTP = new SMTPClientSimulator();78 Assert.IsFalse(oSMTP.Send("someexternaladdress@example.com", account1.Address, "Mail 1", "Mail 1"));79 range.RequireSMTPAuthExternalToLocal = false;80 range.Save();81 Assert.IsTrue(oSMTP.Send("someexternaladdress@example.com", account1.Address, "Mail 1", "Mail 1"));82 TestSetup.AssertRecipientsInDeliveryQueue(0);83 }84 [Test]85 [Description("External to route configured to be treated as local")]86 public void TestSMTPAuthExternalToRouteConfiguredAsLocal()87 {88 // First, make sure hMailServer requires auth.89 SecurityRange range =90 SingletonProvider<TestSetup>.Instance.GetApp().Settings.SecurityRanges.get_ItemByName("My computer");91 range.RequireSMTPAuthExternalToExternal = true;92 range.RequireSMTPAuthLocalToExternal = true;93 range.Save();94 int smtpServerPort = TestSetup.GetNextFreePort();95 Route route = SMTPClientTests.AddRoutePointingAtLocalhost(1, smtpServerPort, false);96 route.TreatRecipientAsLocalDomain = true;97 route.TreatSenderAsLocalDomain = true;98 route.Save();99 // Set up the simulating server to listen.100 var deliveryResults = new Dictionary<string, int>();101 deliveryResults["dummy@dummy-example.com"] = 250;102 103 using (var server = new SMTPServerSimulator(1, smtpServerPort))104 {105 server.AddRecipientResult(deliveryResults);106 server.StartListen();107 // Make sure we can't send to this route without using smtp auth.108 var oSMTP = new SMTPClientSimulator();109 Assert.IsTrue(oSMTP.Send("someexternaladdress@example.com", "dummy@dummy-example.com", "Mail 1", "Mail 1"));110 server.WaitForCompletion();111 112 Assert.IsTrue(server.MessageData.Contains("Mail 1"), server.MessageData);113 }114 }115 [Test]116 [Description("Test require SMTP auth when sending from local address to external address.")]117 public void TestSMTPAuthLocalToExternal()118 {119 SecurityRange range =120 SingletonProvider<TestSetup>.Instance.GetApp().Settings.SecurityRanges.get_ItemByName("My computer");121 range.RequireSMTPAuthLocalToExternal = true;122 range.Save();123 Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "test@test.com", "test");124 var oSMTP = new SMTPClientSimulator();125 string result;126 Assert.IsFalse(oSMTP.Send(account1.Address, "someexternaladdress@example.com", "Mail 1", "Mail 1",127 out result));128 Assert.IsTrue(result.Contains("SMTP authentication is required"));129 }130 [Test]131 [Description("Local to local")]132 public void TestSMTPAuthLocalToLocal()133 {134 SecurityRange range =135 SingletonProvider<TestSetup>.Instance.GetApp().Settings.SecurityRanges.get_ItemByName("My computer");136 range.RequireSMTPAuthLocalToLocal = true;137 range.Save();138 Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "test@test.com", "test");139 var oSMTP = new SMTPClientSimulator();140 string result;141 Assert.IsFalse(oSMTP.Send(account1.Address, account1.Address, "Mail 1", "Mail 1", out result));142 Assert.IsTrue(result.Contains("SMTP authentication is required."));143 range.RequireSMTPAuthLocalToLocal = false;144 range.Save();145 Assert.IsTrue(oSMTP.Send(account1.Address, account1.Address, "Mail 1", "Mail 1", out result));146 TestSetup.AssertRecipientsInDeliveryQueue(0);147 }148 [Test]149 [Description(150 "Test option TestSenderAsLocalDomain. Attempt to send a message from a route configured as external domain to a local account account. Should fail, since SMTP auth is required."151 )]152 public void TestSenderAsExternalDomainSendToLocalAccountFail()153 {154 int smtpServerPort = TestSetup.GetNextFreePort();155 Route route = SMTPClientTests.AddRoutePointingAtLocalhost(1, smtpServerPort, false);156 route.TreatSenderAsLocalDomain = false;157 route.Save();158 SecurityRange range =159 SingletonProvider<TestSetup>.Instance.GetApp().Settings.SecurityRanges.get_ItemByName("My computer");160 range.RequireSMTPAuthExternalToLocal = true;161 range.Save();162 Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "sales@test.com", "test");163 var oSMTP = new SMTPClientSimulator();164 string result;165 Assert.IsFalse(oSMTP.Send("someone@dummy-example.com", account1.Address, "Mail 1", "Mail 1", out result));166 }167 [Test]168 [Description(169 "Test option TestSenderAsLocalDomain. Attempt to send a message from a route configured as external domain to a local account account. Should succeed."170 )]171 public void TestSenderAsExternalDomainSendToLocalAccountPass()172 {173 int smtpServerPort = TestSetup.GetNextFreePort();174 Route route = SMTPClientTests.AddRoutePointingAtLocalhost(1, smtpServerPort, false);175 route.TreatSenderAsLocalDomain = false;176 route.Save();177 SecurityRange range =178 SingletonProvider<TestSetup>.Instance.GetApp().Settings.SecurityRanges.get_ItemByName("My computer");179 range.RequireSMTPAuthLocalToLocal = true;180 range.Save();181 Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "sales@test.com", "test");182 var oSMTP = new SMTPClientSimulator();183 string result;184 Assert.IsTrue(oSMTP.Send("someone@dummy-example.com", account1.Address, "Mail 1", "Mail 1", out result));185 string text = POP3Simulator.AssertGetFirstMessageText(account1.Address, "test");186 Assert.IsTrue(text.Contains("Mail 1"));187 }188 [Test]189 [Description(190 "Test option TestSenderAsLocalDomain. Attempt to send a message from a route configured as local domain to an external account. Should fail, since SMTP auth is required."191 )]192 public void TestSenderAsLocalDomainSendToExternal()193 {194 int smtpServerPort = TestSetup.GetNextFreePort();195 Route route = SMTPClientTests.AddRoutePointingAtLocalhost(1, smtpServerPort, false);196 route.TreatSenderAsLocalDomain = true;197 route.Save();198 SecurityRange range =199 SingletonProvider<TestSetup>.Instance.GetApp().Settings.SecurityRanges.get_ItemByName("My computer");200 range.RequireSMTPAuthLocalToExternal = true;201 range.Save();202 var oSMTP = new SMTPClientSimulator();203 string result;204 Assert.IsFalse(oSMTP.Send("someone@dummy-example.com", "test@example.com", "Mail 1", "Mail 1", out result));205 Assert.IsTrue(result.Contains("530 SMTP authentication is required."));206 }207 [Test]208 [Description(209 "Test option TestSenderAsLocalDomain. Attempt to send a message from a route configured as local domain to a local account account. Should fail, since SMTP auth is required."210 )]211 public void TestSenderAsLocalDomainSendToLocalAccount()212 {213 int smtpServerPort = TestSetup.GetNextFreePort();214 Route route = SMTPClientTests.AddRoutePointingAtLocalhost(1, smtpServerPort, false);215 route.TreatSenderAsLocalDomain = true;216 route.Save();217 SecurityRange range =218 SingletonProvider<TestSetup>.Instance.GetApp().Settings.SecurityRanges.get_ItemByName("My computer");219 range.RequireSMTPAuthLocalToLocal = true;220 range.Save();221 Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "sales@test.com", "test");222 var oSMTP = new SMTPClientSimulator();223 string result;224 Assert.IsFalse(oSMTP.Send("someone@dummy-example.com", account1.Address, "Mail 1", "Mail 1", out result));225 Assert.IsTrue(result.Contains("530 SMTP authentication is required."));226 }227 [Test]228 [Description(229 "Test option TestSenderAsLocalDomain. Attempt to send a message from a route configured as local domain to a local account account. Should succeed."230 )]231 public void TestSenderAsLocalDomainSendToLocalAccountPass()232 {233 int smtpServerPort = TestSetup.GetNextFreePort();234 Route route = SMTPClientTests.AddRoutePointingAtLocalhost(1, smtpServerPort, false);235 route.TreatSenderAsLocalDomain = true;236 route.Save();237 SecurityRange range =238 SingletonProvider<TestSetup>.Instance.GetApp().Settings.SecurityRanges.get_ItemByName("My computer");239 range.RequireSMTPAuthLocalToLocal = false;240 range.Save();241 Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "sales@test.com", "test");242 var oSMTP = new SMTPClientSimulator();243 string result;244 Assert.IsTrue(oSMTP.Send("someone@dummy-example.com", account1.Address, "Mail 1", "Mail 1", out result));245 string text = POP3Simulator.AssertGetFirstMessageText(account1.Address, "test");246 Assert.IsTrue(text.Contains("Mail 1"));247 }248 [Test]249 [Description("Use case 3: Delivery from external user to route (backup MX case).")]250 public void TestUseCase3DeliveryFromExternalUserToPrimaryViaBackup()251 {252 int smtpServerPort = TestSetup.GetNextFreePort();253 Route route = SMTPClientTests.AddRoutePointingAtLocalhost(1, smtpServerPort, false);254 route.TreatRecipientAsLocalDomain = true;255 route.TreatSenderAsLocalDomain = false;256 route.Save();257 var deliveryResults = new Dictionary<string, int>();258 deliveryResults["test@dummy-example.com"] = 250;259 using (var server = new SMTPServerSimulator(1, smtpServerPort))260 {261 server.AddRecipientResult(deliveryResults);262 server.StartListen();263 var oSMTP = new SMTPClientSimulator();264 string result;265 Assert.IsTrue(oSMTP.Send("someone@example.com", "test@dummy-example.com", "Mail 1", "Mail 1", out result));266 server.WaitForCompletion();267 server.MessageData.Contains("Mail 1");268 }269 }270 [Test]271 [Description(272 "Use case 2: Delivery from primary MX to backup server. For example someone@customer to someone@hoster.")]273 public void TestUseCaseDeliveryFromPrimaryMXToBackupMX()274 {275 int smtpServerPort = TestSetup.GetNextFreePort();276 Route route = SMTPClientTests.AddRoutePointingAtLocalhost(1, smtpServerPort, false);277 route.TreatRecipientAsLocalDomain = true;278 route.TreatSenderAsLocalDomain = false;279 route.Save();280 Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "sales@test.com", "test");281 var oSMTP = new SMTPClientSimulator();282 string result;283 Assert.IsTrue(oSMTP.Send("someone@dummy-example.com", account1.Address, "Mail 1", "Mail 1", out result));284 string text = POP3Simulator.AssertGetFirstMessageText(account1.Address, "test");285 Assert.IsTrue(text.Contains("Mail 1"));286 }287 [Test]288 [Description(289 "Use case 1: Delivery from internal users to a route configured as local. SMTP-auth should not be required."290 )]291 public void TestUseCaseDeliveryToLocalRoute()292 {293 int smtpServerPort = TestSetup.GetNextFreePort();294 Route route = SMTPClientTests.AddRoutePointingAtLocalhost(1, smtpServerPort, false);295 route.TreatRecipientAsLocalDomain = true;296 route.TreatSenderAsLocalDomain = false;297 route.Save();298 var deliveryResults = new Dictionary<string, int>();299 deliveryResults["test@dummy-example.com"] = 250;300 using (var server = new SMTPServerSimulator(1, smtpServerPort))301 {302 server.AddRecipientResult(deliveryResults);303 server.StartListen();304 var oSMTP = new SMTPClientSimulator();305 string result;306 Assert.IsTrue(oSMTP.Send("someone@dummy-example.com", "test@dummy-example.com", "Mail 1", "Mail 1",307 out result));308 server.WaitForCompletion();309 server.MessageData.Contains("Mail 1");310 }311 }312 [Test]313 [Description(314 "Test option TreatRecipientAsLocalDomain. Attempt to send message from external account to route configured as exernal. Should fail."315 )]316 public void TreatRecipientAsExternalDomain()317 {318 int smtpServerPort = TestSetup.GetNextFreePort();319 Route route = SMTPClientTests.AddRoutePointingAtLocalhost(1, smtpServerPort, false);320 route.TreatRecipientAsLocalDomain = false;321 route.Save();322 var oSMTP = new SMTPClientSimulator();323 string result;324 Assert.IsFalse(oSMTP.Send("someone@example.com", "test@dummy-example.com", "Mail 1", "Mail 1", out result));325 Assert.IsTrue(result.Contains("530 SMTP authentication is required."));326 }327 [Test]328 [Description(329 "Test option TreatRecipientAsLocalDomain. Attempt to send message from external account to route configured as exernal. Should succeed, since it's permitted by IP range."330 )]331 public void TreatRecipientAsExternalDomainPermitted()332 {333 int smtpServerPort = TestSetup.GetNextFreePort();334 Route route = SMTPClientTests.AddRoutePointingAtLocalhost(1, smtpServerPort, false);335 route.TreatRecipientAsLocalDomain = false;336 route.Save();337 SecurityRange range =338 SingletonProvider<TestSetup>.Instance.GetApp().Settings.SecurityRanges.get_ItemByName("My computer");339 range.AllowDeliveryFromRemoteToRemote = true;340 range.RequireSMTPAuthExternalToExternal = false;341 range.Save();342 var deliveryResults = new Dictionary<string, int>();343 deliveryResults["test@dummy-example.com"] = 250;344 using (var server = new SMTPServerSimulator(1, smtpServerPort))345 {346 server.AddRecipientResult(deliveryResults);347 server.StartListen();348 var oSMTP = new SMTPClientSimulator();349 string result;350 Assert.IsTrue(oSMTP.Send("someone@example.com", "test@dummy-example.com", "Mail 1", "Mail 1", out result));351 server.WaitForCompletion();352 server.MessageData.Contains("Mail 1");353 }354 }355 [Test]356 [Description(357 "Test option TreatRecipientAsLocalDomain. Attempt to send message from external account to route configured as local. Should succeed."358 )]359 public void TreatRecipientAsLocalDomain()360 {361 int smtpServerPort = TestSetup.GetNextFreePort();362 Route route = SMTPClientTests.AddRoutePointingAtLocalhost(1, smtpServerPort, false);363 route.TreatRecipientAsLocalDomain = true;364 route.Save();365 var deliveryResults = new Dictionary<string, int>();366 deliveryResults["test@dummy-example.com"] = 250;367 using (var server = new SMTPServerSimulator(1, smtpServerPort))368 {369 server.AddRecipientResult(deliveryResults);370 server.StartListen();371 var oSMTP = new SMTPClientSimulator();372 string result;373 Assert.IsTrue(oSMTP.Send("someone@example.com", "test@dummy-example.com", "Mail 1", "Mail 1", out result));374 server.WaitForCompletion();375 server.MessageData.Contains("Mail 1");...

Full Screen

Full Screen

DistributionLists.cs

Source:DistributionLists.cs Github

copy

Full Screen

...14 public void TestDistributionListAnnouncementFromDomainAlias()15 {16 var oIMAP = new IMAPSimulator();17 var oSMTP = new SMTPClientSimulator();18 Application application = SingletonProvider<TestSetup>.Instance.GetApp();19 // 20 // TEST LIST SECURITY IN COMBINATION WITH DOMAIN NAME ALIASES21 // 22 Account account = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "test@test.com", "test");23 var oRecipients = new List<string>();24 oRecipients.Add("test@dummy-example.com");25 DistributionList oList3 = SingletonProvider<TestSetup>.Instance.AddDistributionList(_domain, "list@test.com",26 oRecipients);27 oList3.Mode = eDistributionListMode.eLMAnnouncement;28 oList3.RequireSenderAddress = "test@dummy-example.com";29 oList3.Save();30 // THIS MESSAGE SHOULD FAIL31 Assert.IsFalse(oSMTP.Send("test@test.com", "list@test.com", "Mail 1", "Mail 1"));32 DomainAlias oDA = _domain.DomainAliases.Add();33 oDA.AliasName = "dummy-example.com";34 oDA.Save();35 // THIS MESSAGE SHOULD SUCCEED36 Assert.IsTrue(oSMTP.Send("test@dummy-example.com", "list@dummy-example.com", "Mail 1", "Mail 1"));37 IMAPSimulator.AssertMessageCount("test@dummy-example.com", "test", "Inbox", 1);38 }39 [Test]40 public void TestDistributionListPointingAtItself()41 {42 // Add distribution list43 var oRecipients = new List<string>();44 oRecipients.Add("recipient1@test.com");45 oRecipients.Add("recipient2@test.com");46 oRecipients.Add("recipient4@test.com");47 oRecipients.Add("list1@test.com");48 SingletonProvider<TestSetup>.Instance.AddDistributionList(_domain, "list1@test.com", oRecipients);49 Account account = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "test@test.com", "test");50 SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "recipient1@test.com", "test");51 SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "recipient2@test.com", "test");52 SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "recipient3@test.com", "test");53 SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "recipient4@test.com", "test");54 Assert.IsTrue(SMTPClientSimulator.StaticSend("test@test.com", "list1@test.com", "Mail 1", "Mail 1"));55 IMAPSimulator.AssertMessageCount("recipient1@test.com", "test", "Inbox", 1);56 IMAPSimulator.AssertMessageCount("recipient2@test.com", "test", "Inbox", 1);57 IMAPSimulator.AssertMessageCount("recipient4@test.com", "test", "Inbox", 1);58 }59 [Test]60 public void TestDistributionListWithEmptyAddress()61 {62 // Add distribution list63 var oRecipients = new List<string>();64 oRecipients.Add("recipient1@test.com");65 oRecipients.Add("recipient2@test.com");66 oRecipients.Add("");67 oRecipients.Add("recipient4@test.com");68 try69 {70 SingletonProvider<TestSetup>.Instance.AddDistributionList(_domain, "list1@test.com", oRecipients);71 }72 catch (Exception ex)73 {74 Assert.IsTrue(ex.Message.Contains("The recipient address is empty"));75 return;76 }77 Assert.Fail("No error reported when creating distribution list recipient with empty address");78 }79 [Test]80 public void TestDistributionLists()81 {82 // Fetch default domain83 // Add distribution list84 var oRecipients = new List<string>();85 oRecipients.Add("recipient1@test.com");86 oRecipients.Add("recipient2@test.com");87 oRecipients.Add("recipient3@test.com");88 SingletonProvider<TestSetup>.Instance.AddDistributionList(_domain, "list1@test.com", oRecipients);89 Account oAccount;90 oAccount = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "recipient1@test.com", "test");91 oAccount = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "recipient2@test.com", "test");92 oAccount = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "recipient3@test.com", "test");93 // Add alias pointing at the distribution list.94 SingletonProvider<TestSetup>.Instance.AddAlias(_domain, "listalias@test.com", "list1@test.com");95 var oSMTP = new SMTPClientSimulator();96 Assert.IsTrue(oSMTP.Send("test@test.com", "list1@test.com", "Mail 1", "Mail 1"));97 Assert.IsTrue(oSMTP.Send("test@test.com", "listalias@test.com", "Mail 2", "Mail 2"));98 Assert.IsTrue(oSMTP.Send("test@test.com", "listalias@test.com", "Mail 3", "Mail 3"));99 IMAPSimulator.AssertMessageCount("recipient1@test.com", "test", "Inbox", 3);100 IMAPSimulator.AssertMessageCount("recipient2@test.com", "test", "Inbox", 3);101 IMAPSimulator.AssertMessageCount("recipient3@test.com", "test", "Inbox", 3);102 oRecipients.Add("recipient4@test.com");103 oRecipients.Add("recipient5@test.com");104 oRecipients.Add("recipient6@test.com");105 oRecipients.Add("recipient7@test.com");106 oRecipients.Add("recipient8@test.com");107 SingletonProvider<TestSetup>.Instance.AddDistributionList(_domain, "list2@test.com", oRecipients);108 oAccount = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "recipient7@test.com", "test");109 oAccount = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "recipient8@test.com", "test");110 oAccount = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "test@test.com", "test");111 Assert.IsTrue(oSMTP.Send("test@test.com", "list2@test.com", "Mail 1", "Mail 1"));112 IMAPSimulator.AssertMessageCount("recipient7@test.com", "test", "Inbox", 1);113 // Wait for the message to be completely delivered. The above assertion isn't enough to confirm that.114 Thread.Sleep(1000);115 oAccount = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "announcer@test.com", "test");116 DistributionList oList = SingletonProvider<TestSetup>.Instance.AddDistributionList(_domain,117 "list3-security@test.com",118 oRecipients);119 oList.Mode = eDistributionListMode.eLMAnnouncement;120 oList.RequireSenderAddress = oAccount.Address;121 oList.RequireSMTPAuth = false;122 oList.Save();123 Assert.IsFalse(oSMTP.Send("test@test.com", "list3-security@test.com", "Mail 1", "Mail 1"));124 Assert.IsTrue(oSMTP.Send(oAccount.Address, "list3-security@test.com", "Mail 1", "Mail 1"));125 IMAPSimulator.AssertMessageCount("recipient7@test.com", "test", "Inbox", 2);126 oList.Mode = eDistributionListMode.eLMMembership;127 oList.Save();128 Assert.IsFalse(oSMTP.Send(oAccount.Address, "list3-security@test.com", "Mail 1", "Mail 1"));129 // THIS MESSAGE SHOULD SUCCED 130 Assert.IsTrue(oSMTP.Send("recipient5@test.com", "list3-security@test.com", "Mail 1", "Mail 1"));131 IMAPSimulator.AssertMessageCount("recipient7@test.com", "test", "Inbox", 3);132 }133 [Test]134 public void TestDistributionListsMembershipDomainAliases()135 {136 var oIMAP = new IMAPSimulator();137 var oSMTP = new SMTPClientSimulator();138 Application application = SingletonProvider<TestSetup>.Instance.GetApp();139 DomainAlias oDA = _domain.DomainAliases.Add();140 oDA.AliasName = "dummy-example.com";141 oDA.Save();142 Account oAccount = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "account1@test.com", "test");143 oAccount = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "account2@test.com", "test");144 // 145 // TEST LIST SECURITY IN COMBINATION WITH DOMAIN NAME ALIASES146 // 147 var oRecipients = new List<string>();148 oRecipients.Clear();149 oRecipients.Add("vaffe@dummy-example.com");150 DistributionList oList3 = SingletonProvider<TestSetup>.Instance.AddDistributionList(_domain, "list@test.com",151 oRecipients);152 oList3.Mode = eDistributionListMode.eLMMembership;153 oList3.Save();154 // THIS MESSAGE SHOULD FAIL - Membership required, unknown sender domain155 Assert.IsFalse(oSMTP.Send("account1@dummy-example.com", "list@test.com", "Mail 1", "Mail 1"));156 oList3.Delete();157 // THIS MESSAGE SHOULD SUCCED - Membership required, sender domain is now an alias for test.com.158 oRecipients = new List<string>();159 oRecipients.Clear();160 oRecipients.Add("account1@dummy-example.com");161 oRecipients.Add("account2@test.com");162 oList3 = SingletonProvider<TestSetup>.Instance.AddDistributionList(_domain, "list@test.com", oRecipients);163 oList3.Mode = eDistributionListMode.eLMMembership;164 oList3.Save();165 Assert.IsTrue(oSMTP.Send("account1@dummy-example.com", "list@test.com", "Mail 1", "Mail 1"));166 IMAPSimulator.AssertMessageCount("account1@test.com", "test", "Inbox", 1);167 IMAPSimulator.AssertMessageCount("account2@test.com", "test", "Inbox", 1);168 }169 [Test]170 public void TestListContainingLists()171 {172 var test = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "test@test.com", "test");173 SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "acc1@test.com", "test");174 SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "acc2@test.com", "test");175 SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "acc3@test.com", "test");176 SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "outsider1@test.com", "test");177 SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "outsider2@test.com", "test");178 var daRecipients = new List<string>()179 {180 "db@test.com",181 "dc@test.com" 182 };183 var dbRecipients = new List<string>184 {185 "acc2@test.com",186 "acc3@test.com",187 };188 var dcRecipients = new List<string>189 {190 "acc2@test.com",191 "acc3@test.com",192 };193 DistributionList daList = SingletonProvider<TestSetup>.Instance.AddDistributionList(_domain, "da@test.com", daRecipients);194 daList.Mode = eDistributionListMode.eLMPublic;195 daList.Save();196 197 DistributionList dbList = SingletonProvider<TestSetup>.Instance.AddDistributionList(_domain, "db@test.com", dbRecipients);198 dbList.Mode = eDistributionListMode.eLMPublic;199 dbList.Save();200 DistributionList dcList = SingletonProvider<TestSetup>.Instance.AddDistributionList(_domain, "dc@test.com", dcRecipients);201 dbList.Mode = eDistributionListMode.eLMPublic;202 dbList.Save();203 var recipients = new List<string>()204 {205 "da@test.com",206 "outsider1@test.com",207 "outsider2@test.com" 208 };209 var smtpClient = new SMTPClientSimulator();210 Assert.IsTrue(smtpClient.Send(test.Address, recipients, "test" , "test"));211 IMAPSimulator.AssertMessageCount("acc2@test.com", "test", "Inbox", 1); // Member in list212 IMAPSimulator.AssertMessageCount("acc3@test.com", "test", "Inbox", 1); // Member in list213 IMAPSimulator.AssertMessageCount("outsider1@test.com", "test", "Inbox", 1); // Included in To list214 IMAPSimulator.AssertMessageCount("outsider2@test.com", "test", "Inbox", 1); // Included in To list...

Full Screen

Full Screen

SMTPClientStartTLSTests.cs

Source:SMTPClientStartTLSTests.cs Github

copy

Full Screen

...13 [SetUp]14 public new void SetUp()15 {16 _status = _application.Status;17 _account = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "test@test.com", "test");18 }19 [Test]20 public void UseStartTlsIfEnabledAndAvailable()21 {22 // No valid recipients...23 var deliveryResults = new Dictionary<string, int>();24 deliveryResults["test@dummy-example.com"] = 250;25 int smtpServerPort = TestSetup.GetNextFreePort();26 using (var server = new SmtpServerSimulator(1, smtpServerPort, eConnectionSecurity.eCSSTARTTLSOptional))27 {28 server.AddRecipientResult(deliveryResults);29 server.StartListen();30 Route route = TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, false, eConnectionSecurity.eCSSTARTTLSOptional);31 // Send message to this route.32 var smtp = new SmtpClientSimulator();33 smtp.Send("test@test.com", "test@dummy-example.com", "Test", "Test message");34 // Wait for the client to disconnect.35 server.WaitForCompletion();36 CustomAsserts.AssertRecipientsInDeliveryQueue(0, false);37 Assert.That(server.MessageData, Is.Not.Null.Or.Empty);38 Assert.IsTrue(LogHandler.DefaultLogContains("220 Ready to start TLS"));39 }40 }41 [Test]42 public void DoNotUseStartTlsIfEnabledButNotAvailable()43 {44 // No valid recipients...45 var deliveryResults = new Dictionary<string, int>();46 deliveryResults["test@dummy-example.com"] = 250;47 int smtpServerPort = TestSetup.GetNextFreePort();48 using (var server = new SmtpServerSimulator(1, smtpServerPort, eConnectionSecurity.eCSNone))49 {50 server.AddRecipientResult(deliveryResults);51 server.StartListen();52 Route route = TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, false, eConnectionSecurity.eCSSTARTTLSOptional);53 // Send message to this route.54 var smtp = new SmtpClientSimulator();55 smtp.Send("test@test.com", "test@dummy-example.com", "Test", "Test message");56 // Wait for the client to disconnect.57 server.WaitForCompletion();58 CustomAsserts.AssertRecipientsInDeliveryQueue(0, false);59 Assert.That(server.MessageData, Is.Not.Null.Or.Empty);60 Assert.IsFalse(LogHandler.DefaultLogContains("220 Ready to start TLS"));61 }62 }63 [Test]64 public void DoNotUseStartTlsIfNotEnabledButAvailable()65 {66 // No valid recipients...67 var deliveryResults = new Dictionary<string, int>();68 deliveryResults["test@dummy-example.com"] = 250;69 int smtpServerPort = TestSetup.GetNextFreePort();70 using (var server = new SmtpServerSimulator(1, smtpServerPort, eConnectionSecurity.eCSSTARTTLSOptional))71 {72 server.AddRecipientResult(deliveryResults);73 server.StartListen();74 Route route = TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, false, eConnectionSecurity.eCSNone);75 // Send message to this route.76 var smtp = new SmtpClientSimulator();77 smtp.Send("test@test.com", "test@dummy-example.com", "Test", "Test message");78 79 // Wait for the client to disconnect.80 server.WaitForCompletion();81 CustomAsserts.AssertRecipientsInDeliveryQueue(0, false);82 Assert.That(server.MessageData, Is.Not.Null.Or.Empty);83 Assert.IsFalse(LogHandler.DefaultLogContains("220 Ready to start TLS"));84 }85 }86 [Test]87 public void DoNotUseStartTlsIfNotEnabledAndNotAvailable()88 {89 // No valid recipients...90 var deliveryResults = new Dictionary<string, int>();91 deliveryResults["test@dummy-example.com"] = 250;92 int smtpServerPort = TestSetup.GetNextFreePort();93 using (var server = new SmtpServerSimulator(1, smtpServerPort, eConnectionSecurity.eCSNone))94 {95 server.AddRecipientResult(deliveryResults);96 server.StartListen();97 Route route = TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, false, eConnectionSecurity.eCSNone);98 // Send message to this route.99 var smtp = new SmtpClientSimulator();100 smtp.Send("test@test.com", "test@dummy-example.com", "Test", "Test message");101 // Wait for the client to disconnect.102 server.WaitForCompletion();103 CustomAsserts.AssertRecipientsInDeliveryQueue(0, false);104 Assert.That(server.MessageData, Is.Not.Null.Or.Empty);105 Assert.IsFalse(LogHandler.DefaultLogContains("220 Ready to start TLS"));106 }107 }108 109 [Test]110 public void TestDelivertoServerNotSupportingEHLOOptionalConnectionSecurity()111 {112 Assert.AreEqual(0, _status.UndeliveredMessages.Length);113 var deliveryResults = new Dictionary<string, int>()114 {115 {"user1@dummy-example.com", 250}116 };117 int smtpServerPort = TestSetup.GetNextFreePort();118 using (var server = new SmtpServerSimulator(1, smtpServerPort))119 {120 server.ServerSupportsEhlo = false;121 server.AddRecipientResult(deliveryResults);122 server.StartListen();123 // Add a route so we can conenct to localhost.124 TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, false, eConnectionSecurity.eCSSTARTTLSOptional);125 // Send message to this route.126 SmtpClientSimulator.StaticSend("test@test.com", "user1@dummy-example.com", "Test", "Test message");127 // Wait for the client to disconnect.128 server.WaitForCompletion();129 CustomAsserts.AssertRecipientsInDeliveryQueue(0, false);130 Assert.IsTrue(server.MessageData.Contains("Test message"));131 }132 }133 [Test]134 public void TestDeliverToServerNotSupportingEHLORequiredConnectionSecurity()135 {136 Assert.AreEqual(0, _status.UndeliveredMessages.Length);137 var deliveryResults = new Dictionary<string, int>()138 {139 {"user1@dummy-example.com", 250}140 };141 int smtpServerPort = TestSetup.GetNextFreePort();142 using (var server = new SmtpServerSimulator(1, smtpServerPort))143 {144 server.ServerSupportsEhlo = false;145 server.AddRecipientResult(deliveryResults);146 server.StartListen();147 // Add a route so we can conenct to localhost.148 TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, false, eConnectionSecurity.eCSSTARTTLSRequired);149 // Send message to this route.150 SmtpClientSimulator.StaticSend("test@test.com", "user1@dummy-example.com", "Test", "Test message");151 // Wait for the client to disconnect.152 server.WaitForCompletion();153 CustomAsserts.AssertRecipientsInDeliveryQueue(0, true);154 var msg = Pop3ClientSimulator.AssertGetFirstMessageText("test@test.com", "test");155 Assert.IsTrue(msg.Contains("Server does not support EHLO command."));156 }157 }158 }159}...

Full Screen

Full Screen

SmtpDeliverySslTests.cs

Source:SmtpDeliverySslTests.cs Github

copy

Full Screen

...12 {13 [Test]14 public void SmtpServerSupportingSSL()15 {16 Account account = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "sender@test.com", "test");17 // Set up a server listening on port 250 which accepts email for test@otherdomain.com18 var deliveryResults = new Dictionary<string, int>();19 deliveryResults["test@dummy-example.com"] = 250;20 int smtpServerPort = TestSetup.GetNextFreePort();21 using (var server = new SmtpServerSimulator(1, smtpServerPort, eConnectionSecurity.eCSTLS))22 {23 server.SetCertificate(SslSetup.GetCertificate());24 server.AddRecipientResult(deliveryResults);25 server.StartListen();26 Route route = TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, true, eConnectionSecurity.eCSTLS);27 var smtpClient = new SmtpClientSimulator();28 smtpClient.Send(account.Address, "test@dummy-example.com", "Test", "Test message");29 CustomAsserts.AssertRecipientsInDeliveryQueue(0);30 // This should now be processed via the rule -> route -> external server we've set up.31 server.WaitForCompletion();32 Assert.IsTrue(server.MessageData.Contains("Test message"));33 }34 }35 [Test]36 public void SmtpServerSupportingStartTls_StartTlsRequired()37 {38 Account account = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "sender@test.com", "test");39 // Set up a server listening on port 250 which accepts email for test@otherdomain.com40 var deliveryResults = new Dictionary<string, int>();41 deliveryResults["test@dummy-example.com"] = 250;42 int smtpServerPort = TestSetup.GetNextFreePort();43 using (var smtpServer = new SmtpServerSimulator(1, smtpServerPort, eConnectionSecurity.eCSSTARTTLSRequired))44 {45 smtpServer.SetCertificate(SslSetup.GetCertificate());46 smtpServer.AddRecipientResult(deliveryResults);47 smtpServer.StartListen();48 Route route = TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, true, eConnectionSecurity.eCSSTARTTLSRequired);49 var smtpClient = new SmtpClientSimulator();50 smtpClient.Send(account.Address, "test@dummy-example.com", "Test", "Test message");51 CustomAsserts.AssertRecipientsInDeliveryQueue(0);52 smtpServer.WaitForCompletion();53 Assert.IsTrue(smtpServer.MessageData.Contains("Test message"), smtpServer.MessageData);54 }55 }56 [Test]57 public void SmtpServerSupportingStartTls_StartTlsOptional()58 {59 Account account = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "sender@test.com", "test");60 // Set up a server listening on port 250 which accepts email for test@otherdomain.com61 var deliveryResults = new Dictionary<string, int>();62 deliveryResults["test@dummy-example.com"] = 250;63 int smtpServerPort = TestSetup.GetNextFreePort();64 using (var server = new SmtpServerSimulator(1, smtpServerPort, eConnectionSecurity.eCSSTARTTLSRequired))65 {66 server.SetCertificate(SslSetup.GetCertificate());67 server.AddRecipientResult(deliveryResults);68 server.StartListen();69 Route route = TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, true, eConnectionSecurity.eCSSTARTTLSOptional);70 var smtpClient = new SmtpClientSimulator();71 smtpClient.Send(account.Address, "test@dummy-example.com", "Test", "Test message");72 CustomAsserts.AssertRecipientsInDeliveryQueue(0);73 server.WaitForCompletion();74 Assert.IsTrue(server.MessageData.Contains("Test message"));75 }76 }77 [Test]78 public void SmtpServerNOTSupportingStartTls_StartTlsRequired()79 {80 Account account = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "sender@test.com", "test");81 // Set up a server listening on port 250 which accepts email for test@otherdomain.com82 var deliveryResults = new Dictionary<string, int>();83 deliveryResults["test@dummy-example.com"] = 250;84 int smtpServerPort = TestSetup.GetNextFreePort();85 using (var server = new SmtpServerSimulator(1, smtpServerPort, eConnectionSecurity.eCSNone))86 {87 server.SetCertificate(SslSetup.GetCertificate());88 server.AddRecipientResult(deliveryResults);89 server.StartListen();90 Route route = TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, true, eConnectionSecurity.eCSSTARTTLSRequired);91 var smtpClient = new SmtpClientSimulator();92 smtpClient.Send(account.Address, "test@dummy-example.com", "Test", "Test message");93 CustomAsserts.AssertRecipientsInDeliveryQueue(0);94 // This should now be processed via the rule -> route -> external server we've set up.95 server.WaitForCompletion();96 var msg = Pop3ClientSimulator.AssertGetFirstMessageText("sender@test.com", "test");97 Assert.IsTrue(msg.Contains("Server does not support STARTTLS"));98 }99 }100 [Test]101 public void SmtpServerNOTSupportingStartTls_StartTlsOptional()102 {103 Account account = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "sender@test.com", "test");104 // Set up a server listening on port 250 which accepts email for test@otherdomain.com105 var deliveryResults = new Dictionary<string, int>();106 deliveryResults["test@dummy-example.com"] = 250;107 int smtpServerPort = TestSetup.GetNextFreePort();108 using (var server = new SmtpServerSimulator(1, smtpServerPort, eConnectionSecurity.eCSNone))109 {110 server.SetCertificate(SslSetup.GetCertificate());111 server.AddRecipientResult(deliveryResults);112 server.StartListen();113 Route route = TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, true, eConnectionSecurity.eCSSTARTTLSOptional);114 var smtpClient = new SmtpClientSimulator();115 smtpClient.Send(account.Address, "test@dummy-example.com", "Test", "Test message");116 CustomAsserts.AssertRecipientsInDeliveryQueue(0);117 // This should now be processed via the rule -> route -> external server we've set up.118 server.WaitForCompletion();119 Assert.IsTrue(server.MessageData.Contains("Test message"));120 }121 }122 }123}...

Full Screen

Full Screen

StaffTests.cs

Source:StaffTests.cs Github

copy

Full Screen

...3839 [Test]40 public void GetAllStaffUsers()41 {42 StaffUserCollection staffUsers = TestSetup.KayakoApiService.Staff.GetStaffUsers();4344 Assert.IsNotNull(staffUsers, "No staff users were returned");45 Assert.IsNotEmpty(staffUsers, "No staff users were returned");46 }4748 [Test]49 public void GetStaffUser()50 {51 StaffUserCollection staffUsers = TestSetup.KayakoApiService.Staff.GetStaffUsers();5253 Assert.IsNotNull(staffUsers, "No staff users were returned");54 Assert.IsNotEmpty(staffUsers, "No staff users were returned");5556 StaffUser staffUserToGet = staffUsers[new Random().Next(staffUsers.Count)];5758 Trace.WriteLine("GetStaffUser using staff user id: " + staffUserToGet.Id);5960 StaffUser staffUser = TestSetup.KayakoApiService.Staff.GetStaffUser(staffUserToGet.Id);6162 CompareStaffUsers(staffUser, staffUserToGet);63 }6465 [Test(Description="Tests creating, updating and deleting staff users")]66 public void CreateUpdateDeleteStaffUser()67 {68 StaffUser dummyStaffUser = TestData;6970 StaffUserRequest req = StaffUserRequest.FromResponseData(dummyStaffUser);71 req.Password = "password123";7273 StaffUser createdStaffUser = TestSetup.KayakoApiService.Staff.CreateStaffUser(req);7475 Assert.IsNotNull(createdStaffUser);76 dummyStaffUser.Id = createdStaffUser.Id;77 CompareStaffUsers(dummyStaffUser, createdStaffUser);7879 dummyStaffUser.Designation = "Mrs";80 dummyStaffUser.Email = "updatedtest@test.com";81 dummyStaffUser.EnableDst = false;82 dummyStaffUser.FirstName = "UpdatedFirstName";83 dummyStaffUser.Greeting = "UpdatedGreetingtext";84 StaffGroupCollection staffGroups = TestSetup.KayakoApiService.Staff.GetStaffGroups();85 dummyStaffUser.GroupId = staffGroups[staffGroups.Count - 1].Id;86 dummyStaffUser.IsEnabled = false;87 dummyStaffUser.LastName = "UpdatedLastName";88 dummyStaffUser.MobileNumber = "0798765432";89 //Can't test signature as it doesn't come back from the Api90 //dummyStaffUser.Signature = "Signature Updated";91 dummyStaffUser.TimeZone = "GMT BST";92 dummyStaffUser.UserName = "updatedUser";9394 StaffUser updatedStaffUser = TestSetup.KayakoApiService.Staff.UpdateStaffUser(StaffUserRequest.FromResponseData(dummyStaffUser));95 dummyStaffUser.FullName = String.Format("{0} {1}", dummyStaffUser.FirstName, dummyStaffUser.LastName);9697 Assert.IsNotNull(updatedStaffUser);98 CompareStaffUsers(dummyStaffUser, updatedStaffUser);99100 bool success = TestSetup.KayakoApiService.Staff.DeleteStaffUser(updatedStaffUser.Id);101102 Assert.IsTrue(success);103 }104105 private void CompareStaffUsers(StaffUser one, StaffUser two)106 {107 Assert.AreEqual(one.Designation, two.Designation);108 Assert.AreEqual(one.Email, two.Email);109 Assert.AreEqual(one.EnableDst, two.EnableDst);110 Assert.AreEqual(one.FirstName, two.FirstName);111 Assert.AreEqual(one.FullName, two.FullName);112 Assert.AreEqual(one.Greeting, two.Greeting);113 Assert.AreEqual(one.GroupId, two.GroupId);114 Assert.AreEqual(one.Id, two.Id); ...

Full Screen

Full Screen

UserTests.cs

Source:UserTests.cs Github

copy

Full Screen

...4243 [Test]44 public void GetAllGetUsers()45 {46 UserCollection users = TestSetup.KayakoApiService.Users.GetUsers();47 48 Assert.IsNotNull(users, "No users were returned");49 Assert.IsNotEmpty(users, "No users were returned");50 }5152 [Test]53 public void GetUser()54 {55 UserCollection users = TestSetup.KayakoApiService.Users.GetUsers();5657 Assert.IsNotNull(users, "No users were returned");58 Assert.IsNotEmpty(users, "No users were returned");5960 User userToGet = users[new Random().Next(users.Count)];6162 Trace.WriteLine("GetUser using user id: " + userToGet.Id);6364 User user = TestSetup.KayakoApiService.Users.GetUser(userToGet.Id);6566 CompareUserGroup(user, userToGet);67 }6869 [Test(Description="Tests creating, updating and deleting users")]70 public void CreateUpdateDeleteUser()71 {72 User dummyData = TestData;7374 User createdUser = TestSetup.KayakoApiService.Users.CreateUser(UserRequest.FromResponseData(dummyData), "password123!", false);7576 Assert.IsNotNull(createdUser);77 dummyData.Id = createdUser.Id;78 dummyData.Dateline = createdUser.Dateline;7980 CompareUserGroup(dummyData, createdUser);8182 dummyData.FullName = "Updated FullName";83 dummyData.EmailAddresses = new string[] { "test1@test.com", "test2@test.com" };84 dummyData.Salutation = UserSalutation.Mrs;85 dummyData.Designation = "CGE";86 dummyData.Phone = "09876543212";87 dummyData.IsEnabled = false;88 dummyData.Role = UserRole.User;89 90 User updatedUser = TestSetup.KayakoApiService.Users.UpdateUser(UserRequest.FromResponseData(dummyData));91 dummyData.Dateline = createdUser.Dateline;9293 Assert.IsNotNull(updatedUser);94 CompareUserGroup(dummyData, updatedUser);9596 bool success = TestSetup.KayakoApiService.Users.DeleteUser(updatedUser.Id);9798 Assert.IsTrue(success);99 }100101 [Test(Description = "Tests searching for a user")]102 [TestCase("howson")]103 [TestCase("chris")]104 public void UserSearch(string query)105 {106 UserCollection matchedUsers = TestSetup.KayakoApiService.Users.UserSearch(query);107108 Assert.NotNull(matchedUsers);109 Assert.IsNotEmpty(matchedUsers);110 }111112 private void CompareUserGroup(User one, User two)113 {114 Assert.IsTrue(one.Dateline.Equals(two.Dateline), "Dateline is not the same");115 Assert.AreEqual(one.Designation, two.Designation);116 Assert.AreEqual(one.EmailAddresses, two.EmailAddresses);117 Assert.AreEqual(one.EnableDst, two.EnableDst);118 Assert.AreEqual(one.Expiry, two.Expiry);119 Assert.AreEqual(one.FullName, two.FullName);120 Assert.AreEqual(one.GroupId, two.GroupId); ...

Full Screen

Full Screen

DepartmentTests.cs

Source:DepartmentTests.cs Github

copy

Full Screen

...2930 [Test]31 public void GetAllDepartments()32 {33 DepartmentCollection departments = TestSetup.KayakoApiService.Departments.GetDepartments();3435 Assert.IsNotNull(departments, "No departments were returned");36 Assert.IsNotEmpty(departments, "No departments were returned");37 }3839 [Test]40 public void GetDepartment()41 {42 DepartmentCollection departments = TestSetup.KayakoApiService.Departments.GetDepartments();4344 Assert.IsNotNull(departments, "No departments were returned");45 Assert.IsNotEmpty(departments, "No departments were returned");4647 Department deptToGet = departments[new Random().Next(departments.Count)];4849 Trace.WriteLine("GetDepartment using department id: " + deptToGet.Id);5051 Department dept = TestSetup.KayakoApiService.Departments.GetDepartment(deptToGet.Id);5253 CompareDepartments(dept, deptToGet);54 }5556 [Test(Description="Tests creating, updating and deleting departments")]57 public void CreateUpdateDeleteDepartment()58 {59 Department dummyData = TestData;6061 Department createdDept = TestSetup.KayakoApiService.Departments.CreateDepartment(DepartmentRequest.FromResponseData(dummyData));6263 Assert.IsNotNull(createdDept);64 dummyData.Id = createdDept.Id;65 CompareDepartments(dummyData, createdDept);6667 dummyData.Title = "Updated Title";68 dummyData.Type = DepartmentType.Private;69 dummyData.DisplayOrder = 34;70 dummyData.UserVisibilityCustom = false;71 dummyData.UserGroups = new List<int>();7273 Department updatedDept = TestSetup.KayakoApiService.Departments.UpdateDepartment(DepartmentRequest.FromResponseData(dummyData));7475 Assert.IsNotNull(updatedDept);76 CompareDepartments(dummyData, updatedDept);7778 bool success = TestSetup.KayakoApiService.Departments.DeleteDepartment(updatedDept.Id);7980 Assert.IsTrue(success);81 }8283 private void CompareDepartments(Department one, Department two)84 {85 Assert.AreEqual(one.Id, two.Id);86 Assert.AreEqual(one.Title, two.Title);87 Assert.AreEqual(one.Type, two.Type);88 Assert.AreEqual(one.Module, two.Module);89 Assert.AreEqual(one.ParentDepartmentId, two.ParentDepartmentId);90 Assert.AreEqual(one.DisplayOrder, two.DisplayOrder);91 Assert.AreEqual(one.UserVisibilityCustom, two.UserVisibilityCustom);92 Assert.AreEqual(one.UserGroups, two.UserGroups); ...

Full Screen

Full Screen

TestSetup

Using AI Code Generation

copy

Full Screen

1var dummy = new B.Dummy();2dummy.TestSetup();3var dummy = new B.Dummy();4dummy.TestSetup();5var dummy = new B.Dummy();6dummy.TestSetup();7var dummy = new B.Dummy();8dummy.TestSetup();9var dummy = new B.Dummy();10dummy.TestSetup();11var dummy = new B.Dummy();12dummy.TestSetup();13var dummy = new B.Dummy();14dummy.TestSetup();15var dummy = new B.Dummy();16dummy.TestSetup();17var dummy = new B.Dummy();18dummy.TestSetup();19var dummy = new B.Dummy();20dummy.TestSetup();21var dummy = new B.Dummy();22dummy.TestSetup();23var dummy = new B.Dummy();24dummy.TestSetup();25var dummy = new B.Dummy();26dummy.TestSetup();27var dummy = new B.Dummy();28dummy.TestSetup();29var dummy = new B.Dummy();30dummy.TestSetup();31var dummy = new B.Dummy();32dummy.TestSetup();

Full Screen

Full Screen

TestSetup

Using AI Code Generation

copy

Full Screen

1using B;2{3 {4 public static void TestSetup()5 {6 B.Dummy.TestSetup();7 }8 }9}10using A;11{12 {13 public static void TestSetup()14 {15 A.Dummy.TestSetup();16 }17 }18}19A.Dummy.TestSetup();20B.Dummy.TestSetup();21C.Dummy.TestSetup();

Full Screen

Full Screen

TestSetup

Using AI Code Generation

copy

Full Screen

1using B;2{3 {4 static void Main(string[] args)5 {6 B.Dummy d = new B.Dummy();7 d.TestSetup();8 Console.Read();9 }10 }11}12using B;13{14 {15 static void Main(string[] args)16 {17 B.Dummy d = new B.Dummy();18 d.TestSetup();19 Console.Read();20 }21 }22}

Full Screen

Full Screen

TestSetup

Using AI Code Generation

copy

Full Screen

1using B;2{3 {4 public static void TestSetup()5 {6 B.Dummy.TestSetup();7 }8 }9}10using A;11{12 {13 public static void TestSetup()14 {15 A.Dummy.TestSetup();16 }17 }18}19using C;20{21 {22 public static void TestSetup()23 {24 C.Dummy.TestSetup();25 }26 }27}28using D;29{30 {31 public static void TestSetup()32 {33 D.Dummy.TestSetup();34 }35 }36}37using E;38{39 {40 public static void TestSetup()41 {42 E.Dummy.TestSetup();43 }44 }45}46using F;47{48 {49 public static void TestSetup()50 {51 F.Dummy.TestSetup();52 }53 }54}55using G;56{57 {58 public static void TestSetup()59 {60 G.Dummy.TestSetup();61 }62 }63}64using H;65{66 {67 public static void TestSetup()68 {69 H.Dummy.TestSetup();70 }71 }72}73using I;74{75 {76 public static void TestSetup()77 {78 I.Dummy.TestSetup();79 }80 }81}82using J;

Full Screen

Full Screen

TestSetup

Using AI Code Generation

copy

Full Screen

1using B;2{3 {4 public void TestSetup()5 {6 B.Dummy dummy = new B.Dummy();7 dummy.TestSetup();8 }9 }10}11using C;12{13 {14 public void TestSetup()15 {16 C.Dummy dummy = new C.Dummy();17 dummy.TestSetup();18 }19 }20}21using D;22{23 {24 public void TestSetup()25 {26 D.Dummy dummy = new D.Dummy();27 dummy.TestSetup();28 }29 }30}31using E;32{33 {34 public void TestSetup()35 {36 E.Dummy dummy = new E.Dummy();37 dummy.TestSetup();38 }39 }40}41using F;42{43 {44 public void TestSetup()45 {46 F.Dummy dummy = new F.Dummy();47 dummy.TestSetup();48 }49 }50}51using G;52{53 {54 public void TestSetup()55 {56 G.Dummy dummy = new G.Dummy();57 dummy.TestSetup();58 }59 }60}61using H;62{63 {64 public void TestSetup()65 {66 H.Dummy dummy = new H.Dummy();67 dummy.TestSetup();68 }69 }70}71using I;72{73 {74 public void TestSetup()75 {76 I.Dummy dummy = new I.Dummy();77 dummy.TestSetup();78 }79 }80}

Full Screen

Full Screen

TestSetup

Using AI Code Generation

copy

Full Screen

1using B;2{3 {4 public void TestSetup()5 {6 B.Dummy d = new B.Dummy();7 d.TestSetup();8 }9 }10}11{12 {13 public void TestSetup()14 {15 }16 }17}18Output: 2.cs(9,17): error CS0122: 'B.Dummy.TestSetup()' is inaccessible due to its protection level19{20 {21 public void TestSetup()22 {23 }24 }25}26{27 {28 private int a = 10;29 public void TestSetup()30 {31 Console.WriteLine(a);32 }33 }34}35Output: 1.cs(2,12): error CS0122: 'A.Dummy' is inaccessible due to its protection level36Output: 1.cs(2,12): error CS0122: 'A.Dummy' is inaccessible due to its protection level37{

Full Screen

Full Screen

TestSetup

Using AI Code Generation

copy

Full Screen

1using B;2{3 public static void Main()4 {5 Dummy d = new Dummy();6 d.TestSetup();7 }8}9using C;10{11 public static void Main()12 {13 Dummy d = new Dummy();14 d.TestSetup();15 }16}17using B;18{19 public static void Main()20 {21 B.Dummy d = new B.Dummy();22 d.TestSetup();23 }24}

Full Screen

Full Screen

TestSetup

Using AI Code Generation

copy

Full Screen

1B.Dummy.TestSetup();2C.Dummy.TestSetup();3using System.IO;4using System.IO.File;5using System.IO.Path;6using System.IO;7using System.IO.File;8using System.IO.Path;9using System;10using System.Collections.Generic;11using System.Linq;12using System.Text;13using System.Threading.Tasks;14using System.IO;15using System.IO.File;16using System.IO.Path;17using System;18using System.Collections.Generic;19using System.Linq;20using System.Text;21using System.Threading.Tasks;22using System.Windows.Forms;23using System.IO;24using System.IO.File;25using System.IO.Path;26using System;27using System.Collections.Generic;28using System.Linq;29using System.Text;30using System.Threading.Tasks;31using System.Windows.Forms;32using System.Drawing;33using System.IO;34using System.IO.File;35using System.IO.Path;36using System;37using System.Collections.Generic;38using System.Linq;39using System.Text;40using System.Threading.Tasks;41using System.Windows.Forms;42using System.Drawing;43using System.Drawing.Drawing2D;44using System.IO;45using System.IO.File;46using System.IO.Path;47using System;48using System.Collections.Generic;49using System.Linq;50using System.Text;

Full Screen

Full Screen

TestSetup

Using AI Code Generation

copy

Full Screen

1using B;2{3 static void Main()4 {5 var dummy = new Dummy();6 dummy.TestSetup();7 }8}9using B;10using B;11{12 static void Main()13 {14 var dummy = new Dummy();15 dummy.TestSetup();16 }17}18using A, B, C;19This statement imports the namespaces A, B, and C in

Full Screen

Full Screen

Nunit tutorial

Nunit is a well-known open-source unit testing framework for C#. This framework is easy to work with and user-friendly. LambdaTest’s NUnit Testing Tutorial provides a structured and detailed learning environment to help you leverage knowledge about the NUnit framework. The NUnit tutorial covers chapters from basics such as environment setup to annotations, assertions, Selenium WebDriver commands, and parallel execution using the NUnit framework.

Chapters

  1. NUnit Environment Setup - All the prerequisites and setup environments are provided to help you begin with NUnit testing.
  2. NUnit With Selenium - Learn how to use the NUnit framework with Selenium for automation testing and its installation.
  3. Selenium WebDriver Commands in NUnit - Leverage your knowledge about the top 28 Selenium WebDriver Commands in NUnit For Test Automation. It covers web browser commands, web element commands, and drop-down commands.
  4. NUnit Parameterized Unit Tests - Tests on varied combinations may lead to code duplication or redundancy. This chapter discusses how NUnit Parameterized Unit Tests and their methods can help avoid code duplication.
  5. NUnit Asserts - Learn about the usage of assertions in NUnit using Selenium
  6. NUnit Annotations - Learn how to use and execute NUnit annotations for Selenium Automation Testing
  7. Generating Test Reports In NUnit - Understand how to use extent reports and generate reports with NUnit and Selenium WebDriver. Also, look into how to capture screenshots in NUnit extent reports.
  8. Parallel Execution In NUnit - Parallel testing helps to reduce time consumption while executing a test. Deep dive into the concept of Specflow Parallel Execution in NUnit.

NUnit certification -

You can also check out the LambdaTest Certification to enhance your learning in Selenium Automation Testing using the NUnit framework.

YouTube

Watch this tutorial on the LambdaTest Channel to learn how to set up the NUnit framework, run tests and also execute parallel testing.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful