import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.WebService.Param;
import org.sonar.core.permission.ProjectPermissions;
-import org.sonar.db.DbClient;
import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.server.permission.ws.PermissionsWsAction;
import org.sonar.server.user.UserSession;
private static final String PROPERTY_PREFIX = "projects_role.";
private static final String DESCRIPTION_SUFFIX = ".desc";
- private final DbClient dbClient;
private final UserSession userSession;
private final I18n i18n;
private final SearchTemplatesDataLoader dataLoader;
- public SearchTemplatesAction(DbClient dbClient, UserSession userSession, I18n i18n, SearchTemplatesDataLoader dataLoader) {
- this.dbClient = dbClient;
+ public SearchTemplatesAction(UserSession userSession, I18n i18n, SearchTemplatesDataLoader dataLoader) {
this.userSession = userSession;
this.i18n = i18n;
this.dataLoader = dataLoader;
import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.i18n.I18nRule;
+import org.sonar.server.permission.ws.BasePermissionWsTest;
+import org.sonar.server.permission.ws.PermissionsWsAction;
import org.sonar.server.tester.UserSessionRule;
-import org.sonar.server.ws.WsActionTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
import static org.sonar.test.JsonAssert.assertJson;
-public class SearchTemplatesActionTest {
+public class SearchTemplatesActionTest extends BasePermissionWsTest {
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
- WsActionTester ws;
- I18nRule i18n = new I18nRule();
- DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
- ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
- SearchTemplatesDataLoader dataLoader;
- SearchTemplatesAction underTest;
+ private I18nRule i18n = new I18nRule();
+ private DbClient dbClient = db.getDbClient();
+ private DbSession dbSession = db.getSession();
+ private ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
- @Before
- public void setUp() {
- i18n.setProjectPermissions();
+ @Override
+ protected PermissionsWsAction buildWsAction() {
Settings settings = new MapSettings();
settings.setProperty(defaultRootQualifierTemplateProperty(Qualifiers.PROJECT), UUID_EXAMPLE_01);
settings.setProperty(defaultRootQualifierTemplateProperty("DEV"), UUID_EXAMPLE_03);
DefaultPermissionTemplateFinder defaultPermissionTemplateFinder = new DefaultPermissionTemplateFinder(settings, resourceTypes);
+ SearchTemplatesDataLoader dataLoader = new SearchTemplatesDataLoader(dbClient, defaultPermissionTemplateFinder);
+ return new SearchTemplatesAction(userSession, i18n, dataLoader);
+ }
- dataLoader = new SearchTemplatesDataLoader(dbClient, defaultPermissionTemplateFinder);
- underTest = new SearchTemplatesAction(dbClient, userSession, i18n, dataLoader);
-
- ws = new WsActionTester(underTest);
-
+ @Before
+ public void setUp() {
+ i18n.setProjectPermissions();
userSession.login();
}
db.commit();
- String result = newRequest();
+ String result = newRequest().execute().getInput();
assertJson(result)
.withStrictArrayOrder()
@Test
public void empty_result() {
- String result = newRequest();
+ String result = newRequest().execute().getInput();
assertJson(result)
.withStrictArrayOrder()
insertDeveloperTemplate();
db.commit();
- String result = ws.newRequest()
+ String result = newRequest()
.setParam(TEXT_QUERY, "views")
- .execute().getInput();
+ .execute()
+ .getInput();
assertThat(result).contains("Default template for Views")
.doesNotContain("projects")
expectedException.expect(UnauthorizedException.class);
userSession.anonymous();
- ws.newRequest().execute();
+ newRequest().execute();
}
@Test
public void display_all_project_permissions() {
- String result = newRequest();
+ String result = newRequest().execute().getInput();
assertJson(result)
.withStrictArrayOrder()
.isSimilarTo(getClass().getResource("SearchTemplatesActionTest/display_all_project_permissions.json"));
}
- private String newRequest() {
- return ws.newRequest().execute().getInput();
- }
-
private PermissionTemplateDto insertProjectTemplate() {
return insertTemplate(newPermissionTemplateDto()
.setOrganizationUuid(db.getDefaultOrganization().getUuid())