TSK-1777: updated UpdateTaskCommentAccTest to use testapi

This commit is contained in:
ryzheboka 2021-12-15 16:52:45 +01:00 committed by Mustapha Zorgati
parent 828a4e451e
commit 5b2cf855cf
3 changed files with 88 additions and 67 deletions

View File

@ -63,6 +63,7 @@ class TaskCommentServiceImpl {
TaskComment originalTaskComment = getTaskComment(taskCommentImplToUpdate.getId()); TaskComment originalTaskComment = getTaskComment(taskCommentImplToUpdate.getId());
if (originalTaskComment.getCreator().equals(userId) if (originalTaskComment.getCreator().equals(userId)
&& taskCommentImplToUpdate.getCreator().equals(originalTaskComment.getCreator())
|| taskanaEngine.getEngine().isUserInRole(TaskanaRole.ADMIN) || taskanaEngine.getEngine().isUserInRole(TaskanaRole.ADMIN)
|| taskanaEngine.getEngine().isUserInRole(TaskanaRole.TASK_ADMIN)) { || taskanaEngine.getEngine().isUserInRole(TaskanaRole.TASK_ADMIN)) {

View File

@ -39,7 +39,7 @@ import pro.taskana.workbasket.api.models.WorkbasketSummary;
import pro.taskana.workbasket.internal.builder.WorkbasketAccessItemBuilder; import pro.taskana.workbasket.internal.builder.WorkbasketAccessItemBuilder;
@TaskanaIntegrationTest @TaskanaIntegrationTest
public class TaskQueryImplAccTest { class TaskQueryImplAccTest {
@TaskanaInject TaskService taskService; @TaskanaInject TaskService taskService;
@TaskanaInject WorkbasketService workbasketService; @TaskanaInject WorkbasketService workbasketService;

View File

@ -1,119 +1,139 @@
package acceptance.task; package acceptance.task;
import static acceptance.DefaultTestEntities.defaultTestClassification;
import static acceptance.DefaultTestEntities.defaultTestObjectReference;
import static acceptance.DefaultTestEntities.defaultTestWorkbasket;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThatThrownBy;
import acceptance.AbstractAccTest;
import java.util.List; import java.util.List;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable; import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import testapi.TaskanaInject;
import testapi.TaskanaIntegrationTest;
import pro.taskana.classification.api.ClassificationService;
import pro.taskana.classification.api.models.ClassificationSummary;
import pro.taskana.common.api.exceptions.ConcurrencyException; import pro.taskana.common.api.exceptions.ConcurrencyException;
import pro.taskana.common.api.exceptions.NotAuthorizedException; import pro.taskana.common.api.exceptions.NotAuthorizedException;
import pro.taskana.common.test.security.JaasExtension;
import pro.taskana.common.test.security.WithAccessId; import pro.taskana.common.test.security.WithAccessId;
import pro.taskana.task.api.TaskService; import pro.taskana.task.api.TaskService;
import pro.taskana.task.api.models.ObjectReference;
import pro.taskana.task.api.models.Task;
import pro.taskana.task.api.models.TaskComment; import pro.taskana.task.api.models.TaskComment;
import pro.taskana.task.internal.builder.TaskBuilder;
import pro.taskana.task.internal.builder.TaskCommentBuilder;
import pro.taskana.task.internal.models.TaskCommentImpl; import pro.taskana.task.internal.models.TaskCommentImpl;
import pro.taskana.workbasket.api.WorkbasketPermission;
import pro.taskana.workbasket.api.WorkbasketService;
import pro.taskana.workbasket.api.models.WorkbasketSummary;
import pro.taskana.workbasket.internal.builder.WorkbasketAccessItemBuilder;
@ExtendWith(JaasExtension.class) @TaskanaIntegrationTest
class UpdateTaskCommentAccTest extends AbstractAccTest { class UpdateTaskCommentAccTest {
@WithAccessId(user = "user-1-2") @TaskanaInject TaskService taskService;
@Test @TaskanaInject WorkbasketService workbasketService;
void should_UpdateTaskComment_For_TaskComment() throws Exception { @TaskanaInject ClassificationService classificationService;
TaskService taskService = taskanaEngine.getTaskService(); ClassificationSummary defaultClassificationSummary;
WorkbasketSummary defaultWorkbasketSummary;
ObjectReference defaultObjectReference;
List<TaskComment> taskComments = @WithAccessId(user = "businessadmin")
taskService.getTaskComments("TKI:000000000000000000000000000000000025"); @BeforeAll
assertThat(taskComments).hasSize(1); void setup() throws Exception {
assertThat(taskComments.get(0).getTextField()).isEqualTo("some text in textfield"); defaultClassificationSummary =
defaultTestClassification().buildAndStoreAsSummary(classificationService);
defaultWorkbasketSummary = defaultTestWorkbasket().buildAndStoreAsSummary(workbasketService);
TaskComment taskComment = WorkbasketAccessItemBuilder.newWorkbasketAccessItem()
taskService.getTaskComment("TCI:000000000000000000000000000000000003"); .workbasketId(defaultWorkbasketSummary.getId())
taskComment.setTextField("updated textfield"); .accessId("user-1-1")
.permission(WorkbasketPermission.OPEN)
taskService.updateTaskComment(taskComment); .permission(WorkbasketPermission.READ)
.permission(WorkbasketPermission.APPEND)
List<TaskComment> taskCommentsAfterUpdate = .buildAndStore(workbasketService);
taskService.getTaskComments("TKI:000000000000000000000000000000000025"); defaultObjectReference = defaultTestObjectReference().build();
assertThat(taskCommentsAfterUpdate.get(0).getTextField()).isEqualTo("updated textfield");
} }
@WithAccessId(user = "user-1-1") @WithAccessId(user = "user-1-1")
@Test @Test
void should_FailToUpdateTaskComment_When_UserHasNoAuthorization() throws Exception { void should_UpdateTaskComment_For_TaskComment() throws Exception {
Task task = createDefaultTask().buildAndStore(taskService);
TaskService taskService = taskanaEngine.getTaskService();
List<TaskComment> taskComments =
taskService.getTaskComments("TKI:000000000000000000000000000000000000");
assertThat(taskComments).hasSize(3);
assertThat(taskComments.get(0).getTextField()).isEqualTo("some text in textfield");
TaskComment taskComment = TaskComment taskComment =
taskService.getTaskComment("TCI:000000000000000000000000000000000001"); TaskCommentBuilder.newTaskComment()
.taskId(task.getId())
.textField("some text in textfield")
.buildAndStore(taskService);
taskComment.setTextField("updated textfield");
taskService.updateTaskComment(taskComment);
List<TaskComment> taskCommentsAfterUpdate = taskService.getTaskComments(task.getId());
assertThat(taskCommentsAfterUpdate)
.extracting(TaskComment::getTextField)
.containsExactly("updated textfield");
}
@WithAccessId(user = "user-1-2")
@Test
void should_FailToUpdateTaskComment_When_UserHasNoAuthorization() throws Exception {
Task task = createDefaultTask().buildAndStore(taskService, "user-1-1");
TaskComment taskComment =
TaskCommentBuilder.newTaskComment()
.taskId(task.getId())
.textField("some text in textfield")
.buildAndStore(taskService, "user-1-1");
taskComment.setTextField("updated textfield"); taskComment.setTextField("updated textfield");
assertThatThrownBy(() -> taskService.updateTaskComment(taskComment)) assertThatThrownBy(() -> taskService.updateTaskComment(taskComment))
.isInstanceOf(NotAuthorizedException.class); .isInstanceOf(NotAuthorizedException.class);
// make sure the task comment wasn't updated
List<TaskComment> taskCommentsAfterUpdateAttempt =
taskService.getTaskComments("TKI:000000000000000000000000000000000000");
assertThat(taskCommentsAfterUpdateAttempt.get(0).getTextField())
.isEqualTo("some text in textfield");
} }
@WithAccessId(user = "user-1-1") @WithAccessId(user = "user-1-1")
@Test @Test
void should_FailToUpdateTaskComment_When_UserTriesToUpdateTaskByManipulatingOwner() void should_FailToUpdateTaskComment_When_ChangingCreator() throws Exception {
throws Exception { Task task = createDefaultTask().buildAndStore(taskService);
TaskCommentImpl taskComment =
(TaskCommentImpl)
TaskCommentBuilder.newTaskComment()
.taskId(task.getId())
.textField("some text in textfield")
.buildAndStore(taskService);
TaskService taskService = taskanaEngine.getTaskService(); taskComment.setCreator("user-1-2");
TaskCommentImpl taskCommentToUpdate = ThrowingCallable updateTaskCommentCall = () -> taskService.updateTaskComment(taskComment);
(TaskCommentImpl) taskService.getTaskComment("TCI:000000000000000000000000000000000001");
taskCommentToUpdate.setTextField("updated textfield");
taskCommentToUpdate.setCreator("user-1-2");
ThrowingCallable updateTaskCommentCall =
() -> {
taskService.updateTaskComment(taskCommentToUpdate);
};
assertThatThrownBy(updateTaskCommentCall).isInstanceOf(NotAuthorizedException.class); assertThatThrownBy(updateTaskCommentCall).isInstanceOf(NotAuthorizedException.class);
} }
@WithAccessId(user = "user-1-1") @WithAccessId(user = "user-1-1")
@Test @Test
void should_FailToUpdateTaskComment_When_TaskCommentWasModifiedConcurrently() throws Exception { void should_FailToUpdateTaskComment_When_TaskCommentWasModifiedConcurrently() throws Exception {
Task task = createDefaultTask().buildAndStore(taskService);
TaskService taskService = taskanaEngine.getTaskService();
List<TaskComment> taskComments =
taskService.getTaskComments("TKI:000000000000000000000000000000000000");
assertThat(taskComments).hasSize(3);
assertThat(taskComments.get(2).getTextField()).isEqualTo("some other text in textfield");
TaskComment taskCommentToUpdate = TaskComment taskCommentToUpdate =
taskService.getTaskComment("TCI:000000000000000000000000000000000002"); TaskCommentBuilder.newTaskComment()
taskCommentToUpdate.setTextField("updated textfield"); .taskId(task.getId())
.textField("some text in textfield")
.buildAndStore(taskService);
taskCommentToUpdate.setTextField("updated textfield");
TaskComment concurrentTaskCommentToUpdate = TaskComment concurrentTaskCommentToUpdate =
taskService.getTaskComment("TCI:000000000000000000000000000000000002"); taskService.getTaskComment(taskCommentToUpdate.getId());
concurrentTaskCommentToUpdate.setTextField("concurrently updated textfield"); concurrentTaskCommentToUpdate.setTextField("concurrently updated textfield");
taskService.updateTaskComment(taskCommentToUpdate); taskService.updateTaskComment(taskCommentToUpdate);
assertThatThrownBy(() -> taskService.updateTaskComment(concurrentTaskCommentToUpdate)) assertThatThrownBy(() -> taskService.updateTaskComment(concurrentTaskCommentToUpdate))
.isInstanceOf(ConcurrencyException.class); .isInstanceOf(ConcurrencyException.class);
}
// make sure the task comment wasn't updated private TaskBuilder createDefaultTask() {
List<TaskComment> taskCommentsAfterUpdateAttempt = return (TaskBuilder.newTask()
taskService.getTaskComments("TKI:000000000000000000000000000000000000"); .classificationSummary(defaultClassificationSummary)
assertThat(taskCommentsAfterUpdateAttempt.get(2).getTextField()).isEqualTo("updated textfield"); .workbasketSummary(defaultWorkbasketSummary)
.primaryObjRef(defaultObjectReference));
} }
} }