Exemplo n.º 1
0
        /// <summary>
        /// Updates information about a user
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task <bool> UpdateDeviceAddressAsync(UserPairingDTO dto)
        {
            var user = await _context.Users
                       .Include(u => u.UserRole)
                       .Include(u => u.Producer)
                       .FirstOrDefaultAsync(u => u.Producer.PairingSecret == dto.PairingSecret);

            // Return null if user not found or password don't match
            if (user == null || user.UserRole.UserRoleEnum != UserRoleEnum.Producer)
            {
                return(false);
            }

            // Update user
            user.Producer.DeviceAddress = dto.DeviceAddress;
            user.Producer.WalletAddress = dto.WalletAddress;

            try
            {
                await _context.SaveChangesAsync();

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 2
0
        public async Task PutDeviceAddress_given_Existing_secret_and_Request_on_local_access_port_from_localhost_returns_NoContent()
        {
            var dto = new UserPairingDTO
            {
                PairingSecret = "ABCD",
                DeviceAddress = "Test"
            };

            var repository = new Mock <IUserRepository>();

            repository.Setup(r => r.UpdateDeviceAddressAsync(dto)).ReturnsAsync(true);

            var logger = new Mock <ILogger <UsersController> >();

            var controller = new UsersController(repository.Object, logger.Object);

            var httpContext = new DefaultHttpContext();

            httpContext.Connection.LocalIpAddress    = IPAddress.Parse("127.0.0.1");
            httpContext.Connection.LocalPort         = 4001;
            httpContext.Connection.RemoteIpAddress   = IPAddress.Parse("127.0.0.1");
            controller.ControllerContext.HttpContext = httpContext;

            var get = await controller.PutDeviceAddress(dto);

            Assert.IsType <NoContentResult>(get);
        }
Exemplo n.º 3
0
        public async Task PutDeviceAddress_given_Request_on_open_access_port_from_localhost_returns_Forbidden()
        {
            var dto = new UserPairingDTO
            {
                PairingSecret = "ABCD",
                DeviceAddress = "Test"
            };

            var repository = new Mock <IUserRepository>();

            var logger = new Mock <ILogger <UsersController> >();

            var controller = new UsersController(repository.Object, logger.Object);

            var httpContext = new DefaultHttpContext();

            httpContext.Connection.LocalIpAddress    = IPAddress.Parse("127.0.0.1");
            httpContext.Connection.LocalPort         = 5001;
            httpContext.Connection.RemoteIpAddress   = IPAddress.Parse("127.0.0.1");
            controller.ControllerContext.HttpContext = httpContext;

            var get = await controller.PutDeviceAddress(dto);

            Assert.IsType <ForbidResult>(get);
        }
Exemplo n.º 4
0
        public async Task PutDeviceAddress_given_nonExisting_secret_returns_NotFound()
        {
            var dto = new UserPairingDTO
            {
                PairingSecret = "ABCD",
                DeviceAddress = "Test"
            };

            var repository = new Mock <IUserRepository>();

            var logger = new Mock <ILogger <UsersController> >();

            var controller = new UsersController(repository.Object, logger.Object);

            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            var result = await controller.PutDeviceAddress(dto);

            Assert.IsType <NotFoundResult>(result);
        }
Exemplo n.º 5
0
        public async Task <ActionResult> PutDeviceAddress([FromBody] UserPairingDTO dto)
        {
            if (!HttpContext.Request.IsLocal())
            {
                return(Forbid());
            }

            _logger.LogInformation($"Updating device address information for user with pairing secret {dto.PairingSecret}");

            var result = await _userRepository.UpdateDeviceAddressAsync(dto);

            if (!result)
            {
                _logger.LogError($"Failed updating deveice address information for user with pairing secret {dto.PairingSecret}, user not found or not correct user role of Producer");

                return(NotFound());
            }

            _logger.LogInformation($"Successfully updating device address information for user with pairing secret {dto.PairingSecret}, with the new device address {dto.DeviceAddress} and wallet adress {dto.WalletAddress} ");

            return(NoContent());
        }
Exemplo n.º 6
0
        public async Task PutDeviceAddress_given_Existing_secret_returns_NoContent()
        {
            var dto = new UserPairingDTO
            {
                PairingSecret = "ABCD",
                DeviceAddress = "Test"
            };

            var repository = new Mock <IUserRepository>();

            repository.Setup(r => r.UpdateDeviceAddressAsync(dto)).ReturnsAsync(true);

            var logger = new Mock <ILogger <UsersController> >();

            var controller = new UsersController(repository.Object, logger.Object);

            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            var result = await controller.PutDeviceAddress(dto);

            repository.Verify(s => s.UpdateDeviceAddressAsync(dto));
            Assert.IsType <NoContentResult>(result);
        }