Exemple #1
0
 private static void AddParameters(UserPageSettings userPageSettings, SqlCommand command)
 {
     command.Parameters.AddRange(new SqlParameter[] {
         new SqlParameter("@pageId", userPageSettings.PageId),
         new SqlParameter("@hidePage", userPageSettings.HidePage),
         new SqlParameter("@userName", userPageSettings.UserName),
     });
 }
        public void DeleteExcept_Success()
        {
            // Arrange
            var keptHelp = Models.CreateFirstTimeHelp();

            FirstTimeHelpRepository.Create(keptHelp);

            var deletedHelp = Models.CreateFirstTimeHelp();

            FirstTimeHelpRepository.Create(deletedHelp);

            var anotherDeletedHelp = Models.CreateFirstTimeHelp();

            FirstTimeHelpRepository.Create(anotherDeletedHelp);

            var userName1 = "*****@*****.**";
            var userName2 = "*****@*****.**";

            var keptPageSetting = new UserPageSettings {
                PageId = keptHelp.Id, UserName = userName1
            };

            UserPageSettingsRepository.Create(keptPageSetting);
            var deletedPageSetting1 = new UserPageSettings {
                PageId = deletedHelp.Id, UserName = userName1
            };

            UserPageSettingsRepository.Create(deletedPageSetting1);
            var deletedPageSetting2 = new UserPageSettings {
                PageId = deletedHelp.Id, UserName = userName2
            };

            UserPageSettingsRepository.Create(deletedPageSetting2);
            var anotherDeletedPageSetting = new UserPageSettings {
                PageId = anotherDeletedHelp.Id, UserName = userName1
            };

            UserPageSettingsRepository.Create(anotherDeletedPageSetting);

            // Act
            UserPageSettingsRepository.DeleteExcept(new List <int> {
                keptHelp.Id
            });

            // Assert
            var keptResult = UserPageSettingsRepository.Read(userName1, keptHelp.Id);

            Assert.That(keptResult, Is.Not.Null, "The kept settings should still be in the data store.");
            var deletedResult1 = UserPageSettingsRepository.Read(userName1, deletedHelp.Id);

            Assert.That(deletedResult1, Is.Null, "Settings shoudl not exist for the first deleted page and first user.");
            var deletedResult2 = UserPageSettingsRepository.Read(userName2, deletedHelp.Id);

            Assert.That(deletedResult2, Is.Null, "Settings should not exist for the first deleted page and second user.");
            var anotherDeletedResult = UserPageSettingsRepository.Read(userName1, anotherDeletedHelp.Id);

            Assert.That(anotherDeletedResult, Is.Null, "Settings should not exist for the second deleted page and first user.");
        }
Exemple #3
0
        public void Create(UserPageSettings userPageSettings)
        {
            using (var connection = SqlConnectionFactory.GetConnection())
                using (var command = connection.CreateCommand())
                {
                    command.CommandType = CommandType.Text;
                    command.CommandText = SqlScripts.UserPageSettings_Create;
                    AddParameters(userPageSettings, command);

                    connection.Open();
                    userPageSettings.Id = (int)command.ExecuteScalar();
                }
        }
Exemple #4
0
        public void Update(UserPageSettings userPageSettings)
        {
            using (var connection = SqlConnectionFactory.GetConnection())
                using (var command = connection.CreateCommand())
                {
                    command.CommandType = CommandType.Text;
                    command.CommandText = SqlScripts.UserPageSettings_Update;
                    AddParameters(userPageSettings, command);
                    command.Parameters.Add(new SqlParameter("@id", userPageSettings.Id));

                    connection.Open();
                    command.ExecuteNonQuery();
                }
        }
        public void DeleteByPageId_Success()
        {
            // Arrange
            var targetHelp = Models.CreateFirstTimeHelp();

            FirstTimeHelpRepository.Create(targetHelp);

            var noiseHelp = Models.CreateFirstTimeHelp();

            FirstTimeHelpRepository.Create(noiseHelp);

            var userName1 = "*****@*****.**";
            var userName2 = "*****@*****.**";

            var targetSettings1 = new UserPageSettings {
                PageId = targetHelp.Id, UserName = userName1
            };

            UserPageSettingsRepository.Create(targetSettings1);
            var targetSettings2 = new UserPageSettings {
                PageId = targetHelp.Id, UserName = userName2
            };

            UserPageSettingsRepository.Create(targetSettings2);
            var noiseSettings = new UserPageSettings {
                PageId = noiseHelp.Id, UserName = userName1
            };

            UserPageSettingsRepository.Create(noiseSettings);

            // Act
            UserPageSettingsRepository.DeleteByPageId(targetHelp.Id);

            // Assert
            var deletedSettings1 = UserPageSettingsRepository.Read(userName1, targetHelp.Id);

            Assert.That(deletedSettings1, Is.Null, "First user's settings for target page should have been deleted.");
            var deletedSettings2 = UserPageSettingsRepository.Read(userName2, targetHelp.Id);

            Assert.That(deletedSettings2, Is.Null, "Second user's settings for target page should have been deleted.");
            var existingSettings = UserPageSettingsRepository.Read(userName1, noiseHelp.Id);

            Assert.That(existingSettings, Is.Not.Null, "Settings for other pages should remain.");
        }
        public void Crud_Success()
        {
            var userName = "******";
            var help     = Models.CreateFirstTimeHelp();

            FirstTimeHelpRepository.Create(help);

            var settings = new UserPageSettings()
            {
                UserName = userName,
                PageId   = help.Id,
                HidePage = false,
            };

            UserPageSettingsRepository.Create(settings);

            settings.HidePage = true;
            UserPageSettingsRepository.Update(settings);

            var existingSettings = UserPageSettingsRepository.Read(settings.UserName, settings.PageId);

            Assert.That(existingSettings.HidePage, Is.True, "The value in the database should have been changed to true.");
        }
Exemple #7
0
        public UserPageSettings Read(string userName, int pageId)
        {
            UserPageSettings result = null;

            using (var connection = SqlConnectionFactory.GetConnection())
                using (var command = connection.CreateCommand())
                {
                    command.CommandType = CommandType.Text;
                    command.CommandText = SqlScripts.UserPageSettings_ReadByUserAndPage;
                    command.Parameters.AddRange(new SqlParameter[] {
                        new SqlParameter("@userName", userName),
                        new SqlParameter("@pageId", pageId)
                    });

                    connection.Open();
                    using (var reader = command.ExecuteReader())
                    {
                        result = HydrateUserPageSettings(reader).FirstOrDefault();
                    }
                }

            return(result);
        }
Exemple #8
0
        public void Process_NewPage()
        {
            // Arrange
            var pageId   = 100;
            var userName = "******";

            var requestContract = new UserPageSettings
            {
                HidePage = true,
                PageId   = pageId,
                UserName = "******",
            };
            var serializer  = new JavaScriptSerializer();
            var requestData = serializer.Serialize(requestContract);

            var httpContext = Mocks.Create <HttpContextBase>();

            httpContext.Setup(c => c.User.Identity.Name).Returns(userName);

            var userPageSettingsRepository = Mocks.Create <IUserPageSettingsRepository>();

            userPageSettingsRepository.Setup(r => r.Read(userName, pageId)).Returns(null as UserPageSettings);
            userPageSettingsRepository.Setup(r => r.Create(It.Is <UserPageSettings>(
                                                               s => s.HidePage == requestContract.HidePage &&
                                                               s.PageId == pageId &&
                                                               s.UserName == userName
                                                               )));

            var processor = new SaveUserPageSettingsRequestProcessor(httpContext.Object, userPageSettingsRepository.Object);

            // Act
            var result = processor.Process(requestData);

            // Assert
            Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK), "The new settings record should be created successfully.");
            Mocks.VerifyAll();
        }
Exemple #9
0
        public string CreateFirstTimeHelpHtml()
        {
            var result = string.Empty;

            if (_memoryCache.Contains(_firstTimeHelpHtmlCacheKey))
            {
                result = (string)_memoryCache.Get(_firstTimeHelpHtmlCacheKey);
            }
            else
            {
                // Determine if content should be minified.
                var popupViewContent = _minifier.Minify(HtmlContent.FirstTimeView, HtmlContent.FirstTimeView_min);

                // Modify buttons per configuration settings.
                var hideHelpConfiguration = _docmahConfiguration.PopupViewerConfiguration.HidePopupButtonConfiguration;
                popupViewContent = popupViewContent.Replace("[HIDEHELPBUTTON]", CreateButtonHelp(hideHelpConfiguration.IsHidden, HtmlContent.HideHelpButton, hideHelpConfiguration.Text, hideHelpConfiguration.Description));
                var closeHelpConfiguration = _docmahConfiguration.PopupViewerConfiguration.ClosePopupButtonConfiguration;
                popupViewContent = popupViewContent.Replace("[CLOSEHELPBUTTON]", CreateButtonHelp(closeHelpConfiguration.IsHidden, HtmlContent.CloseHelpButton, closeHelpConfiguration.Text, closeHelpConfiguration.Description));

                // When injecting into other requests, the initialization scripts must be included.
                result += popupViewContent;

                // Attach jQueryUi CDN locations if not configured.
                var javaScriptDependencies = _docmahConfiguration.JsUrl;
                if (string.IsNullOrEmpty(javaScriptDependencies))
                {
                    result += string.Format("<script src='{0}' type='application/javascript'></script>", CdnUrls.jsJQuery);
                    result += string.Format("<script src='{0}' type='application/javascript'></script>", CdnUrls.jsJQueryUi);
                }

                result += _minifier.Minify(HtmlContent.FirstTimeViewInjectedScripts, HtmlContent.FirstTimeViewInjectedScripts_min);

                _memoryCache.Set(_firstTimeHelpHtmlCacheKey, result, new CacheItemPolicy());
            }

            // Begin reading request specific values.
            if (_editAuthorizer.Authorize())
            {
                result += _minifier.Minify(HtmlContent.FirstTimeEdit, HtmlContent.FirstTimeEdit_min);
            }

            var requestUrl = HttpContext.Current.Request.Url.AbsolutePath;
            var page       = _firstTimeHelpRepository.ReadByUrl(requestUrl.Replace('*', '%'));
            UserPageSettings userPageSettings = null;

            if (null != page)
            {
                page.Bullets = _bulletRepository.ReadByPageId(page.Id);
                if (_httpContext.Request.IsAuthenticated)
                {
                    var userName = _httpContext.User.Identity.Name;
                    userPageSettings = _userPageSettingsRepository.Read(userName, page.Id);
                }
            }

            var serializer = new JavaScriptSerializer();
            var pageJson   = serializer.Serialize(page);

            result = result.Replace("[PAGEJSON]", pageJson);

            var userPageSettingsJson = serializer.Serialize(userPageSettings);

            result = result.Replace("[USERPAGESETTINGSJSON]", userPageSettingsJson);

            // TODO: Refactor application settings creation into factory. Replace here and application settings request processor.
            var applicationSettings = new ApplicationSettings {
                CanEdit = _editAuthorizer.Authorize(), DisableDocumentation = _docmahConfiguration.DocumentationConfiguration.Disabled
            };
            var applicationSettingsJson = serializer.Serialize(applicationSettings);

            result = result.Replace("[APPLICATIONSETTINGSJSON]", applicationSettingsJson);

            return(result);
        }