Exemplo n.º 1
0
        public void Setup()
        {
            var adminClaims = new List <Claim>();

            adminClaims.Add(new Claim(ClaimTypes.NameIdentifier, "1"));
            adminClaims.Add(new Claim(ClaimTypes.Role, "Admin"));
            var adminHttpContext             = GetHttpContext(adminClaims);
            var adminHttpContextAccessorMock = new Mock <IHttpContextAccessor>();

            adminHttpContextAccessorMock.Setup(x => x.HttpContext).Returns(adminHttpContext);

            var customerClaims = new List <Claim>();

            customerClaims.Add(new Claim(ClaimTypes.NameIdentifier, "2"));
            customerClaims.Add(new Claim(ClaimTypes.Role, "Customer"));
            var customerHttpContext             = GetHttpContext(customerClaims);
            var customerHttpContextAccessorMock = new Mock <IHttpContextAccessor>();

            customerHttpContextAccessorMock.Setup(x => x.HttpContext).Returns(customerHttpContext);

            var nullLoggerFactory = new NullLoggerFactory();
            var nullLogger        = nullLoggerFactory.CreateLogger("NullLogger");

            var builder = new DbContextOptionsBuilder <LoanContext>();

            builder.UseInMemoryDatabase("CustomerLoans");
            var options                            = builder.Options;
            var loanContext                        = new LoanContext(options);
            var inMemoryEFLoanQueryable            = new EntityFrameworkCodeFirstQueryable <Loan>(loanContext);
            var inMemoryLoanQueryableRepository    = new QueryableRepository <Loan>(inMemoryEFLoanQueryable);
            var inMemoryLoanQueryableDomainService = new QueryableDomainService <Loan, int>(inMemoryLoanQueryableRepository);
            var inMemoryEFLoanCommand              = new EntityFrameworkCodeFirstCommand <Loan, int>(loanContext);
            var inMemoryLoanCommandRepository      = new CommandRepository <Loan>(inMemoryEFLoanCommand);
            var inMemoryLoanCommandServiceAsync    = new CommandDomainServiceAsync <Loan>(inMemoryLoanCommandRepository, nullLogger);

            var retryPolicy      = new RetryNTimesPolicy(nullLogger, 3);
            var exceptionHandler = new BasicPollyExceptionHandler(new IPolicy[] { retryPolicy }, nullLogger, true);

            var myConfiguration = new Dictionary <string, string>
            {
                { "CustomerUrl", "CustomerUrl" }
            };
            var configuration = new ConfigurationBuilder()
                                .AddInMemoryCollection(myConfiguration)
                                .Build();

            //var configOptions = new OptionsWrapper<MemoryDistributedCacheOptions>(new MemoryDistributedCacheOptions());
            //var distributedCache = new MemoryDistributedCache(configOptions);
            var tokenManagerMock = new Mock <ITokenManager>();

            tokenManagerMock.Setup(x => x.GetTokenCacheDataAsync(It.IsAny <string>(), default)).Returns(Task.Run(() => new TokenCacheData {
                Token = "TestToken"
            }));

            var httpClientFactoryMock = new Mock <IHttpClientFactory>();

            httpClientFactoryMock.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(new HttpClient());

            _adminLoanController = new LoanController(adminHttpContextAccessorMock.Object,
                                                      inMemoryLoanQueryableDomainService, inMemoryLoanCommandServiceAsync,
                                                      exceptionHandler, configuration,
                                                      tokenManagerMock.Object, httpClientFactoryMock.Object);
            _customerLoanController = new LoanController(customerHttpContextAccessorMock.Object,
                                                         inMemoryLoanQueryableDomainService, inMemoryLoanCommandServiceAsync,
                                                         exceptionHandler, configuration,
                                                         tokenManagerMock.Object, httpClientFactoryMock.Object);
        }
Exemplo n.º 2
0
        public void Setup()
        {
            var fakeAdminUser = UserFakes.GetFakeAdminUser();
            var adminClaims   = new List <Claim>();

            adminClaims.Add(new Claim(ClaimTypes.NameIdentifier, fakeAdminUser.Id.ToString()));
            adminClaims.Add(new Claim(ClaimTypes.Role, fakeAdminUser.Role.ToString()));
            var adminHttpContext             = GetHttpContext(adminClaims);
            var adminHttpContextAccessorMock = new Mock <IHttpContextAccessor>();

            adminHttpContextAccessorMock.Setup(x => x.HttpContext).Returns(adminHttpContext);

            var fakeCustomerUser = UserFakes.GetFakeCustomerUser();
            var customerClaims   = new List <Claim>();

            customerClaims.Add(new Claim(ClaimTypes.NameIdentifier, fakeCustomerUser.Id.ToString()));
            customerClaims.Add(new Claim(ClaimTypes.Role, fakeCustomerUser.Role.ToString()));
            var customerHttpContext             = GetHttpContext(customerClaims);
            var customerHttpContextAccessorMock = new Mock <IHttpContextAccessor>();

            customerHttpContextAccessorMock.Setup(x => x.HttpContext).Returns(customerHttpContext);

            var nullLoggerFactory = new NullLoggerFactory();
            var nullLogger        = nullLoggerFactory.CreateLogger("NullLogger");

            var builder = new DbContextOptionsBuilder <UserContext>();

            builder.UseInMemoryDatabase("CustomerLoans");
            var options                            = builder.Options;
            var userContext                        = new UserContext(options);
            var inMemoryEFUserQueryable            = new EntityFrameworkCodeFirstQueryable <User>(userContext);
            var inMemoryUserQueryableRepository    = new QueryableRepository <User>(inMemoryEFUserQueryable);
            var inMemoryUserQueryableDomainService = new QueryableDomainService <User, int>(inMemoryUserQueryableRepository);
            var inMemoryEFUserCommand              = new EntityFrameworkCodeFirstCommand <User, int>(userContext);
            var inMemoryUserCommandRepository      = new CommandRepository <User>(inMemoryEFUserCommand);
            var inMemoryUserCommandServiceAsync    = new CommandDomainServiceAsync <User>(inMemoryUserCommandRepository, nullLogger);

            var retryPolicy      = new RetryNTimesPolicy(nullLogger, 3);
            var exceptionHandler = new BasicPollyExceptionHandler(new IPolicy[] { retryPolicy }, nullLogger, true);

            var myConfiguration = new Dictionary <string, string>
            {
                { "Audience:Secret", "Y2F0Y2hlciUyMHdvbmclMjBsb3ZlJTIwLm5ldA==" },
                { "Audience:Iss", "Sandip Ray" },
                { "Audience:Aud", "All" },
            };
            var configuration = new ConfigurationBuilder()
                                .AddInMemoryCollection(myConfiguration)
                                .Build();

            var configOptions    = new OptionsWrapper <MemoryDistributedCacheOptions>(new MemoryDistributedCacheOptions());
            var distributedCache = new MemoryDistributedCache(configOptions);
            var tokenManagerMock = new Mock <TokenManager>(distributedCache);

            _adminUserController = new UserController(adminHttpContextAccessorMock.Object,
                                                      inMemoryUserQueryableDomainService, inMemoryUserCommandServiceAsync,
                                                      exceptionHandler, configuration,
                                                      tokenManagerMock.Object);
            _customerUserController = new UserController(customerHttpContextAccessorMock.Object,
                                                         inMemoryUserQueryableDomainService, inMemoryUserCommandServiceAsync,
                                                         exceptionHandler, configuration,
                                                         tokenManagerMock.Object);
        }