def dao_add_sms_sender_for_service(service_id,
                                   sms_sender,
                                   is_default,
                                   inbound_number_id=None):
    default_sms_sender = _get_default_sms_sender_for_service(
        service_id=service_id)

    if not default_sms_sender and not is_default:
        raise SmsSenderDefaultValidationException(
            'You must have at least one SMS sender as the default.')

    if is_default:
        _set_default_sms_sender_to_not_default(default_sms_sender)

    if inbound_number_id:
        inbound_number = _allocate_inbound_number_for_service(
            service_id, inbound_number_id)

        if inbound_number.number != sms_sender:
            raise SmsSenderInboundNumberIntegrityException(
                f"You cannot create an SMS sender with the number '{sms_sender}' "
                f"and the Inbound Number '{inbound_number.id}' ('{inbound_number.number}')"
            )

    new_sms_sender = ServiceSmsSender(service_id=service_id,
                                      sms_sender=sms_sender,
                                      is_default=is_default,
                                      inbound_number_id=inbound_number_id)

    db.session.add(new_sms_sender)
    return new_sms_sender
def insert_service_sms_sender(service, sms_sender):
    """
    This method is called from create_service which is wrapped in a transaction.
    """
    new_sms_sender = ServiceSmsSender(sms_sender=sms_sender,
                                      service=service,
                                      is_default=True)
    db.session.add(new_sms_sender)
示例#3
0
    def test_returns_none_if_no_matching_number(self, db_session):
        service = create_service()
        sms_sender = ServiceSmsSender(sms_sender='+15551234567',
                                      service_id=service.id)
        db_session.add(sms_sender)
        db_session.commit()

        found_sms_sender = dao_get_sms_sender_by_service_id_and_number(
            service_id=service.id, number='+15557654321')

        assert found_sms_sender is None
示例#4
0
    def test_returns_none_if_no_matching_service_id(self, db_session):
        service_with_sms_sender = create_service(service_name="Service one")
        sms_sender = ServiceSmsSender(sms_sender='+15551234567',
                                      service_id=service_with_sms_sender.id)
        db_session.add(sms_sender)
        db_session.commit()

        service_without_sms_sender = create_service(service_name="Service two")
        found_sms_sender = dao_get_sms_sender_by_service_id_and_number(
            service_id=service_without_sms_sender.id, number='+15551234567')

        assert found_sms_sender is None
示例#5
0
def test_add_service_sms_sender_calls_dao_method(admin_request, mocker):
    added_service_sms_sender = ServiceSmsSender(created_at=datetime.utcnow())
    dao_add_sms_sender_for_service = mocker.patch(
        'app.service.sms_sender_rest.dao_add_sms_sender_for_service',
        return_value=added_service_sms_sender)
    service_id = uuid.uuid4()

    mocker.patch('app.service.sms_sender_rest.dao_fetch_service_by_id',
                 return_value=Service())

    response_json = admin_request.post(
        'service_sms_sender.add_service_sms_sender',
        service_id=service_id,
        _data={
            "sms_sender": 'second',
            "is_default": False,
        },
        _expected_status=201)

    dao_add_sms_sender_for_service.assert_called_with(service_id=service_id,
                                                      sms_sender='second',
                                                      is_default=False)

    assert response_json == added_service_sms_sender.serialize()
示例#6
0
文件: db.py 项目: cds-snc/CANotifier
def create_service_sms_sender(service,
                              sms_sender,
                              is_default=True,
                              inbound_number_id=None):
    data = {
        'service_id': service.id,
        'sms_sender': sms_sender,
        'is_default': is_default,
        'inbound_number_id': inbound_number_id
    }
    service_sms_sender = ServiceSmsSender(**data)

    db.session.add(service_sms_sender)
    db.session.commit()

    return service_sms_sender
示例#7
0
def dao_add_sms_sender_for_service(service_id, sms_sender, is_default, inbound_number_id=None):
    old_default = _get_existing_default(service_id=service_id)
    if is_default:
        _reset_old_default_to_false(old_default)
    else:
        _raise_when_no_default(old_default)

    new_sms_sender = ServiceSmsSender(
        service_id=service_id,
        sms_sender=sms_sender,
        is_default=is_default,
        inbound_number_id=inbound_number_id
    )

    db.session.add(new_sms_sender)
    return new_sms_sender
示例#8
0
def create_service_sms_sender(service,
                              sms_sender,
                              is_default=True,
                              inbound_number_id=None,
                              archived=False):
    data = {
        "service_id": service.id,
        "sms_sender": sms_sender,
        "is_default": is_default,
        "inbound_number_id": inbound_number_id,
        "archived": archived,
    }
    service_sms_sender = ServiceSmsSender(**data)

    db.session.add(service_sms_sender)
    db.session.commit()

    return service_sms_sender