import userEvent from '@testing-library/user-event';
import selectEvent from 'react-select-event';
import { QualityGatesServiceMock } from '../../../../api/mocks/QualityGatesServiceMock';
+import UsersServiceMock from '../../../../api/mocks/UsersServiceMock';
import { searchProjects, searchUsers } from '../../../../api/quality-gates';
+import { dismissNotice } from '../../../../api/users';
+import { mockLoggedInUser } from '../../../../helpers/testMocks';
import { RenderContext, renderAppRoutes } from '../../../../helpers/testReactTestingUtils';
+import { byRole } from '../../../../helpers/testSelector';
import { Feature } from '../../../../types/features';
+import { NoticeType } from '../../../../types/users';
import routes from '../../routes';
-let handler: QualityGatesServiceMock;
+let qualityGateHandler: QualityGatesServiceMock;
+let usersHandler: UsersServiceMock;
beforeAll(() => {
- handler = new QualityGatesServiceMock();
+ qualityGateHandler = new QualityGatesServiceMock();
+ usersHandler = new UsersServiceMock();
});
-afterEach(() => handler.reset());
+afterEach(() => {
+ qualityGateHandler.reset();
+ usersHandler.reset();
+});
it('should open the default quality gates', async () => {
renderQualityGateApp();
- const defaultQualityGate = handler.getDefaultQualityGate();
+ const defaultQualityGate = qualityGateHandler.getDefaultQualityGate();
expect(
await screen.findByRole('button', {
current: 'page',
expect(
await screen.findByRole('button', {
- name: `${handler.getDefaultQualityGate().name} default`,
+ name: `${qualityGateHandler.getDefaultQualityGate().name} default`,
}),
).toBeInTheDocument();
expect(
screen.getByRole('button', {
- name: `${handler.getBuiltInQualityGate().name} quality_gates.built_in`,
+ name: `${qualityGateHandler.getBuiltInQualityGate().name} quality_gates.built_in`,
}),
).toBeInTheDocument();
});
it('should be able to create a quality gate then delete it', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
let createButton = await screen.findByRole('button', { name: 'create' });
it('should be able to copy a quality gate which is CAYC compliant', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
const notDefaultQualityGate = await screen.findByText('Sonar way');
it('should not be able to copy a quality gate which is not CAYC compliant', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
const notDefaultQualityGate = await screen.findByText('SonarSource way - CFamily');
it('should be able to rename a quality gate', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
await user.click(await screen.findByLabelText('menu'));
const renameButton = screen.getByRole('menuitem', { name: 'rename' });
it('should not be able to set as default a quality gate which is not CAYC compliant', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
const notDefaultQualityGate = await screen.findByText('SonarSource way - CFamily');
it('should be able to set as default a quality gate which is CAYC compliant', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
const notDefaultQualityGate = await screen.findByRole('button', { name: /Sonar way/ });
it('should be able to add a condition', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
await user.click(await screen.findByText('SonarSource way - CFamily'));
it('should be able to edit a condition', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
const newConditions = within(await screen.findByTestId('quality-gates__conditions-new'));
it('should be able to handle duplicate or deprecated condition', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
await user.click(
// make it a regexp to ignore badges:
- await screen.findByRole('button', { name: new RegExp(handler.getCorruptedQualityGateName()) }),
+ await screen.findByRole('button', {
+ name: new RegExp(qualityGateHandler.getCorruptedQualityGateName()),
+ }),
);
expect(await screen.findByText('quality_gates.duplicated_conditions')).toBeInTheDocument();
it('should be able to handle delete condition', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
await user.click(await screen.findByText('Non Cayc QG'));
it('should show warning banner when CAYC condition is not properly set and should be able to update them', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
const qualityGate = await screen.findByText('SonarSource way - CFamily');
it('should warn user when quality gate is not CAYC compliant and user has permission to edit it', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
const nonCompliantQualityGate = await screen.findByRole('button', { name: /Non Cayc QG/ });
});
it('should render CaYC conditions on a separate table', async () => {
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
expect(await screen.findByTestId('quality-gates__conditions-cayc')).toBeInTheDocument();
expect(await screen.findByTestId('quality-gates__conditions-new')).toBeInTheDocument();
});
+it('should display CaYC condition simplification tour for users who didnt dismissed it', async () => {
+ const user = userEvent.setup();
+ qualityGateHandler.setIsAdmin(true);
+ renderQualityGateApp({ currentUser: mockLoggedInUser() });
+
+ const qualityGate = await screen.findByText('SonarSource way');
+
+ await act(async () => {
+ await user.click(qualityGate);
+ });
+
+ expect(await byRole('alertdialog').find()).toBeInTheDocument();
+
+ await act(async () => {
+ await user.click(byRole('alertdialog').byRole('button', { name: 'dismiss' }).get());
+ });
+
+ expect(await byRole('alertdialog').query()).not.toBeInTheDocument();
+ expect(dismissNotice).toHaveBeenLastCalledWith(NoticeType.QG_CAYC_CONDITIONS_SIMPLIFICATION);
+});
+
+it('should not display CaYC condition simplification tour for users who dismissed it', async () => {
+ const user = userEvent.setup();
+ qualityGateHandler.setIsAdmin(true);
+ renderQualityGateApp({
+ currentUser: mockLoggedInUser({
+ dismissedNotices: { [NoticeType.QG_CAYC_CONDITIONS_SIMPLIFICATION]: true },
+ }),
+ });
+
+ const qualityGate = await screen.findByText('SonarSource way');
+
+ await act(async () => {
+ await user.click(qualityGate);
+ });
+
+ expect(await byRole('alertdialog').query()).not.toBeInTheDocument();
+});
+
describe('The Project section', () => {
it('should render list of projects correctly in different tabs', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
const notDefaultQualityGate = await screen.findByText('SonarSource way - CFamily');
it('should handle select and deselect correctly', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
const notDefaultQualityGate = await screen.findByText('SonarSource way - CFamily');
it('should handle the search of projects', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
const notDefaultQualityGate = await screen.findByText('SonarSource way - CFamily');
});
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
const notDefaultQualityGate = await screen.findByText('SonarSource way - CFamily');
// await just to make sure we've loaded the page
expect(
await screen.findByRole('button', {
- name: `${handler.getDefaultQualityGate().name} default`,
+ name: `${qualityGateHandler.getDefaultQualityGate().name} default`,
}),
).toBeInTheDocument();
expect(screen.queryByText('quality_gates.permissions')).not.toBeInTheDocument();
});
it('should show button to grant permission when user is admin', async () => {
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
const grantPermissionButton = await screen.findByRole('button', {
it('should assign permission to a user and delete it later', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
expect(screen.queryByText('userlogin')).not.toBeInTheDocument();
it('should assign permission to a group and delete it later', async () => {
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
expect(screen.queryByText('userlogin')).not.toBeInTheDocument();
(searchUsers as jest.Mock).mockRejectedValue('error');
const user = userEvent.setup();
- handler.setIsAdmin(true);
+ qualityGateHandler.setIsAdmin(true);
renderQualityGateApp();
const grantPermissionButton = await screen.findByRole('button', {
*/
package org.sonar.server.user.ws;
+import java.util.Collection;
import java.util.Map;
import org.assertj.core.groups.Tuple;
import org.junit.Rule;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Suite;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.ResourceType;
import org.sonar.api.resources.ResourceTypeTree;
import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
import static org.sonar.db.permission.GlobalPermission.SCAN;
import static org.sonar.db.user.GroupTesting.newGroupDto;
+import static org.sonar.server.user.ws.DismissNoticeAction.AVAILABLE_NOTICE_KEYS;
import static org.sonar.test.JsonAssert.assertJson;
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+ CurrentActionIT.OtherTest.class,
+ CurrentActionIT.DimissableNoticeTest.class
+})
public class CurrentActionIT {
- @Rule
- public UserSessionRule userSession = UserSessionRule.standalone();
- @Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
-
- private final PlatformEditionProvider platformEditionProvider = mock(PlatformEditionProvider.class);
- private final HomepageTypesImpl homepageTypes = new HomepageTypesImpl();
- private final PermissionService permissionService = new PermissionServiceImpl(new ResourceTypes(new ResourceTypeTree[] {
- ResourceTypeTree.builder().addType(ResourceType.builder(Qualifiers.PROJECT).build()).build()}));
- private final WsActionTester ws = new WsActionTester(
- new CurrentAction(userSession, db.getDbClient(), new AvatarResolverImpl(), homepageTypes, platformEditionProvider, permissionService));
-
- @Test
- public void return_user_info() {
- UserDto user = db.users().insertUser(u -> u
- .setLogin("obiwan.kenobi")
- .setName("Obiwan Kenobi")
- .setEmail("obiwan.kenobi@starwars.com")
- .setLocal(true)
- .setExternalLogin("obiwan")
- .setExternalIdentityProvider("sonarqube")
- .setScmAccounts(newArrayList("obiwan:github", "obiwan:bitbucket")));
- userSession.logIn(user);
-
- CurrentWsResponse response = call();
-
- assertThat(response)
- .extracting(CurrentWsResponse::getIsLoggedIn, CurrentWsResponse::getLogin, CurrentWsResponse::getName, CurrentWsResponse::getEmail, CurrentWsResponse::getAvatar,
- CurrentWsResponse::getLocal,
- CurrentWsResponse::getExternalIdentity, CurrentWsResponse::getExternalProvider, CurrentWsResponse::getScmAccountsList)
- .containsExactly(true, "obiwan.kenobi", "Obiwan Kenobi", "obiwan.kenobi@starwars.com", "f5aa64437a1821ffe8b563099d506aef", true, "obiwan", "sonarqube",
- newArrayList("obiwan:bitbucket", "obiwan:github"));
+ public static class OtherTest {
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+ @Rule
+ public DbTester db = DbTester.create(System2.INSTANCE);
+
+ private final PlatformEditionProvider platformEditionProvider = mock(PlatformEditionProvider.class);
+ private final HomepageTypesImpl homepageTypes = new HomepageTypesImpl();
+ private final PermissionService permissionService = new PermissionServiceImpl(new ResourceTypes(new ResourceTypeTree[] {
+ ResourceTypeTree.builder().addType(ResourceType.builder(Qualifiers.PROJECT).build()).build()}));
+ private final WsActionTester ws = new WsActionTester(
+ new CurrentAction(userSession, db.getDbClient(), new AvatarResolverImpl(), homepageTypes, platformEditionProvider, permissionService));
+
+ private CurrentWsResponse call() {
+ return ws.newRequest().executeProtobuf(CurrentWsResponse.class);
+ }
+
+ @Test
+ public void return_user_info() {
+ UserDto user = db.users().insertUser(u -> u
+ .setLogin("obiwan.kenobi")
+ .setName("Obiwan Kenobi")
+ .setEmail("obiwan.kenobi@starwars.com")
+ .setLocal(true)
+ .setExternalLogin("obiwan")
+ .setExternalIdentityProvider("sonarqube")
+ .setScmAccounts(newArrayList("obiwan:github", "obiwan:bitbucket")));
+ userSession.logIn(user);
+
+ CurrentWsResponse response = call();
+
+ assertThat(response)
+ .extracting(CurrentWsResponse::getIsLoggedIn, CurrentWsResponse::getLogin, CurrentWsResponse::getName, CurrentWsResponse::getEmail, CurrentWsResponse::getAvatar,
+ CurrentWsResponse::getLocal,
+ CurrentWsResponse::getExternalIdentity, CurrentWsResponse::getExternalProvider, CurrentWsResponse::getScmAccountsList)
+ .containsExactly(true, "obiwan.kenobi", "Obiwan Kenobi", "obiwan.kenobi@starwars.com", "f5aa64437a1821ffe8b563099d506aef", true, "obiwan", "sonarqube",
+ newArrayList("obiwan:bitbucket", "obiwan:github"));
+ }
+
+ @Test
+ public void return_minimal_user_info() {
+ UserDto user = db.users().insertUser(u -> u
+ .setLogin("obiwan.kenobi")
+ .setName("Obiwan Kenobi")
+ .setEmail(null)
+ .setLocal(true)
+ .setExternalLogin("obiwan")
+ .setExternalIdentityProvider("sonarqube")
+ .setScmAccounts(emptyList()));
+ userSession.logIn(user);
+
+ CurrentWsResponse response = call();
+
+ assertThat(response)
+ .extracting(CurrentWsResponse::getIsLoggedIn, CurrentWsResponse::getLogin, CurrentWsResponse::getName, CurrentWsResponse::hasAvatar, CurrentWsResponse::getLocal,
+ CurrentWsResponse::getExternalIdentity, CurrentWsResponse::getExternalProvider, CurrentWsResponse::getUsingSonarLintConnectedMode)
+ .containsExactly(true, "obiwan.kenobi", "Obiwan Kenobi", false, true, "obiwan", "sonarqube", false);
+ assertThat(response.hasEmail()).isFalse();
+ assertThat(response.getScmAccountsList()).isEmpty();
+ assertThat(response.getGroupsList()).isEmpty();
+ assertThat(response.getPermissions().getGlobalList()).isEmpty();
+ }
+
+ @Test
+ public void convert_empty_email_to_null() {
+ UserDto user = db.users().insertUser(u -> u
+ .setLogin("obiwan.kenobi")
+ .setEmail(""));
+ userSession.logIn(user);
+
+ CurrentWsResponse response = call();
+
+ assertThat(response.hasEmail()).isFalse();
+ }
+
+ @Test
+ public void return_group_membership() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user);
+ db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Jedi")), user);
+ db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Rebel")), user);
+
+ CurrentWsResponse response = call();
+
+ assertThat(response.getGroupsList()).containsOnly("Jedi", "Rebel");
+ }
+
+ @Test
+ public void return_permissions() {
+ UserDto user = db.users().insertUser();
+ userSession
+ .logIn(user)
+ .addPermission(SCAN)
+ .addPermission(ADMINISTER_QUALITY_PROFILES);
+
+ CurrentWsResponse response = call();
+ assertThat(response.getPermissions().getGlobalList()).containsOnly("profileadmin", "scan");
+ }
+
+ @Test
+ public void fail_with_ISE_when_user_login_in_db_does_not_exist() {
+ db.users().insertUser(usert -> usert.setLogin("another"));
+ userSession.logIn("obiwan.kenobi");
+
+ assertThatThrownBy(this::call)
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("User login 'obiwan.kenobi' cannot be found");
+ }
+
+ @Test
+ public void anonymous() {
+ userSession
+ .anonymous()
+ .addPermission(SCAN)
+ .addPermission(PROVISION_PROJECTS);
+
+ CurrentWsResponse response = call();
+
+ assertThat(response.getIsLoggedIn()).isFalse();
+ assertThat(response.getPermissions().getGlobalList()).containsOnly("scan", "provisioning");
+ assertThat(response)
+ .extracting(CurrentWsResponse::hasLogin, CurrentWsResponse::hasName, CurrentWsResponse::hasEmail, CurrentWsResponse::hasLocal,
+ CurrentWsResponse::hasExternalIdentity, CurrentWsResponse::hasExternalProvider)
+ .containsOnly(false);
+ assertThat(response.getScmAccountsList()).isEmpty();
+ assertThat(response.getGroupsList()).isEmpty();
+ }
+
+ @Test
+ public void json_example() {
+ ComponentDto componentDto = db.components().insertPrivateProject(u -> u.setUuid("UUID-of-the-death-star").setKey("death-star-key")).getMainBranchComponent();
+ UserDto obiwan = db.users().insertUser(user -> user
+ .setLogin("obiwan.kenobi")
+ .setName("Obiwan Kenobi")
+ .setEmail("obiwan.kenobi@starwars.com")
+ .setLocal(true)
+ .setExternalLogin("obiwan.kenobi")
+ .setExternalIdentityProvider("sonarqube")
+ .setScmAccounts(newArrayList("obiwan:github", "obiwan:bitbucket"))
+ .setHomepageType("PROJECT")
+ .setHomepageParameter("UUID-of-the-death-star"));
+ userSession
+ .logIn(obiwan)
+ .addPermission(SCAN)
+ .addPermission(ADMINISTER_QUALITY_PROFILES)
+ .addProjectPermission(USER, db.components().getProjectDtoByMainBranch(componentDto));
+ db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Jedi")), obiwan);
+ db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Rebel")), obiwan);
+
+ String response = ws.newRequest().execute().getInput();
+
+ assertJson(response).isSimilarTo(getClass().getResource("current-example.json"));
+ }
+
+ @Test
+ public void handle_givenSonarLintUserInDatabase_returnSonarLintUserFromTheEndpoint() {
+ UserDto user = db.users().insertUser(u -> u.setLastSonarlintConnectionDate(System.currentTimeMillis()));
+ userSession.logIn(user);
+
+ CurrentWsResponse response = call();
+
+ assertThat(response.getUsingSonarLintConnectedMode()).isTrue();
+ }
+
+ @Test
+ public void test_definition() {
+ WebService.Action definition = ws.getDef();
+ assertThat(definition.key()).isEqualTo("current");
+ assertThat(definition.description()).isEqualTo("Get the details of the current authenticated user.");
+ assertThat(definition.since()).isEqualTo("5.2");
+ assertThat(definition.isPost()).isFalse();
+ assertThat(definition.isInternal()).isTrue();
+ assertThat(definition.responseExampleAsString()).isNotEmpty();
+ assertThat(definition.params()).isEmpty();
+ assertThat(definition.changelog()).isNotEmpty();
+ }
}
- @Test
- public void return_educationPrinciples_dismiss_notice() {
- UserDto user = db.users().insertUser();
- userSession.logIn(user);
-
- PropertyDto property = new PropertyDto().setUserUuid(user.getUuid()).setKey("user.dismissedNotices.educationPrinciples");
- db.properties().insertProperties(userSession.getLogin(), null, null, null, property);
-
- CurrentWsResponse response = call();
-
- assertThat(response.getDismissedNoticesMap().entrySet())
- .extracting(Map.Entry::getKey, Map.Entry::getValue)
- .contains(Tuple.tuple("educationPrinciples", true));
- }
-
- @Test
- public void return_educationPrinciples_not_dismissed() {
- UserDto user = db.users().insertUser();
- userSession.logIn(user);
-
- CurrentWsResponse response = call();
-
- assertThat(response.getDismissedNoticesMap().entrySet())
- .extracting(Map.Entry::getKey, Map.Entry::getValue)
- .contains(Tuple.tuple("educationPrinciples", false));
- }
-
- @Test
- public void return_minimal_user_info() {
- UserDto user = db.users().insertUser(u -> u
- .setLogin("obiwan.kenobi")
- .setName("Obiwan Kenobi")
- .setEmail(null)
- .setLocal(true)
- .setExternalLogin("obiwan")
- .setExternalIdentityProvider("sonarqube")
- .setScmAccounts(emptyList()));
- userSession.logIn(user);
-
- CurrentWsResponse response = call();
-
- assertThat(response)
- .extracting(CurrentWsResponse::getIsLoggedIn, CurrentWsResponse::getLogin, CurrentWsResponse::getName, CurrentWsResponse::hasAvatar, CurrentWsResponse::getLocal,
- CurrentWsResponse::getExternalIdentity, CurrentWsResponse::getExternalProvider, CurrentWsResponse::getUsingSonarLintConnectedMode)
- .containsExactly(true, "obiwan.kenobi", "Obiwan Kenobi", false, true, "obiwan", "sonarqube", false);
- assertThat(response.hasEmail()).isFalse();
- assertThat(response.getScmAccountsList()).isEmpty();
- assertThat(response.getGroupsList()).isEmpty();
- assertThat(response.getPermissions().getGlobalList()).isEmpty();
- }
-
- @Test
- public void convert_empty_email_to_null() {
- UserDto user = db.users().insertUser(u -> u
- .setLogin("obiwan.kenobi")
- .setEmail(""));
- userSession.logIn(user);
-
- CurrentWsResponse response = call();
-
- assertThat(response.hasEmail()).isFalse();
- }
-
- @Test
- public void return_group_membership() {
- UserDto user = db.users().insertUser();
- userSession.logIn(user);
- db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Jedi")), user);
- db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Rebel")), user);
-
- CurrentWsResponse response = call();
-
- assertThat(response.getGroupsList()).containsOnly("Jedi", "Rebel");
- }
-
- @Test
- public void return_permissions() {
- UserDto user = db.users().insertUser();
- userSession
- .logIn(user)
- .addPermission(SCAN)
- .addPermission(ADMINISTER_QUALITY_PROFILES);
-
- CurrentWsResponse response = call();
- assertThat(response.getPermissions().getGlobalList()).containsOnly("profileadmin", "scan");
- }
-
- @Test
- public void fail_with_ISE_when_user_login_in_db_does_not_exist() {
- db.users().insertUser(usert -> usert.setLogin("another"));
- userSession.logIn("obiwan.kenobi");
-
- assertThatThrownBy(this::call)
- .isInstanceOf(IllegalStateException.class)
- .hasMessage("User login 'obiwan.kenobi' cannot be found");
+ @RunWith(Parameterized.class)
+ public static class DimissableNoticeTest {
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+ @Rule
+ public DbTester db = DbTester.create(System2.INSTANCE);
+
+ private final PlatformEditionProvider platformEditionProvider = mock(PlatformEditionProvider.class);
+ private final HomepageTypesImpl homepageTypes = new HomepageTypesImpl();
+ private final PermissionService permissionService = new PermissionServiceImpl(new ResourceTypes(new ResourceTypeTree[] {
+ ResourceTypeTree.builder().addType(ResourceType.builder(Qualifiers.PROJECT).build()).build()}));
+ private final WsActionTester ws = new WsActionTester(
+ new CurrentAction(userSession, db.getDbClient(), new AvatarResolverImpl(), homepageTypes, platformEditionProvider, permissionService));
+
+ private CurrentWsResponse call() {
+ return ws.newRequest().executeProtobuf(CurrentWsResponse.class);
+ }
+
+ @Parameterized.Parameters
+ public static Collection<String> parameterCombination() {
+ return AVAILABLE_NOTICE_KEYS;
+ }
+
+ private final String notice;
+
+ public DimissableNoticeTest(String notice) {
+ this.notice = notice;
+ }
+
+ @Test
+ public void return_dismissed_notice() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user);
+
+ PropertyDto property = new PropertyDto().setUserUuid(user.getUuid()).setKey("user.dismissedNotices." + this.notice);
+ db.properties().insertProperties(userSession.getLogin(), null, null, null, property);
+
+ CurrentWsResponse response = call();
+
+ assertThat(response.getDismissedNoticesMap().entrySet())
+ .extracting(Map.Entry::getKey, Map.Entry::getValue)
+ .contains(Tuple.tuple(this.notice, true));
+ }
+
+ @Test
+ public void return_not_dismissed_notice() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user);
+
+ CurrentWsResponse response = call();
+
+ assertThat(response.getDismissedNoticesMap().entrySet())
+ .extracting(Map.Entry::getKey, Map.Entry::getValue)
+ .contains(Tuple.tuple(this.notice, false));
+ }
}
-
- @Test
- public void anonymous() {
- userSession
- .anonymous()
- .addPermission(SCAN)
- .addPermission(PROVISION_PROJECTS);
-
- CurrentWsResponse response = call();
-
- assertThat(response.getIsLoggedIn()).isFalse();
- assertThat(response.getPermissions().getGlobalList()).containsOnly("scan", "provisioning");
- assertThat(response)
- .extracting(CurrentWsResponse::hasLogin, CurrentWsResponse::hasName, CurrentWsResponse::hasEmail, CurrentWsResponse::hasLocal,
- CurrentWsResponse::hasExternalIdentity, CurrentWsResponse::hasExternalProvider)
- .containsOnly(false);
- assertThat(response.getScmAccountsList()).isEmpty();
- assertThat(response.getGroupsList()).isEmpty();
- }
-
- @Test
- public void json_example() {
- ComponentDto componentDto = db.components().insertPrivateProject(u -> u.setUuid("UUID-of-the-death-star").setKey("death-star-key")).getMainBranchComponent();
- UserDto obiwan = db.users().insertUser(user -> user
- .setLogin("obiwan.kenobi")
- .setName("Obiwan Kenobi")
- .setEmail("obiwan.kenobi@starwars.com")
- .setLocal(true)
- .setExternalLogin("obiwan.kenobi")
- .setExternalIdentityProvider("sonarqube")
- .setScmAccounts(newArrayList("obiwan:github", "obiwan:bitbucket"))
- .setHomepageType("PROJECT")
- .setHomepageParameter("UUID-of-the-death-star"));
- userSession
- .logIn(obiwan)
- .addPermission(SCAN)
- .addPermission(ADMINISTER_QUALITY_PROFILES)
- .addProjectPermission(USER, db.components().getProjectDtoByMainBranch(componentDto));
- db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Jedi")), obiwan);
- db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Rebel")), obiwan);
-
- String response = ws.newRequest().execute().getInput();
-
- assertJson(response).isSimilarTo(getClass().getResource("current-example.json"));
- }
-
- @Test
- public void handle_givenSonarLintUserInDatabase_returnSonarLintUserFromTheEndpoint() {
- UserDto user = db.users().insertUser(u -> u.setLastSonarlintConnectionDate(System.currentTimeMillis()));
- userSession.logIn(user);
-
- CurrentWsResponse response = call();
-
- assertThat(response.getUsingSonarLintConnectedMode()).isTrue();
- }
-
- @Test
- public void return_sonarlintAd_dismiss_notice() {
- UserDto user = db.users().insertUser();
- userSession.logIn(user);
-
- PropertyDto property = new PropertyDto().setUserUuid(user.getUuid()).setKey("user.dismissedNotices.sonarlintAd");
- db.properties().insertProperties(userSession.getLogin(), null, null, null, property);
-
- CurrentWsResponse response = call();
-
- assertThat(response.getDismissedNoticesMap().entrySet())
- .extracting(Map.Entry::getKey, Map.Entry::getValue)
- .contains(Tuple.tuple("sonarlintAd", true));
- }
-
- @Test
- public void return_sonarlintAd_not_dismissed() {
- UserDto user = db.users().insertUser();
- userSession.logIn(user);
-
- CurrentWsResponse response = call();
-
- assertThat(response.getDismissedNoticesMap().entrySet())
- .extracting(Map.Entry::getKey, Map.Entry::getValue)
- .contains(Tuple.tuple("sonarlintAd", false));
- }
-
-
- @Test
- public void test_definition() {
- WebService.Action definition = ws.getDef();
- assertThat(definition.key()).isEqualTo("current");
- assertThat(definition.description()).isEqualTo("Get the details of the current authenticated user.");
- assertThat(definition.since()).isEqualTo("5.2");
- assertThat(definition.isPost()).isFalse();
- assertThat(definition.isInternal()).isTrue();
- assertThat(definition.responseExampleAsString()).isNotEmpty();
- assertThat(definition.params()).isEmpty();
- assertThat(definition.changelog()).isNotEmpty();
- }
-
- private CurrentWsResponse call() {
- return ws.newRequest().executeProtobuf(CurrentWsResponse.class);
- }
-
}