Ejemplo n.º 1
0
        public async Task Get_given_existing_id_and_user_role_Producer_returns_Producer()
        {
            var expected = new DetailedProducerDTO
            {
                UserId       = 1,
                FirstName    = "Test",
                UserRole     = UserRoleEnum.Producer.ToString(),
                Street       = "Testvej",
                StreetNumber = "12324",
                Zipcode      = "2457"
            };

            var repository = new Mock <IUserRepository>();

            repository.Setup(s => s.FindAsync(expected.UserId)).ReturnsAsync(expected);

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

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

            var get = await controller.Get(expected.UserId);

            Assert.Equal(expected.FirstName, get.Value.FirstName);
            Assert.Equal(expected.UserRole, get.Value.UserRole);
            Assert.Equal(expected.Street, get.Value.Street);
        }
Ejemplo n.º 2
0
        public async Task Me_given_existing_id_and_role_receiver_returns_producer()
        {
            var input = 1;

            var expected = new DetailedProducerDTO
            {
                UserId    = input,
                Email     = "Test@test",
                FirstName = "Test",
                UserRole  = UserRoleEnum.Producer.ToString(),
            };

            var repository = new Mock <IUserRepository>();

            repository.Setup(s => s.FindAsync(input)).ReturnsAsync(expected);

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

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

            // Needs HttpContext to mock it.
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            var cp = MockClaimsSecurity(input);

            //Update the HttpContext to use mocked claim
            controller.ControllerContext.HttpContext.User = cp.Object;

            var get = await controller.Me();

            Assert.Equal(expected.UserId, get.Value.UserId);
            Assert.Equal(expected.Email, get.Value.Email);
            Assert.Equal(expected.FirstName, get.Value.FirstName);
            Assert.Equal(expected.UserRole, get.Value.UserRole);
        }
Ejemplo n.º 3
0
        public async Task Get_given_existing_id_and_role_receiver_returns_receiver()
        {
            var input = 1;

            var expected = new DetailedProducerDTO
            {
                FirstName = "test",
                UserRole  = UserRoleEnum.Receiver.ToString()
            };

            var repository = new Mock <IUserRepository>();

            repository.Setup(s => s.FindAsync(input)).ReturnsAsync(expected);

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

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

            var get = await controller.Get(input);

            Assert.Equal(expected.UserRole, get.Value.UserRole);
            Assert.Equal(expected.FirstName, get.Value.FirstName);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create a full user with a role and sub entity of the given role
        /// </summary>
        /// <param name="dto"></param>
        /// <returns name="TokenDTO"></returns>
        public async Task <TokenDTO> CreateAsync(UserCreateDTO dto)
        {
            if (dto == null || dto.Password == null || dto.Password.Length < 8)
            {
                return(null);
            }

            // Creates initial DTO with the static
            // user information
            var userDTO = new DetailedUserDTO
            {
                Email     = dto.Email,
                FirstName = dto.FirstName,
                SurName   = dto.SurName,
                Country   = dto.Country,
            };

            // Wrapped into a try catch as there are many DB restrictions
            // that need to be upheld to succeed with the transaction
            try
            {
                var user = new User
                {
                    FirstName = dto.FirstName,
                    SurName   = dto.SurName,
                    Email     = dto.Email,
                    Country   = dto.Country,
                    Created   = DateTime.UtcNow,
                    // Important to hash the password
                    Password = PasswordHasher.HashPassword(dto.Email, dto.Password),
                };

                var createdUser = _context.Users.Add(user);

                // Add the user to a role and add a foreign key for the ISA relationship
                // Used to extend the information on a user and give access restrictions
                if (dto.UserRole.Equals(nameof(UserRoleEnum.Producer)))
                {
                    // Set user role on DTO
                    userDTO.UserRole = UserRoleEnum.Producer.ToString();

                    // Can be seperated into different method
                    var producerUserRole = new UserRole
                    {
                        UserId       = createdUser.Entity.Id,
                        UserRoleEnum = UserRoleEnum.Producer
                    };

                    var producerUserRoleEntity = _context.UserRoles.Add(producerUserRole);

                    var producer = new Producer
                    {
                        UserId        = createdUser.Entity.Id,
                        PairingSecret = GeneratePairingSecret(),
                        Street        = dto.Street,
                        StreetNumber  = dto.StreetNumber,
                        Zipcode       = dto.Zipcode,
                        City          = dto.City
                    };

                    var producerEntity = _context.Producers.Add(producer);

                    await _context.SaveChangesAsync();

                    userDTO = new DetailedProducerDTO
                    {
                        UserId    = producer.UserId,
                        Email     = dto.Email,
                        FirstName = dto.FirstName,
                        SurName   = dto.SurName,
                        Country   = dto.Country,

                        // Set user role on DTO
                        UserRole = UserRoleEnum.Producer.ToString(),

                        // Get pairing link for OByte wallet immediately.
                        PairingLink = !string.IsNullOrEmpty(producerEntity.Entity.PairingSecret)
                        ? "byteball:" + _deviceAddress + "@" + _obyteHub + "#" + producerEntity.Entity.PairingSecret
                        : default(string),
                        Street       = dto.Street,
                        StreetNumber = dto.StreetNumber,
                        Zipcode      = dto.Zipcode,
                        City         = dto.City
                    };
                }
                else if (dto.UserRole.Equals(nameof(UserRoleEnum.Receiver)))
                {
                    // Set user role on DTO
                    userDTO.UserRole = UserRoleEnum.Receiver.ToString();

                    // Can be seperated into different method
                    var receiverUserRole = new UserRole
                    {
                        UserId       = createdUser.Entity.Id,
                        UserRoleEnum = UserRoleEnum.Receiver
                    };

                    var receiverUserRoleEntity = _context.UserRoles.Add(receiverUserRole);

                    await _context.SaveChangesAsync();

                    var receiver = new Receiver
                    {
                        UserId = receiverUserRoleEntity.Entity.UserId
                    };

                    _context.Receivers.Add(receiver);
                }
                else
                {
                    // Invalid role
                    return(null);
                }

                // Save changes at last,
                // to make it a transaction
                await _context.SaveChangesAsync();

                // Set generated user id after saving the changes to DB
                userDTO.UserId = user.Id;
            }
            catch (Exception)
            {
                // Could also throw an exception for more information when failing the user creation
                return(null);
            }


            // Return the user information along with an authorized tokens
            // To login the user after creation
            var tokenDTO = new TokenDTO
            {
                UserDTO = userDTO,
                Token   = (await Authenticate(dto.Email, dto.Password)).token,
            };

            return(tokenDTO);
        }