Example #1
0
    def test_forgot_password_email(email,
                                   text_template=None,
                                   html_template=None,
                                   subject=None,
                                   sender=None,
                                   reply_to=None,
                                   sender_name=None,
                                   reply_to_name=None):
        access_key_type = current_context().get('access_key_type')
        if not access_key_type or access_key_type != 'master':
            raise SkygearException('master key is required',
                                   skyerror.AccessKeyNotAccepted)

        url_prefix = settings.url_prefix
        if url_prefix.endswith('/'):
            url_prefix = url_prefix[:-1]

        dummy_user = namedtuple('User',
                                ['id', 'email'])('dummy-id',
                                                 '*****@*****.**')

        dummy_record_id = RecordID('user', 'dummy-id')
        dummy_record = Record(dummy_record_id, dummy_record_id.key, None)

        template_params = {
            'appname': settings.app_name,
            'code': 'dummy-reset-code',
            'url_prefix': url_prefix,
            'link': '{}/example-reset-password-link'.format(url_prefix),
            'user_record': dummy_record,
            'user': dummy_user,
            'email': dummy_user.email,
            'user_id': dummy_user.id
        }

        email_sender = (sender_name, sender) if sender \
            else (settings.sender_name, settings.sender)
        email_subject = subject if subject else settings.subject
        email_reply_to = (reply_to_name, reply_to) if reply_to \
            else (settings.reply_to_name, settings.reply_to)

        try:
            mail_sender.send(email_sender,
                             email,
                             email_subject,
                             reply_to=email_reply_to,
                             text_template_string=text_template,
                             html_template_string=html_template,
                             template_params=template_params)
        except Exception as ex:
            logger.exception('An error occurred sending test reset password'
                             ' email to user.')
            raise SkygearException(str(ex), skyerror.UnexpectedError)

        return {'status': 'OK'}
Example #2
0
def current_context_has_master_key():
    # FIXME: skygear-server does not pass access_key_type information
    # yet. This is a temporary workaround before skygear-server has support.
    return 'access_key_type' not in current_context() or \
        current_context().get('access_key_type', '') == 'master'
Example #3
0
    def test_verify_request_lambda(record_key,
                                   record_value,
                                   provider_settings={},
                                   templates={}):
        """
        Allow passing extra provider_settings from api for
        provider configuration testing. e.g. sms api key is provided by user

        Example:
        curl 'http://127.0.0.1:3000/' --data-binary '{
            "action": "user:verify_request:test",
            "api_key": "master_key",
            "args": {
                "record_key": "email",
                "record_value": "*****@*****.**",
                "provider_settings": {
                    "name": "smtp"
                },
                "templates": {
                    "text_template": "testing",
                    "html_template": "testing html"
                }
            }
        }'

        curl 'http://127.0.0.1:3000/' --data-binary '{
            "action": "user:verify_request:test",
            "api_key": "master_key",
            "args": {
                "record_key": "phone",
                "record_value": "+15005550009",
                "provider_settings": {
                    "name": "twilio",
                    "twilio_from": "+15005550009",
                    "twilio_account_sid": "",
                    "twilio_auth_token": ""
                },
                "templates": {
                    "template": "testing sms"
                }
            }
        }'
        """
        access_key_type = current_context().get('access_key_type')
        if not access_key_type or access_key_type != 'master':
            raise SkygearException('master key is required',
                                   skyerror.AccessKeyNotAccepted)

        provider_name = provider_settings.get('name')
        if not provider_name:
            raise SkygearException('Missing provider',
                                   skyerror.InvalidArgument)

        merged_settings = {
            **vars(test_provider_settings[provider_name]),
            **provider_settings
        }

        string_templates = {
            k: StringTemplate('verify_{}_{}'.format(record_key, k), v)
            for k, v in templates.items()
        }

        _providers = {}
        _providers[record_key] = get_provider(
            argparse.Namespace(**merged_settings), record_key,
            **string_templates)

        thelambda = VerifyRequestTestLambda(settings, _providers)
        return thelambda(record_key, record_value)
Example #4
0
def current_context_has_master_key():
    # FIXME: skygear-server does not pass access_key_type information
    # yet. This is a temporary workaround before skygear-server has support.
    return 'access_key_type' not in current_context() or \
        current_context().get('access_key_type', '') == 'master'
Example #5
0
 def test_pop_context(self):
     ctx.push_context({'hello': 'world'})
     ctx.pop_context()
     assert ctx.current_context().get('hello', None) is None
Example #6
0
 def test_push_context(self):
     ctx.push_context({'hello': 'world'})
     assert ctx.current_context()['hello'] == 'world'
Example #7
0
 def test_start_context(self):
     with ctx.start_context({'hello': 'world'}):
         assert ctx.current_context()['hello'] == 'world'
     assert ctx.current_context().get('hello', None) is None