/// <summary>
        /// Creates a new instance of MocksAndStubsContainer.
        /// </summary>
        /// <param name="useCacheMock">The 'Roadkill' MemoryCache is used by default, but as this is static it can have problems with 
        /// the test runner unless you clear the Container.MemoryCache on setup each time, but then doing that doesn't give a realistic 
        /// reflection of how the MemoryCache is used inside an ASP.NET environment.</param>
        public MocksAndStubsContainer(bool useCacheMock = false)
        {
            ApplicationSettings = new ApplicationSettings();
            ApplicationSettings.Installed = true;
            ApplicationSettings.AttachmentsFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "attachments");

            // Cache
            MemoryCache = useCacheMock ? new CacheMock() : CacheMock.RoadkillCache;
            ListCache = new ListCache(ApplicationSettings, MemoryCache);
            SiteCache = new SiteCache(ApplicationSettings, MemoryCache);
            PageViewModelCache = new PageViewModelCache(ApplicationSettings, MemoryCache);

            // Repository
            Repository = new RepositoryMock();
            Repository.SiteSettings = new SiteSettings();
            Repository.SiteSettings.MarkupType = "Creole";

            PluginFactory = new PluginFactoryMock();
            MarkupConverter = new MarkupConverter(ApplicationSettings, Repository, PluginFactory);

            // Dependencies for PageService. Be careful to make sure the class using this Container isn't testing the mock.
            SettingsService = new SettingsService(ApplicationSettings, Repository);
            UserService = new UserServiceMock(ApplicationSettings, Repository);
            UserContext = new UserContext(UserService);
            SearchService = new SearchServiceMock(ApplicationSettings, Repository, PluginFactory);
            SearchService.PageContents = Repository.PageContents;
            SearchService.Pages = Repository.Pages;
            HistoryService = new PageHistoryService(ApplicationSettings, Repository, UserContext, PageViewModelCache, PluginFactory);

            PageService = new PageService(ApplicationSettings, Repository, SearchService, HistoryService, UserContext, ListCache, PageViewModelCache, SiteCache, PluginFactory);

            // EmailTemplates
            EmailClient = new EmailClientMock();
        }
Example #2
0
        public void Setup()
        {
            _container = new MocksAndStubsContainer();

            _applicationSettings = _container.ApplicationSettings;
            _context             = _container.UserContext;

            _settingsRepository = _container.SettingsRepository;
            _userRepository     = _container.UserRepository;
            _pageRepository     = _container.PageRepository;

            _settingsService = _container.SettingsService;
            _userService     = _container.UserService;
            _pageCache       = _container.PageViewModelCache;
            _listCache       = _container.ListCache;
            _siteCache       = _container.SiteCache;
            _cache           = _container.MemoryCache;
            _container.ClearCache();

            _pageService   = _container.PageService;
            _wikiImporter  = new WikiImporterMock();
            _pluginFactory = _container.PluginFactory;
            _searchService = _container.SearchService;

            // There's no point mocking WikiExporter (and turning it into an interface) as
            // a lot of usefulness of these tests would be lost when creating fake Streams and zip files.
            _wikiExporter = new WikiExporter(_applicationSettings, _pageService, _settingsRepository, _pageRepository, _userRepository, _pluginFactory);
            _wikiExporter.ExportFolder = AppDomain.CurrentDomain.BaseDirectory;

            _toolsController = new ToolsController(_applicationSettings, _userService, _settingsService, _pageService,
                                                   _searchService, _context, _listCache, _pageCache, _wikiImporter,
                                                   _pluginFactory, _wikiExporter);
        }
Example #3
0
        private PageService CreatePageService(ObjectCache pageObjectCache, ObjectCache listObjectCache, SettingsRepositoryMock settingsRepository, PageRepositoryMock pageRepository)
        {
            // Stick to memorycache when each one isn't used
            if (pageObjectCache == null)
            {
                pageObjectCache = CacheMock.RoadkillCache;
            }

            if (listObjectCache == null)
            {
                listObjectCache = CacheMock.RoadkillCache;
            }

            // Settings
            ApplicationSettings appSettings = new ApplicationSettings()
            {
                Installed = true, UseObjectCache = true
            };
            UserContextStub userContext = new UserContextStub()
            {
                IsLoggedIn = false
            };

            // PageService
            PageViewModelCache pageViewModelCache = new PageViewModelCache(appSettings, pageObjectCache);
            ListCache          listCache          = new ListCache(appSettings, listObjectCache);
            SiteCache          siteCache          = new SiteCache(CacheMock.RoadkillCache);
            SearchServiceMock  searchService      = new SearchServiceMock(appSettings, settingsRepository, pageRepository, _pluginFactory);
            PageHistoryService historyService     = new PageHistoryService(appSettings, settingsRepository, pageRepository, userContext, pageViewModelCache, _pluginFactory);
            PageService        pageService        = new PageService(appSettings, settingsRepository, pageRepository, searchService, historyService, userContext, listCache, pageViewModelCache, siteCache, _pluginFactory);

            return(pageService);
        }
Example #4
0
		public void Setup()
		{
			_container = new MocksAndStubsContainer();	

			_applicationSettings = _container.ApplicationSettings;
			_context = _container.UserContext;
			_repository = _container.Repository;
			_settingsService = _container.SettingsService;
			_userService = _container.UserService;
			_pageCache = _container.PageViewModelCache;
			_listCache = _container.ListCache;
			_siteCache = _container.SiteCache;
			_cache = _container.MemoryCache;
			_container.ClearCache();

			_pageService = _container.PageService;
			_wikiImporter = new WikiImporterMock();
			_pluginFactory = _container.PluginFactory;
			_searchService = _container.SearchService;

			// There's no point mocking WikiExporter (and turning it into an interface) as it 
			// a lot of usefulness of these tests would be lost when creating fake Streams and zip files.
			_wikiExporter = new WikiExporter(_applicationSettings, _pageService, _repository, _pluginFactory);
			_wikiExporter.ExportFolder = AppDomain.CurrentDomain.BaseDirectory;

			_toolsController = new ToolsController(_applicationSettings, _userService, _settingsService, _pageService,
													_searchService, _context, _listCache, _pageCache, _wikiImporter, 
													_repository, _pluginFactory, _wikiExporter);
		}
Example #5
0
        public void CheckConstructor()
        {
            SearchServiceMock mock = new SearchServiceMock();
            var service            = new SearchServiceRealizer(mock.data);

            Assert.NotNull(service);
            Assert.Throws <ArgumentNullException>(() => new SearchServiceRealizer(null));
        }
Example #6
0
        public void CheckOnExceptions()
        {
            SearchServiceMock mock = new SearchServiceMock();
            var service            = new SearchServiceRealizer(mock.data);

            Assert.Throws <ArgumentException>(() => service.GetSearchResult(string.Empty));
            Assert.Throws <ArgumentNullException>(() => service.GetSearchResult(null));
        }
Example #7
0
        public async Task TestGoogleClientResults_ResultsOk()
        {
            //Arrange
            var googleMock = new GoogleMock();
            var service    = new SearchService(googleMock, new BingMock());

            service.SearchQueries = SearchServiceMock.BuildSearchQueries();

            //Act
            var actual = await service.PerformGoogleSearch();

            //Assert
            googleMock.ExpectedResults.Should().BeEquivalentTo(actual);
        }
Example #8
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public static void ConfigureServices(IServiceCollection services)
        {
            SearchServiceMock mock = new SearchServiceMock();

            services.AddControllers();
            services.AddSingleton((System.Func <System.IServiceProvider, ISearchService>)(options => new SearchServiceRealizer(mock.data)));
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title   = "SearchService",
                    Version = "v1",
                });
            });
            services.AddCors(); // добавляем сервисы CORS
        }
Example #9
0
            public async Task Not_return_deleted_costs()
            {
                // Arrange
                var costId = Guid.NewGuid();
                var cost   = new Cost
                {
                    Id      = costId,
                    Deleted = true
                };
                var userId = Guid.NewGuid();
                var user   = new UserIdentity {
                    Id = userId
                };

                ActionBuilderMock.Setup(a => a.GetActions(It.IsAny <IEnumerable <Guid> >(), user))
                .ReturnsAsync(new Dictionary <Guid, CostActionsModel> {
                    {
                        costId,
                        new CostActionsModel(cost, new Dictionary <string, ActionModel>())
                    }
                }
                              );

                EFContext.Cost.Add(cost);
                await EFContext.SaveChangesAsync();

                var costQuery = new CostQuery();

                SearchServiceMock.Setup(ss => ss.SearchCosts(costQuery, user.Id)).ReturnsAsync((
                                                                                                   new[] {
                    new CostSearchItem
                    {
                        Id = costId.ToString(),
                        CreatedBy = userId.ToString()
                    }
                },
                                                                                                   1)
                                                                                               );

                // Act
                var searchResult = await CostService.Search(costQuery, user);

                // Assert
                searchResult.Count.Should().Be(0);
                searchResult.Costs.Count.Should().Be(0);
            }
Example #10
0
		public void Setup()
		{
			_container = new MocksAndStubsContainer();

			_applicationSettings = _container.ApplicationSettings;
			_applicationSettings.Installed = false;

			_context = _container.UserContext;
			_repository = _container.Repository;
			_pluginFactory = _container.PluginFactory;
			_settingsService = _container.SettingsService;
			_userService = _container.UserService;
			_historyService = _container.HistoryService;
			_pageService = _container.PageService;
			_searchService = _container.SearchService;
			_configReaderWriter = new ConfigReaderWriterStub();

			_installController = new InstallController(_applicationSettings, _userService, _pageService, _searchService, _repository, _settingsService, _context, _configReaderWriter);
		}
Example #11
0
		public void Setup()
		{
			_container = new MocksAndStubsContainer();

			_applicationSettings = _container.ApplicationSettings;

			_settingsRepository = _container.SettingsRepository;
			_pageRepository = _container.PageRepository;
			
			_pluginFactory = _container.PluginFactory;
			_settingsService = _container.SettingsService;
			_userService = _container.UserService;
			_historyService = _container.HistoryService;
			_markupConverter = _container.MarkupConverter;
			_searchService = _container.SearchService;

			// Use a stub instead of the MocksAndStubsContainer's default
			_contextStub = new UserContextStub();

			// Customise the page service so we can verify what was called
			_pageServiceMock = new Mock<IPageService>();
			_pageServiceMock.Setup(x => x.GetMarkupConverter()).Returns(new MarkupConverter(_applicationSettings, _settingsRepository, _pageRepository, _pluginFactory));
			_pageServiceMock.Setup(x => x.GetById(It.IsAny<int>(), false)).Returns<int, bool>((int id, bool loadContent) =>
				{
					Page page = _pageRepository.GetPageById(id);
					return new PageViewModel(page);
				});
			_pageServiceMock.Setup(x => x.GetById(It.IsAny<int>(), true)).Returns<int,bool>((int id, bool loadContent) =>
			{
				PageContent content = _pageRepository.GetLatestPageContent(id);

				if (content != null)
					return new PageViewModel(content, _markupConverter);
				else
					return null;
			});
			_pageServiceMock.Setup(x => x.FindByTag(It.IsAny<string>()));
			_pageService = _pageServiceMock.Object;

			_pagesController = new PagesController(_applicationSettings, _userService, _settingsService, _pageService, _searchService, _historyService, _contextStub);
			_mocksContainer = _pagesController.SetFakeControllerContext();
		}
Example #12
0
		public void Setup()
		{
			_container = new MocksAndStubsContainer();

			_applicationSettings = _container.ApplicationSettings;
			_context = _container.UserContext;
			_settingsService = _container.SettingsService;
			_userService = _container.UserService;

			_controller = new ControllerBaseStub(_applicationSettings, _userService, _context, _settingsService);
			MvcMockContainer container = _controller.SetFakeControllerContext("~/");

			// Used by InstallController
			_repository = _container.Repository;
			_pluginFactory = _container.PluginFactory;
			_historyService = _container.HistoryService;
			_pageService = _container.PageService;
			_searchService = _container.SearchService;
			_configReaderWriter = new ConfigReaderWriterStub();	
		}
Example #13
0
        private WikiController CreateWikiController(BrowserCacheAttribute attribute)
        {
            // Settings
            ApplicationSettings appSettings = new ApplicationSettings()
            {
                Installed = true, UseBrowserCache = true
            };
            UserContextStub userContext = new UserContextStub()
            {
                IsLoggedIn = false
            };

            // PageService
            PageViewModelCache pageViewModelCache = new PageViewModelCache(appSettings, CacheMock.RoadkillCache);
            ListCache          listCache          = new ListCache(appSettings, CacheMock.RoadkillCache);
            SiteCache          siteCache          = new SiteCache(appSettings, CacheMock.RoadkillCache);
            SearchServiceMock  searchService      = new SearchServiceMock(appSettings, _repositoryMock, _pluginFactory);
            PageHistoryService historyService     = new PageHistoryService(appSettings, _repositoryMock, userContext, pageViewModelCache, _pluginFactory);
            PageService        pageService        = new PageService(appSettings, _repositoryMock, searchService, historyService, userContext, listCache, pageViewModelCache, siteCache, _pluginFactory);

            // WikiController
            SettingsService settingsService = new SettingsService(appSettings, _repositoryMock);
            UserServiceStub userManager     = new UserServiceStub();
            WikiController  wikiController  = new WikiController(appSettings, userManager, pageService, userContext, settingsService);

            // Create a page that the request is for
            Page page = new Page()
            {
                Title = "title", ModifiedOn = _pageModifiedDate
            };

            _repositoryMock.AddNewPage(page, "text", "user", _pageCreatedDate);

            // Update the BrowserCacheAttribute
            attribute.ApplicationSettings = appSettings;
            attribute.Context             = userContext;
            attribute.PageService         = pageService;

            return(wikiController);
        }
Example #14
0
		public void Setup()
		{
			_container = new MocksAndStubsContainer();

			_applicationSettings = _container.ApplicationSettings;
			_repository = _container.Repository;
			_pluginFactory = _container.PluginFactory;
			_settingsService = _container.SettingsService;
			_userService = _container.UserService;
			_historyService = _container.HistoryService;
			_markupConverter = _container.MarkupConverter;
			_searchService = _container.SearchService;

			// Use a stub instead of the MocksAndStubsContainer's default
			_contextStub = new UserContextStub();

			// Customise the page service so we can verify what was called
			_pageServiceMock = new Mock<IPageService>();
			_pageServiceMock.Setup(x => x.GetMarkupConverter()).Returns(new MarkupConverter(_applicationSettings, _repository, _pluginFactory));
			_pageServiceMock.Setup(x => x.GetById(It.IsAny<int>(), false)).Returns<int, bool>((int id, bool loadContent) =>
				{
					Page page = _repository.GetPageById(id);
					return new PageViewModel(page);
				});
			_pageServiceMock.Setup(x => x.GetById(It.IsAny<int>(), true)).Returns<int,bool>((int id, bool loadContent) =>
			{
				PageContent content = _repository.GetLatestPageContent(id);

				if (content != null)
					return new PageViewModel(content, _markupConverter);
				else
					return null;
			});
			_pageServiceMock.Setup(x => x.FindByTag(It.IsAny<string>()));
			_pageService = _pageServiceMock.Object;

			_pagesController = new PagesController(_applicationSettings, _userService, _settingsService, _pageService, _searchService, _historyService, _contextStub);
			_mocksContainer = _pagesController.SetFakeControllerContext();
		}
Example #15
0
		public void Setup()
		{
			_container = new MocksAndStubsContainer();

			_applicationSettings = _container.ApplicationSettings;
			_context = _container.UserContext;
			_repository = _container.Repository;
			_pluginFactory = _container.PluginFactory;
			_settingsService = _container.SettingsService;
			_userService = _container.UserService;
			_historyService = _container.HistoryService;
			_pageService = _container.PageService;
			_searchService = _container.SearchService;
			_markupConverter = _container.MarkupConverter;

			_listCache = _container.ListCache;
			_siteCache = _container.SiteCache;
			_pageViewModelCache = _container.PageViewModelCache;
			_memoryCache = _container.MemoryCache;

			_homeController = new HomeController(_applicationSettings, _userService, _markupConverter, _pageService, _searchService, _context, _settingsService);
			_homeController.SetFakeControllerContext();
		}
Example #16
0
        public void Setup()
        {
            _container = new MocksAndStubsContainer();

            _applicationSettings = _container.ApplicationSettings;
            _context             = _container.UserContext;
            _pageRepository      = _container.PageRepository;
            _pluginFactory       = _container.PluginFactory;
            _settingsService     = _container.SettingsService;
            _userService         = _container.UserService;
            _historyService      = _container.HistoryService;
            _pageService         = _container.PageService;
            _searchService       = _container.SearchService;
            _markupConverter     = _container.MarkupConverter;

            _listCache          = _container.ListCache;
            _siteCache          = _container.SiteCache;
            _pageViewModelCache = _container.PageViewModelCache;
            _memoryCache        = _container.MemoryCache;

            _homeController = new HomeController(_applicationSettings, _userService, _markupConverter, _pageService, _searchService, _context, _settingsService);
            _homeController.SetFakeControllerContext();
        }
		/// <summary>
		/// Creates a new instance of MocksAndStubsContainer.
		/// </summary>
		/// <param name="useCacheMock">The 'Roadkill' MemoryCache is used by default, but as this is static it can have problems with 
		/// the test runner unless you clear the Container.MemoryCache on setup each time, but then doing that doesn't give a realistic 
		/// reflection of how the MemoryCache is used inside an ASP.NET environment.</param>
		public MocksAndStubsContainer(bool useCacheMock = false)
		{
			ApplicationSettings = new ApplicationSettings();
			ApplicationSettings.Installed = true;
			ApplicationSettings.AttachmentsFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "attachments");
			ConfigReaderWriter = new ConfigReaderWriterStub();

			// Cache
			MemoryCache = useCacheMock ? new CacheMock() : CacheMock.RoadkillCache;
			ListCache = new ListCache(ApplicationSettings, MemoryCache);
			SiteCache = new SiteCache(MemoryCache);
			PageViewModelCache = new PageViewModelCache(ApplicationSettings, MemoryCache);

			// Repositories
			SettingsRepository = new SettingsRepositoryMock();
			SettingsRepository.SiteSettings = new SiteSettings();
			SettingsRepository.SiteSettings.MarkupType = "Creole";
			UserRepository = new UserRepositoryMock();
			PageRepository = new PageRepositoryMock();
			InstallerRepository = new InstallerRepositoryMock();

			RepositoryFactory = new RepositoryFactoryMock()
			{
				SettingsRepository = SettingsRepository,
				UserRepository = UserRepository,
				PageRepository = PageRepository,
				InstallerRepository = InstallerRepository
			};
			DatabaseTester = new DatabaseTesterMock();

			// Plugins
			PluginFactory = new PluginFactoryMock();
			MarkupConverter = new MarkupConverter(ApplicationSettings, SettingsRepository, PageRepository, PluginFactory);

			// Services
			// Dependencies for PageService. Be careful to make sure the class using this Container isn't testing the mock.
			SettingsService = new SettingsService(RepositoryFactory, ApplicationSettings);
			UserService = new UserServiceMock(ApplicationSettings, UserRepository);
			UserContext = new UserContext(UserService);
			SearchService = new SearchServiceMock(ApplicationSettings, SettingsRepository, PageRepository, PluginFactory);
			SearchService.PageContents = PageRepository.PageContents;
			SearchService.Pages = PageRepository.Pages;
			HistoryService = new PageHistoryService(ApplicationSettings, SettingsRepository, PageRepository, UserContext,
				PageViewModelCache, PluginFactory);
			FileService = new FileServiceMock();
			PageService = new PageService(ApplicationSettings, SettingsRepository, PageRepository, SearchService, HistoryService,
				UserContext, ListCache, PageViewModelCache, SiteCache, PluginFactory);

			StructureMapContainer = new Container(x =>
			{
				x.AddRegistry(new TestsRegistry(this));
			});

			Locator = new StructureMapServiceLocator(StructureMapContainer, false);

			InstallationService = new InstallationService((databaseName, connectionString) =>
			{
				InstallerRepository.DatabaseName = databaseName;
				InstallerRepository.ConnectionString = connectionString;

				return InstallerRepository;
			}, Locator);

			// EmailTemplates
			EmailClient = new EmailClientMock();
		}