3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.db.permission.template;
22 import java.util.Date;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.sonar.core.util.Uuids;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.audit.AuditPersister;
30 import org.sonar.db.audit.model.PermissionTemplateNewValue;
31 import org.sonar.db.user.GroupDto;
32 import org.sonar.db.user.UserDto;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.mockito.ArgumentMatchers.eq;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.verify;
38 import static org.sonar.api.web.UserRole.ADMIN;
39 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
40 import static org.sonar.db.user.GroupTesting.newGroupDto;
42 public class PermissionTemplateDaoWithPersisterTest {
43 private final AuditPersister auditPersister = mock(AuditPersister.class);
45 private static final Date PAST = new Date(100_000_000_000L);
46 private static final Date NOW = new Date(500_000_000_000L);
49 public final DbTester db = DbTester.create(auditPersister);
50 private final DbSession session = db.getSession();
52 private final ArgumentCaptor<PermissionTemplateNewValue> newValueCaptor = ArgumentCaptor.forClass(PermissionTemplateNewValue.class);
53 private final PermissionTemplateDao underTest = db.getDbClient().permissionTemplateDao();
56 public void insertPermissionTemplateIsPersisted() {
57 PermissionTemplateDto dto = insertPermissionTemplate();
59 verify(auditPersister).addPermissionTemplate(eq(session), newValueCaptor.capture());
60 PermissionTemplateNewValue newValue = newValueCaptor.getValue();
62 .extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName)
63 .containsExactly(dto.getUuid(), dto.getName());
64 assertThat(newValue.toString()).doesNotContain("keyPattern");
68 public void updatePermissionTemplateIsPersisted() {
69 PermissionTemplateDto dto = insertPermissionTemplate();
70 underTest.update(session, dto);
72 verify(auditPersister).updatePermissionTemplate(eq(session), newValueCaptor.capture());
73 PermissionTemplateNewValue newValue = newValueCaptor.getValue();
75 .extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName,
76 PermissionTemplateNewValue::getDescription, PermissionTemplateNewValue::getKeyPattern)
77 .containsExactly(dto.getUuid(), dto.getName(), dto.getDescription(), dto.getKeyPattern());
78 assertThat(newValue.toString()).contains("keyPattern");
82 public void deletePermissionTemplateIsPersisted() {
83 PermissionTemplateDto dto = insertPermissionTemplate();
84 underTest.deleteByUuid(session, dto.getUuid(), dto.getName());
86 verify(auditPersister).deletePermissionTemplate(eq(session), newValueCaptor.capture());
87 PermissionTemplateNewValue newValue = newValueCaptor.getValue();
89 .extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName)
90 .containsExactly(dto.getUuid(), dto.getName());
91 assertThat(newValue.toString()).doesNotContain("keyPattern");
95 public void insertAndDeleteUserPermissionToTemplateIsPersisted() {
96 PermissionTemplateDto dto = insertPermissionTemplate();
97 UserDto user = db.users().insertUser();
98 underTest.insertUserPermission(session, dto.getUuid(), user.getUuid(), ADMIN, dto.getName(), user.getLogin());
100 verify(auditPersister).addUserToPermissionTemplate(eq(session), newValueCaptor.capture());
101 PermissionTemplateNewValue newValue = newValueCaptor.getValue();
103 .extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName,
104 PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::getUserUuid, PermissionTemplateNewValue::getUserLogin)
105 .containsExactly(dto.getUuid(), dto.getName(), ADMIN, user.getUuid(), user.getLogin());
106 assertThat(newValue.toString()).doesNotContain("groupUuid");
108 underTest.deleteUserPermission(session, dto.getUuid(), user.getUuid(), ADMIN, dto.getName(), user.getLogin());
110 verify(auditPersister).deleteUserFromPermissionTemplate(eq(session), newValueCaptor.capture());
111 newValue = newValueCaptor.getValue();
113 .extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName,
114 PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::getUserUuid, PermissionTemplateNewValue::getUserLogin)
115 .containsExactly(dto.getUuid(), dto.getName(), ADMIN, user.getUuid(), user.getLogin());
116 assertThat(newValue.toString()).doesNotContain("groupUuid");
120 public void insertAndDeleteUserPermissionByUserUuidToTemplateIsPersisted() {
121 PermissionTemplateDto dto = insertPermissionTemplate();
122 UserDto user = db.users().insertUser();
123 underTest.insertUserPermission(session, dto.getUuid(), user.getUuid(), ADMIN, dto.getName(), user.getLogin());
124 underTest.deleteUserPermissionsByUserUuid(session, user.getUuid(), user.getLogin());
126 verify(auditPersister).deleteUserFromPermissionTemplate(eq(session), newValueCaptor.capture());
127 PermissionTemplateNewValue newValue = newValueCaptor.getValue();
129 .extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName,
130 PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::getUserUuid, PermissionTemplateNewValue::getUserLogin)
131 .containsExactly(null, null, null, user.getUuid(), user.getLogin());
132 assertThat(newValue.toString()).doesNotContain("groupUuid");
136 public void insertAndDeleteGroupPermissionToTemplateIsPersisted() {
137 PermissionTemplateDto dto = insertPermissionTemplate();
138 GroupDto group = db.users().insertGroup(newGroupDto());
139 underTest.insertGroupPermission(session, dto.getUuid(), group.getUuid(), ADMIN, dto.getName(), group.getName());
141 verify(auditPersister).addGroupToPermissionTemplate(eq(session), newValueCaptor.capture());
142 PermissionTemplateNewValue newValue = newValueCaptor.getValue();
144 .extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName,
145 PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::getGroupUuid, PermissionTemplateNewValue::getGroupName)
146 .containsExactly(dto.getUuid(), dto.getName(), ADMIN, group.getUuid(), group.getName());
147 assertThat(newValue.toString()).contains("groupUuid");
149 underTest.deleteGroupPermission(session, dto.getUuid(), group.getUuid(), ADMIN, dto.getName(), group.getName());
151 verify(auditPersister).deleteGroupFromPermissionTemplate(eq(session), newValueCaptor.capture());
152 newValue = newValueCaptor.getValue();
154 .extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName,
155 PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::getGroupUuid, PermissionTemplateNewValue::getGroupName)
156 .containsExactly(dto.getUuid(), dto.getName(), ADMIN, group.getUuid(), group.getName());
157 assertThat(newValue.toString()).contains("groupUuid");
161 public void insertAndDeleteGroupPermissionByGroupUuidToTemplateIsPersisted() {
162 PermissionTemplateDto templateDto = insertPermissionTemplate();
163 PermissionTemplateGroupDto templateGroupDto = new PermissionTemplateGroupDto()
164 .setUuid(Uuids.createFast())
165 .setGroupName("group")
166 .setPermission(ADMIN)
167 .setTemplateUuid(templateDto.getUuid())
168 .setCreatedAt(new Date())
169 .setUpdatedAt(new Date());
170 underTest.insertGroupPermission(session, templateGroupDto, templateDto.getName());
172 verify(auditPersister).addGroupToPermissionTemplate(eq(session), newValueCaptor.capture());
173 PermissionTemplateNewValue newValue = newValueCaptor.getValue();
175 .extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName,
176 PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::getGroupUuid, PermissionTemplateNewValue::getGroupName)
177 .containsExactly(templateDto.getUuid(), templateDto.getName(), ADMIN, templateGroupDto.getGroupUuid(), templateGroupDto.getGroupName());
178 assertThat(newValue.toString()).doesNotContain("userUuid");
180 underTest.deleteByGroup(session, templateGroupDto.getGroupUuid(), templateGroupDto.getGroupName());
182 verify(auditPersister).deleteGroupFromPermissionTemplate(eq(session), newValueCaptor.capture());
183 newValue = newValueCaptor.getValue();
185 .extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName,
186 PermissionTemplateNewValue::getRole, PermissionTemplateNewValue::getGroupUuid, PermissionTemplateNewValue::getGroupName)
187 .containsExactly(null, null, null, templateGroupDto.getGroupUuid(), templateGroupDto.getGroupName());
188 assertThat(newValue.toString()).doesNotContain("userUuid");
191 private PermissionTemplateDto insertPermissionTemplate() {
192 return underTest.insert(session, newPermissionTemplateDto()
194 .setName("my template")
195 .setDescription("my description")
196 .setKeyPattern("myregexp")