public async Task user_should_be_created_with_normal_user_role_and_return_ok()
        {
            HungryBearUser userPassedIntoCreateAsync = null;

            var newUserId = "daaasas";

            _userManagerMock
            .Setup(it => it.CreateAsync(It.IsAny <HungryBearUser>(), ValidSampleCreateUserCommand.Password))
            .Callback((HungryBearUser user, string password) => { user.Id = newUserId; userPassedIntoCreateAsync = user; })
            .Returns(Task.FromResult(IdentityResult.Success));

            _userManagerMock
            .Setup(it => it.AddToRoleAsync(It.IsAny <HungryBearUser>(), RoleNames.NormalUser))
            .Returns(Task.FromResult(IdentityResult.Success));

            var result = await Sut.Handle(ValidSampleCreateUserCommand, CancellationToken.None);

            result.ResultType.ShouldBe(ExecutionResultType.Ok);
            result.Value.ShouldBe(newUserId);

            userPassedIntoCreateAsync.Email.ShouldBe(ValidSampleCreateUserCommand.Email);
            userPassedIntoCreateAsync.FirstName.ShouldBe(ValidSampleCreateUserCommand.FirstName);
            userPassedIntoCreateAsync.LastName.ShouldBe(ValidSampleCreateUserCommand.LastName);
            userPassedIntoCreateAsync.PhoneNumber.ShouldBe(ValidSampleCreateUserCommand.PhoneNumber);

            _userManagerMock.Verify(it => it.AddToRoleAsync(userPassedIntoCreateAsync, RoleNames.NormalUser));
        }
Example #2
0
        public async Task <ExecutionResult <string> > Handle(CreateUserCommand command,
                                                             CancellationToken cancellationToken)
        {
            var newUser = new HungryBearUser
            {
                Email       = command.Email,
                UserName    = command.Email,
                FirstName   = command.FirstName,
                LastName    = command.LastName,
                PhoneNumber = command.PhoneNumber
            };

            var createUserResult = await _userManager.CreateAsync(newUser, command.Password);

            if (createUserResult.IsFailure())
            {
                return(createUserResult.ToFailedExecutionResult <string>());
            }

            var addToRoleResult = await _userManager.AddToRoleAsync(newUser, RoleNames.NormalUser);

            if (addToRoleResult.IsFailure())
            {
                return(addToRoleResult.ToFailedExecutionResult <string>());
            }

            return(ExecutionResult <string> .Ok(newUser.Id));
        }
        private async Task <AuthenticationTicket> CreateTicketAsync(
            OpenIdConnectRequest request, HungryBearUser user,
            AuthenticationProperties properties = null)
        {
            var principal = await _signInManager.CreateUserPrincipalAsync(user);

            var ticket = new AuthenticationTicket(principal, properties,
                                                  OpenIddictServerDefaults.AuthenticationScheme);

            if (!request.IsRefreshTokenGrantType())
            {
                ticket.SetScopes(new[]
                {
                    OpenIdConnectConstants.Scopes.OpenId,
                    OpenIdConnectConstants.Scopes.Email,
                    OpenIdConnectConstants.Scopes.Profile,
                    OpenIdConnectConstants.Scopes.OfflineAccess,
                    OpenIddictConstants.Scopes.Roles
                }.Intersect(request.GetScopes()));
            }

            ticket.SetResources(_authOptions.Audience);

            foreach (var claim in ticket.Principal.Claims.Where(claim => claim.Type != _identityOptions.Value.ClaimsIdentity.SecurityStampClaimType))
            {
                var destinations = new List <string>
                {
                    OpenIdConnectConstants.Destinations.AccessToken
                };

                if (claim.Type == OpenIdConnectConstants.Claims.Name &&
                    ticket.HasScope(OpenIdConnectConstants.Scopes.Profile) ||
                    claim.Type == OpenIdConnectConstants.Claims.Email &&
                    ticket.HasScope(OpenIdConnectConstants.Scopes.Email) ||
                    claim.Type == OpenIdConnectConstants.Claims.Role &&
                    ticket.HasScope(OpenIddictConstants.Claims.Roles))
                {
                    destinations.Add(OpenIdConnectConstants.Destinations.IdentityToken);
                }

                claim.SetDestinations(destinations);
            }

            return(ticket);
        }
Example #4
0
        private async Task EnsureUserExistsAsync(string email, string password, string role, string firstName, string lastName, string phoneNumber)
        {
            if (await _userManager.FindByEmailAsync(email) != null)
            {
                return;
            }

            var user = new HungryBearUser
            {
                UserName    = email,
                Email       = email,
                FirstName   = firstName,
                LastName    = lastName,
                PhoneNumber = phoneNumber
            };

            await _userManager.CreateAsync(user, password);

            await _userManager.AddToRoleAsync(user, role);
        }