Exemplo n.º 1
0
        public async Task ShouldCreate_A_Client()
        {
            string name     = "Vader";
            Guid   clientId = Guid.NewGuid();

            var request = new CreateClientCommand {
                Name = name
            };
            var expected = new CreateClientResponse {
                Name = name, Id = clientId
            };

            var mapper         = PetShopMappingConfiguration.GetPetShopMappings();
            var mockRepository = new Mock <IClientRepository>();

            mockRepository.Setup(p => p.Add(It.Is <Client>(c => c.Name == name)))
            .Returns((Client client) => Task.Run(() =>
            {
                client.Id = clientId;
            }));

            var handler = new CreateClientCommandHandler(mapper, mockRepository.Object);

            var result = await handler.Handle(request, CancellationToken.None);

            result.Data.Should().BeEquivalentTo(expected);
            result.Message.Should().BeEquivalentTo("Client Created");
            mockRepository.Verify(m => m.Add(It.IsAny <Client>()), Times.Once());
        }
Exemplo n.º 2
0
        public async Task ShouldAdd_A_Pet()
        {
            string name = "Vader";

            var clientId = Guid.NewGuid();

            var request = new AddPetCommand {
                Name = name, ClientId = clientId
            };
            var expected = new AddPetResponse {
                Id = new Guid("994aa42a-e292-42f1-b5d4-749cd19a4d29"), Name = name, ClientId = clientId
            };

            var mapper         = PetShopMappingConfiguration.GetPetShopMappings();
            var mockRepository = new Mock <IPetRepository>();

            mockRepository.Setup(p => p.Add(It.Is <Pet>(c => c.Name == name)))
            .Returns((Pet pet) => Task.Run(() =>
            {
                pet.Id = new Guid("994aa42a-e292-42f1-b5d4-749cd19a4d29");
            }));

            var handler = new AddPetCommandHandler(mapper, mockRepository.Object);

            var result = await handler.Handle(request, CancellationToken.None);

            result.Data.Should().BeEquivalentTo(expected);
            result.Message.Should().BeEquivalentTo("Pet Added");
            mockRepository.Verify(m => m.Add(It.IsAny <Pet>()), Times.Once());
        }
        public async Task Should_Return_a_Client()
        {
            var id = new Guid("994aa42a-e292-42f1-b5d4-749cd19a4d29");

            var expected = new ClientDto {
                Name = "BBB", Id = id, Pets = new List <PetDto> {
                    new PetDto {
                        Name = "Link"
                    }
                }
            };

            var mapper     = PetShopMappingConfiguration.GetPetShopMappings();
            var clientMock = new Mock <IClientRepository>();

            clientMock.Setup(p => p.GetById(id)).ReturnsAsync(new Client {
                Name = "BBB", Id = id, Pets = new List <Pet> {
                    new Pet {
                        Name = "Link"
                    }
                }
            });

            var handler = new GetClientByIdQueryHandler(mapper, clientMock.Object);

            var result = await handler.Handle(new GetClientByIdQuery { Id = id }, CancellationToken.None);

            result.Data.Should().BeEquivalentTo(expected);
            clientMock.Verify(m => m.GetById(It.IsAny <Guid>()), Times.Once());
        }
Exemplo n.º 4
0
        public async Task ShouldUpdate_A_Pet()
        {
            var name    = "Vader";
            var request = new UpdatePetCommand(name, Guid.NewGuid());

            var mapper         = PetShopMappingConfiguration.GetPetShopMappings();
            var mockRepository = new Mock <IPetRepository>();

            mockRepository.Setup(p => p.Update(It.Is <Pet>(c => c.Name == name)));

            var handler = new UpdatePetCommandHandler(mapper, mockRepository.Object);

            var result = await handler.Handle(request, CancellationToken.None);

            result.Message.Should().BeEquivalentTo("Pet Updated");
            mockRepository.Verify(m => m.Update(It.IsAny <Pet>()), Times.Once());
        }
Exemplo n.º 5
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
            services.AddMvc();
            services.AddControllers();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "My API", Version = "v1"
                });
                c.EnableAnnotations();

                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    In          = ParameterLocation.Header,
                    Description = "Please insert JWT with Bearer into field",
                    Name        = "Authorization",
                    Type        = SecuritySchemeType.ApiKey
                });
                c.AddSecurityRequirement(new OpenApiSecurityRequirement {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            }
                        },
                        new string[] { }
                    }
                });
                //c.OperationFilter<SecurityRequirementsOperationFilter>();
            });

            services.AddSingleton(PetShopMappingConfiguration.GetPetShopMappings());

            services.AddPetShopDependecies();
            services.AddDbContext <PetShopDbContext>(options =>
            {
                options.UseSqlServer(
                    Configuration.GetConnectionString("PetShopConnection"));
            });

            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlServer(
                                                             Configuration.GetConnectionString("IdentityConnection")));

            services.AddIdentity <IdentityUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = false)
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            var signingConfigurations = new SigningConfigurations();

            services.AddSingleton(signingConfigurations);

            var tokenConfigurations = new TokenConfigurations();

            new ConfigureFromConfigurationOptions <TokenConfigurations>(
                Configuration.GetSection("TokenConfigurations"))
            .Configure(tokenConfigurations);
            services.AddSingleton(tokenConfigurations);

            //services.Configure<IdentityOptions>(options =>
            //{
            //    // Password settings.
            //    options.Password.RequireDigit = true;
            //    options.Password.RequireLowercase = true;
            //    options.Password.RequireNonAlphanumeric = true;
            //    options.Password.RequireUppercase = true;
            //    options.Password.RequiredLength = 6;
            //    options.Password.RequiredUniqueChars = 1;

            //    // Lockout settings.
            //    options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
            //    options.Lockout.MaxFailedAccessAttempts = 5;
            //    options.Lockout.AllowedForNewUsers = true;

            //    // User settings.
            //    options.User.AllowedUserNameCharacters =
            //    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
            //    options.User.RequireUniqueEmail = false;
            //});

            //services.ConfigureApplicationCookie(options =>
            //{
            //    // Cookie settings
            //    options.Cookie.HttpOnly = true;
            //    options.ExpireTimeSpan = TimeSpan.FromMinutes(5);

            //    options.LoginPath = "/Identity/Account/Login";
            //    options.AccessDeniedPath = "/Identity/Account/AccessDenied";
            //    options.SlidingExpiration = true;
            //});

            services.AddMediatR(typeof(AddPetCommandHandler).Assembly);
            services.AddValidatorsFromAssembly(typeof(CreateClientCommandValidator).Assembly);

            services.AddTransient(typeof(IPipelineBehavior <,>), typeof(LoggingBehaviour <,>));
            services.AddTransient(typeof(IPipelineBehavior <,>), typeof(ValidationBehaviour <,>));
            services.AddTransient(typeof(IPipelineBehavior <,>), typeof(UnhandledExceptionBehaviour <,>));

            services.AddPetShopAuthentication(Configuration);
        }