]> source.dussan.org Git - sonarqube.git/blob
fe5d845c557590edb11348bf14ade25a080db976
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.db.permission.template;
21
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;
33
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;
41
42 public class PermissionTemplateDaoWithPersisterTest {
43   private final AuditPersister auditPersister = mock(AuditPersister.class);
44
45   private static final Date PAST = new Date(100_000_000_000L);
46   private static final Date NOW = new Date(500_000_000_000L);
47
48   @Rule
49   public final DbTester db = DbTester.create(auditPersister);
50   private final DbSession session = db.getSession();
51
52   private final ArgumentCaptor<PermissionTemplateNewValue> newValueCaptor = ArgumentCaptor.forClass(PermissionTemplateNewValue.class);
53   private final PermissionTemplateDao underTest = db.getDbClient().permissionTemplateDao();
54
55   @Test
56   public void insertPermissionTemplateIsPersisted() {
57     PermissionTemplateDto dto = insertPermissionTemplate();
58
59     verify(auditPersister).addPermissionTemplate(eq(session), newValueCaptor.capture());
60     PermissionTemplateNewValue newValue = newValueCaptor.getValue();
61     assertThat(newValue)
62       .extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName)
63       .containsExactly(dto.getUuid(), dto.getName());
64     assertThat(newValue.toString()).doesNotContain("keyPattern");
65   }
66
67   @Test
68   public void updatePermissionTemplateIsPersisted() {
69     PermissionTemplateDto dto = insertPermissionTemplate();
70     underTest.update(session, dto);
71
72     verify(auditPersister).updatePermissionTemplate(eq(session), newValueCaptor.capture());
73     PermissionTemplateNewValue newValue = newValueCaptor.getValue();
74     assertThat(newValue)
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");
79   }
80
81   @Test
82   public void deletePermissionTemplateIsPersisted() {
83     PermissionTemplateDto dto = insertPermissionTemplate();
84     underTest.deleteByUuid(session, dto.getUuid(), dto.getName());
85
86     verify(auditPersister).deletePermissionTemplate(eq(session), newValueCaptor.capture());
87     PermissionTemplateNewValue newValue = newValueCaptor.getValue();
88     assertThat(newValue)
89       .extracting(PermissionTemplateNewValue::getTemplateUuid, PermissionTemplateNewValue::getName)
90       .containsExactly(dto.getUuid(), dto.getName());
91     assertThat(newValue.toString()).doesNotContain("keyPattern");
92   }
93
94   @Test
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());
99
100     verify(auditPersister).addUserToPermissionTemplate(eq(session), newValueCaptor.capture());
101     PermissionTemplateNewValue newValue = newValueCaptor.getValue();
102     assertThat(newValue)
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");
107
108     underTest.deleteUserPermission(session, dto.getUuid(), user.getUuid(), ADMIN, dto.getName(), user.getLogin());
109
110     verify(auditPersister).deleteUserFromPermissionTemplate(eq(session), newValueCaptor.capture());
111     newValue = newValueCaptor.getValue();
112     assertThat(newValue)
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");
117   }
118
119   @Test
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());
125
126     verify(auditPersister).deleteUserFromPermissionTemplate(eq(session), newValueCaptor.capture());
127     PermissionTemplateNewValue newValue = newValueCaptor.getValue();
128     assertThat(newValue)
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");
133   }
134
135   @Test
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());
140
141     verify(auditPersister).addGroupToPermissionTemplate(eq(session), newValueCaptor.capture());
142     PermissionTemplateNewValue newValue = newValueCaptor.getValue();
143     assertThat(newValue)
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");
148
149     underTest.deleteGroupPermission(session, dto.getUuid(), group.getUuid(), ADMIN, dto.getName(), group.getName());
150
151     verify(auditPersister).deleteGroupFromPermissionTemplate(eq(session), newValueCaptor.capture());
152     newValue = newValueCaptor.getValue();
153     assertThat(newValue)
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");
158   }
159
160   @Test
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());
171
172     verify(auditPersister).addGroupToPermissionTemplate(eq(session), newValueCaptor.capture());
173     PermissionTemplateNewValue newValue = newValueCaptor.getValue();
174     assertThat(newValue)
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");
179
180     underTest.deleteByGroup(session, templateGroupDto.getGroupUuid(), templateGroupDto.getGroupName());
181
182     verify(auditPersister).deleteGroupFromPermissionTemplate(eq(session), newValueCaptor.capture());
183     newValue = newValueCaptor.getValue();
184     assertThat(newValue)
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");
189   }
190
191   private PermissionTemplateDto insertPermissionTemplate() {
192     return underTest.insert(session, newPermissionTemplateDto()
193       .setUuid("ABCD")
194       .setName("my template")
195       .setDescription("my description")
196       .setKeyPattern("myregexp")
197       .setCreatedAt(PAST)
198       .setUpdatedAt(NOW));
199   }
200 }