예제 #1
0
def test_add_subscriber_full_bond(table, bond_with_subs):
    """
    Add a new subscriber to a bond that already has subscribers.
    """
    table.put_item(
        Item={
            'bond_id': bond_with_subs.bond_id,
            'host_account_id': bond_with_subs.host_account_id,
            'sub_account_id': bond_with_subs.sub_account_id,
            'host_cost_center': bond_with_subs.host_cost_center,
            'sub_cost_center': bond_with_subs.sub_cost_center,
            'subscribers': jsonable_encoder(bond_with_subs.subscribers)
        })

    crud.add_subscriber(
        bond_with_subs.bond_id,
        Subscriber(sid='jbojo', name='Joe Bojo', email='*****@*****.**'))

    # make sure the subscriber was added.
    response = table.query(
        ProjectionExpression="bond_id, host_account_id, sub_account_id, "
        "host_cost_center, sub_cost_center, subscribers",
        KeyConditionExpression=Key('bond_id').eq(bond_with_subs.bond_id))
    assert response.get("Count") == 1
    items = response.get("Items")
    subs = items[0]["subscribers"]
    assert len(subs) == 4
    assert subs['jbojo']['sid'] == 'jbojo'
    assert subs['jbojo']['name'] == 'Joe Bojo'
    assert subs['jbojo']['email'] == '*****@*****.**'
예제 #2
0
def test_add_subscriber_exists(table, bond_with_subs):
    """
    Try to add a subscriber that is already listed in the bond. It will
    overwrite what was there, i.e. an update.
    """
    table.put_item(
        Item={
            'bond_id': bond_with_subs.bond_id,
            'host_account_id': bond_with_subs.host_account_id,
            'sub_account_id': bond_with_subs.sub_account_id,
            'host_cost_center': bond_with_subs.host_cost_center,
            'sub_cost_center': bond_with_subs.sub_cost_center,
            'subscribers': jsonable_encoder(bond_with_subs.subscribers)
        })

    crud.add_subscriber(
        bond_with_subs.bond_id,
        Subscriber(sid='tfomoo100', name='Foo', email='*****@*****.**'))

    # make sure the subscriber was added.
    response = table.query(
        ProjectionExpression="bond_id, host_account_id, sub_account_id, "
        "host_cost_center, sub_cost_center, subscribers",
        KeyConditionExpression=Key('bond_id').eq(bond_with_subs.bond_id))
    assert response.get("Count") == 1
    items = response.get("Items")
    subs = items[0]["subscribers"]
    assert len(subs) == 3
    assert subs['tfomoo100']['sid'] == 'tfomoo100'
    assert subs['tfomoo100']['name'] == 'Foo'
    assert subs['tfomoo100']['email'] == '*****@*****.**'
예제 #3
0
def test_add_subscriber_no_bond(populated_table):
    """
    Try to add a subscriber to a non-existent bond.
    It will throw a ConditionalCheckError as that bond does not exist.
    """
    with pytest.raises(ConditionalCheckError):
        crud.add_subscriber(
            "garbage",
            Subscriber(sid='jbojo', name='Joe Bojo', email='*****@*****.**'))
예제 #4
0
def add_subscriber(bond_id: str, sub: Subscriber) -> Bond:
    """
    Add a subscriber to a bond. Overwrite subscriber details if the
    subscriber is already in the bond.

    Args:
        bond_id (str): The bond id of the bond to add the subscriber to.
        sub (Subscriber): The subscriber to add (or update).

    Raises:
         HTTPException(400) if the bond does not exist.

    Returns:
        The bond updated with the new subscriber."""
    logger.info(f"Add subscriber: bond_id={bond_id}, subscriber={sub}")
    try:
        bond = crud.add_subscriber(bond_id, sub)
    except ConditionalCheckError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except RegistryClientError as err:
        raise HTTPException(status_code=500, detail=str(err))
    return bond