Represents the system, and its settings, that hosts the blogs within this Subtext installation.
예제 #1
0
        public void RequestNotMatchingAnyBlog_ButWithASingleBlogInSystemWithLocalHostButNotMatchingSubfolder_ReturnsUpdatesItsHostThenRedirectsToSubfolder()
        {
            //arrange
            var onlyBlog = new Blog { Host = "localhost", Subfolder = "sub" };
            var pagedCollection = new PagedCollection<Blog> { onlyBlog };
            pagedCollection.MaxItems = 1;

            var repository = new Mock<ObjectRepository>();
            repository.Setup(r => r.GetBlog("example.com", It.IsAny<string>())).Returns((Blog)null);
            repository.Setup(r => r.GetPagedBlogs(null, 0, It.IsAny<int>(), ConfigurationFlags.None)).Returns(
                pagedCollection);
            var appSettings = new NameValueCollection();
            appSettings.Add("AggregateEnabled", "false");
            var hostInfo = new HostInfo(appSettings);
            var service = new BlogLookupService(repository.Object, new LazyNotNull<HostInfo>(() => hostInfo));
            var blogRequest = new BlogRequest("example.com", string.Empty, new Uri("http://example.com/foo/bar"), false);

            //act
            BlogLookupResult result = service.Lookup(blogRequest);

            //assert
            Assert.IsNull(result.Blog);
            Assert.IsNotNull(result.AlternateUrl);
            Assert.AreEqual("http://example.com/sub/foo/bar", result.AlternateUrl.ToString());
            Assert.AreEqual("example.com", onlyBlog.Host);
            repository.Verify(r => r.UpdateBlog(It.IsAny<Blog>()));
        }
예제 #2
0
        public void OpmlHandler_WithRequestForAggregateBlog_GetsGroupIdFromQueryString()
        {
            //arrange
            var queryString = new NameValueCollection { { "GroupID", "310" } };

            var context = new Mock<ISubtextContext>();
            context.Stub(c => c.HttpContext.Response.ContentType);
            context.Setup(c => c.HttpContext.Response.Output).Returns(new StringWriter());
            context.Setup(c => c.HttpContext.Request.QueryString).Returns(queryString);
            context.Setup(c => c.HttpContext.Request.Url).Returns(new Uri("http://example.com/"));
            context.SetupUrlHelper(new Mock<BlogUrlHelper>());
            var repository = new Mock<ObjectRepository>();
            int? parsedGroupId = null;
            repository.Setup(r => r.GetBlogsByGroup("example.com", It.IsAny<int?>())).Callback<string, int?>(
                (host, groupId) => parsedGroupId = groupId);
            context.SetupRepository(repository);
            var writer = new Mock<OpmlWriter>();
            writer.Setup(w => w.Write(It.IsAny<IEnumerable<Blog>>(), It.IsAny<TextWriter>(), It.IsAny<BlogUrlHelper>()));

            var appSettings = new NameValueCollection();
            appSettings.Add("AggregateEnabled", "true");
            var hostInfo = new HostInfo(appSettings);
            var handler = new OpmlHandler(context.Object, writer.Object, new LazyNotNull<HostInfo>(() => hostInfo));

            //act
            handler.ProcessRequest();

            //assert
            Assert.AreEqual(310, parsedGroupId.Value);
        }
예제 #3
0
 public static void SetHostPassword(HostInfo host, string newPassword)
 {
     host.Salt = SecurityHelper.CreateRandomSalt();
     if (Config.Settings.UseHashedPasswords)
     {
         string hashedPassword = SecurityHelper.HashPassword(newPassword, host.Salt);
         host.Password = hashedPassword;
     }
     else
     {
         host.Password = newPassword;
     }
 }
예제 #4
0
        /// <summary>
        /// Creates the host in the persistent store.
        /// </summary>
        /// <returns></returns>
        public static bool CreateHost(ObjectRepository repository, string hostUserName, string hostPassword, string email)
        {
            if (_instance.Value != null)
            {
                throw new InvalidOperationException(Resources.InvalidOperation_HostRecordAlreadyExists);
            }

            var host = new HostInfo(ConfigurationManager.AppSettings)
            {
                HostUserName = hostUserName, Email = email
            };

            SetHostPassword(host, hostPassword);
            return(repository.UpdateHost(host));
        }
예제 #5
0
        /// <summary>
        /// Creates the host in the persistent store.
        /// </summary>
        /// <returns></returns>
        public static bool CreateHost(string hostUserName, string hostPassword, string email)
        {
            if (Instance != null)
            {
                throw new InvalidOperationException(Resources.InvalidOperation_HostRecordAlreadyExists);
            }

            var host = new HostInfo {
                HostUserName = hostUserName, Email = email
            };

            SetHostPassword(host, hostPassword);
            _instance = host;
            return(UpdateHost(host));
        }
예제 #6
0
        public void RequestNotMatchingAnyBlog_ButWithAggregateBlogsEnabledAndActiveBlogsInTheSystem_ReturnsAggregateBlog()
        {
            //arrange
            var repository = new Mock<ObjectRepository>();
            repository.Setup(r => r.GetBlog("example.com", It.IsAny<string>())).Returns((Blog)null);
            var onlyBlog = new Blog { Host = "example.com", Subfolder = "not-sub" };
            var pagedCollection = new PagedCollection<Blog> { onlyBlog };
            pagedCollection.MaxItems = 1;
            repository.Setup(r => r.GetPagedBlogs(null, 0, It.IsAny<int>(), ConfigurationFlags.None)).Returns(
                pagedCollection);
            var appSettings = new NameValueCollection();
            appSettings.Add("AggregateEnabled", "true");
            var hostInfo = new HostInfo(appSettings);
            var service = new BlogLookupService(repository.Object,
                                                new LazyNotNull<HostInfo>(() => hostInfo));
            var blogRequest = new BlogRequest("example.com", string.Empty, new Uri("http://example.com/foo/bar"), false);

            //act
            BlogLookupResult result = service.Lookup(blogRequest);

            //assert
            Assert.AreSame(hostInfo.AggregateBlog, result.Blog);
        }
예제 #7
0
        public void RequestNotMatchingAnyBlog_ButWithASingleBlogInSystemWithMatchingHostButDifferentSubfolder_RedirectsToOnlyBlog()
        {
            //arrange
            var onlyBlog = new Blog { Host = "example.com", Subfolder = "not-sub" };
            var pagedCollection = new PagedCollection<Blog> { onlyBlog };
            pagedCollection.MaxItems = 1;

            var repository = new Mock<ObjectRepository>();
            repository.Setup(r => r.GetBlog("example.com", "sub")).Returns((Blog)null);
            repository.Setup(r => r.GetBlog("example.com", "not-sub")).Returns(onlyBlog);
            repository.Setup(r => r.GetPagedBlogs(null, 0, It.IsAny<int>(), ConfigurationFlags.None)).Returns(
                pagedCollection);
            var appSettings = new NameValueCollection();
            appSettings.Add("AggregateEnabled", "false");
            var hostInfo = new HostInfo(appSettings);
            var service = new BlogLookupService(repository.Object, new LazyNotNull<HostInfo>(() => hostInfo));
            var blogRequest = new BlogRequest("example.com", "sub", new Uri("http://example.com/Subtext.Web/sub/bar"),
                                              false, RequestLocation.Blog, "/Subtext.Web");

            //act
            BlogLookupResult result = service.Lookup(blogRequest);

            //assert
            Assert.IsNull(result.Blog);
            Assert.AreEqual("http://example.com/Subtext.Web/not-sub/bar", result.AlternateUrl.ToString());
        }
예제 #8
0
파일: HostInfo.cs 프로젝트: ayende/Subtext
 /// <summary>
 /// Updates the host in the persistent store.
 /// </summary>
 /// <param name="host">Host.</param>
 /// <returns></returns>
 public static bool UpdateHost(HostInfo host)
 {
     if(ObjectProvider.Instance().UpdateHost(host))
     {
         _instance = host;
         return true;
     }
     return false;
 }
예제 #9
0
파일: HostInfo.cs 프로젝트: ayende/Subtext
 public static void SetHostPassword(HostInfo host, string newPassword)
 {
     host.Salt = SecurityHelper.CreateRandomSalt();
     if(Config.Settings.UseHashedPasswords)
     {
         string hashedPassword = SecurityHelper.HashPassword(newPassword, host.Salt);
         host.Password = hashedPassword;
     }
     else
         host.Password = newPassword;
 }
예제 #10
0
파일: HostInfo.cs 프로젝트: ayende/Subtext
 /// <summary>
 /// Loads the host from the Object Provider. This is provided for 
 /// those cases when we really need to hit the data strore. Calling this
 /// method will also reload the HostInfo.Instance from the data store.
 /// </summary>
 /// <param name="suppressException">If true, won't throw an exception.</param>
 /// <returns></returns>
 public static HostInfo LoadHost(bool suppressException)
 {
     try
     {
         _instance = ObjectProvider.Instance().LoadHostInfo(new HostInfo());
         return _instance;
     }
     catch(SqlException e)
     {
         // LoadHostInfo now executes the stored proc subtext_GetHost, instead of checking the table subtext_Host
         if (e.Message.IndexOf("Invalid object name 'subtext_Host'") >= 0 || e.Message.IndexOf("Could not find stored procedure 'subtext_GetHost'") >= 0)
         {
             if(suppressException)
                 return null;
             else
                 throw new HostDataDoesNotExistException();
         }
         else
             throw;
     }
 }
예제 #11
0
파일: HostInfo.cs 프로젝트: ayende/Subtext
        /// <summary>
        /// Creates the host in the persistent store.
        /// </summary>
        /// <returns></returns>
        public static bool CreateHost(string hostUserName, string hostPassword)
        {
            if(!InstallationManager.HostInfoRecordNeeded)
                throw new InvalidOperationException("Cannot create a Host record.  One already exists.");

            HostInfo host = new HostInfo();
            host.HostUserName = hostUserName;

            SetHostPassword(host, hostPassword);

            return UpdateHost(host);
        }
예제 #12
0
        /// <summary>
        /// Creates the host in the persistent store.
        /// </summary>
        /// <returns></returns>
        public static bool CreateHost(ObjectRepository repository, string hostUserName, string hostPassword, string email)
        {
            if (_instance.Value != null)
            {
                throw new InvalidOperationException(Resources.InvalidOperation_HostRecordAlreadyExists);
            }

            var host = new HostInfo(ConfigurationManager.AppSettings) { HostUserName = hostUserName, Email = email };

            SetHostPassword(host, hostPassword);
            return repository.UpdateHost(host);
        }
예제 #13
0
 /// <summary>
 /// Loads the host from the Object Provider. This is provided for 
 /// those cases when we really need to hit the data strore. Calling this
 /// method will also reload the HostInfo.Instance from the data store.
 /// </summary>
 /// <param name="suppressException">If true, won't throw an exception.</param>
 /// <returns></returns>
 public static HostInfo LoadHost(bool suppressException)
 {
     try
     {
         _instance = ObjectProvider.Instance().LoadHostInfo(new HostInfo());
         if(_instance != null)
         {
             _instance.BlogAggregationEnabled =
                 String.Equals(ConfigurationManager.AppSettings["AggregateEnabled"], "true",
                               StringComparison.OrdinalIgnoreCase);
             if(_instance.BlogAggregationEnabled)
             {
                 InitAggregateBlog(_instance);
             }
         }
         return _instance;
     }
     catch(SqlException e)
     {
         // LoadHostInfo now executes the stored proc subtext_GetHost, instead of checking the table subtext_Host
         if(e.Message.IndexOf("Invalid object name 'subtext_Host'") >= 0 ||
            e.Message.IndexOf("Could not find stored procedure 'subtext_GetHost'") >= 0)
         {
             if(suppressException)
             {
                 return null;
             }
             throw new HostDataDoesNotExistException();
         }
         throw;
     }
 }
예제 #14
0
        /// <summary>
        /// Creates the host in the persistent store.
        /// </summary>
        /// <returns></returns>
        public static bool CreateHost(string hostUserName, string hostPassword, string email)
        {
            if(Instance != null)
            {
                throw new InvalidOperationException(Resources.InvalidOperation_HostRecordAlreadyExists);
            }

            var host = new HostInfo {HostUserName = hostUserName, Email = email};

            SetHostPassword(host, hostPassword);
            _instance = host;
            return UpdateHost(host);
        }
예제 #15
0
        public void ValidateHostAdminPassword_WithValidUsernameAndPasswordCombo_ReturnsTrue()
        {
            // arrange
            const string password = "******";
            Byte[] clearBytes = new UnicodeEncoding().GetBytes(password);
            Byte[] hashedBytes = new MD5CryptoServiceProvider().ComputeHash(clearBytes);
            string hashedPassword = Convert.ToBase64String(hashedBytes);
            var hostInfo = new HostInfo(new NameValueCollection()) { HostUserName = "******", Password = hashedPassword };

            // act
            bool isValid = hostInfo.ValidateHostAdminPassword("user", "myPassword");

            // assert
            Assert.IsTrue(isValid);
        }
예제 #16
0
 /// <summary>
 /// Updates the host in the persistent store.
 /// </summary>
 /// <param name="host">Host.</param>
 /// <returns></returns>
 public static bool UpdateHost(ObjectRepository repository, HostInfo host)
 {
     if (repository.UpdateHost(host))
     {
         return true;
     }
     return false;
 }
예제 #17
0
        private static void InitAggregateBlog(HostInfo hostInfo)
        {
            string aggregateHost = ConfigurationManager.AppSettings["AggregateUrl"];
            if(aggregateHost == null)
            {
                return;
            }

            // validate host.
            var regex = new Regex(@"^(https?://)?(?<host>.+?)(/.*)?$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Match match = regex.Match(aggregateHost);

            if(match.Success)
            {
                aggregateHost = match.Groups["host"].Value;
            }

            var blog = new Blog(true /*isAggregateBlog*/)
            {
                Title = ConfigurationManager.AppSettings["AggregateTitle"],
                Skin = SkinConfig.DefaultSkin,
                Host = aggregateHost,
                Subfolder = string.Empty,
                IsActive = true
            };
            //TODO: blog.MobileSkin = ...

            if(hostInfo != null)
            {
                blog.UserName = hostInfo.HostUserName;
                hostInfo.AggregateBlog = blog;
            }
        }
예제 #18
0
 /// <summary>
 /// Updates the host in the persistent store.
 /// </summary>
 /// <param name="host">Host.</param>
 /// <returns></returns>
 public static bool UpdateHost(ObjectRepository repository, HostInfo host)
 {
     if (repository.UpdateHost(host))
     {
         _instance = new Lazy<HostInfo>(() => host);
         return true;
     }
     return false;
 }