Search in sources :

Example 6 with ForbiddenException

use of org.candlepin.common.exceptions.ForbiddenException in project candlepin by candlepin.

the class ConsumerBindUtilTest method registerPassWhenAutobindDisabledForOwnerAndKeyHasAutobindEnabled.

@Test
public void registerPassWhenAutobindDisabledForOwnerAndKeyHasAutobindEnabled() throws Exception {
    List<ActivationKey> keys = new ArrayList<>();
    ActivationKey key1 = new ActivationKey("key1", owner);
    key1.setAutoAttach(true);
    keys.add(key1);
    Product prod1 = TestUtil.createProduct();
    Pool pool1 = TestUtil.createPool(owner, prod1, 5);
    pool1.setId("pool1");
    key1.addPool(pool1, 10L);
    Consumer consumer = new Consumer("sys.example.com", null, null, system);
    when(entitler.bindByPoolQuantity(eq(consumer), eq(pool1.getId()), eq(10))).thenThrow(new ForbiddenException("fail"));
    consumerBindUtil.handleActivationKeys(consumer, keys, true);
}
Also used : ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Consumer(org.candlepin.model.Consumer) ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Pool(org.candlepin.model.Pool) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) Test(org.junit.Test)

Example 7 with ForbiddenException

use of org.candlepin.common.exceptions.ForbiddenException in project candlepin by candlepin.

the class ManifestManagerTest method verifyConsumerIsDistributorBeforeGeneratingManifest.

@Test
public void verifyConsumerIsDistributorBeforeGeneratingManifest() throws Exception {
    Consumer consumer = this.createMockConsumer(false);
    ConsumerType ctype = consumerTypeCurator.getConsumerType(consumer);
    Cdn cdn = new Cdn("test-cdn", "Test CDN", "");
    String webAppPrefix = "webapp-prefix";
    String apiUrl = "api-url";
    Map<String, String> extData = new HashMap<>();
    when(consumerCurator.verifyAndLookupConsumer(eq(consumer.getUuid()))).thenReturn(consumer);
    when(cdnCurator.lookupByLabel(eq(cdn.getLabel()))).thenReturn(cdn);
    try {
        manager.generateManifest(consumer.getUuid(), cdn.getLabel(), webAppPrefix, apiUrl, extData);
        fail("Expected ForbiddenException not thrown");
    } catch (Exception e) {
        assertTrue(e instanceof ForbiddenException);
        String expectedMsg = String.format("Unit %s cannot be exported. A manifest cannot be made for " + "units of type \"%s\".", consumer.getUuid(), ctype.getLabel());
        assertEquals(e.getMessage(), expectedMsg);
    }
}
Also used : ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Consumer(org.candlepin.model.Consumer) HashMap(java.util.HashMap) ConsumerType(org.candlepin.model.ConsumerType) Cdn(org.candlepin.model.Cdn) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Test(org.junit.Test)

Example 8 with ForbiddenException

use of org.candlepin.common.exceptions.ForbiddenException in project candlepin by candlepin.

the class ManifestManagerTest method verifyConsumerIsDistributorBeforeSchedulingManifestGeneration.

@Test
public void verifyConsumerIsDistributorBeforeSchedulingManifestGeneration() throws Exception {
    Owner owner = TestUtil.createOwner();
    Consumer consumer = this.createMockConsumer(owner, false);
    ConsumerType ctype = consumerTypeCurator.getConsumerType(consumer);
    Cdn cdn = new Cdn("test-cdn", "Test CDN", "");
    String webAppPrefix = "webapp-prefix";
    String apiUrl = "api-url";
    Map<String, String> extData = new HashMap<>();
    when(consumerCurator.verifyAndLookupConsumer(eq(consumer.getUuid()))).thenReturn(consumer);
    when(cdnCurator.lookupByLabel(eq(cdn.getLabel()))).thenReturn(cdn);
    try {
        manager.generateManifestAsync(consumer.getUuid(), owner.getKey(), cdn.getLabel(), webAppPrefix, apiUrl, extData);
        fail("Expected ForbiddenException not thrown");
    } catch (Exception e) {
        assertTrue(e instanceof ForbiddenException);
        String expectedMsg = String.format("Unit %s cannot be exported. A manifest cannot be made for " + "units of type \"%s\".", consumer.getUuid(), ctype.getLabel());
        assertEquals(e.getMessage(), expectedMsg);
    }
}
Also used : Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Consumer(org.candlepin.model.Consumer) HashMap(java.util.HashMap) ConsumerType(org.candlepin.model.ConsumerType) Cdn(org.candlepin.model.Cdn) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Test(org.junit.Test)

Example 9 with ForbiddenException

use of org.candlepin.common.exceptions.ForbiddenException in project candlepin by candlepin.

the class OwnerContentResourceTest method deleteLockedContent.

@Test(expected = ForbiddenException.class)
public void deleteLockedContent() {
    Owner owner = this.createOwner("test_owner");
    Content content = this.createContent("test_content", "test_content", owner);
    content.setLocked(true);
    this.contentCurator.merge(content);
    Environment environment = this.createEnvironment(owner, "test_env", "test_env", null, null, Arrays.asList(content));
    assertNotNull(this.ownerContentCurator.getContentById(owner, content.getId()));
    try {
        this.ownerContentResource.remove(owner.getKey(), content.getId());
    } catch (ForbiddenException e) {
        assertNotNull(this.ownerContentCurator.getContentById(owner, content.getId()));
        this.environmentCurator.evict(environment);
        environment = this.environmentCurator.find(environment.getId());
        assertEquals(1, environment.getEnvironmentContent().size());
        throw e;
    }
}
Also used : Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Content(org.candlepin.model.Content) Environment(org.candlepin.model.Environment) Test(org.junit.Test)

Example 10 with ForbiddenException

use of org.candlepin.common.exceptions.ForbiddenException in project candlepin by candlepin.

the class EntitlerJobTest method handleException.

@Test(expected = JobExecutionException.class)
public void handleException() throws JobExecutionException, EntitlementRefusedException {
    PoolIdAndQuantity[] pQs = new PoolIdAndQuantity[1];
    pQs[0] = new PoolIdAndQuantity("pool10", 1);
    JobDetail detail = EntitlerJob.bindByPoolAndQuantities(consumer, owner.getKey(), pQs);
    JobExecutionContext ctx = mock(JobExecutionContext.class);
    when(ctx.getMergedJobDataMap()).thenReturn(detail.getJobDataMap());
    Class<HashMap<String, Integer>> className = (Class<HashMap<String, Integer>>) (Class) Map.class;
    ArgumentCaptor<HashMap<String, Integer>> pqMapCaptor = ArgumentCaptor.forClass(className);
    when(e.bindByPoolQuantities(eq(consumerUuid), pqMapCaptor.capture())).thenThrow(new ForbiddenException("job should fail"));
    EntitlerJob job = new EntitlerJob(e, null, null, null);
    injector.injectMembers(job);
    job.execute(ctx);
}
Also used : ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) PoolIdAndQuantity(org.candlepin.model.dto.PoolIdAndQuantity) HashMap(java.util.HashMap) JobDetail(org.quartz.JobDetail) JobExecutionContext(org.quartz.JobExecutionContext) HashMap(java.util.HashMap) Map(org.hibernate.mapping.Map) Test(org.junit.Test)

Aggregations

ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)37 Owner (org.candlepin.model.Owner)22 Consumer (org.candlepin.model.Consumer)19 Test (org.junit.Test)15 BadRequestException (org.candlepin.common.exceptions.BadRequestException)13 Product (org.candlepin.model.Product)13 ApiOperation (io.swagger.annotations.ApiOperation)12 Produces (javax.ws.rs.Produces)12 Pool (org.candlepin.model.Pool)12 ArrayList (java.util.ArrayList)11 Path (javax.ws.rs.Path)11 NotFoundException (org.candlepin.common.exceptions.NotFoundException)9 Transactional (com.google.inject.persist.Transactional)8 ApiResponses (io.swagger.annotations.ApiResponses)8 Content (org.candlepin.model.Content)7 HashMap (java.util.HashMap)6 Consumes (javax.ws.rs.Consumes)6 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)6 ActivationKey (org.candlepin.model.activationkeys.ActivationKey)6 DELETE (javax.ws.rs.DELETE)5