public static async Task StaleUploads_Success() { List <int> expectedIds = Enumerable.Range(0, 3).ToList(); List <IDictionary <string, object> > datasets = expectedIds .Select <int, IDictionary <string, object> >(id => new Dictionary <string, object> { { "Id", id } }) .ToList(); Mock <IDataReader> mockDataReader = MockDatabaseHelper.CreateMockDataReader(datasets); Mock <IDatabaseCommand> mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(new Dictionary <string, object>(), mockDataReader.Object); Mock <IDatabaseCommandFactory> mockDatabaseCommandFactory = new(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); AdminController controller = new(mockDatabaseCommandFactory.Object); Microsoft.AspNetCore.Mvc.ActionResult <List <int> > result = await controller.StaleUploadsAsync(); Assert.NotNull(result); List <int> actualIds = result.Value; Assert.Equal(expectedIds.Count, actualIds.Count); for (int i = 0; i < actualIds.Count; i++) { Assert.Equal(expectedIds[i], actualIds[i]); } mockDatabaseCommandFactory.VerifyAll(); }
public static async Task StaleUploads_Success() { var expectedIds = Enumerable.Range(0, 3).ToList(); var datasets = expectedIds .Select <int, IDictionary <string, object> >(id => new Dictionary <string, object> { { "Id", id } }) .ToList(); var mockDataReader = MockDatabaseHelper.CreateMockDataReader(datasets); var mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(new Dictionary <string, object>(), mockDataReader.Object); var mockDatabaseCommandFactory = new Mock <IDatabaseCommandFactory>(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); var controller = new AdminController(mockDatabaseCommandFactory.Object); var result = await controller.StaleUploads(); Assert.NotNull(result); var actualIds = result.Value; Assert.Equal(expectedIds.Count, actualIds.Count); for (var i = 0; i < actualIds.Count; i++) { Assert.Equal(expectedIds[i], actualIds[i]); } mockDatabaseCommandFactory.VerifyAll(); }
public static async Task Get_UnknownSettings() { var dataSets = new List <IDictionary <string, object> >(); var unknownSetting = new Dictionary <string, object> { { "SettingId", byte.MaxValue }, { "SettingValue", "true" } }; dataSets.Add(unknownSetting); foreach (var dataSet in CreateDataSetsForAllSettings()) { dataSets.Add(dataSet); dataSets.Add(unknownSetting); } var mockDataReader = MockDatabaseHelper.CreateMockDataReader(dataSets); var mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(new Dictionary <string, object> { { "@UserId", UserId } }, mockDataReader.Object); var mockDatabaseCommandFactory = new Mock <IDatabaseCommandFactory>(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); var provider = new UserSettingsProvider(mockDatabaseCommandFactory.Object); var settings = await provider.GetAsync(UserId); VerifyPopulatedSettings(settings); mockDataReader.VerifyAll(); mockDatabaseCommand.VerifyAll(); mockDatabaseCommandFactory.VerifyAll(); }
public static async Task PruneInvalidAuthTokens_Success() { var model = new PruneInvalidAuthTokensRequest { BatchSize = 100, }; var mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(new Dictionary <string, object> { { "BatchSize", model.BatchSize } }); var mockDatabaseCommandFactory = new Mock <IDatabaseCommandFactory>(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); var mockUploadScheduler = new Mock <IUploadScheduler>(MockBehavior.Strict); var mockUserManager = MockUserManager.CreateMock(); var controller = new AdminController( mockDatabaseCommandFactory.Object, mockUploadScheduler.Object, mockUserManager.Object); await controller.PruneInvalidAuthTokens(model); mockDatabaseCommandFactory.VerifyAll(); mockUploadScheduler.VerifyAll(); // Workaround for a Moq bug. See: https://github.com/moq/moq4/issues/456#issuecomment-331692858 mockUserManager.Object.Logger = mockUserManager.Object.Logger; mockUserManager.VerifyAll(); }
public static async Task ListAuthTokens_Success() { const int NumInvalidTokens = 100; var mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(new Dictionary <string, object>(), NumInvalidTokens); var mockDatabaseCommandFactory = new Mock <IDatabaseCommandFactory>(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); var mockUploadScheduler = new Mock <IUploadScheduler>(MockBehavior.Strict); var mockUserManager = MockUserManager.CreateMock(); var controller = new AdminController( mockDatabaseCommandFactory.Object, mockUploadScheduler.Object, mockUserManager.Object); var result = await controller.CountInvalidAuthTokens(); Assert.NotNull(result); var actualCount = result.Value; Assert.Equal(NumInvalidTokens, actualCount); mockDatabaseCommandFactory.VerifyAll(); mockUploadScheduler.VerifyAll(); // Workaround for a Moq bug. See: https://github.com/moq/moq4/issues/456#issuecomment-331692858 mockUserManager.Object.Logger = mockUserManager.Object.Logger; mockUserManager.VerifyAll(); }
public static async Task Get_InvalidSettings() { List <IDictionary <string, object> > dataSets = new(); foreach (IDictionary <string, object> dataSet in CreateDataSetsForAllSettings()) { dataSet["SettingValue"] = "SomethingInvalid"; dataSets.Add(dataSet); } Mock <IDataReader> mockDataReader = MockDatabaseHelper.CreateMockDataReader(dataSets); Mock <IDatabaseCommand> mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(new Dictionary <string, object> { { "@UserId", UserId } }, mockDataReader.Object); Mock <IDatabaseCommandFactory> mockDatabaseCommandFactory = new(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); UserSettingsProvider provider = new(mockDatabaseCommandFactory.Object); UserSettings settings = await provider.GetAsync(UserId); VerifyDefaultSettings(settings); mockDataReader.VerifyAll(); mockDatabaseCommand.VerifyAll(); mockDatabaseCommandFactory.VerifyAll(); }
/// <summary> /// Gets the mocked security controller, with usermanager and request calls mocked. /// </summary> /// <returns></returns> public Mock <AnnouncementController> GetMockedAnnouncementController() { Mock <AnnouncementController> controller = new Mock <AnnouncementController>(); controller.Setup(c => c.GetDatabaseContext()).Returns(MockDatabaseHelper.MockAnnouncementDatabaseCreator()); controller.CallBase = true; return(controller); }
/// <summary> /// Gets the mocked chat controller. /// </summary> /// <returns></returns> public Mock <ChatController> GetMockedChatController() { Mock <ChatController> controller = new Mock <ChatController>(); controller.CallBase = true; controller.Setup(c => c.GetDatabaseContext()).Returns(MockDatabaseHelper.MockChatDatabaseContext()); return(controller); }
/// <summary> /// Gets the mocked task controller. /// </summary> /// <returns></returns> public Mock <TaskController> GetMockedTaskController() { Mock <TaskController> controller = new Mock <TaskController>(MockUserManager.GetMockUserManager().Object); controller.CallBase = true; controller.Setup(c => c.GetCurrentUser()).Returns(new User() { UserName = "******", Id = "1" }); controller.Setup(c => c.GetDatabaseContext()).Returns(MockDatabaseHelper.MockTaskDatabaseCreator()); return(controller); }
public void View_NotExistingUpload() { var user = new ClaimsPrincipal(); var mockDataReader = MockDatabaseHelper.CreateMockDataReader(null); var mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(new Dictionary <string, object> { { "@UploadId", 1 } }, mockDataReader.Object); var mockDatabaseCommandFactory = new Mock <IDatabaseCommandFactory>(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); var mockUserSettingsProvider = new Mock <IUserSettingsProvider>(MockBehavior.Strict); var userManager = new MockUserManager(); var mockHttpContext = new Mock <HttpContext>(MockBehavior.Strict); mockHttpContext.SetupGet(_ => _.User).Returns(user).Verifiable(); mockHttpContext.SetupGet(_ => _.RequestServices).Returns((IServiceProvider)null).Verifiable(); var controller = new CalculatorController( mockDatabaseCommandFactory.Object, mockUserSettingsProvider.Object, userManager); controller.ControllerContext = new ControllerContext { HttpContext = mockHttpContext.Object }; var result = controller.View(1); Assert.NotNull(result); Assert.IsType <ViewResult>(result); var viewResult = (ViewResult)result; Assert.Null(viewResult.Model); Assert.Equal("Error", viewResult.ViewName); Assert.Equal("The upload does not exist", viewResult.ViewData["ErrorMessage"]); mockDataReader.Verify(); mockDatabaseCommand.Verify(); mockDatabaseCommandFactory.Verify(); mockUserSettingsProvider.Verify(); mockHttpContext.Verify(); }
public static async Task Patch_Success() { var settings = new UserSettings { PlayStyle = PlayStyle.Hybrid, UseScientificNotation = true, ScientificNotationThreshold = 1, UseLogarithmicGraphScale = true, LogarithmicGraphScaleThreshold = 2, HybridRatio = 3, Theme = SiteThemeType.Dark, ShouldLevelSkillAncients = true, SkillAncientBaseAncient = 4, SkillAncientLevelDiff = 5, GraphSpacingType = GraphSpacingType.Ascension, }; var parameters = new Dictionary <string, object> { { "@UserId", UserId }, { "@Value" + UserSettingsConstants.PlayStyle, settings.PlayStyle.ToString() }, { "@Value" + UserSettingsConstants.UseScientificNotation, settings.UseScientificNotation.ToString() }, { "@Value" + UserSettingsConstants.ScientificNotationThreshold, settings.ScientificNotationThreshold.ToString() }, { "@Value" + UserSettingsConstants.UseLogarithmicGraphScale, settings.UseLogarithmicGraphScale.ToString() }, { "@Value" + UserSettingsConstants.LogarithmicGraphScaleThreshold, settings.LogarithmicGraphScaleThreshold.ToString() }, { "@Value" + UserSettingsConstants.HybridRatio, settings.HybridRatio.ToString() }, { "@Value" + UserSettingsConstants.Theme, settings.Theme.ToString() }, { "@Value" + UserSettingsConstants.ShouldLevelSkillAncients, settings.ShouldLevelSkillAncients.ToString() }, { "@Value" + UserSettingsConstants.SkillAncientBaseAncient, settings.SkillAncientBaseAncient.ToString() }, { "@Value" + UserSettingsConstants.SkillAncientLevelDiff, settings.SkillAncientLevelDiff.ToString() }, { "@Value" + UserSettingsConstants.GraphSpacingType, settings.GraphSpacingType.ToString() }, }; var mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(parameters); var mockDatabaseCommandFactory = new Mock <IDatabaseCommandFactory>(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); var provider = new UserSettingsProvider(mockDatabaseCommandFactory.Object); await provider.PatchAsync(UserId, settings); mockDatabaseCommand.VerifyAll(); mockDatabaseCommandFactory.VerifyAll(); }
public static async Task Get_MissingSettings() { Mock <IDataReader> mockDataReader = MockDatabaseHelper.CreateMockDataReader(); Mock <IDatabaseCommand> mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(new Dictionary <string, object> { { "@UserId", UserId } }, mockDataReader.Object); Mock <IDatabaseCommandFactory> mockDatabaseCommandFactory = new(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); UserSettingsProvider provider = new(mockDatabaseCommandFactory.Object); UserSettings settings = await provider.GetAsync(UserId); VerifyDefaultSettings(settings); mockDataReader.VerifyAll(); mockDatabaseCommand.VerifyAll(); mockDatabaseCommandFactory.VerifyAll(); }
public static async Task ListAuthTokens_Success() { const int NumInvalidTokens = 100; var mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(new Dictionary <string, object>(), NumInvalidTokens); var mockDatabaseCommandFactory = new Mock <IDatabaseCommandFactory>(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); var controller = new AdminController(mockDatabaseCommandFactory.Object); var result = await controller.CountInvalidAuthTokens(); Assert.NotNull(result); var actualCount = result.Value; Assert.Equal(NumInvalidTokens, actualCount); mockDatabaseCommandFactory.VerifyAll(); }
public static async Task PruneInvalidAuthTokens_Success() { var model = new PruneInvalidAuthTokensRequest { BatchSize = 100, }; var mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(new Dictionary <string, object> { { "BatchSize", model.BatchSize } }); var mockDatabaseCommandFactory = new Mock <IDatabaseCommandFactory>(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); var controller = new AdminController(mockDatabaseCommandFactory.Object); await controller.PruneInvalidAuthTokens(model); mockDatabaseCommandFactory.VerifyAll(); }
public static async Task Get_Success() { List <IDictionary <string, object> > dataSets = CreateDataSetsForAllSettings(); Mock <IDataReader> mockDataReader = MockDatabaseHelper.CreateMockDataReader(dataSets); Mock <IDatabaseCommand> mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(new Dictionary <string, object> { { "@UserId", UserId } }, mockDataReader.Object); Mock <IDatabaseCommandFactory> mockDatabaseCommandFactory = new(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); UserSettingsProvider provider = new(mockDatabaseCommandFactory.Object); UserSettings settings = await provider.GetAsync(UserId); VerifyPopulatedSettings(settings); mockDataReader.VerifyAll(); mockDatabaseCommand.VerifyAll(); mockDatabaseCommandFactory.VerifyAll(); }
public static async Task StaleUploads_Success() { var expectedIds = Enumerable.Range(0, 3).ToList(); var datasets = expectedIds .Select <int, IDictionary <string, object> >(id => new Dictionary <string, object> { { "Id", id } }) .ToList(); var mockDataReader = MockDatabaseHelper.CreateMockDataReader(datasets); var mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(new Dictionary <string, object>(), mockDataReader.Object); var mockDatabaseCommandFactory = new Mock <IDatabaseCommandFactory>(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); var mockUploadScheduler = new Mock <IUploadScheduler>(MockBehavior.Strict); var mockUserManager = MockUserManager.CreateMock(); var controller = new AdminController( mockDatabaseCommandFactory.Object, mockUploadScheduler.Object, mockUserManager.Object); var result = await controller.StaleUploads(); Assert.NotNull(result); var actualIds = result.Value; Assert.Equal(expectedIds.Count, actualIds.Count); for (var i = 0; i < actualIds.Count; i++) { Assert.Equal(expectedIds[i], actualIds[i]); } mockDatabaseCommandFactory.VerifyAll(); mockUploadScheduler.VerifyAll(); // Workaround for a Moq bug. See: https://github.com/moq/moq4/issues/456#issuecomment-331692858 mockUserManager.Object.Logger = mockUserManager.Object.Logger; mockUserManager.VerifyAll(); }
public static async Task Patch_PartialSettings() { var settings = new UserSettings { PlayStyle = PlayStyle.Hybrid, }; var parameters = new Dictionary <string, object> { { "@UserId", UserId }, { "@Value" + UserSettingsConstants.PlayStyle, settings.PlayStyle.ToString() }, }; var mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(parameters); var mockDatabaseCommandFactory = new Mock <IDatabaseCommandFactory>(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); var provider = new UserSettingsProvider(mockDatabaseCommandFactory.Object); await provider.PatchAsync(UserId, settings); mockDatabaseCommand.VerifyAll(); mockDatabaseCommandFactory.VerifyAll(); }
public void View_ExistingUpload(string userId, string uploadUserId, bool isPublic, bool expectedPermitted) { var claims = new List <Claim>(); if (userId != null) { claims.Add(new Claim(ClaimTypes.NameIdentifier, userId)); claims.Add(new Claim(ClaimTypes.Role, userId)); } var user = new ClaimsPrincipal(new ClaimsIdentity(claims)); var mockDataReader = MockDatabaseHelper.CreateMockDataReader(new Dictionary <string, object> { { "UserId", uploadUserId } }); var mockDatabaseCommand = MockDatabaseHelper.CreateMockDatabaseCommand(new Dictionary <string, object> { { "@UploadId", 1 } }, mockDataReader.Object); var mockDatabaseCommandFactory = new Mock <IDatabaseCommandFactory>(MockBehavior.Strict); mockDatabaseCommandFactory.Setup(_ => _.Create()).Returns(mockDatabaseCommand.Object).Verifiable(); var mockUserSettings = new Mock <IUserSettings>(MockBehavior.Strict); if (uploadUserId != null) { mockUserSettings.SetupGet(_ => _.AreUploadsPublic).Returns(isPublic).Verifiable(); } var mockUserSettingsProvider = new Mock <IUserSettingsProvider>(MockBehavior.Strict); mockUserSettingsProvider.Setup(_ => _.Get(uploadUserId ?? string.Empty)).Returns(mockUserSettings.Object).Verifiable(); var userManager = new MockUserManager(); var mockHttpContext = new Mock <HttpContext>(MockBehavior.Strict); mockHttpContext.SetupGet(_ => _.User).Returns(user).Verifiable(); mockHttpContext.SetupGet(_ => _.RequestServices).Returns((IServiceProvider)null).Verifiable(); var controller = new CalculatorController( mockDatabaseCommandFactory.Object, mockUserSettingsProvider.Object, userManager); controller.ControllerContext = new ControllerContext { HttpContext = mockHttpContext.Object }; var result = controller.View(1); Assert.NotNull(result); Assert.IsType <ViewResult>(result); var viewResult = (ViewResult)result; if (expectedPermitted) { Assert.Equal("Calculator", viewResult.ViewName); Assert.NotNull(viewResult.Model); Assert.IsType <CalculatorViewModel>(viewResult.Model); var model = (CalculatorViewModel)viewResult.Model; Assert.Equal(1, model.UploadId); Assert.True(model.IsValid); Assert.True(model.IsPermitted); Assert.Equal(userId == uploadUserId, model.IsOwn); Assert.Equal(isPublic || uploadUserId == null, model.IsPublic); Assert.NotNull(model.SuggestedAncientIds); Assert.NotEmpty(model.SuggestedAncientIds); } else { Assert.Equal("Error", viewResult.ViewName); Assert.Null(viewResult.Model); Assert.Equal("This upload belongs to a user with private uploads", viewResult.ViewData["ErrorMessage"]); } mockDataReader.Verify(); mockDatabaseCommand.Verify(); mockDatabaseCommandFactory.Verify(); mockUserSettings.Verify(); mockUserSettingsProvider.Verify(); mockHttpContext.Verify(); }