Skip to content

Commit

Permalink
[Hexlet#159] fix to encode password
Browse files Browse the repository at this point in the history
  • Loading branch information
d1z3d committed Jul 29, 2024
2 parents 1aa1ce9 + d495bd2 commit 1f8ca2a
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
CustomOAuth2User oAuth2User = (CustomOAuth2User) authentication.getPrincipal();
if (oAuth2User.getEmail() == null) {
if (oAuth2User.getEmail() == null
|| (oAuth2User.getFirstName() == null && oAuth2User.getLastName() == null)) {
//TODO: добавить ошибку на фронт, если email приватный
response.sendRedirect("/login");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,13 @@ public Account updatePassword(final UpdatePassword updatePassword, final String
}
@Transactional
public void processOAuthPostLogin(CustomOAuth2User user) {
//TODO: убрать после тестирования
//accountRepository.deleteAll();
var existUser = accountRepository.existsByEmail(user.getEmail());
if (!existUser) {
Account account = new Account();
account.setEmail(user.getEmail());
account.setAuthProvider(AuthProvider.GITHUB);
account.setUsername(user.getLogin());
account.setPassword(user.getPassword());
account.setPassword(passwordEncoder.encode(user.getPassword()));
account.setFirstName(user.getFirstName());
account.setLastName(user.getLastName());
accountRepository.save(account);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class UpdateProfile {
@AccountUsername
private String username;

@Email(regexp = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$", message = "The email \"{0}\" incorrect")
@Email(regexp = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$",
message = "The email \"${validatedValue}\" is not valid")
private String email;

@NotBlank
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public class SignupAccountModel {
@AccountUsername
private String username;

@Email(regexp = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$", message = "The email \"{0}\" incorrect")
@Email(regexp = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$",
message = "The email \"${validatedValue}\" is not valid")
private String email;

@AccountPassword
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@NoArgsConstructor
public class WorkspaceUserModel {

@Email(regexp = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$", message = "The email \"{0}\" is not valid")
@Email(regexp = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",
message = "The email \"${validatedValue}\" is not valid")
private String email;
}
12 changes: 6 additions & 6 deletions src/main/resources/templates/account/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
th:classappend="${!#fields.hasErrors('username') && formModified}? 'is-valid'"
th:errorclass="is-invalid">
<label for="inputUsername" th:text="#{username}"></label>
<div class="alert alert-danger" th:if="${#fields.hasErrors('username')}">
<div class="invalid-feedback" th:if="${#fields.hasErrors('username')}">
<p th:each="err : ${#fields.errors('username')}" th:text="${err}"></p>
</div>
</div>
Expand All @@ -24,7 +24,7 @@
th:classappend="${!#fields.hasErrors('email') && formModified}? 'is-valid'"
th:errorclass="is-invalid">
<label for="inputEmail" th:text="#{email}"></label>
<div class="alert alert-danger" th:if="${#fields.hasErrors('email')}">
<div class="invalid-feedback" th:if="${#fields.hasErrors('email')}">
<p th:each="err : ${#fields.errors('email')}" th:text="${err}"></p>
</div>
</div>
Expand All @@ -35,7 +35,7 @@
th:classappend="${!#fields.hasErrors('firstName') && formModified}? 'is-valid'"
th:errorclass="is-invalid">
<label for="inputFirstName" th:text="#{firstname}"></label>
<div class="alert alert-danger" th:if="${#fields.hasErrors('firstName')}">
<div class="invalid-feedback" th:if="${#fields.hasErrors('firstName')}">
<p th:each="err : ${#fields.errors('firstName')}" th:text="${err}"></p>
</div>
</div>
Expand All @@ -46,7 +46,7 @@
th:classappend="${!#fields.hasErrors('lastName') && formModified}? 'is-valid'"
th:errorclass="is-invalid">
<label for="inputLastName" th:text="#{lastname}"></label>
<div class="alert alert-danger" th:if="${#fields.hasErrors('lastName')}">
<div class="invalid-feedback" th:if="${#fields.hasErrors('lastName')}">
<p th:each="err : ${#fields.errors('lastName')}" th:text="${err}"></p>
</div>
</div>
Expand All @@ -55,7 +55,7 @@
<input id="inputPassword" placeholder="p" type="password" th:field="*{password}"
class="form-control" th:errorclass="is-invalid">
<label for="inputPassword" th:text="#{password}"></label>
<div class="alert alert-danger" th:if="${#fields.hasErrors('password')}">
<div class="invalid-feedback" th:if="${#fields.hasErrors('password')}">
<p th:each="err : ${#fields.errors('password')}" th:text="${err}"></p>
</div>
</div>
Expand All @@ -64,7 +64,7 @@
<input id="inputConfirmPassword" placeholder="p" type="password" th:field="*{confirmPassword}"
class="form-control" th:errorclass="is-invalid">
<label for="inputConfirmPassword" th:text="#{password.confirm}"></label>
<div class="alert alert-danger" th:if="${#fields.hasErrors('confirmPassword')}">
<div class="invalid-feedback" th:if="${#fields.hasErrors('confirmPassword')}">
<p th:each="err : ${#fields.errors('confirmPassword')}" th:text="${err}"></p>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/error-general.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{fragments/header :: head}"></head>
<body>
<div class="alert alert-danger">
<div class="invalid-feedback">
<span th:text="#{alert.general-error}"></span>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<main class="container">
<div class="row">
<div class="col">
<div class="alert alert-danger" role="alert" th:if="${param.error}" th:text="#{alert.bad-credential}"></div>
<div class="invalid-feedback" role="alert" th:if="${param.error}" th:text="#{alert.bad-credential}"></div>
<div class="alert alert-warning" role="alert" th:if="${param.logout}" th:text="#{alert.logout}"></div>
<form method="post" th:action="@{/login}">
<div class="form-floating mb-3">
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/widget/report-typo-error.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{fragments/header :: head}"></head>
<body>
<div class="alert alert-danger">
<div class="invalid-feedback">
<span th:text="#{alert.report-error}"></span>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/widget/typo-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
type="text"
></textarea>
<label class="form-label" for="commentTextarea" th:text="#{text.leave-comment}"></label>
<div class="alert alert-danger" th:if="${#fields.hasErrors('reporterComment')}">
<div class="invalid-feedback" th:if="${#fields.hasErrors('reporterComment')}">
<p th:each="err : ${#fields.errors('reporterComment')}" th:text="${err}"></p>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/workspace/wks-settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</div>
<div class="row mt-2">
<div class="col-12">
<ul class="alert alert-danger ps-5" th:if="${#fields.hasErrors('url')}">
<ul class="invalid-feedback" th:if="${#fields.hasErrors('url')}">
<li th:each="err : ${#fields.errors('url')}" th:text="${err}"></li>
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/workspace/wks-users.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
placeholder="Enter user email. For example: [email protected]" th:field="*{email}"
th:classappend="${!#fields.hasErrors('email') && formModified}? 'is-valid'"
th:errorclass="is-invalid" required>
<div class="alert alert-danger mt-1" th:if="${#fields.hasErrors('email')}">
<div class="invalid-feedback" th:if="${#fields.hasErrors('email')}">
<p class="mb-0" th:each="err : ${#fields.errors('email')}" th:text="${err}"></p>
</div>
<small id="emailHelp" class="form-text text-muted"></small>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static io.hexlet.typoreporter.test.Constraints.POSTGRES_IMAGE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;

Expand Down Expand Up @@ -74,6 +75,7 @@ void updateAccountWithWrongEmailDomain() throws Exception {
.param("confirmPassword", password)
.param("firstName", userName)
.param("lastName", userName)
.with(user(correctEmailDomain))
.with(csrf()));
assertThat(accountRepository.findAccountByEmail(wrongEmailDomain)).isEmpty();
assertThat(accountRepository.findAccountByEmail(correctEmailDomain).orElseThrow().getEmail())
Expand Down Expand Up @@ -103,6 +105,7 @@ void updateAccountEmailUsingDifferentCase() throws Exception {
.param("lastName", username)
.param("username", username)
.param("email", emailUpperCase)
.with(user(emailLowerCase))
.with(csrf()));
assertThat(accountRepository.findAccountByEmail(emailUpperCase)).isEmpty();
assertThat(accountRepository.findAccountByEmail(emailLowerCase)).isNotEmpty();
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/io/hexlet/typoreporter/web/SignupControllerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.testcontainers.containers.PostgreSQLContainer;

import static com.github.database.rider.core.api.configuration.Orthography.LOWERCASE;
import static io.hexlet.typoreporter.test.factory.EntitiesFactory.ACCOUNT_INCORRECT_EMAIL;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -123,4 +124,20 @@ void createAccountWithWrongPassword() throws Exception {
assertThat(accountRepository.findAccountByEmail(email)).isEmpty();

}

@Test
void signupInAccountWithBadEmail() throws Exception {
model.setEmail(ACCOUNT_INCORRECT_EMAIL);
var response = mockMvc.perform(post("/signup")
.param("username", model.getUsername())
.param("email", model.getEmail())
.param("password", model.getPassword())
.param("confirmPassword", model.getConfirmPassword())
.param("firstName", model.getFirstName())
.param("lastName", model.getLastName())
.with(csrf()))
.andReturn();
var body = response.getResponse().getContentAsString();
assertThat(body).contains(String.format("The email &quot;%s&quot; is not valid", ACCOUNT_INCORRECT_EMAIL));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ void addUserNonValidEmailTest() throws Exception {
.with(csrf()))
.andReturn();
var body = response.getResponse().getContentAsString();
assertThat(body).contains("The email %s is not valid", ACCOUNT_INCORRECT_EMAIL);
assertThat(body).contains(String.format("The email &quot;%s&quot; is not valid", ACCOUNT_INCORRECT_EMAIL));
}
}

Expand Down

0 comments on commit 1f8ca2a

Please sign in to comment.