Example #1
0
        /// <summary>
        /// Send http request to add new user to db. Before sending checking is form is filled properly (every text input is filled in, check if nickname and
        /// e-mail are unique and check if password and repeated password are the same).
        /// </summary>
        /// <param name="sender">The control/object that raised the event.</param>
        /// <param name="e">Event Data.</param>
        private async void RegisterButton_Click(object sender, RoutedEventArgs e)
        {
            HttpClient  client = new HttpClient();
            List <User> users  = new List <User>();

            try
            {
                HttpResponseMessage response = await client.GetAsync(ConfigurationManager.AppSettings["ServerURL"] + "users");

                string responseBody = await response.Content.ReadAsStringAsync();

                users = JsonConvert.DeserializeObject <List <User> >(responseBody);
            }
            catch (HttpRequestException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            if (users is null)
            {
                users = new List <User>();
            }
            try
            {
                UserFormUtils.CheckForm(NameTextBox.Text.Trim(), SurnameTextBox.Text.Trim(), EmailTextBox.Text.Trim(), NicknameTextBox.Text.Trim(), PasswordField.Password.Trim(), RepeatPasswordField.Password.Trim(), users);
            }
            catch (EmptyFormException) { MessageBox.Show("Pozostawiono puste pole!"); return; }
            catch (NicknameAlredyUsedException) { MessageBox.Show("Użytkownik o podanym loginie już istnieje!"); return; }
            catch (NotCorrectEmailException) { MessageBox.Show("Nie poprawny adres e-mail!"); return; }
            catch (EmailAlredyUsedException) { MessageBox.Show("Użytkownik o podanym adresie e-mail już istnieje!"); return; }
            catch (DiffrentPasswordsException) { MessageBox.Show("Podane hasła różnią się!"); return; }


            User user = new User
            {
                Name     = NameTextBox.Text,
                Surname  = SurnameTextBox.Text,
                Nickname = NicknameTextBox.Text,
                Password = BCrypt.Net.BCrypt.HashPassword(PasswordField.Password),
                Email    = EmailTextBox.Text
            };

            var json = JsonConvert.SerializeObject(user, Formatting.Indented, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            try
            {
                HttpContent content = new StringContent(json);
                content.Headers.Remove("Content-Type");
                content.Headers.Add("Content-Type", "application/json");
                HttpResponseMessage response = await client.PostAsync(ConfigurationManager.AppSettings["ServerURL"] + "users", content);

                MessageBox.Show("Utworzono uzytkownika o nicku: " + user.Nickname);
                NavigationService.Navigate(previousPage);
            }
            catch (HttpRequestException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
 public void CheckFormEmailAlredyUsedExceptionTest()
 {
     models.User user = new models.User()
     {
         Nickname = "uniqueNicknames",
         Email    = email
     };
     users.Add(user);
     Assert.ThrowsException <EmailAlredyUsedException>(() => UserFormUtils.CheckForm(name, surname, email, nickname, password, password2, users));
 }
 public void CheckFormDiffrentPasswordsExceptionTest2()
 {
     Assert.ThrowsException <DiffrentPasswordsException>(() => UserFormUtils.CheckForm(name, surname, email, nickname, wrongPassword, password2, users));
 }
 public void CheckFormNotCorrectEmailExceptionTest()
 {
     Assert.ThrowsException <NotCorrectEmailException>(() => UserFormUtils.CheckForm(name, surname, badEmail, nickname, password, password2, users));
 }
 public void CheckFormEmptyFormExceptionRepeatedPasswordEmptyTest()
 {
     Assert.ThrowsException <EmptyFormException>(() => UserFormUtils.CheckForm(name, surname, email, nickname, password, emptyString, users));
 }
 public void CheckFormEmptyFormExceptionNicknameEmptyTest()
 {
     Assert.ThrowsException <EmptyFormException>(() => UserFormUtils.CheckForm(name, surname, email, emptyString, password, password2, users));
 }
 public void CheckFormTest()
 {
     Assert.IsTrue(UserFormUtils.CheckForm(name, surname, email, nickname, password, password2, users));
 }