use of org.cloudfoundry.credhub.auth.UserContext in project credhub by cloudfoundry-incubator.
the class PermissionedCertificateServiceTest method deleteVersion_deletesTheProvidedVersion.
@Test
public void deleteVersion_deletesTheProvidedVersion() {
UUID versionUuid = UUID.randomUUID();
UUID certificateUuid = UUID.randomUUID();
CertificateCredentialVersion versionToDelete = mock(CertificateCredentialVersion.class);
when(certificateVersionDataService.findVersion(versionUuid)).thenReturn(versionToDelete);
UserContext userContext = mock(UserContext.class);
when(userContextHolder.getUserContext()).thenReturn(userContext);
String user = "my-user";
String credentialName = "my-credential";
when(userContext.getActor()).thenReturn(user);
when(permissionCheckingService.hasPermission(user, credentialName, PermissionOperation.DELETE)).thenReturn(true);
Credential certificate = mock(Credential.class);
when(certificate.getName()).thenReturn(credentialName);
when(certificateDataService.findByUuid(certificateUuid)).thenReturn(certificate);
when(certificate.getUuid()).thenReturn(UUID.randomUUID());
when(certificateVersionDataService.findVersion(versionUuid)).thenReturn(versionToDelete);
when(versionToDelete.getCredential()).thenReturn(certificate);
CertificateCredentialVersion certificateCredentialVersion = subject.deleteVersion(certificateUuid, versionUuid, newArrayList());
assertThat(certificateCredentialVersion, equalTo(versionToDelete));
}
use of org.cloudfoundry.credhub.auth.UserContext in project credhub by cloudfoundry-incubator.
the class PermissionedCertificateServiceTest method getVersions_withCurrentTrue_returnsCurrentVersions.
@Test
public void getVersions_withCurrentTrue_returnsCurrentVersions() throws Exception {
Credential aCredential = new Credential("my-credential");
CredentialVersion credentialVersion1 = mock(CredentialVersion.class);
when(credentialVersion1.getName()).thenReturn("my-credential");
CredentialVersion credentialVersion2 = mock(CredentialVersion.class);
when(credentialVersion2.getName()).thenReturn("my-credential");
List<CredentialVersion> versions = newArrayList(credentialVersion1, credentialVersion2);
UserContext userContext = mock(UserContext.class);
when(userContextHolder.getUserContext()).thenReturn(userContext);
String user = "my-user";
when(userContext.getActor()).thenReturn(user);
when(permissionCheckingService.hasPermission(user, "my-credential", PermissionOperation.READ)).thenReturn(true);
when(certificateDataService.findByUuid(uuid)).thenReturn(aCredential);
when(certificateVersionDataService.findActiveWithTransitional("my-credential")).thenReturn(versions);
final List<CredentialVersion> certificates = subject.getVersions(uuid, true, newArrayList());
assertThat(certificates, equalTo(versions));
}
use of org.cloudfoundry.credhub.auth.UserContext in project credhub by cloudfoundry-incubator.
the class PermissionedCredentialServiceTest method setUp.
@Before
public void setUp() throws Exception {
initMocks(this);
userContext = mock(UserContext.class);
UserContextHolder userContextHolder = new UserContextHolder();
userContextHolder.setUserContext(userContext);
subject = new PermissionedCredentialService(credentialVersionDataService, credentialFactory, permissionCheckingService, certificateAuthorityService, userContextHolder, credentialDataService);
auditRecordParameters = new ArrayList<>();
generationParameters = mock(StringGenerationParameters.class);
credentialValue = mock(CredentialValue.class);
credential = new Credential(CREDENTIAL_NAME);
accessControlEntries = new ArrayList<>();
when(userContext.getActor()).thenReturn(USER);
existingCredentialVersion = new PasswordCredentialVersion(CREDENTIAL_NAME);
existingCredentialVersion.setEncryptor(encryptor);
when(permissionCheckingService.hasPermission(USER, CREDENTIAL_NAME, READ)).thenReturn(true);
when(permissionCheckingService.hasPermission(USER, CREDENTIAL_NAME, WRITE)).thenReturn(true);
when(credentialDataService.findByUUID(CREDENTIAL_UUID)).thenReturn(credential);
when(credentialVersionDataService.findByUuid(VERSION_UUID_STRING)).thenReturn(existingCredentialVersion);
when(request.getName()).thenReturn(CREDENTIAL_NAME);
when(request.getGenerationParameters()).thenReturn(generationParameters);
when(request.getAdditionalPermissions()).thenReturn(accessControlEntries);
}
use of org.cloudfoundry.credhub.auth.UserContext in project credhub by cloudfoundry-incubator.
the class AuditInterceptorTest method setup.
@Before
public void setup() {
requestAuditRecordDataService = mock(RequestAuditRecordDataService.class);
securityEventsLogService = mock(SecurityEventsLogService.class);
auditLogFactory = mock(AuditLogFactory.class);
userContextFactory = mock(UserContextFactory.class);
userContext = mock(UserContext.class);
requestUuid = new RequestUuid();
subject = new AuditInterceptor(requestAuditRecordDataService, securityEventsLogService, auditLogFactory, userContextFactory);
request = mock(HttpServletRequest.class);
response = mock(HttpServletResponse.class);
final Authentication authentication = mock(Authentication.class);
when(request.getUserPrincipal()).thenReturn(authentication);
userContext = mock(UserContext.class);
when(userContextFactory.createUserContext(any())).thenReturn(userContext);
when(userContext.getActor()).thenReturn("user");
}
use of org.cloudfoundry.credhub.auth.UserContext in project credhub by cloudfoundry-incubator.
the class CertificateAuthorityServiceTest method beforeEach.
@Before
public void beforeEach() {
certificate = new CertificateCredentialValue(null, CertificateStringConstants.SELF_SIGNED_CA_CERT, "my-key", null);
certificateCredential = mock(CertificateCredentialVersion.class);
permissionCheckingService = mock(PermissionCheckingService.class);
userContext = mock(UserContext.class);
when(userContext.getActor()).thenReturn(USER_NAME);
when(certificateCredential.getName()).thenReturn(CREDENTIAL_NAME);
when(permissionCheckingService.hasPermission(USER_NAME, CREDENTIAL_NAME, PermissionOperation.READ)).thenReturn(true);
certificateVersionDataService = mock(CertificateVersionDataService.class);
UserContextHolder userContextHolder = new UserContextHolder();
userContextHolder.setUserContext(userContext);
certificateAuthorityService = new CertificateAuthorityService(certificateVersionDataService, permissionCheckingService, userContextHolder);
}
Aggregations