コード例 #1
0
        public async Task Add_new_User_and_check_auth()
        {
            //Arrange
            AddViewUsers addUser = new AddViewUsers
            {
                Login        = "******",
                Name         = "Name",
                Password     = "******",
                Surname      = "Surname",
                AccountImage = ""
            };

            //Act
            var response = await TestClient.PostAsync("/api/Users/newUser",
                                                      new StringContent(JsonConvert.SerializeObject(addUser),
                                                                        Encoding.UTF8,
                                                                        "application/json"));

            var isAuth = await AuthenticateAsync(addUser.Login, addUser.Password);

            //Assert
            response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
            Assert.True(isAuth);
            using (var scope = serviceProvider.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <JLDatabaseContext>();
                var actual  = context.Users.Single(u => u.Login == addUser.Login);

                Assert.Equal(addUser.Name, actual.Name);
                Assert.Equal(addUser.Password, actual.Password);
                Assert.Equal(addUser.Surname, actual.Surname);
                Assert.Empty(actual.AccountImage);
            }
        }
コード例 #2
0
        private async Task <InfoViewUsers> CheckingUserAsync(string login, string password)
        {
            AddViewUsers user = mapper.Map <AddViewUsers>(await userRepository.GetUserByLogin(login));

            if (user == null || !user.Password.Equals(password))
            {
                return(null);
            }
            return(mapper.Map <InfoViewUsers>(user));
        }
コード例 #3
0
        public async Task Update_and_get_by_id_user()
        {
            //Arrange

            AddViewUsers addUser = new AddViewUsers
            {
                Login        = "******",
                Name         = "Name",
                Password     = "******",
                Surname      = "Surname",
                AccountImage = ""
            };
            UpdateViewUsers updateUser = new UpdateViewUsers
            {
                Name     = "UpdateName",
                Password = "******",
                Surname  = "UpdateSurname"
            };

            //Act
            var addResponse = await TestClient.PostAsync("/api/Users/newUser",
                                                         new StringContent(JsonConvert.SerializeObject(addUser),
                                                                           Encoding.UTF8,
                                                                           "application/json"));


            int userId = serviceProvider.CreateScope()
                         .ServiceProvider
                         .GetRequiredService <JLDatabaseContext>()
                         .Users.Single(u => u.Login == addUser.Login).Id;

            var updateResponse = await TestClient.PutAsync("/api/Users/updatingUser/" + userId,
                                                           new StringContent(JsonConvert.SerializeObject(updateUser),
                                                                             Encoding.UTF8,
                                                                             "application/json"));

            var gettingResponse = await TestClient.GetAsync("/api/Users/" + userId);

            var resultUser = JsonConvert.DeserializeObject <InfoViewUsers>(await gettingResponse.Content.ReadAsStringAsync());

            //Assert
            using (var scope = serviceProvider.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <JLDatabaseContext>();

                string resultUserPassword = context.Users.Single(u => u.Login == addUser.Login).Password;

                updateResponse.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
                gettingResponse.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);

                Assert.Equal(resultUser.Name, updateUser.Name);
                Assert.Equal(resultUserPassword, updateUser.Password);
                Assert.Equal(resultUser.Surname, updateUser.Surname);
            }
        }
コード例 #4
0
        public async Task <IActionResult> AddNewUser(AddViewUsers user)
        {
            if (!await IsUserExist(user.Login))
            {
                int userId = await userRepository.AddUser(mapper.Map <Users>(user));

                return(Ok(userId));
            }

            var jsonResponse =
                new
            {
                errors = new
                {
                    general = new[] { "Пользователь с данным адресом уже существует" }
                }
            };

            return(BadRequest(jsonResponse));
        }
コード例 #5
0
        private async Task <bool> IsUserExist(string login)
        {
            AddViewUsers user = mapper.Map <AddViewUsers>(await userRepository.GetUserByLogin(login));

            return(user != null);
        }