public void TestReportUnimicroUserNotMappedException(int userId, int unitId) { var externalSystemFactory = ServicesProvider.GetService <IExternalSystemFactory>(); var entityMapService = A.Fake <IEntityMapService>(); A.CallTo(() => entityMapService.SearchEntityMaps(A <SearchQueryEntityMap> .Ignored)) .Returns(new List <EntityMap>()); var currentUserContext = new StaticUserContext(new UserContext { UserId = userId }); var timeRegService = new TimeRegServiceBuilder() .WithExternalSystemFactory(externalSystemFactory) .WithCurrentUserContext(currentUserContext) .WithIntegrationService(_integrationService) .WithValidator(new UnimicroValidator(_integrationService, entityMapService)) .Build(); ReportController Controller = new ReportController(timeRegService, Mapper); // Assert Assert.NotNull(Controller); var thrownException = Assert.ThrowsAsync <ForbiddenException>(async() => await Controller.GetHourBalance(unitId, 1337)); Assert.AreEqual("Employee is not mapped", thrownException.Message); }
public async Task Setup() { Action <IServiceCollection> actions = (sc) => { sc.ReplaceTransient <IDbContextFactory <PersonalCommonLegacyContext>, InMemoryPersonalCommonLegacyContextFactory>(); sc.ReplaceTransient <IDbContextFactory <PersonalLegacyContext>, InMemoryPersonalLegacyContextFactory>(); var usercontext = new StaticUserContext(new UserContext { UserId = _userId }); sc.Remove <ICurrentUserContext>(); sc.AddScoped <ICurrentUserContext>(i => usercontext); }; _testServer = new TestServerBuilder() .WithPostConfigureCollection(actions) .Build <Startup>(); _client = _testServer.CreateClientWithJwtToken(_customerId, _userId); var fakeCustomerIdService = A.Fake <ICustomerIdService>(); A.CallTo(() => fakeCustomerIdService.GetCustomerIdNotNull()).Returns(_customerId); var personalLegacyFactory = new InMemoryPersonalLegacyContextFactory(fakeCustomerIdService); _personalLegacyDb = await personalLegacyFactory.CreateDbContext(); var personalCommonLegacyFactory = _testServer.Host.Services.GetService <IDbContextFactory <PersonalCommonLegacyContext> >(); _personalCommonLegacyDb = await personalCommonLegacyFactory.CreateDbContext(); }
public static IServiceCollection AddMvc(this IServiceCollection services, bool addAuthorization, IConfiguration configuration, Type[] globalControllers = null) { if (addAuthorization) { services.AddMvc(options => { var policy = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() .Build(); options.Filters.Add(new AuthorizeFilter(policy)); options.UseCustomerIdRoutePrefix(globalControllers); }); services.AddJwtAuthentication(configuration); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddScoped <ICurrentUserContext, TokenUserContext>(); services.AddTransient <IAuthorizationContextService, AuthorizationContextService>(); } else { services.AddMvc(options => { options.UseCustomerIdRoutePrefix(globalControllers); options.Filters.Add(new AllowAnonymousFilter()); }); var userContext = new StaticUserContext(new UserContext { UserId = 89727 }); services.AddSingleton <ICurrentUserContext>(userContext); var authContextService = new StaticAuthorizationContextService(400423, 89727, configuration.GetValue <string>(AuthConstants.SystemUserSecret)); services.AddSingleton <IAuthorizationContextService>(authContextService); } return(services); }
public ServiceProvider BuildServices() { var services = new ServiceCollection(); var baseUrl = "http://localhost:5000/"; var configSettings = new Dictionary <string, string>() { { common.Constants.API_URL_CONFIG_KEY, baseUrl }, { Constants.API.API_URL_CONFIG_KEY, baseUrl }, { Integrations.Api.ProxyClient.Client.Constants.API_URL_CONFIG_KEY, baseUrl }, }; var configuration = new ConfigurationBuilder() .AddInMemoryCollection(configSettings) .Build(); // Set up default services services.AddScoped <IConfiguration>(c => configuration); services.AddIocMapping(); services.AddAutoMapper(AutoMapperSetup.Config); services.AddHttpClients(configuration); services.AddSharedServices(); var userContext = new StaticUserContext(new UserContext { UserId = 81730 }); services.AddSingleton <ICurrentUserContext>(userContext); _integrationService = A.Fake <IIntegrationService>(); _uniMicroClient = A.Fake <IUnimicroClient>(); _unitService = A.Fake <IUnitService>(); _entityMapService = A.Fake <IEntityMapService>(); _unitQueries = A.Fake <IUnitQueries>(); var absenceTypeService = A.Fake <IAbsenceTypeService>(); var fakeEmployeeService = A.Fake <IEmployeeService>(); var fakeAuthorizationContext = A.Fake <IAuthorizationContextService>(); services.ReplaceScoped <IUnitQueries>(_unitQueries); services.ReplaceScoped <IUnimicroClient>(_uniMicroClient); services.ReplaceScoped <IEntityMapService>(_entityMapService); services.ReplaceScoped <IUnitService>(_unitService); services.ReplaceScoped <IEmployeeService>(fakeEmployeeService); services.ReplaceScoped <IAbsenceTypeService>(absenceTypeService); services.ReplaceScoped <IAuthorizationContextService>(fakeAuthorizationContext); services.ReplaceSingleton(_integrationService); services.ReplaceScoped <IDbContextFactory <TimeregDbContext>, InMemoryDbContextFactory>(); services.AddScoped <IConsumer <IAbsenceApproved>, AbsenceApprovedConsumer>(); services.AddScoped <IConsumer <IAbsenceDeleted>, AbsenceDeletedConsumer>(); services.AddScoped <IConsumer <IIntegrationDeleted>, IntegrationDeleteConsumer>(); services.AddScoped <IConsumer <IEmployeeDeleted>, EmployeeDeletedConsumer>(); return(services.BuildServiceProvider()); }
public void ConfigureServices(IServiceCollection services) { //override or replace services in ioc here services.ReplaceTransient <IDbContextFactory <PersonalLegacyContext>, InMemoryPersonalLegacyContextFactory>(); services.ReplaceTransient <IDbContextFactory <PersonalCommonLegacyContext>, InMemoryPersonalCommonLegacyContextFactory>(); services.ReplaceTransient <IDbContextFactory <SticosWidgetDbContext>, InMemorySticosWidgetDbContextFactory>(); var usercontext = new StaticUserContext(new UserContext { UserId = 1 }); services.Remove <ICurrentUserContext>(); services.AddScoped <ICurrentUserContext>(i => usercontext); }
public static void AddIocMapping(this IServiceCollection services) { var userContext = new StaticUserContext(new UserContext()); services.AddSingleton <ICurrentUserContext>(userContext); services.AddScoped <IAltinnAdapter, AltinnAdapter>(); services.AddScoped <INavMessageService, NavMessageService>(); services.AddScoped <IExternalSystemService, ExternalSystemService>(); services.AddScoped <IXmlSerializer, NavMessageXmlSerializer>(); services.AddScoped <IRepository <NavMessage, SearchQueryNavMessage>, NavMessageRepository>(); services.AddScoped <IRepository <ExternalSystem>, ExternalSystemRepository>(); services.AddScoped <AltinnExternalDataService, AltinnExternalDataService>(); services.AddScoped <IExternalSystemFactory, ExternalSystemFactory>(); services.AddTransient <IDbContextFactory <AltinnDbContext>, DbContextFactory>(); }
public ServiceCollection GetServices() { var services = new ServiceCollection(); services.AddAutoMapper(AutoMapperSetup.Config); services.ReplaceTransient <IDbContextFactory <SticosWidgetDbContext>, InMemorySticosWidgetDbContextFactory>(); services.ReplaceTransient <IDbContextFactory <PersonalLegacyContext>, InMemoryPersonalLegacyContextFactory>(); var userContext = new StaticUserContext(new UserContext { UserId = 1 }); services.Remove <ICurrentUserContext>(); services.AddScoped <ICurrentUserContext>(i => userContext); return(services); }
public async Task TestReportIntegrationDoesNotExist(int userId, int unitId) { var currentUserContext = new StaticUserContext(new UserContext { UserId = userId }); var timeRegService = new TimeRegServiceBuilder() .WithCurrentUserContext(currentUserContext) .WithIntegrationService(_integrationService) .WithValidator(new UnimicroValidator(_integrationService, _entityMapService)) .Build(); ReportController Controller = new ReportController(timeRegService, Mapper); // Assert Assert.NotNull(Controller); var thrownException = Assert.ThrowsAsync <ForbiddenException>(async() => await Controller.GetHourBalance(unitId, 1337)); Assert.AreEqual("Company doesn't have integration", thrownException.Message); }
public async Task SetUp() { _testServer = new TestServerBuilder() .WithPostConfigureCollection((sc) => { sc.ReplaceTransient <IDbContextFactory <PersonalCommonLegacyContext>, InMemoryPersonalCommonLegacyContextFactory>(); var usercontext = new StaticUserContext(new UserContext { UserId = _userId }); sc.Remove <ICurrentUserContext>(); sc.AddScoped <ICurrentUserContext>(i => usercontext); }) .Build <Startup>(); _client = _testServer.CreateClientWithJwtToken(_customerId, _userId); var personalCommonLegacyFactory = _testServer.Host.Services.GetService <IDbContextFactory <PersonalCommonLegacyContext> >(); _personalCommonLegacyDb = await personalCommonLegacyFactory.CreateDbContext(); }
public async Task SetUp() { var settings = new Dictionary <string, string>(); settings.Add("Common_Api_Url", "http://localhost"); _actions = (sc) => { sc.ReplaceScoped <IDbContextFactory <NewsContext>, InMemoryContextFactory <NewsContext> >(); var usercontext = new StaticUserContext(new UserContext { UserId = _userId }); sc.Remove <ICurrentUserContext>(); sc.AddScoped <ICurrentUserContext>(i => usercontext); var userService = A.Fake <IUserService>(); A.CallTo(() => userService.GetUser(_userId)).Returns(new User() { IsPersonalCustomerAdmin = true, CustomerId = _customerId, UserId = _userId }); sc.ReplaceScoped(userService); }; _testServer = new TestServerBuilder() .WithPostConfigureCollection(_actions) .WithConfigSettings(settings) .WithCurrentUser(_userId, _customerId, true) .Build <Startup>(); _client = _testServer.CreateClientWithJwtToken(_customerId, _userId); var fakeCustomerIdService = A.Fake <ICustomerIdService>(); A.CallTo(() => fakeCustomerIdService.GetCustomerIdNotNull()).Returns(_customerId); var dbFactory = _testServer.Host.Services.GetService <IDbContextFactory <NewsContext> >(); _db = await dbFactory.CreateDbContext(); }