PoolResource { @ApiOperation( notes = "Retrieves a list of Pools @deprecated Use the method on /owners", value = "") @ApiResponses({ @ApiResponse(code = 400, message = "if both consumer(unit) and owner are given, or if a" + " product id is specified without a consumer(unit) or owner"), @ApiResponse(code = 404, message = "if a specified consumer(unit) or owner is not found"), @ApiResponse(code = 403, message = "") }) @GET @Produces(MediaType.APPLICATION_JSON) @Wrapped(element = "pools") @Deprecated @SecurityHole public List<PoolDTO> list(@QueryParam("owner") String ownerId, @QueryParam("consumer") String consumerUuid, @QueryParam("product") String productId, @ApiParam("Use with consumerUuid to list all pools available to the consumer. " + "This will include pools which would otherwise be omitted due to a rules" + " warning. (i.e. not recommended) Pools that trigger an error however will" + " still be omitted. (no entitlements available, consumer type mismatch, etc)") @QueryParam("listall") @DefaultValue("false") boolean listAll, @ApiParam("Uses ISO 8601 format") @QueryParam("activeon") String activeOn, @Context Principal principal, @Context PageRequest pageRequest) { if (consumerUuid != null && ownerId != null) { throw new BadRequestException(i18n.tr("Cannot filter on both owner and unit")); } if (consumerUuid == null && ownerId == null && productId != null) { throw new BadRequestException(i18n.tr("A unit or owner is needed to filter on product")); } Date activeOnDate = activeOn != null ? ResourceDateParser.parseDateString(activeOn) : new Date(); Consumer c = null; String oId = null; if (consumerUuid != null) { c = consumerCurator.findByUuid(consumerUuid); if (c == null) { throw new NotFoundException(i18n.tr("Unit: {0} not found", consumerUuid)); } if (!principal.canAccess(c, SubResource.NONE, Access.READ_ONLY)) { throw new ForbiddenException(i18n.tr("User {0} cannot access unit {1}", principal.getPrincipalName(), consumerUuid)); } if (listAll) { oId = c.getOwnerId(); } } if (ownerId != null) { Owner o = ownerCurator.secureGet(ownerId); if (o == null) { throw new NotFoundException(i18n.tr("owner: {0}", ownerId)); } oId = o.getId(); if (!principal.canAccess(o, SubResource.POOLS, Access.READ_ONLY)) { throw new ForbiddenException(i18n.tr("User {0} cannot access owner {1}", principal.getPrincipalName(), o.getKey())); } } if (consumerUuid == null && ownerId == null && !principal.hasFullAccess()) { throw new ForbiddenException(i18n.tr("User {0} cannot access all pools.", principal.getPrincipalName())); } Page<List<Pool>> page = poolManager.listAvailableEntitlementPools(c, null, oId, productId, null, activeOnDate, listAll, new PoolFilterBuilder(), pageRequest, false, false, null); List<Pool> poolList = page.getPageData(); calculatedAttributesUtil.setCalculatedAttributes(poolList, activeOnDate); calculatedAttributesUtil.setQuantityAttributes(poolList, c, activeOnDate); ResteasyContext.pushContext(Page.class, page); List<PoolDTO> poolDTOs = new ArrayList<>(); for (Pool pool : poolList) { poolDTOs.add(translator.translate(pool, PoolDTO.class)); } return poolDTOs; } @Inject  PoolResource(ConsumerCurator consumerCurator, OwnerCurator ownerCurator,
        I18n i18n, PoolManager poolManager, CalculatedAttributesUtil calculatedAttributesUtil,
        ModelTranslator translator); @ApiOperation( notes = "Retrieves a list of Pools @deprecated Use the method on /owners", value = "") @ApiResponses({ @ApiResponse(code = 400, message = "if both consumer(unit) and owner are given, or if a" + " product id is specified without a consumer(unit) or owner"), @ApiResponse(code = 404, message = "if a specified consumer(unit) or owner is not found"), @ApiResponse(code = 403, message = "") }) @GET @Produces(MediaType.APPLICATION_JSON) @Wrapped(element = "pools") @Deprecated @SecurityHole List<PoolDTO> list(@QueryParam("owner") String ownerId,
        @QueryParam("consumer") String consumerUuid,
        @QueryParam("product") String productId,
        @ApiParam("Use with consumerUuid to list all pools available to the consumer. " +
        "This will include pools which would otherwise be omitted due to a rules" +
        " warning. (i.e. not recommended) Pools that trigger an error however will" +
        " still be omitted. (no entitlements available, consumer type mismatch, etc)")
        @QueryParam("listall") @DefaultValue("false") boolean listAll,
        @ApiParam("Uses ISO 8601 format") @QueryParam("activeon") String activeOn,
        @Context Principal principal,
        @Context PageRequest pageRequest); @ApiOperation(notes = "Retrieves a single Pool", value = "getPool") @ApiResponses({ @ApiResponse(code = 404, message = "if the pool with the specified id is not found"), @ApiResponse(code = 404, message = "") }) @GET @Path("/{pool_id}") @Produces(MediaType.APPLICATION_JSON) PoolDTO getPool(@PathParam("pool_id") @Verify(Pool.class) String id,
        @QueryParam("consumer") String consumerUuid,
        @ApiParam("Uses ISO 8601 format") @QueryParam("activeon") String activeOn,
        @Context Principal principal); @ApiOperation(notes = "Remove a Pool", value = "deletePool") @ApiResponses({ @ApiResponse(code = 404, message = "if the pool with the specified id is not found") }) @DELETE @Path("/{pool_id}") @Produces(MediaType.APPLICATION_JSON) void deletePool(@PathParam("pool_id") String id); @ApiOperation(notes = "Retrieve a CDN for a Pool", value = "getPoolCdn") @ApiResponses({ @ApiResponse(code = 400, message = "") }) @GET @Path("{pool_id}/cdn") @Produces(MediaType.APPLICATION_JSON) CdnDTO getPoolCdn(
        @PathParam("pool_id") @Verify(Pool.class) String id); @ApiOperation( notes = "Retrieve a list of Consumer UUIDs attached to a pool. Available only to superadmins", value = "listEntitledConsumerUuids") @ApiResponses({ @ApiResponse(code = 400, message = "") }) @GET @Path("{pool_id}/entitlements/consumer_uuids") @Produces(MediaType.APPLICATION_JSON) List<String> listEntitledConsumerUuids(@PathParam("pool_id") String id); @ApiOperation(notes = "Retrieve a list of Entitlements for a Pool", value = "getPoolEntitlements") @ApiResponses({ @ApiResponse(code = 400, message = "") }) @GET @Path("{pool_id}/entitlements") @Produces(MediaType.APPLICATION_JSON) List<EntitlementDTO> getPoolEntitlements(@PathParam("pool_id")
        @Verify(value = Pool.class, subResource = SubResource.ENTITLEMENTS) String id,
        @Context Principal principal); @ApiOperation(notes = "Retrieves a Subscription Certificate As a PEM", value = "getSubCertAsPem") @DoNotUseJAXBProvider @GET @Path("{pool_id}/cert") // cpc passes up content-type on all calls, make sure we don't 415 it @Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON}) @Produces({ MediaType.TEXT_PLAIN }) String getSubCertAsPem(
        @PathParam("pool_id") String poolId); @ApiOperation(notes = "Retrieves a Subscription Certificate", value = "getSubCert") @GET @Path("{pool_id}/cert") @Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON}) @Produces({ MediaType.APPLICATION_JSON}) CertificateDTO getSubCert(
        @PathParam("pool_id") String poolId);  }
@Test public void testUserCannotListAllPools() { assertThrows(ForbiddenException.class, () -> poolResource.list(null, null, null, false, null, adminPrincipal, null) ); }
@Test public void testListAll() { List<PoolDTO> pools = poolResource.list(null, null, null, false, null, setupAdminPrincipal("superadmin"), null); assertEquals(3, pools.size()); }
@Test public void testListForOrg() { List<PoolDTO> pools = poolResource.list(owner1.getId(), null, null, false, null, adminPrincipal, null); assertEquals(2, pools.size()); Principal p = setupPrincipal(owner2, Access.ALL); pools = poolResource.list(owner2.getId(), null, null, false, null, p, null); assertEquals(1, pools.size()); }
@Disabled @Test public void testListForProduct() { List<PoolDTO> pools = poolResource.list(null, null, product1.getId(), false, null, adminPrincipal, null); assertEquals(2, pools.size()); pools = poolResource.list(null, null, product2.getId(), false, null, adminPrincipal, null); assertEquals(1, pools.size()); }
@Test public void testListForOrgAndProduct() { List<PoolDTO> pools = poolResource.list(owner1.getId(), null, product1.getId(), false, null, adminPrincipal, null); assertEquals(1, pools.size()); }
@Test public void testCannotListPoolsInAnotherOwner() { assertThrows(NotFoundException.class, () -> poolResource.list(owner2.getId(), null, product2.getId(), false, null, adminPrincipal, null) ); }
@Test public void testListConsumerAndProductFiltering() { List<PoolDTO> pools = poolResource.list(null, passConsumer.getUuid(), product1.getId(), false, null, adminPrincipal, null); assertEquals(1, pools.size()); verify(attrUtil, times(1)) .setCalculatedAttributes(argThat(x -> x.size() == 1), any(Date.class)); }
@Test public void testCannotListPoolsForConsumerInAnotherOwner() { assertThrows(NotFoundException.class, () -> poolResource.list(null, failConsumer.getUuid(), product1.getId(), false, null, adminPrincipal, null) ); }
@Test public void testListBlocksConsumerOwnerFiltering() { assertThrows(BadRequestException.class, () -> poolResource.list(owner1.getId(), passConsumer.getUuid(), product1.getId(), false, null, adminPrincipal, null) ); }
@Test public void testListConsumerFiltering() { setupPrincipal(new ConsumerPrincipal(passConsumer, owner1)); List<PoolDTO> pools = poolResource.list( null, passConsumer.getUuid(), null, false, null, adminPrincipal, null ); assertEquals(2, pools.size()); verify(attrUtil, times(1)) .setCalculatedAttributes(argThat(x -> x.size() == 2), any(Date.class)); }
@Test public void testListNoSuchOwner() { assertThrows(NotFoundException.class, () -> poolResource.list("-1", null, null, false, null, adminPrincipal, null) ); }
@Test public void testListNoSuchConsumer() { assertThrows(NotFoundException.class, () -> poolResource.list(null, "blah", null, false, null, adminPrincipal, null) ); }
@Test public void testListNoSuchProduct() { assertEquals(0, poolResource.list(owner1.getId(), null, "boogity", false, null, adminPrincipal, null).size()); }
@Test public void ownerAdminCannotListAnotherOwnersPools() { List<PoolDTO> pools = poolResource.list(owner1.getId(), null, null, false, null, adminPrincipal, null); assertEquals(2, pools.size()); Principal anotherPrincipal = setupPrincipal(owner2, Access.ALL); securityInterceptor.enable(); assertThrows(NotFoundException.class, () -> poolResource.list(owner1.getId(), null, null, false, null, anotherPrincipal, null) ); }
@Test public void testConsumerCannotListPoolsForAnotherOwnersConsumer() { Principal p = setupPrincipal(new ConsumerPrincipal(foreignConsumer, owner2)); securityInterceptor.enable(); assertThrows(NotFoundException.class, () -> poolResource.list(null, passConsumer.getUuid(), null, false, null, p, null) ); }
@Test public void consumerCannotListPoolsForAnotherOwner() { Principal p = setupPrincipal(new ConsumerPrincipal(foreignConsumer, owner2)); securityInterceptor.enable(); assertThrows(NotFoundException.class, () -> poolResource.list(owner1.getId(), null, null, false, null, p, null) ); }
@Test public void consumerCanListOwnersPools() { Principal p = setupPrincipal(new ConsumerPrincipal(passConsumer, owner1)); securityInterceptor.enable(); poolResource.list(owner1.getId(), null, null, false, null, p, null); }
@Test public void testBadActiveOnDate() { assertThrows(BadRequestException.class, () -> poolResource.list(owner1.getId(), null, null, false, "bc", adminPrincipal, null) ); }
@Test public void testActiveOnDate() { String activeOn = Integer.toString(START_YEAR + 1); List<PoolDTO> pools = poolResource.list(null, null, null, false, activeOn, setupAdminPrincipal("superadmin"), null); assertEquals(3, pools.size()); activeOn = Integer.toString(START_YEAR - 1); pools = poolResource.list(owner1.getId(), null, null, false, activeOn, adminPrincipal, null); assertEquals(0, pools.size()); }