public void UploadToWebonaryThrowsOnNullInput()
 {
     using (var controller = new MockUploadToWebonaryController(Cache, m_mediator, null, null))
     {
         var view  = new MockWebonaryDlg();
         var model = new UploadToWebonaryModel(m_mediator);
         Assert.Throws <ArgumentNullException>(() => controller.UploadToWebonary(null, model, view));
         Assert.Throws <ArgumentNullException>(() => controller.UploadToWebonary("notNull", null, view));
         Assert.Throws <ArgumentNullException>(() => controller.UploadToWebonary("notNull", model, null));
     }
 }
        public void RealUploadToWebonaryHandlesNetworkErrors()
        {
            using (var controller = new MockUploadToWebonaryController(Cache, m_mediator))
            {
                var view     = new MockWebonaryDlg();
                var filepath = "../../Src/xWorks/xWorksTests/lubwisi-d-new.zip";

                controller.UploadURI = "http://nameresolutionfailure.local/import.php";
                Assert.DoesNotThrow(() => controller.UploadToWebonary(filepath, view.Model, view));
                Assert.That(view.StatusStrings.Any(s => s.Contains("An error occurred uploading your data:")));
                controller.UploadURI = "http://localhost:12345/import/connectfailure.php";
                Assert.DoesNotThrow(() => controller.UploadToWebonary(filepath, view.Model, view));
                Assert.That(view.StatusStrings.Any(s => s.Contains("An error occurred uploading your data:")));
                controller.UploadURI = "http://192.168.0.1/import/requesttimedout.php";
                Assert.DoesNotThrow(() => controller.UploadToWebonary(filepath, view.Model, view));
                Assert.That(view.StatusStrings.Any(s => s.Contains("An error occurred uploading your data:")));
            }
        }
        public void UploadToWebonaryReportsFailedAuthentication()
        {
            var responseText = Encoding.UTF8.GetBytes("Wrong username or password.\nauthentication failed\n");

            using (var controller = new MockUploadToWebonaryController(Cache, m_mediator, null, responseText))
            {
                var view = new MockWebonaryDlg()
                {
                    Model = new UploadToWebonaryModel(m_mediator)
                    {
                        UserName = "******",
                        Password = "******"
                    }
                };
                controller.UploadToWebonary("Fakefile.zip", view.Model, view);
                Assert.That(view.StatusStrings.Any(s => s.Contains("Error: Wrong username or password")));
            }
        }
 public void RealUploadWithBadDataReportsErrorInProcessing()
 {
     using (var controller = new MockUploadToWebonaryController(Cache, m_mediator))
     {
         var view = new MockWebonaryDlg
         {
             Model = new UploadToWebonaryModel(m_mediator)
             {
                 UserName = "******",
                 Password = "******"
             }
         };
         // Contains a filename in the zip that isn't correct, so no data will be found by webonary.
         controller.UploadToWebonary("../../Src/xWorks/xWorksTests/lubwisi-d-new-bad.zip", view.Model, view);
         //view.StatusStrings.ForEach(Console.WriteLine); // Debugging output
         Assert.That(view.StatusStrings.Any(s => s.IndexOf("Error", StringComparison.OrdinalIgnoreCase) >= 0), "Should be an error reported");
     }
 }
        public void UploadToWebonaryReportsSuccess()
        {
            var success = "Upload successful.";

            using (var controller = new MockUploadToWebonaryController(Cache, m_mediator, null, Encoding.UTF8.GetBytes(success)))
            {
                var view = new MockWebonaryDlg()
                {
                    Model = new UploadToWebonaryModel(m_mediator)
                    {
                        UserName = "******",
                        Password = "******"
                    }
                };
                controller.UploadToWebonary("../../Src/xWorks/xWorksTests/lubwisi-d-new.zip", view.Model, view);
                //view.StatusStrings.ForEach(Console.WriteLine); // Debugging output
                Assert.That(view.StatusStrings.Any(s => s.Contains("Upload successful")));
            }
        }
        public void UploadToWebonaryErrorInProcessingHandled()
        {
            var webonaryProcessingErrorContent = Encoding.UTF8.GetBytes("Error processing data: bad data.");

            using (var controller = new MockUploadToWebonaryController(Cache, m_mediator, null, webonaryProcessingErrorContent))
            {
                var view = new MockWebonaryDlg
                {
                    Model = new UploadToWebonaryModel(m_mediator)
                    {
                        UserName = "******",
                        Password = "******"
                    }
                };
                // Contains a filename in the zip that isn't correct, so no data will be found by webonary.
                controller.UploadToWebonary("fakebaddata.zip", view.Model, view);
                //view.StatusStrings.ForEach(Console.WriteLine); // Debugging output
                Assert.That(view.StatusStrings.Any(s => s.IndexOf("Error", StringComparison.OrdinalIgnoreCase) >= 0), "Should be an error reported");
            }
        }
        public void UploadToWebonaryReportsLackingPermissionsToUpload()
        {
            var ex = new WebonaryClient.WebonaryException(new WebException("Unable to connect to Webonary.  Please check your username and password and your Internet connection."));

            ex.StatusCode = HttpStatusCode.BadRequest;
            using (var controller = new MockUploadToWebonaryController(Cache, m_mediator, ex, null))
            {
                var view = new MockWebonaryDlg()
                {
                    Model = new UploadToWebonaryModel(m_mediator)
                    {
                        SiteName = "test-india",
                        UserName = "******",
                        Password = "******"
                    }
                };
                controller.UploadToWebonary("../../Src/xWorks/xWorksTests/lubwisi-d-new.zip", view.Model, view);
                Assert.That(view.StatusStrings.Any(s => s.Contains("Unable to connect to Webonary.  Please check your username and password and your Internet connection.")));
            }
        }
        public void UploadToWebonaryReportsIncorrectSiteName()
        {
            // Test for a successful response indicating that a redirect should happen
            using (var controller = new MockUploadToWebonaryController(Cache, m_mediator, null, new byte[] {}, HttpStatusCode.Found))
            {
                var view = new MockWebonaryDlg()
                {
                    Model = new UploadToWebonaryModel(m_mediator)
                    {
                        SiteName = "test",
                        UserName = "******",
                        Password = "******"
                    }
                };
                controller.UploadToWebonary("fakefile.zip", view.Model, view);
                Assert.That(view.StatusStrings.Any(s => s.Contains("Error: There has been an error accessing webonary. Is your sitename correct?")));
            }

            // Test with an exception which indicates a redirect should happen
            var redirectException = new WebonaryClient.WebonaryException(new WebException("Redirected."));

            redirectException.StatusCode = HttpStatusCode.Redirect;
            using (var controller = new MockUploadToWebonaryController(Cache, m_mediator, redirectException, new byte[] { }))
            {
                var view = new MockWebonaryDlg()
                {
                    Model = new UploadToWebonaryModel(m_mediator)
                    {
                        SiteName = "test",
                        UserName = "******",
                        Password = "******"
                    }
                };
                controller.UploadToWebonary("fakefile.zip", view.Model, view);
                Assert.That(view.StatusStrings.Any(s => s.Contains("Error: There has been an error accessing webonary. Is your sitename correct?")));
            }
        }