Exemple #1
0
        /// <summary>
        /// Sync the users account properties upstream to the backend upstream.
        /// </summary>
        /// <returns>Was Sync successful</returns>
        public async Task <bool> SyncUpAsync(UpdatedAccountDto dto)
        {
            var requestBody = JsonConvert.SerializeObject(dto);
            var request     = new OAuth2BearerRequest("POST",
                                                      _backendAddress,
                                                      null,
                                                      AuthenticationService.AuthAccount);

            request.SetRequestBody(requestBody);

            try
            {
                var response = await request.GetResponseAsync();

                var result = response.StatusCode == System.Net.HttpStatusCode.OK;
                _userDialogs.Toast(result ? "Updated user account." : "Failed to update user account.");

                return(result);
            }
            catch (Exception)
            {
                _userDialogs.ShowError("Connection to service failed.");

                return(false);
            }
        }
        public async void GetAccount_OnValidUserContext_OkWithDto()
        {
            //Arrange
            var user = new ApplicationUser {
                UserName = "******", Nickname = "Nick"
            };
            var dto = new UpdatedAccountDto {
                Nickname = user.Nickname
            };
            var userManagerMock = MockHelpers.GetMockUserManager();

            userManagerMock
            .Setup(u => u.FindByNameAsync(user.UserName))
            .ReturnsAsync(user);

            var httpContextMock = new Mock <DefaultHttpContext>(null);

            httpContextMock
            .Setup(h => h.User.Identity.Name)
            .Returns(user.UserName);

            //Act
            var controller = new AccountController(_logger, userManagerMock.Object);

            controller.ControllerContext.HttpContext = httpContextMock.Object;

            var response = await controller.GetAccount();

            var responseDto = ((OkObjectResult)response).Value as UpdatedAccountDto;

            //Assert
            Assert.IsType <OkObjectResult>(response);
            Assert.Equal(dto.Nickname, responseDto.Nickname);
        }
Exemple #3
0
        public async Task <IActionResult> UpdateAccount([FromBody] UpdatedAccountDto dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var userInDb = await _userManager.FindByNameAsync(User.Identity.Name);

            if (userInDb == null)
            {
                return(NotFound());
            }

            if (string.CompareOrdinal(userInDb.Nickname, dto.Nickname) != 0)
            {
                userInDb.Nickname = dto.Nickname;
                _logger.LogInformation($"{userInDb.Nickname} changed Nickname to: {dto.Nickname}");
            }

            if (string.CompareOrdinal(userInDb.Status, dto.StatusMessage) != 0)
            {
                userInDb.Status = dto.StatusMessage;
                _logger.LogInformation($"{userInDb.Status} changed Status to: {dto.StatusMessage}");
            }

            var result = await _userManager.UpdateAsync(userInDb);

            if (!result.Succeeded)
            {
                return(StatusCode(500));
            }

            return(Ok());
        }
Exemple #4
0
        public async Task <IActionResult> GetAccount()
        {
            var userInDb = await _userManager.FindByNameAsync(User.Identity.Name);

            if (userInDb == null)
            {
                return(StatusCode(500));
            }

            var dto = new UpdatedAccountDto
            {
                Nickname      = userInDb.Nickname,
                StatusMessage = userInDb.Status
            };

            return(Ok(dto));
        }
Exemple #5
0
        /// <summary>
        /// Sync the users account properties upstream to the backend upstream.
        /// </summary>
        /// <returns>Was Sync successful</returns>
        public async Task <bool> SyncUpAsync()
        {
            var dto = new UpdatedAccountDto
            {
                Nickname      = User.Nickname,
                StatusMessage = User.Status
            };

            var result = await _accountService.SyncUpAsync(dto);

            if (result == false)
            {
                return(false);
            }

            await PersistUser();

            return(true);
        }
        public async void UpdateAccount_OnUpdateNicknameAndStatuts_OkStatus(string username,
                                                                            string oldNickname, string newNickname, string oldStatus, string newStatus)
        {
            //Arrange
            var fakeUser = new ApplicationUser
            {
                UserName = username,
                Nickname = oldNickname,
                Status   = oldStatus
            };

            var identityResult = IdentityResult.Success;

            var userManagerMock = MockHelpers.GetMockUserManager();

            userManagerMock
            .Setup(r => r.FindByNameAsync(It.IsAny <string>()))
            .ReturnsAsync(fakeUser);

            userManagerMock
            .Setup(m => m.UpdateAsync(fakeUser))
            .ReturnsAsync(identityResult);

            var httpContextMock = new Mock <DefaultHttpContext>(null);

            httpContextMock
            .SetupGet(am => am.User.Identity.Name)
            .Returns(username);

            //Act
            var controller = new AccountController(_logger, userManagerMock.Object);

            controller.ControllerContext.HttpContext = httpContextMock.Object;

            var dto = new UpdatedAccountDto {
                Nickname = newNickname, StatusMessage = newStatus
            };
            var response = await controller.UpdateAccount(dto);

            //Assert
            Assert.IsType <OkResult>(response);
        }