Skip to content

Commit

Permalink
Merge pull request #269 from d1z3d/bugfix/#260
Browse files Browse the repository at this point in the history
Bugfix/#260
  • Loading branch information
fey committed Jun 13, 2024
2 parents 467da60 + 7a6e9fc commit b4413bb
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,45 +244,13 @@ public String getWorkspaceUsersPage(final Model model,

model.addAttribute("inputEmail", new WorkspaceUserModel());
model.addAttribute("formModified", false);
Optional<WorkspaceInfo> workSpaceInfoOptional = workspaceService.getWorkspaceInfoById(wksId);
if (workSpaceInfoOptional.isEmpty()) {
Optional<WorkspaceInfo> workSpaceInfo = workspaceService.getWorkspaceInfoById(wksId);
if (workSpaceInfo.isEmpty()) {
//TODO send error page
log.error("Workspace with id {} not found", wksId);
return "redirect:/workspaces";
}

WorkspaceInfo wksInfo = workSpaceInfoOptional.get();
model.addAttribute("wksName", wksInfo.name());
model.addAttribute("wksInfo", wksInfo);
getStatisticDataToModel(model, wksId);
getLastTypoDataToModel(model, wksId);

Optional<Workspace> workspaceOptional = workspaceService.getWorkspaceById(wksId);
Set<WorkspaceRole> workspaceRoles = workspaceOptional.get().getWorkspaceRoles();
List<Account> linkedAccounts = workspaceRoles.stream()
.map(WorkspaceRole::getAccount)
.collect(Collectors.toList());
List<Account> allAccounts = accountService.findAll();
List<Account> nonLinkedAccounts = getNonLinkedAccounts(allAccounts, linkedAccounts);
final Account authenticatedAccount = getAccountFromAuthentication();
final boolean accountIsAdminRole = workspaceService.isAdminRoleUserInWorkspace(wksId,
authenticatedAccount.getEmail());
List<Account> excludeDeleteAccounts = Collections.singletonList(authenticatedAccount);
Page<Account> userPage = new PageImpl<>(linkedAccounts, pageable, linkedAccounts.size());
var sort = userPage.getSort()
.stream()
.findFirst()
.orElseGet(() -> asc("createdDate"));

model.addAttribute("nonLinkedAccounts", nonLinkedAccounts);
model.addAttribute("isAdmin", accountIsAdminRole);
model.addAttribute("excludeDeleteAccounts", excludeDeleteAccounts);
model.addAttribute("userPage", userPage);
model.addAttribute("availableSizes", availableSizes);
model.addAttribute("sortProp", sort.getProperty());
model.addAttribute("sortDir", sort.getDirection());
model.addAttribute("DESC", DESC);
model.addAttribute("ASC", ASC);
prepareDataToRenderPage(model, workSpaceInfo.get(), pageable);
return "workspace/wks-users";
}

Expand All @@ -291,10 +259,19 @@ public String getWorkspaceUsersPage(final Model model,
public String addUser(@ModelAttribute("inputEmail") @Valid WorkspaceUserModel workspaceUserModel,
BindingResult bindingResult,
Model model,
@PathVariable Long wksId) {
@PathVariable Long wksId,
@SortDefault("createdDate") Pageable pageable
) {
model.addAttribute("formModified", true);
if (bindingResult.hasErrors()) {
return "redirect:/workspace/{wksId}/users";
Optional<WorkspaceInfo> workspaceInfo = workspaceService.getWorkspaceInfoById(wksId);
if (workspaceInfo.isEmpty()) {
//TODO send error page
log.error("Workspace with id {} not found", wksId);
return "redirect:/workspaces";
}
prepareDataToRenderPage(model, workspaceInfo.get(), pageable);
return "workspace/wks-users";
}
try {
workspaceRoleService.addAccountToWorkspace(wksId, workspaceUserModel.getEmail());
Expand Down Expand Up @@ -351,6 +328,48 @@ private List<Account> getNonLinkedAccounts(Collection<Account> allAccounts, Coll

private Account getAccountFromAuthentication() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return accountService.findByEmail(authentication.getName());
return accountService.findByEmail(authentication.getName());
}

/**
* Preparing data for page rendering /workspace/{id}/users
* @param model - it supplies attributes that used for rendering page
* @param workspaceInfo - dto of workspace model
* @param pageable - abstract interface for pagination page
*/
private void prepareDataToRenderPage(final Model model,
WorkspaceInfo workspaceInfo,
Pageable pageable) {
model.addAttribute("wksName", workspaceInfo.name());
model.addAttribute("wksInfo", workspaceInfo);
getStatisticDataToModel(model, workspaceInfo.id());
getLastTypoDataToModel(model, workspaceInfo.id());

Optional<Workspace> workspaceOptional = workspaceService.getWorkspaceById(workspaceInfo.id());
Set<WorkspaceRole> workspaceRoles = workspaceOptional.get().getWorkspaceRoles();
List<Account> linkedAccounts = workspaceRoles.stream()
.map(WorkspaceRole::getAccount)
.collect(Collectors.toList());
List<Account> allAccounts = accountService.findAll();
List<Account> nonLinkedAccounts = getNonLinkedAccounts(allAccounts, linkedAccounts);
final Account authenticatedAccount = getAccountFromAuthentication();
final boolean accountIsAdminRole = workspaceService.isAdminRoleUserInWorkspace(workspaceInfo.id(),
authenticatedAccount.getEmail());
List<Account> excludeDeleteAccounts = Collections.singletonList(authenticatedAccount);
Page<Account> userPage = new PageImpl<>(linkedAccounts, pageable, linkedAccounts.size());
var sort = userPage.getSort()
.stream()
.findFirst()
.orElseGet(() -> asc("createdDate"));

model.addAttribute("nonLinkedAccounts", nonLinkedAccounts);
model.addAttribute("isAdmin", accountIsAdminRole);
model.addAttribute("excludeDeleteAccounts", excludeDeleteAccounts);
model.addAttribute("userPage", userPage);
model.addAttribute("availableSizes", availableSizes);
model.addAttribute("sortProp", sort.getProperty());
model.addAttribute("sortDir", sort.getDirection());
model.addAttribute("DESC", DESC);
model.addAttribute("ASC", ASC);
}
}
4 changes: 2 additions & 2 deletions src/main/resources/templates/workspace/wks-users.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
placeholder="Enter user email. For example: hexlet@gmail.com" th:field="*{email}"
th:classappend="${!#fields.hasErrors('email') && formModified}? 'is-valid'"
th:errorclass="is-invalid" required>
<div class="alert alert-danger" th:if="${#fields.hasErrors('email')}">
<p th:each="err : ${#fields.errors('email')}" th:text="${err}"></p>
<div class="alert alert-danger mt-1" 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>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class EntitiesFactory {
public static final String ACCOUNT_102_EMAIL = "test102@gmail.com";

public static final String ACCOUNT_103_EMAIL = "test103@gmail.com";
public static final String ACCOUNT_INCORRECT_EMAIL = "incorrect.email@gmail";

public static final Long ACCOUNT_101_ID = 101L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,27 @@
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.time.Instant;
import java.util.UUID;
import java.util.Set;
import java.util.HashSet;
import java.util.Optional;
import java.util.Comparator;

import org.junit.jupiter.api.Disabled;

import static com.github.database.rider.core.api.configuration.Orthography.LOWERCASE;

import io.hexlet.typoreporter.domain.workspace.WorkspaceRole;
import io.hexlet.typoreporter.service.WorkspaceRoleService;

import static io.hexlet.typoreporter.test.Constraints.POSTGRES_IMAGE;
import static io.hexlet.typoreporter.test.factory.EntitiesFactory.WORKSPACE_103_ID;
import static io.hexlet.typoreporter.test.factory.EntitiesFactory.ACCOUNT_102_ID;
import static io.hexlet.typoreporter.test.factory.EntitiesFactory.ACCOUNT_102_EMAIL;
import static io.hexlet.typoreporter.test.factory.EntitiesFactory.ACCOUNT_102_ID;
import static io.hexlet.typoreporter.test.factory.EntitiesFactory.ACCOUNT_103_EMAIL;
import static io.hexlet.typoreporter.test.factory.EntitiesFactory.ACCOUNT_INCORRECT_EMAIL;
import static io.hexlet.typoreporter.test.factory.EntitiesFactory.WORKSPACE_103_ID;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
Expand Down Expand Up @@ -309,7 +314,7 @@ void putWorkspaceUpdateWithExistingWksUpdateUrl(final Long wksId, final String u
@ParameterizedTest
@MethodSource("io.hexlet.typoreporter.test.factory.EntitiesFactory#getWorkspacesAndUsersRelated")
void deleteWorkspaceByIdIsSuccessful(final Long wksId,
final String username) throws Exception {
final String username) throws Exception {

assertThat(repository.existsWorkspaceById(wksId)).isTrue();

Expand Down Expand Up @@ -359,10 +364,10 @@ void delUserFromWorkspace() throws Exception {
final Long rolesCountBeforeAdding = workspaceRoleRepository.count();

mockMvc.perform(
post("/workspace/{wksId}/users", WORKSPACE_103_ID)
.param("email", ACCOUNT_102_EMAIL)
.with(user(ACCOUNT_103_EMAIL))
.with(csrf()));
post("/workspace/{wksId}/users", WORKSPACE_103_ID)
.param("email", ACCOUNT_102_EMAIL)
.with(user(ACCOUNT_103_EMAIL))
.with(csrf()));
assertThat(workspaceRoleRepository.count()).isEqualTo(rolesCountBeforeAdding + 1L);
Optional<WorkspaceRole> addedWksRoleOptional = workspaceRoleRepository
.getWorkspaceRoleByAccountIdAndWorkspaceId(ACCOUNT_102_ID, WORKSPACE_103_ID);
Expand All @@ -388,6 +393,18 @@ void delUserFromWorkspace() throws Exception {
.getWorkspaceRoleByAccountIdAndWorkspaceId(ACCOUNT_102_ID, WORKSPACE_103_ID);
assertThat(addedWksRoleDeletedOptional).isEmpty();
}

@Test
void addUserNonValidEmailTest() throws Exception {
var response = mockMvc.perform(
post("/workspace/{wksId}/users", WORKSPACE_103_ID)
.param("email", ACCOUNT_INCORRECT_EMAIL)
.with(user(ACCOUNT_103_EMAIL))
.with(csrf()))
.andReturn();
var body = response.getResponse().getContentAsString();
assertThat(body).contains("The email %s is not valid", ACCOUNT_INCORRECT_EMAIL);
}
}


0 comments on commit b4413bb

Please sign in to comment.