use of org.cloudfoundry.credhub.auth.UserContext in project credhub by cloudfoundry-incubator.
the class PermissionedCertificateServiceTest method getAll_returnsAllCertificatesTheCurrentUserCanAccess.
@Test
public void getAll_returnsAllCertificatesTheCurrentUserCanAccess() throws Exception {
Credential myCredential = mock(Credential.class);
when(myCredential.getName()).thenReturn("my-credential");
Credential yourCredential = mock(Credential.class);
when(yourCredential.getName()).thenReturn("your-credential");
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(permissionCheckingService.hasPermission(user, "your-credential", PermissionOperation.READ)).thenReturn(false);
when(certificateDataService.findAll()).thenReturn(newArrayList(myCredential, yourCredential));
final List<Credential> certificates = subject.getAll(newArrayList());
assertThat(certificates, equalTo(newArrayList(myCredential)));
}
use of org.cloudfoundry.credhub.auth.UserContext in project credhub by cloudfoundry-incubator.
the class PermissionedCertificateServiceTest method deleteVersion_whenTheUserDoesNotHavePermission_returnsAnError.
@Test(expected = EntryNotFoundException.class)
public void deleteVersion_whenTheUserDoesNotHavePermission_returnsAnError() throws Exception {
UUID versionUuid = UUID.randomUUID();
UUID certificateUuid = UUID.randomUUID();
UserContext userContext = mock(UserContext.class);
when(userContextHolder.getUserContext()).thenReturn(userContext);
String user = "my-user";
when(userContext.getActor()).thenReturn(user);
String credentialName = "my-credential";
when(permissionCheckingService.hasPermission(user, credentialName, PermissionOperation.DELETE)).thenReturn(false);
Credential certificate = mock(Credential.class);
when(certificate.getName()).thenReturn(credentialName);
when(certificateDataService.findByUuid(certificateUuid)).thenReturn(certificate);
CertificateCredentialVersion versionToDelete = mock(CertificateCredentialVersion.class);
when(certificate.getUuid()).thenReturn(UUID.randomUUID());
when(certificateVersionDataService.findVersion(versionUuid)).thenReturn(versionToDelete);
when(versionToDelete.getCredential()).thenReturn(certificate);
subject.deleteVersion(certificateUuid, versionUuid, newArrayList());
}
use of org.cloudfoundry.credhub.auth.UserContext in project credhub by cloudfoundry-incubator.
the class PermissionedCertificateServiceTest method set_whenTheUserDoesNotHavePermission_throwsAnException.
@Test(expected = EntryNotFoundException.class)
public void set_whenTheUserDoesNotHavePermission_throwsAnException() {
UUID certificateUuid = UUID.randomUUID();
String credentialName = "my-credential";
Credential certificate = mock(Credential.class);
when(certificate.getName()).thenReturn(credentialName);
String user = "my-user";
UserContext userContext = mock(UserContext.class);
when(userContextHolder.getUserContext()).thenReturn(userContext);
when(userContext.getActor()).thenReturn(user);
when(certificateDataService.findByUuid(certificateUuid)).thenReturn(certificate);
when(permissionCheckingService.hasPermission(user, credentialName, PermissionOperation.WRITE)).thenReturn(false);
subject.set(certificateUuid, mock(CertificateCredentialValue.class), newArrayList());
}
use of org.cloudfoundry.credhub.auth.UserContext in project credhub by cloudfoundry-incubator.
the class PermissionedCertificateServiceTest method getVersions_returnsListWithVersions.
@Test
public void getVersions_returnsListWithVersions() throws Exception {
CredentialVersion myCredential = mock(CredentialVersion.class);
when(myCredential.getName()).thenReturn("my-credential");
CredentialVersion secondVersion = mock(CredentialVersion.class);
when(secondVersion.getName()).thenReturn("my-credential");
List<CredentialVersion> versions = newArrayList(myCredential, secondVersion);
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);
uuid = UUID.randomUUID();
when(certificateVersionDataService.findAllVersions(uuid)).thenReturn(versions);
final List<CredentialVersion> certificates = subject.getVersions(uuid, false, newArrayList());
assertThat(certificates, equalTo(versions));
}
use of org.cloudfoundry.credhub.auth.UserContext in project credhub by cloudfoundry-incubator.
the class PermissionedCertificateServiceTest method getVersions_returnsAnError_whenUserDoesntHavePermission.
@Test(expected = EntryNotFoundException.class)
public void getVersions_returnsAnError_whenUserDoesntHavePermission() throws Exception {
CredentialVersion myCredential = mock(CredentialVersion.class);
when(myCredential.getName()).thenReturn("my-credential");
CredentialVersion secondVersion = mock(CredentialVersion.class);
when(secondVersion.getName()).thenReturn("my-credential");
List<CredentialVersion> versions = newArrayList(myCredential, secondVersion);
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(false);
when(certificateVersionDataService.findAllVersions(uuid)).thenReturn(versions);
subject.getVersions(uuid, false, newArrayList());
}
Aggregations