Search in sources :

Example 21 with UserPrincipal

use of org.candlepin.auth.UserPrincipal in project candlepin by candlepin.

the class ConsumerResourceTest method testValidateShareConsumerRequiresRecipientFact.

@Test
public void testValidateShareConsumerRequiresRecipientFact() {
    ConsumerType share = this.mockConsumerType(new ConsumerType(ConsumerTypeEnum.SHARE));
    ConsumerTypeDTO shareDto = this.translator.translate(share, ConsumerTypeDTO.class);
    OwnerDTO ownerDto = new OwnerDTO().setDisplayName("Test Owner");
    ConsumerDTO c = createConsumerDTO("test-consumer", "test-user", ownerDto, shareDto);
    ConsumerResource consumerResource = new ConsumerResource(mockConsumerCurator, mockConsumerTypeCurator, null, null, null, mockEntitlementCurator, null, mockEntitlementCertServiceAdapter, i18n, null, null, null, null, null, mockPoolManager, null, mockOwnerCurator, null, null, null, null, null, null, new CandlepinCommonTestConfig(), null, null, null, consumerBindUtil, null, null, factValidator, null, consumerEnricher, migrationProvider, translator);
    UserPrincipal uap = mock(UserPrincipal.class);
    when(uap.canAccess(any(Object.class), any(SubResource.class), any(Access.class))).thenReturn(Boolean.TRUE);
    Owner o = mock(Owner.class);
    when(mockOwnerCurator.lookupByKey(any(String.class))).thenReturn(o);
    c.setFact("foo", "bar");
    thrown.expect(BadRequestException.class);
    thrown.expectMessage("must specify a recipient org");
    consumerResource.create(c, uap, "test-user", "test-owner", null, false);
}
Also used : SubResource(org.candlepin.auth.SubResource) Owner(org.candlepin.model.Owner) CandlepinCommonTestConfig(org.candlepin.config.CandlepinCommonTestConfig) ConsumerDTO(org.candlepin.dto.api.v1.ConsumerDTO) OwnerDTO(org.candlepin.dto.api.v1.OwnerDTO) Access(org.candlepin.auth.Access) Matchers.anyString(org.mockito.Matchers.anyString) ConsumerType(org.candlepin.model.ConsumerType) ConsumerTypeDTO(org.candlepin.dto.api.v1.ConsumerTypeDTO) UserPrincipal(org.candlepin.auth.UserPrincipal) Test(org.junit.Test)

Example 22 with UserPrincipal

use of org.candlepin.auth.UserPrincipal in project candlepin by candlepin.

the class UserResourceTest method testListAllOwners.

@Test
public void testListAllOwners() {
    User user = new User();
    user.setUsername("dummyuser" + TestUtil.randomInt());
    user.setPassword("password");
    userResource.createUser(user);
    Owner owner1 = createOwner();
    Owner owner2 = createOwner();
    Role owner1Role = new Role(owner1.getKey() + " role");
    Role owner2Role = new Role(owner2.getKey() + " role");
    owner1Role.addPermission(new PermissionBlueprint(PermissionType.OWNER, owner1, Access.ALL));
    owner1Role.addPermission(new PermissionBlueprint(PermissionType.OWNER, owner2, Access.READ_ONLY));
    owner1Role.addUser(user);
    owner2Role.addUser(user);
    roleCurator.create(owner1Role);
    roleCurator.create(owner2Role);
    Set<Permission> perms = new HashSet<>();
    perms.add(new OwnerPermission(owner1, Access.ALL));
    perms.add(new OwnerPermission(owner2, Access.READ_ONLY));
    Principal userPrincipal = new UserPrincipal(user.getUsername(), perms, false);
    // Requesting the list of owners for this user should assume ALL, and not
    // return owner2:
    Iterable<Owner> response = userResource.listUsersOwners(user.getUsername(), userPrincipal);
    List<Owner> owners = new LinkedList<>();
    for (Object entity : response) {
        owners.add((Owner) entity);
    }
    assertEquals(1, owners.size());
    assertEquals(owner1.getKey(), owners.get(0).getKey());
}
Also used : Owner(org.candlepin.model.Owner) OwnerPermission(org.candlepin.auth.permissions.OwnerPermission) User(org.candlepin.model.User) UserPrincipal(org.candlepin.auth.UserPrincipal) LinkedList(java.util.LinkedList) Role(org.candlepin.model.Role) PermissionBlueprint(org.candlepin.model.PermissionBlueprint) UsernameConsumersPermission(org.candlepin.auth.permissions.UsernameConsumersPermission) OwnerPermission(org.candlepin.auth.permissions.OwnerPermission) Permission(org.candlepin.auth.permissions.Permission) UserPrincipal(org.candlepin.auth.UserPrincipal) Principal(org.candlepin.auth.Principal) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 23 with UserPrincipal

use of org.candlepin.auth.UserPrincipal in project candlepin by candlepin.

the class PinsetterAsyncFilterTest method noJobMapPrincipal.

@Test
public void noJobMapPrincipal() {
    List<Permission> permissions = Arrays.asList(new Permission[] { new OwnerPermission(new Owner("test_owner"), Access.ALL) });
    Principal principal = new UserPrincipal("testing", permissions, false);
    when(this.principalProvider.get()).thenReturn(principal);
    JobDetail detail = newJob(RefreshPoolsJob.class).build();
    when(response.getEntity()).thenReturn(detail);
    this.interceptor.postProcess(response);
    Assert.assertEquals(principal, detail.getJobDataMap().get(PinsetterJobListener.PRINCIPAL_KEY));
}
Also used : RefreshPoolsJob(org.candlepin.pinsetter.tasks.RefreshPoolsJob) OwnerPermission(org.candlepin.auth.permissions.OwnerPermission) Owner(org.candlepin.model.Owner) JobDetail(org.quartz.JobDetail) OwnerPermission(org.candlepin.auth.permissions.OwnerPermission) Permission(org.candlepin.auth.permissions.Permission) UserPrincipal(org.candlepin.auth.UserPrincipal) Principal(org.candlepin.auth.Principal) UserPrincipal(org.candlepin.auth.UserPrincipal) Test(org.junit.Test)

Example 24 with UserPrincipal

use of org.candlepin.auth.UserPrincipal in project candlepin by candlepin.

the class ManifestManagerTest method testGenerateAndStoreManifest.

@Test
public void testGenerateAndStoreManifest() throws Exception {
    Consumer consumer = this.createMockConsumer(true);
    Cdn cdn = new Cdn("test-cdn", "Test CDN", "");
    String webAppPrefix = "webapp-prefix";
    String apiUrl = "api-url";
    Map<String, String> extData = new HashMap<>();
    Event event = mock(Event.class);
    when(eventFactory.exportCreated(eq(consumer))).thenReturn(event);
    UserPrincipal principal = TestUtil.createOwnerPrincipal();
    when(principalProvider.get()).thenReturn(principal);
    String exportId = "export-id";
    ManifestFile manifest = mock(ManifestFile.class);
    when(manifest.getId()).thenReturn(exportId);
    when(fileService.store(eq(ManifestFileType.EXPORT), any(File.class), eq(principal.getName()), any(String.class))).thenReturn(manifest);
    when(consumerCurator.verifyAndLookupConsumer(eq(consumer.getUuid()))).thenReturn(consumer);
    when(cdnCurator.lookupByLabel(eq(cdn.getLabel()))).thenReturn(cdn);
    List<Entitlement> ents = new ArrayList<>();
    when(entitlementCurator.listByConsumer(eq(consumer))).thenReturn(ents);
    ExportResult result = manager.generateAndStoreManifest(consumer.getUuid(), cdn.getLabel(), webAppPrefix, apiUrl, extData);
    assertEquals(consumer.getUuid(), result.getExportedConsumer());
    assertEquals(exportId, result.getExportId());
    verify(entitlementCurator).listByConsumer(eq(consumer));
    verify(exporter).getFullExport(eq(consumer), eq(cdn.getLabel()), eq(webAppPrefix), eq(apiUrl), eq(extData));
    verify(eventFactory).exportCreated(eq(consumer));
    verify(eventSink).queueEvent(eq(event));
    verify(fileService).delete(eq(ManifestFileType.EXPORT), eq(consumer.getUuid()));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cdn(org.candlepin.model.Cdn) UserPrincipal(org.candlepin.auth.UserPrincipal) ManifestFile(org.candlepin.sync.file.ManifestFile) Consumer(org.candlepin.model.Consumer) Event(org.candlepin.audit.Event) Entitlement(org.candlepin.model.Entitlement) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) ExportResult(org.candlepin.sync.ExportResult) Test(org.junit.Test)

Example 25 with UserPrincipal

use of org.candlepin.auth.UserPrincipal in project candlepin by candlepin.

the class ManifestManagerTest method testManifestImportAsync.

@Test
public void testManifestImportAsync() throws Exception {
    Owner owner = TestUtil.createOwner();
    File file = mock(File.class);
    String filename = "manifest.zip";
    ConflictOverrides overrides = new ConflictOverrides(Conflict.DISTRIBUTOR_CONFLICT);
    UserPrincipal principal = TestUtil.createOwnerPrincipal();
    when(principalProvider.get()).thenReturn(principal);
    ManifestFile manifest = mock(ManifestFile.class);
    when(fileService.store(ManifestFileType.IMPORT, file, principal.getName(), owner.getKey())).thenReturn(manifest);
    JobDetail job = manager.importManifestAsync(owner, file, filename, overrides);
    JobDataMap jobData = job.getJobDataMap();
    assertEquals(owner.getKey(), jobData.get("owner_id"));
    assertEquals(JobStatus.TargetType.OWNER, jobData.get("target_type"));
    assertEquals(owner.getKey(), jobData.get("target_id"));
    assertEquals(manifest.getId(), jobData.get("stored_manifest_file_id"));
    assertEquals(filename, jobData.get("uploaded_file_name"));
    ConflictOverrides retrievedOverrides = new ConflictOverrides((String[]) jobData.get("conflict_overrides"));
    assertTrue(retrievedOverrides.isForced(Conflict.DISTRIBUTOR_CONFLICT));
    verify(fileService).store(eq(ManifestFileType.IMPORT), eq(file), eq(principal.getName()), eq(owner.getKey()));
}
Also used : ConflictOverrides(org.candlepin.sync.ConflictOverrides) Owner(org.candlepin.model.Owner) JobDetail(org.quartz.JobDetail) JobDataMap(org.quartz.JobDataMap) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) UserPrincipal(org.candlepin.auth.UserPrincipal) ManifestFile(org.candlepin.sync.file.ManifestFile) Test(org.junit.Test)

Aggregations

UserPrincipal (org.candlepin.auth.UserPrincipal)30 Test (org.junit.Test)18 Owner (org.candlepin.model.Owner)16 Principal (org.candlepin.auth.Principal)14 OwnerPermission (org.candlepin.auth.permissions.OwnerPermission)10 User (org.candlepin.model.User)10 Permission (org.candlepin.auth.permissions.Permission)9 HashSet (java.util.HashSet)7 ConsumerDTO (org.candlepin.dto.api.v1.ConsumerDTO)7 ConsumerType (org.candlepin.model.ConsumerType)6 ConsumerTypeDTO (org.candlepin.dto.api.v1.ConsumerTypeDTO)5 OwnerDTO (org.candlepin.dto.api.v1.OwnerDTO)4 Consumer (org.candlepin.model.Consumer)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 NoAuthPrincipal (org.candlepin.auth.NoAuthPrincipal)3 UsernameConsumersPermission (org.candlepin.auth.permissions.UsernameConsumersPermission)3 Role (org.candlepin.model.Role)3 ManifestFile (org.candlepin.sync.file.ManifestFile)3 TestUtil.createConsumerDTO (org.candlepin.test.TestUtil.createConsumerDTO)3