Search in sources :

Example 26 with Encryption

use of org.sonar.api.config.internal.Encryption in project sonarqube by SonarSource.

the class AlmSettingDaoTest method selectByUuid.

@Test
public void selectByUuid() {
    when(uuidFactory.create()).thenReturn(A_UUID);
    when(encryption.isEncrypted(any())).thenReturn(false);
    AlmSettingDto almSettingDto = newGithubAlmSettingDto();
    underTest.insert(dbSession, almSettingDto);
    assertThat(underTest.selectByUuid(dbSession, A_UUID).get()).extracting(AlmSettingDto::getUuid, AlmSettingDto::getKey, AlmSettingDto::getRawAlm, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getCreatedAt, AlmSettingDto::getUpdatedAt, s -> almSettingDto.getDecryptedPrivateKey(encryption), s -> almSettingDto.getDecryptedPersonalAccessToken(encryption), s -> almSettingDto.getDecryptedClientSecret(encryption)).containsExactly(A_UUID, almSettingDto.getKey(), ALM.GITHUB.getId(), almSettingDto.getUrl(), almSettingDto.getAppId(), NOW, NOW, almSettingDto.getDecryptedPrivateKey(encryption), almSettingDto.getDecryptedPersonalAccessToken(encryption), almSettingDto.getDecryptedClientSecret(encryption));
    assertThat(underTest.selectByUuid(dbSession, "foo")).isNotPresent();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DbTester(org.sonar.db.DbTester) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UuidFactory(org.sonar.core.util.UuidFactory) Test(org.junit.Test) NoOpAuditPersister(org.sonar.db.audit.NoOpAuditPersister) Mockito.when(org.mockito.Mockito.when) DbSession(org.sonar.db.DbSession) TestSystem2(org.sonar.api.impl.utils.TestSystem2) List(java.util.List) Rule(org.junit.Rule) AlmSettingsTesting(org.sonar.db.almsettings.AlmSettingsTesting) Encryption(org.sonar.api.config.internal.Encryption) AlmSettingsTesting.newGithubAlmSettingDto(org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto) Mockito.mock(org.mockito.Mockito.mock) AlmSettingsTesting.newGithubAlmSettingDto(org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto) Test(org.junit.Test)

Example 27 with Encryption

use of org.sonar.api.config.internal.Encryption in project sonarqube by SonarSource.

the class AlmSettingDaoTest method selectByKey.

@Test
public void selectByKey() {
    when(uuidFactory.create()).thenReturn(A_UUID);
    String decrypted = "decrypted";
    when(encryption.isEncrypted(any())).thenReturn(true);
    when(encryption.decrypt(any())).thenReturn(decrypted);
    AlmSettingDto almSettingDto = AlmSettingsTesting.newGithubAlmSettingDto();
    underTest.insert(dbSession, almSettingDto);
    assertThat(underTest.selectByKey(dbSession, almSettingDto.getKey()).get()).extracting(AlmSettingDto::getUuid, AlmSettingDto::getKey, AlmSettingDto::getRawAlm, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getCreatedAt, AlmSettingDto::getUpdatedAt, s -> almSettingDto.getDecryptedPrivateKey(encryption), s -> almSettingDto.getDecryptedPersonalAccessToken(encryption), s -> almSettingDto.getDecryptedClientSecret(encryption)).containsExactly(A_UUID, almSettingDto.getKey(), ALM.GITHUB.getId(), almSettingDto.getUrl(), almSettingDto.getAppId(), NOW, NOW, almSettingDto.getDecryptedPrivateKey(encryption), null, almSettingDto.getDecryptedClientSecret(encryption));
    assertThat(underTest.selectByKey(dbSession, "foo")).isNotPresent();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DbTester(org.sonar.db.DbTester) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UuidFactory(org.sonar.core.util.UuidFactory) Test(org.junit.Test) NoOpAuditPersister(org.sonar.db.audit.NoOpAuditPersister) Mockito.when(org.mockito.Mockito.when) DbSession(org.sonar.db.DbSession) TestSystem2(org.sonar.api.impl.utils.TestSystem2) List(java.util.List) Rule(org.junit.Rule) AlmSettingsTesting(org.sonar.db.almsettings.AlmSettingsTesting) Encryption(org.sonar.api.config.internal.Encryption) AlmSettingsTesting.newGithubAlmSettingDto(org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto) Mockito.mock(org.mockito.Mockito.mock) AlmSettingsTesting.newGithubAlmSettingDto(org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto) Test(org.junit.Test)

Example 28 with Encryption

use of org.sonar.api.config.internal.Encryption in project sonarqube by SonarSource.

the class AlmSettingDaoTest method selectByKey_withEmptySecrets.

@Test
public void selectByKey_withEmptySecrets() {
    when(uuidFactory.create()).thenReturn(A_UUID);
    String decrypted = "decrypted";
    when(encryption.isEncrypted(any())).thenReturn(true);
    when(encryption.decrypt(any())).thenReturn(decrypted);
    AlmSettingDto almSettingDto = AlmSettingsTesting.newAlmSettingDtoWithEmptySecrets();
    underTest.insert(dbSession, almSettingDto);
    assertThat(underTest.selectByKey(dbSession, almSettingDto.getKey()).get()).extracting(AlmSettingDto::getUuid, AlmSettingDto::getKey, AlmSettingDto::getRawAlm, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getCreatedAt, AlmSettingDto::getUpdatedAt, s -> almSettingDto.getDecryptedPrivateKey(encryption), s -> almSettingDto.getDecryptedPersonalAccessToken(encryption), s -> almSettingDto.getDecryptedClientSecret(encryption)).containsExactly(A_UUID, almSettingDto.getKey(), ALM.GITHUB.getId(), almSettingDto.getUrl(), almSettingDto.getAppId(), NOW, NOW, null, null, null);
    assertThat(underTest.selectByKey(dbSession, "foo")).isNotPresent();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DbTester(org.sonar.db.DbTester) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UuidFactory(org.sonar.core.util.UuidFactory) Test(org.junit.Test) NoOpAuditPersister(org.sonar.db.audit.NoOpAuditPersister) Mockito.when(org.mockito.Mockito.when) DbSession(org.sonar.db.DbSession) TestSystem2(org.sonar.api.impl.utils.TestSystem2) List(java.util.List) Rule(org.junit.Rule) AlmSettingsTesting(org.sonar.db.almsettings.AlmSettingsTesting) Encryption(org.sonar.api.config.internal.Encryption) AlmSettingsTesting.newGithubAlmSettingDto(org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto) Mockito.mock(org.mockito.Mockito.mock) AlmSettingsTesting.newGithubAlmSettingDto(org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto) Test(org.junit.Test)

Example 29 with Encryption

use of org.sonar.api.config.internal.Encryption in project sonarqube by SonarSource.

the class CreateGithubActionTest method remove_trailing_slash.

@Test
public void remove_trailing_slash() {
    UserDto user = db.users().insertUser();
    userSession.logIn(user).setSystemAdministrator();
    ws.newRequest().setParam("key", "GitHub Server - Dev Team").setParam("url", "https://github.enterprise.com/").setParam("appId", "12345").setParam("privateKey", "678910").setParam("clientId", "client_1234").setParam("clientSecret", "client_so_secret").execute();
    assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession())).extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, s -> s.getDecryptedPrivateKey(encryption), AlmSettingDto::getClientId, s -> s.getDecryptedClientSecret(encryption)).containsOnly(tuple("GitHub Server - Dev Team", "https://github.enterprise.com", "12345", "678910", "client_1234", "client_so_secret"));
}
Also used : AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) ComponentFinder(org.sonar.server.component.ComponentFinder) UserDto(org.sonar.db.user.UserDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultipleAlmFeatureProvider(org.sonar.server.almsettings.MultipleAlmFeatureProvider) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) WsActionTester(org.sonar.server.ws.WsActionTester) BadRequestException(org.sonar.server.exceptions.BadRequestException) Rule(org.junit.Rule) WebService(org.sonar.api.server.ws.WebService) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Encryption(org.sonar.api.config.internal.Encryption) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) Tuple.tuple(org.assertj.core.groups.Tuple.tuple) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) UserDto(org.sonar.db.user.UserDto) Test(org.junit.Test)

Example 30 with Encryption

use of org.sonar.api.config.internal.Encryption in project sonarqube by SonarSource.

the class UpdateBitbucketCloudActionTest method update_with_new_key.

@Test
public void update_with_new_key() {
    when(encryption.isEncrypted(any())).thenReturn(false);
    UserDto user = db.users().insertUser();
    userSession.logIn(user).setSystemAdministrator();
    AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
    ws.newRequest().setParam("key", almSettingDto.getKey()).setParam("newKey", "Bitbucket Server - Infra Team").setParam("workspace", "workspace").setParam("clientId", "id").setParam("clientSecret", "secret").execute();
    assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession())).extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId, s -> s.getDecryptedClientSecret(encryption), AlmSettingDto::getAppId).containsOnly(tuple("Bitbucket Server - Infra Team", "id", "secret", "workspace"));
}
Also used : AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DbTester(org.sonar.db.DbTester) ComponentFinder(org.sonar.server.component.ComponentFinder) UserDto(org.sonar.db.user.UserDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultipleAlmFeatureProvider(org.sonar.server.almsettings.MultipleAlmFeatureProvider) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) WsActionTester(org.sonar.server.ws.WsActionTester) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) Rule(org.junit.Rule) WebService(org.sonar.api.server.ws.WebService) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Encryption(org.sonar.api.config.internal.Encryption) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) Tuple.tuple(org.assertj.core.groups.Tuple.tuple) ResourceTypes(org.sonar.api.resources.ResourceTypes) Mockito.mock(org.mockito.Mockito.mock) UserDto(org.sonar.db.user.UserDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Test(org.junit.Test)

Aggregations

Encryption (org.sonar.api.config.internal.Encryption)36 Test (org.junit.Test)35 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)30 Rule (org.junit.Rule)30 Mockito.mock (org.mockito.Mockito.mock)30 DbTester (org.sonar.db.DbTester)30 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)26 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)26 UserDto (org.sonar.db.user.UserDto)26 UserSessionRule (org.sonar.server.tester.UserSessionRule)26 WsActionTester (org.sonar.server.ws.WsActionTester)26 Tuple.tuple (org.assertj.core.groups.Tuple.tuple)25 WebService (org.sonar.api.server.ws.WebService)25 MultipleAlmFeatureProvider (org.sonar.server.almsettings.MultipleAlmFeatureProvider)25 ComponentFinder (org.sonar.server.component.ComponentFinder)25 ForbiddenException (org.sonar.server.exceptions.ForbiddenException)25 Mockito.when (org.mockito.Mockito.when)20 NotFoundException (org.sonar.server.exceptions.NotFoundException)20 String.format (java.lang.String.format)17 TestRequest (org.sonar.server.ws.TestRequest)17