Пример #1
0
        public async Task UpdateUser_Should_UpdateValidUser()
        {
            // Arrange
            _context = new PizzaProblemContext(
                new DbContextOptionsBuilder <PizzaProblemContext>()
                .UseInMemoryDatabase(databaseName: "UserServiceTests")
                .Options);
            _context.Database.EnsureCreated();
            _testingService = new UserService(_testingOptions, _mockMapper, _context);
            var validUser = new UserDto
            {
                Id        = 1,
                Username  = "******",
                Password  = "******",
                FirstName = "test",
                LastName  = "test",
                PizzaLove = 3
            };

            // Act
            await _testingService.UpdateUserAsync(validUser);

            // Assert
            var user = await _testingService.GetByIdAsync(1);

            Assert.Equal(3, user.PizzaLove);
        }
Пример #2
0
        public AuthorizeAttributeTests()
        {
            _testingSettings        = new AppSettings();
            _testingSettings.Secret = "THIS IS MY VERY LONG TESTING SECRET THAT NO ONE SHOULD KNOW";
            // Arrange testing service for all the testing class
            _testingOptions = Options.Create(_testingSettings);

            var mapperConfig = new MapperConfiguration(cfg => {
                cfg.AddProfile <UsersProfile>();
                cfg.AddProfile <AuthenticateProfile>();
            });

            _mockMapper = mapperConfig.CreateMapper();

            _pizzaContext = new PizzaProblemContext(
                new DbContextOptionsBuilder <PizzaProblemContext>()
                .UseInMemoryDatabase(databaseName: "AuthorizeAttributeTests")
                .Options);
            _pizzaContext.Database.EnsureCreated();

            _testingService = new UserService(_pizzaContext);

            _mockHttpContext = new DefaultHttpContext();

            _actionContext =
                new ActionContext(_mockHttpContext,
                                  new Microsoft.AspNetCore.Routing.RouteData(),
                                  new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor());

            _authorizationFilterContext = new AuthorizationFilterContext(_actionContext,
                                                                         new List <IFilterMetadata>());

            _authorizeAttribute = new AuthorizeAttribute();
        }
Пример #3
0
        public async Task UpdateUser_Should_DoNothingForInvalidUser()
        {
            // Arrange
            _context = new PizzaProblemContext(
                new DbContextOptionsBuilder <PizzaProblemContext>()
                .UseInMemoryDatabase(databaseName: "UpdateUser")
                .Options);
            _context.Database.EnsureCreated();
            _testingService = new UserService(_testingOptions, _mockMapper, _context);
            var invalidUser = new UserDto
            {
                Id        = 10,
                Username  = "******",
                Password  = "******",
                FirstName = "",
                LastName  = "",
                PizzaLove = 0
            };

            // Act
            await _testingService.UpdateUserAsync(invalidUser);

            // Assert
            Assert.True(await _testingService.GetByIdAsync(invalidUser.Id) == null);
        }
Пример #4
0
        public async Task GetById_ShouldReturn_NullForNonExistentUser()
        {
            // Arrange
            _context = new PizzaProblemContext(
                new DbContextOptionsBuilder <PizzaProblemContext>()
                .UseInMemoryDatabase(databaseName: "UserServiceTests")
                .Options);
            _context.Database.EnsureCreated();
            _testingService = new UserService(_testingOptions, _mockMapper, _context);

            // Act
            var firstUser = await _testingService.GetByIdAsync(0);

            // Assert
            Assert.Null(firstUser);
        }
Пример #5
0
        public async Task GetAll_ShouldReturn_LenghtOfTwoInitially()
        {
            // Arrange
            _context = new PizzaProblemContext(
                new DbContextOptionsBuilder <PizzaProblemContext>()
                .UseInMemoryDatabase(databaseName: "GetAll")
                .Options);
            _context.Database.EnsureCreated();
            _testingService = new UserService(_testingOptions, _mockMapper, _context);

            // Act
            var listOfUsers = await _testingService.GetAllAsync();

            // Assert
            Assert.Equal(2, listOfUsers.Count);
        }
Пример #6
0
        public async Task GetAll_ShouldReturn_NotNullCollectionInitially()
        {
            // Arrange
            _context = new PizzaProblemContext(
                new DbContextOptionsBuilder <PizzaProblemContext>()
                .UseInMemoryDatabase(databaseName: "UserServiceTests")
                .Options);
            _context.Database.EnsureCreated();
            _testingService = new UserService(_testingOptions, _mockMapper, _context);

            // Act
            var listOfUsers = await _testingService.GetAllAsync();

            // Assert
            Assert.NotNull(listOfUsers);
        }
Пример #7
0
        public async Task GetTopTenPizzaLove_ShouldReturn_AListOfUsers()
        {
            // Arrange
            _context = new PizzaProblemContext(
                new DbContextOptionsBuilder <PizzaProblemContext>()
                .UseInMemoryDatabase(databaseName: "GetTopTenPizzaLove")
                .Options);
            _context.Database.EnsureCreated();
            _testingService = new UserService(_testingOptions, _mockMapper, _context);

            // Act
            var list = await _testingService.GetTopTenPizzaLoveAsync();

            // Assert
            Assert.Equal(2, list.Count);
        }
Пример #8
0
        public async Task GetById_ShouldReturn_ValidUser()
        {
            // Arrange
            _context = new PizzaProblemContext(
                new DbContextOptionsBuilder <PizzaProblemContext>()
                .UseInMemoryDatabase(databaseName: "UserServiceTests")
                .Options);
            _context.Database.EnsureCreated();
            _testingService = new UserService(_context);

            // Act
            var firstUser = await _testingService.GetByIdAsync(1);

            // Assert
            Assert.Equal(1, firstUser.Id);
        }
Пример #9
0
        public async Task AddNewUser_ShouldCreate_AValidUser()
        {
            // Arrange
            _context = new PizzaProblemContext(
                new DbContextOptionsBuilder <PizzaProblemContext>()
                .UseInMemoryDatabase(databaseName: "UserServiceTests")
                .Options);
            _context.Database.EnsureCreated();
            _testingService = new UserService(_testingOptions, _mockMapper, _context);
            var newUser = new UserDto {
                Id = 0, FirstName = "Test", LastName = "Test", Username = "******", Password = "******"
            };

            // Act
            var addedUser = await _testingService.AddNewUserAsync(newUser);

            // Assert
            Assert.IsType <UserDto>(addedUser);
        }
Пример #10
0
        public async Task AddNewUser_ShouldCreate_ANewIdForTheNewUser()
        {
            // Arrange
            _context = new PizzaProblemContext(
                new DbContextOptionsBuilder <PizzaProblemContext>()
                .UseInMemoryDatabase(databaseName: "AddNewUser")
                .Options);
            _context.Database.EnsureCreated();
            _testingService = new UserService(_context);
            var newUser = new User {
                Id = 0, FirstName = "Test", LastName = "Test", Username = "******", Password = "******"
            };

            // Act
            var addedUser = await _testingService.AddNewUserAsync(newUser);

            // Assert
            Assert.Equal(3, addedUser.Id);
        }
Пример #11
0
        public async Task Authenticate_ShouldReturn_AValidResponseForValidUser()
        {
            // Arrange
            _context = new PizzaProblemContext(
                new DbContextOptionsBuilder <PizzaProblemContext>()
                .UseInMemoryDatabase(databaseName: "Authenticate")
                .Options);
            _context.Database.EnsureCreated();
            _testingService = new UserService(_testingOptions, _mockMapper, _context);
            var newAuthenticateRequest = new AuthenticateRequestDto
            {
                Username = "******",
                Password = "******"
            };

            // Act
            var response = await _testingService.AuthenticateAsync(newAuthenticateRequest);

            // Assert
            Assert.Equal(1, response.Id);
        }
Пример #12
0
        public async Task Authenticate_ShouldReturn_NullForInvalidPassword()
        {
            // Arrange
            _context = new PizzaProblemContext(
                new DbContextOptionsBuilder <PizzaProblemContext>()
                .UseInMemoryDatabase(databaseName: "UserServiceTests")
                .Options);
            _context.Database.EnsureCreated();
            _testingService = new UserService(_context);
            var newAuthenticateRequest = new AuthenticateRequest
            {
                Username = "******",
                Password = "******"
            };

            // Act
            var response = await _testingService.AuthenticateAsync(newAuthenticateRequest);

            // Assert
            Assert.Null(response);
        }
Пример #13
0
        public JwtMiddlewareTests()
        {
            _testingSettings        = new AppSettings();
            _testingSettings.Secret = "THIS IS MY VERY LONG TESTING SECRET THAT NO ONE SHOULD KNOW";
            // Arrange testing service for all the testing class
            _testingOptions = Options.Create(_testingSettings);

            var mapperConfig = new MapperConfiguration(cfg => cfg.AddProfile(new UsersProfile()));

            _mockMapper = mapperConfig.CreateMapper();

            _context = new PizzaProblemContext(
                new DbContextOptionsBuilder <PizzaProblemContext>()
                .UseInMemoryDatabase(databaseName: "MiddlewareTests")
                .Options);
            _context.Database.EnsureCreated();

            _testingService = new UserService(_testingOptions, _mockMapper, _context);

            _mockContext = new DefaultHttpContext();
            _next        = async(HttpContext hc) => await Task.CompletedTask;

            _authenticationMiddleware = new JwtMiddleware(_testingService, _testingOptions);
        }
Пример #14
0
 public UserService(PizzaProblemContext context)
 {
     _context = context;
     _context.Database.EnsureCreated();
 }