Exemple #1
0
 def test_cleanup_expiry_date(self):
     config = SiteConfiguration.get_solo()
     config.sms_expiration_date = None
     config.save()
     logs.check_incoming_log()
     logs.check_outgoing_log()
     num_sms = models.SmsInbound.objects.count() + models.SmsOutbound.objects.count()
     logs.cleanup_expired_sms()
     assert num_sms > 0
     assert models.SmsInbound.objects.count() + models.SmsOutbound.objects.count() == num_sms
     config = SiteConfiguration.get_solo()
     config.sms_expiration_date = today + timedelta(days=1)
     config.save()
     logs.cleanup_expired_sms()
     assert models.SmsInbound.objects.count() + models.SmsOutbound.objects.count() == 0
Exemple #2
0
 def test_cleanup_expired_sms(self, smsin):
     config = SiteConfiguration.get_solo()
     config.sms_expiration_date = None
     config.save()
     for sms in SmsInbound.objects.all():
         # move back in time so they can be deleted
         sms.time_received = sms.time_received - timedelta(days=5)
         sms.save()
     cleanup_expired_sms()
     assert SmsInbound.objects.count() == len(smsin)
     config = SiteConfiguration.get_solo()
     config.sms_expiration_date = timezone.localdate()
     config.save()
     cleanup_expired_sms()
     assert SmsInbound.objects.count() == 0
Exemple #3
0
 def test_cleanup(self):
     # setup
     config = SiteConfiguration.get_solo()
     config.sms_rolling_expiration_days = None
     config.sms_expiration_date = today - timedelta(days=100)
     config.save()
     sms = models.SmsInbound.objects.create(
         content='test message',
         time_received=timezone.now() - timedelta(50),
         sender_name="John Calvin",
         sender_num="+447927401749",
         matched_keyword="test",
         sid='12345'
     )
     sms.save()
     assert models.SmsInbound.objects.count() == 1  # we have one sms
     logs.cleanup_expired_sms()
     assert models.SmsInbound.objects.count() == 1  # cleanup should leave it untouched
     config.sms_rolling_expiration_days = 50
     config.save()
     logs.cleanup_expired_sms()
     assert models.SmsInbound.objects.count() == 1  # cleanup should leave it untouched
     config.sms_rolling_expiration_days = 49
     config.save()
     logs.cleanup_expired_sms()
     assert models.SmsInbound.objects.count() == 0  # cleanup should remove sms
Exemple #4
0
def fetch_elvanto_groups(force=False):
    """Fetch all Elvanto groups."""
    from site_config.models import SiteConfiguration
    config = SiteConfiguration.get_solo()
    if force or config.sync_elvanto:
        from elvanto.models import ElvantoGroup
        ElvantoGroup.fetch_all_groups()
Exemple #5
0
 def test_import_outgoing_rolling(self):
     config = SiteConfiguration.get_solo()
     config.sms_expiration_date = None
     config.sms_rolling_expiration_days = 0
     config.save()
     logs.check_outgoing_log()
     assert models.SmsOutbound.objects.filter(time_sent__lt=today).count() == 0
Exemple #6
0
def get_person_or_ask_for_name(from_, sms_body, keyword_obj):
    """
    Return the Recipient object for the sender of the message.

    Perform a look up on the sender of the message.
    If they exist in the system, they are returned.
    Otherwise a message is queued to ask them for their name.
    """
    try:
        person_from = Recipient.objects.get(number=from_)
    except Recipient.DoesNotExist:
        person_from = Recipient.objects.create(
            number=from_,
            first_name='Unknown',
            last_name='Person'
        )
        person_from.save()
        if keyword_obj == "name":
            pass
        else:
            from site_config.models import SiteConfiguration
            config = SiteConfiguration.get_solo()
            if not config.disable_all_replies:
                person_from.send_message(
                    content=fetch_default_reply('auto_name_request'),
                    sent_by="auto name request"
                )
                notify_office_mail.delay(
                    '[Apostello] Unknown Contact!',
                    'SMS: {0}\nFrom: {1}\n\n\nThis person is unknown and has been asked for their name.'.format(
                        sms_body, from_
                    ),
                )

    return person_from
Exemple #7
0
def pull_elvanto_groups(force=False):
    """Pull all the Elvanto groups that are set to sync."""
    from site_config.models import SiteConfiguration
    config = SiteConfiguration.get_solo()
    if force or config.sync_elvanto:
        from elvanto.models import ElvantoGroup
        ElvantoGroup.pull_all_groups()
def global_settings(request):
    """Expose TWILIO_FROM_NUM, DEBUG and site config in templates."""
    return {
        'CONFIG': SiteConfiguration.get_solo(),
        'DISPLAY_GOOGLE_LOGIN': SocialApp.objects.filter(provider='google').count(),
        'ROLLBAR_ACCESS_TOKEN_CLIENT': settings.ROLLBAR_ACCESS_TOKEN_CLIENT,
    }
Exemple #9
0
def recipient_send_message_task(recipient_pk, body, group, sent_by):
    """Send a message asynchronously."""
    from apostello.models import Recipient
    from site_config.models import SiteConfiguration
    recipient = Recipient.objects.get(pk=recipient_pk)
    if recipient.is_archived:
        # if recipient is not active, fail silently
        return

    from apostello.models import SmsOutbound, RecipientGroup
    # if %name% is present, replace:
    body = recipient.personalise(body)
    # send twilio message
    try:
        message = get_twilio_client().messages.create(
            body=body, to=str(recipient.number), from_=str(SiteConfiguration.get_solo().twilio_from_num)
        )
        # add to sms out table
        sms = SmsOutbound(sid=message.sid, content=body, time_sent=timezone.now(), recipient=recipient, sent_by=sent_by)
        if group is not None:
            sms.recipient_group = RecipientGroup.objects.filter(name=group)[0]
        sms.save()
    except TwilioRestException as e:
        if e.code == 21610:
            recipient.is_blocking = True
            recipient.save()
            async('apostello.tasks.blacklist_notify', recipient.pk, '', 'stop')
        else:
            raise e
Exemple #10
0
def send_async_mail(subject, body, to):
    """Send email."""
    # read email settings from DB
    from site_config.models import SiteConfiguration
    s = SiteConfiguration.get_solo()
    from_ = s.email_from
    send_mail(subject, body, from_, to)
Exemple #11
0
def elm_settings(user):
    try:
        profile = user.profile
    except AttributeError:
        profile = UserProfile.nullProfile()

    config = SiteConfiguration.get_solo()
    try:
        twilio_settings = config.get_twilio_settings()
        # remove sensitive settings:
        del twilio_settings['auth_token']
        del twilio_settings['sid']
    except ConfigurationError:
        twilio_settings = None

    bk_key = f'blocked_keywords_user_{user.pk}'
    blocked_keywords = cache.get(bk_key)
    if blocked_keywords is None:
        blocked_keywords = [
            x.keyword for x in Keyword.objects.all().prefetch_related('owners') if not x.can_user_access(user)
        ]
        cache.set(bk_key, blocked_keywords, 120)

    elm = {
        'userPerms': UserProfileSerializer(profile).data,
        'twilio': twilio_settings,
        'isEmailSetup': config.is_email_setup(),
        'smsCharLimit': config.sms_char_limit,
        'defaultNumberPrefix': config.default_number_prefix,
        'noAccessMessage': config.not_approved_msg,
        'blockedKeywords': blocked_keywords,
    }
    return mark_safe(json.dumps(elm))
def global_settings(request):
    """Expose TWILIO_FROM_NUM, DEBUG and site config in templates."""
    return {
        'TWILIO_FROM_NUM': settings.TWILIO_FROM_NUM,
        'TWILIO_SENDING_COST': settings.TWILIO_SENDING_COST,
        'DEBUG': settings.DEBUG,
        'CONFIG': SiteConfiguration.get_solo(),
    }
Exemple #13
0
def send_async_mail(subject, body, to):
    """Send email."""
    # read email settings from DB, if they are empty, they will be read from
    # settings.py instead
    from site_config.models import SiteConfiguration
    s = SiteConfiguration.get_solo()
    from_ = s.email_from or settings.EMAIL_FROM
    send_mail(subject, body, from_, to)
Exemple #14
0
 def __init__(self, *args, **kwargs):
     super(ApostelloEmailBackend, self).__init__(*args, **kwargs)
     from site_config.models import SiteConfiguration
     s = SiteConfiguration.get_solo()
     self.host = s.email_host
     self.port = s.email_port
     self.username = s.email_username
     self.password = s.email_password
 def test_no_name_raises(self):
     """Test shorter limit imposed with %name% present."""
     s = SiteConfiguration.get_solo()
     with pytest.raises(ValidationError):
         less_than_sms_char_limit(
             't %name%' *
             (s.sms_char_limit - settings.MAX_NAME_LENGTH + len('%name%'))
         )
Exemple #16
0
def post_to_slack(attachments):
    """Post message to slack webhook."""
    from site_config.models import SiteConfiguration
    config = SiteConfiguration.get_solo()
    url = config.slack_url
    if url:
        data = {'username': '******', 'icon_emoji': ':speech_balloon:', 'attachments': attachments}
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
        requests.post(url, data=json.dumps(data), headers=headers)
def global_settings(request):
    """Expose TWILIO_FROM_NUM, DEBUG and site config in templates."""
    return {
        'TWILIO_FROM_NUM': settings.TWILIO_FROM_NUM,
        'TWILIO_SENDING_COST': settings.TWILIO_SENDING_COST,
        'DEBUG': settings.DEBUG,
        'CONFIG': SiteConfiguration.get_solo(),
        'DISPLAY_GOOGLE_LOGIN':
        SocialApp.objects.filter(provider='google').count(),
    }
def test_get_email():
    """Test pulling email from settings and SiteConfiguration."""
    s = SiteConfiguration.get_solo()
    a = ApostelloAccountAdapter()
    # test env var is migrated into db:
    assert s.email_from == '*****@*****.**'
    assert a.get_from_email() == '*****@*****.**'
    # test change in db
    s.email_from = '*****@*****.**'
    s.save()
    assert a.get_from_email() == '*****@*****.**'
Exemple #19
0
def less_than_sms_char_limit(value):
    """Ensure message is less than the maximum character limit."""
    from site_config.models import SiteConfiguration
    s = SiteConfiguration.get_solo()
    sms_char_lim = s.sms_char_limit - settings.MAX_NAME_LENGTH + len('%name%')
    if len(value) > sms_char_lim:
        raise ValidationError(
            'You have exceeded the maximum char limit of {0}.'.format(
                sms_char_lim
            )
        )
Exemple #20
0
def setup_twilio():
    config = SiteConfiguration.get_solo()
    config.twilio_account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
    config.twilio_auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
    config.twilio_from_num = os.environ.get('TWILIO_FROM_NUM')
    try:
        cost = float(os.environ.get('TWILIO_SENDING_COST'))
    except TypeError:
        cost = None
    config.twilio_sending_cost = cost
    config.save()
def test_get_email():
    """Test pulling email from settings and SiteConfiguration."""
    s = SiteConfiguration.get_solo()
    a = ApostelloAccountAdapter()
    # test env var is migrated into db:
    assert s.email_from == "*****@*****.**"
    assert a.get_from_email() == "*****@*****.**"
    # test change in db
    s.email_from = "*****@*****.**"
    s.save()
    assert a.get_from_email() == "*****@*****.**"
Exemple #22
0
def add_new_contact_to_groups(contact_pk):
    """Add contact to any groups that are in "auto populate with new contacts."""
    logger.info('Adding new person to designated groups')
    from apostello.models import Recipient
    from site_config.models import SiteConfiguration
    contact = Recipient.objects.get(pk=contact_pk)
    for grp in SiteConfiguration.get_solo().auto_add_new_groups.all():
        logger.info('Adding %s to %s', contact.full_name, grp.name)
        contact.groups.add(grp)
        contact.save()
        logger.info('Added %s to %s', contact.full_name, grp.name)
Exemple #23
0
    def test_handle_outgoing_sms(self):
        config = SiteConfiguration.get_solo()
        config.sms_expiration_date = None
        config.save()

        msg = MockMsg("447932537999")
        cnp = logs.handle_outgoing_sms(msg)
        assert models.SmsOutbound.objects.all()[0].content == msg.body
        assert models.SmsOutbound.objects.count() == 1

        logs.handle_outgoing_sms(msg)
        assert models.SmsOutbound.objects.count() == 1
Exemple #24
0
def add_new_contact_to_groups(contact_pk):
    """Add contact to any groups that are in "auto populate with new contacts."""
    logger.info("Adding new person to designated groups")
    from apostello.models import Recipient
    from site_config.models import SiteConfiguration

    contact = Recipient.objects.get(pk=contact_pk)
    for grp in SiteConfiguration.get_solo().auto_add_new_groups.all():
        logger.info("Adding %s to %s", contact.full_name, grp.name)
        contact.groups.add(grp)
        contact.save()
        logger.info("Added %s to %s", contact.full_name, grp.name)
Exemple #25
0
def cleanup_expired_sms():
    """Remove expired messages."""
    config = SiteConfiguration.get_solo()
    if config.sms_expiration_date is None:
        # no expiration, skip checks
        return
    for sms in SmsInbound.objects.filter(
            time_received__date__lt=config.sms_expiration_date):
        sms.delete()
    for sms in SmsOutbound.objects.filter(
            time_sent__date__lt=config.sms_expiration_date):
        sms.delete()
Exemple #26
0
def less_than_sms_char_limit(value):
    """Ensure message is less than the maximum character limit."""
    from site_config.models import SiteConfiguration
    s = SiteConfiguration.get_solo()
    sms_char_lim = s.sms_char_limit

    if '%name%' in value:
        # if `%name%` in value, then adjust limit to handle substitutions
        sms_char_lim = sms_char_lim - settings.MAX_NAME_LENGTH + len('%name%')

    if len(value) > sms_char_lim:
        raise ValidationError('You have exceeded the maximum char limit of {0}.'.format(sms_char_lim))
def test_apostello_mail_backend():
    """Test email backend pulling from settings and db."""
    # test migration pulled env var
    mail_backend = ApostelloEmailBackend()
    assert mail_backend.host == 'smtp.test.apostello'
    # test Siteconfiguration change
    s = SiteConfiguration.get_solo()
    s.get_solo()
    s.email_host = 'smtp.test2.apostello'
    s.save()
    mail_backend = ApostelloEmailBackend()
    assert mail_backend.host == 'smtp.test2.apostello'
 def test_not_logged_in(self, keywords, recipients):
     # make sure replies are on:
     from site_config.models import SiteConfiguration
     config = SiteConfiguration.get_solo()
     config.disable_all_replies = False
     config.save()
     factory = TwilioRequestFactory(token=settings.TWILIO_AUTH_TOKEN)
     data = test_request_data_blocked()
     data['Body'] = u'Test'
     request = factory.post(uri, data=data)
     resp = sms(request)
     assert '<Message><Body /></Message>' in str(resp.content)
Exemple #29
0
    def test_handle_outgoing_sms(self):
        config = SiteConfiguration.get_solo()
        config.sms_expiration_date = None
        config.save()

        msg = MockMsg('447932537999')
        cnp = logs.handle_outgoing_sms(msg)
        assert models.SmsOutbound.objects.all()[0].content == msg.body
        assert models.SmsOutbound.objects.count() == 1

        logs.handle_outgoing_sms(msg)
        assert models.SmsOutbound.objects.count() == 1
Exemple #30
0
 def test_not_logged_in(self, keywords, recipients):
     # make sure replies are on:
     from site_config.models import SiteConfiguration
     config = SiteConfiguration.get_solo()
     config.disable_all_replies = False
     config.save()
     factory = TwilioRequestFactory(token=settings.TWILIO_AUTH_TOKEN)
     data = test_request_data_blocked()
     data['Body'] = u'Test'
     request = factory.post(uri, data=data)
     resp = sms(request)
     assert '<Response />' in str(resp.content)
Exemple #31
0
def test_twilio_view(msg, reply, keywords, recipients):
    # make sure replies are on:
    from site_config.models import SiteConfiguration
    config = SiteConfiguration.get_solo()
    config.disable_all_replies = False
    config.save()
    factory = TwilioRequestFactory(token=settings.TWILIO_AUTH_TOKEN)
    data = test_request_data()
    data['Body'] = msg
    request = factory.post(uri, data=data)
    resp = sms(request)
    assert reply in str(resp.content)
Exemple #32
0
def test_get_email():
    """Test pulling email from settings and SiteConfiguration."""
    s = SiteConfiguration.get_solo()
    a = ApostelloAccountAdapter()
    # test from db
    s.email_from = '*****@*****.**'
    s.save()
    assert a.get_from_email() == '*****@*****.**'
    # test from settings
    s.email_from = ''
    s.save()
    assert a.get_from_email() == '*****@*****.**'
Exemple #33
0
    def test_handle_incoming_sms(self):
        config = SiteConfiguration.get_solo()
        config.sms_expiration_date = None
        config.save()

        msg = MockMsg('447922537999')
        logs.handle_incoming_sms(msg)
        assert models.SmsInbound.objects.all()[0].content == msg.body
        assert models.SmsInbound.objects.count() == 1

        cnp = logs.handle_incoming_sms(msg)
        assert models.SmsInbound.objects.count() == 1
def test_twilio_view_blocking_contact(keywords, recipients):
    # make sure replies are on:
    from site_config.models import SiteConfiguration

    config = SiteConfiguration.get_solo()
    config.disable_all_replies = False
    config.save()
    factory = TwilioRequestFactory(token=get_token())
    data = test_request_data_blocked()
    data["Body"] = "Test"
    request = factory.post(uri, data=data)
    resp = sms(request)
    assert "<Response />" in str(resp.content)
 def test_not_logged_in(self, msg, reply, keywords):
     factory = TwilioRequestFactory(token=settings.TWILIO_AUTH_TOKEN)
     data = test_request_data()
     data['Body'] = msg
     request = factory.post(uri, data=data)
     # turn off responses
     from site_config.models import SiteConfiguration
     config = SiteConfiguration.get_solo()
     config.disable_all_replies = True
     config.save()
     # run test
     resp = sms(request)
     assert reply not in str(resp.content)
Exemple #36
0
def post_to_slack(attachments):
    """Post message to slack webhook."""
    from site_config.models import SiteConfiguration
    config = SiteConfiguration.get_solo()
    url = config.slack_url
    if url:
        data = {
            'username': '******',
            'icon_emoji': ':speech_balloon:',
            'attachments': attachments
        }
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
        requests.post(url, data=json.dumps(data), headers=headers)
Exemple #37
0
 def test_default_prefix(self, live_server, browser_in, users,
                         driver_wait_time):
     """Test prefix shows on form."""
     from site_config.models import SiteConfiguration
     config = SiteConfiguration.get_solo()
     config.default_number_prefix = '+45'
     config.save()
     b = load_page(browser_in, driver_wait_time, live_server + URI)
     assert 'value="+45"' in b.page_source
     config.default_number_prefix = ''
     config.save()
     b = load_page(browser_in, driver_wait_time, live_server + URI)
     assert 'value="+45"' not in b.page_source
Exemple #38
0
def less_than_sms_char_limit(value):
    """Ensure message is less than the maximum character limit."""
    from site_config.models import SiteConfiguration

    s = SiteConfiguration.get_solo()
    sms_char_lim = s.sms_char_limit

    if "%name%" in value:
        # if `%name%` in value, then adjust limit to handle substitutions
        sms_char_lim = sms_char_lim - settings.MAX_NAME_LENGTH + len("%name%")

    if len(value) > sms_char_lim:
        raise ValidationError("You have exceeded the maximum char limit of {0}.".format(sms_char_lim))
Exemple #39
0
def email_admin_on_signup(request, user, **kwargs):
    """Email office on new user sign up."""
    body = (
        "New User Signed Up: {}\n\n"
        "Please go to the admin page to approve their account.\n"
        "If you do not approve their account (and they are not using a "
        "whitelisted domain), they will be unable to access apostello."
    )
    body = body.format(str(user))
    from site_config.models import SiteConfiguration
    to_ = SiteConfiguration.get_solo().office_email
    if to_:
        send_async_mail("[apostello] New User", body, [to_], )
 def test_not_logged_in(self, msg, reply, keywords):
     factory = TwilioRequestFactory(token=settings.TWILIO_AUTH_TOKEN)
     data = test_request_data()
     data['Body'] = msg
     request = factory.post(uri, data=data)
     # turn off responses
     from site_config.models import SiteConfiguration
     config = SiteConfiguration.get_solo()
     config.disable_all_replies = True
     config.save()
     # run test
     resp = sms(request)
     assert reply not in str(resp.content)
Exemple #41
0
 def test_add_new_ppl_to_groups(self, groups):
     config = SiteConfiguration.get_solo()
     grp = groups['empty_group']
     assert grp.recipient_set.count() == 0
     config.auto_add_new_groups.add(grp)
     config.save()
     new_c = Recipient.objects.create(
         first_name='test',
         last_name='new',
         number='+44715620857',
     )
     assert grp.recipient_set.count() == 1
     assert new_c in grp.recipient_set.all()
def test_twilio_view_no_replies(msg, reply, keywords):
    factory = TwilioRequestFactory(token=get_token())
    data = test_request_data()
    data["Body"] = msg
    request = factory.post(uri, data=data)
    # turn off responses
    from site_config.models import SiteConfiguration

    config = SiteConfiguration.get_solo()
    config.disable_all_replies = True
    config.save()
    # run test
    resp = sms(request)
    assert reply not in str(resp.content)
Exemple #43
0
def global_settings(request):
    """Expose TWILIO_FROM_NUM, DEBUG and site config in templates."""
    return {
        'TWILIO_FROM_NUM':
        settings.TWILIO_FROM_NUM,
        'TWILIO_SENDING_COST':
        settings.TWILIO_SENDING_COST,
        'DEBUG':
        settings.DEBUG,
        'CONFIG':
        SiteConfiguration.get_solo(),
        'DISPLAY_GOOGLE_LOGIN':
        SocialApp.objects.filter(provider='google').count(),
    }
Exemple #44
0
 def test_not_logged_in(self, keywords):
     # make sure replies are on:
     from site_config.models import SiteConfiguration
     config = SiteConfiguration.get_solo()
     config.disable_all_replies = False
     config.save()
     factory = TwilioRequestFactory(token=settings.TWILIO_AUTH_TOKEN)
     data = test_request_data()
     data['Body'] = u'Test'
     request = factory.post(uri, data=data)
     resp = sms(request)
     assert 'Unknown' in str(resp.content)
     assert len(mail.outbox) == 1
     assert 'asked for their name' in mail.outbox[0].body
Exemple #45
0
def test_apostello_mail_backend():
    """Test email backend pulling from settings and db."""
    # get from Siteconfiguration
    s = SiteConfiguration.get_solo()
    s.get_solo()
    s.email_host = 'smtp.test2.apostello'
    s.save()
    mail_backend = ApostelloEmailBackend()
    assert mail_backend.host == 'smtp.test2.apostello'
    # get from settings
    s.email_host = ''
    s.save()
    mail_backend = ApostelloEmailBackend()
    assert mail_backend.host == 'smtp.test.apostello'
Exemple #46
0
    def test_sms_too_long(self, live_server, browser_in, users, driver_wait_time, recipients):
        """Test form submission with a message that is too long."""
        from site_config.models import SiteConfiguration
        s = SiteConfiguration.get_solo()
        s.sms_char_limit = 2
        s.save()
        b = load_page(browser_in, driver_wait_time, live_server + ADHOC_URI)
        b = add_content_and_recipient(b, driver_wait_time)
        b = click_send(b, driver_wait_time)

        assert 'You have exceeded' in b.page_source
        assert ADHOC_URI in b.current_url
        s.sms_char_limit = 200
        s.save()
 def test_default_prefix(
     self, live_server, browser_in, users, driver_wait_time
 ):
     """Test prefix shows on form."""
     from site_config.models import SiteConfiguration
     config = SiteConfiguration.get_solo()
     config.default_number_prefix = '+45'
     config.save()
     b = load_page(browser_in, driver_wait_time, live_server + URI)
     assert '+45' in b.page_source
     config.default_number_prefix = ''
     config.save()
     b = load_page(browser_in, driver_wait_time, live_server + URI)
     assert '+45' not in b.page_source
Exemple #48
0
def email_admin_on_signup(request, user, **kwargs):
    """Email office on new user sign up."""
    body = ("New User Signed Up: {}\n\n"
            "Please go to the admin page to approve their account.\n"
            "If you do not approve their account (and they are not using a "
            "whitelisted domain), they will be unable to access apostello.")
    body = body.format(str(user))
    from site_config.models import SiteConfiguration
    to_ = SiteConfiguration.get_solo().office_email
    if to_:
        send_async_mail(
            "[apostello] New User",
            body,
            [to_],
        )
def test_twilio_view_ask_for_name(keywords):
    # make sure replies are on:
    from site_config.models import SiteConfiguration

    config = SiteConfiguration.get_solo()
    config.disable_all_replies = False
    config.office_email = "*****@*****.**"
    config.save()
    factory = TwilioRequestFactory(token=get_token())
    data = test_request_data()
    data["Body"] = "Test"
    request = factory.post(uri, data=data)
    resp = sms(request)
    assert "Unknown" not in str(resp.content)
    assert "Thanks new person!" in str(resp.content)
    assert len(mail.outbox) == 1
    assert "asked for their name" in mail.outbox[0].body
Exemple #50
0
def ask_for_name(person_from_pk, sms_body, ask_for_name):
    """Asks a contact to provide their name."""
    if not ask_for_name:
        return
    from site_config.models import SiteConfiguration
    config = SiteConfiguration.get_solo()
    if not config.disable_all_replies:
        from apostello.models import Recipient
        contact = Recipient.objects.get(pk=person_from_pk)
        contact.send_message(content=fetch_default_reply('auto_name_request'),
                             sent_by="auto name request")
        async (
            'apostello.tasks.notify_office_mail',
            '[Apostello] Unknown Contact!',
            'SMS: {0}\nFrom: {1}\n\n\nThis person is unknown and has been'
            ' asked for their name.'.format(sms_body, str(contact)),
        )
Exemple #51
0
def sms(request):
    """
    Handle all incoming messages from Twilio.

    This is the start of the message processing pipeline.
    """
    logger.info('Received new sms')
    r = twiml.Response()
    msg = InboundSms(request.POST)
    msg.start_bg_tasks()

    config = SiteConfiguration.get_solo()
    if msg.reply and not config.disable_all_replies:
        logger.info('Add reply (%s) to response', msg.reply)
        r.message(msg.reply)

    logger.info('Return response to Twilio')
    return r
Exemple #52
0
def sms(request):
    """
    Handle all incoming messages from Twilio.

    This is the start of the message processing pipeline.
    """
    logger.info("Received new sms")
    r = MessagingResponse()
    msg = InboundSms(request.POST)
    msg.start_bg_tasks()

    config = SiteConfiguration.get_solo()
    if msg.reply and not config.disable_all_replies:
        logger.info("Add reply (%s) to response", msg.reply)
        r.message(msg.reply)

    logger.info("Return response to Twilio")
    return HttpResponse(str(r), content_type="application/xml")
Exemple #53
0
def email_admin_on_signup(request, user, **kwargs):
    """Email office on new user sign up."""
    body = ("New User Signed Up: {}\n\n"
            "Please go to the admin page to approve their account.\n"
            "If you do not approve their account (and they are not using a "
            "whitelisted domain), they will be unable to access apostello.")
    body = body.format(str(user))
    from site_config.models import SiteConfiguration
    to_ = SiteConfiguration.get_solo().office_email
    try:
        if len(to_) > 0:
            send_mail(
                "[apostello] New User",
                body,
                '',
                [to_],
            )
    except Exception:
        logger.error('Error sending email to office on new user sign up',
                     exc_info=True)
Exemple #54
0
    def test_sign_up(self, live_server, browser, driver_wait_time, users):
        """
        Tests the sign up form and checks that the appropriate emails
        have been sent afterwards.
        """
        # add an office email to test correct email is sent on sign up
        config = SiteConfiguration.get_solo()
        config.office_email = '*****@*****.**'
        config.save()
        # signup
        uri = '/accounts/signup/'
        browser.get(live_server + uri)
        email_box = browser.find_elements_by_name('email')[0]
        email_box.send_keys('*****@*****.**')
        password_box1 = browser.find_elements_by_name('password1')[0]
        password_box1.send_keys('top_secret')
        password_box2 = browser.find_elements_by_name('password2')[0]
        password_box2.send_keys('top_secret')
        login_button = browser.find_element_by_id('signupButton')
        login_button.click()

        def _test():
            assert '/accounts/confirm-email/' in browser.current_url
            assert len(mail.outbox) == 2
            assert '[apostello] New User' in mail.outbox[0].subject

        assert_with_timeout(_test, 10 * driver_wait_time)
        # when we have no office email set
        assert 'Please Confirm Your E-mail Address' in mail.outbox[1].subject
        for x in mail.outbox[1].body.split():
            if x.startswith('http'):
                confirm_url = x
        browser.get(confirm_url)
        confirm_button = browser.find_element_by_id('confirmButton')
        confirm_button.click()
        user = User.objects.get(email='*****@*****.**')
        assert not user.is_staff
        assert not user.is_superuser
Exemple #55
0
def sms(request):
    """
    Handle all incoming messages from Twilio.

    This is the start of the message processing pipeline.
    """
    r = twiml.Response()
    params = request.POST
    from_ = params['From']
    sms_body = params['Body'].strip()
    keyword_obj = Keyword.match(sms_body)
    # get person object and optionally ask for their name
    person_from = get_person_or_ask_for_name(from_, sms_body, keyword_obj)
    log_msg_in.delay(params, timezone.now(), person_from.pk)
    sms_to_slack.delay(sms_body, person_from, keyword_obj)

    reply = reply_to_incoming(person_from, from_, sms_body, keyword_obj)

    config = SiteConfiguration.get_solo()
    if not config.disable_all_replies:
        r.message(reply)

    return r
 def test_first_user_sign_up(self, live_server, browser):
     """
     Tests the sign up form and checks that the appropriate emails
     have been sent afterwards.
     Then we confirm the email and verify the user has been made an admin.
     """
     # add an office email to test correct email is sent on sign up
     config = SiteConfiguration.get_solo()
     config.office_email = '*****@*****.**'
     config.save()
     # signup
     uri = '/accounts/signup'
     browser.get(live_server + uri)
     email_box = browser.find_elements_by_name('email')[0]
     email_box.send_keys('*****@*****.**')
     password_box1 = browser.find_elements_by_name('password1')[0]
     password_box1.send_keys('top_secret')
     password_box2 = browser.find_elements_by_name('password2')[0]
     password_box2.send_keys('top_secret')
     login_button = browser.find_elements_by_xpath(
         'html/body/div/div/form/button'
     )[0]
     login_button.click()
     # check we have been redirected
     assert '/accounts/confirm-email/' in browser.current_url
     assert len(mail.outbox) == 2
     assert '[apostello] New User' in mail.outbox[0].subject
     assert 'Please Confirm Your E-mail Address' in mail.outbox[1].subject
     for x in mail.outbox[1].body.split():
         if x.startswith('http'):
             confirm_url = x
     browser.get(confirm_url)
     confirm_button = browser.find_element_by_class_name('button')
     confirm_button.click()
     user = User.objects.get(email='*****@*****.**')
     assert user.is_staff
     assert user.is_superuser
Exemple #57
0
 def test_no_name_raises(self):
     """Test raises error with no %name% sub."""
     s = SiteConfiguration.get_solo()
     with pytest.raises(ValidationError):
         less_than_sms_char_limit('t' * (s.sms_char_limit + 1))