Beispiel #1
0
    def test_get_github_emails(self):
        """Test the github utility get_github_emails method."""
        headers = dict({'Authorization': f'token {self.user_oauth_token}'},
                       **JSON_HEADER)
        data = [{
            'email': '*****@*****.**'
        }, {
            'email': '*****@*****.**'
        }, {
            'email': '*****@*****.**'
        }]
        url = 'https://api.github.com/user/emails'
        responses.add(responses.GET,
                      url,
                      json=data,
                      headers=headers,
                      status=200)
        responses.add(responses.GET,
                      url,
                      json=data,
                      headers=headers,
                      status=404)
        emails = get_github_emails(self.user_oauth_token)
        no_emails = get_github_emails(self.user_oauth_token)

        assert responses.calls[0].request.url == url
        assert emails == ['*****@*****.**', '*****@*****.**']
        assert no_emails == []
Beispiel #2
0
def send_tip_2(request):
    """Handle the second stage of sending a tip.

    TODO:
        * Convert this view-based logic to a django form.

    Returns:
        JsonResponse: If submitting tip, return response with success state.
        TemplateResponse: Render the submission form.

    """
    from_username = request.session.get('handle', '')
    primary_from_email = request.session.get('email', '')
    access_token = request.session.get('access_token')
    to_emails = []

    if request.body:
        # http response
        response = {
            'status': 'OK',
            'message': 'Notification has been sent',
        }
        params = json.loads(request.body)

        to_username = params['username'].lstrip('@')
        try:
            to_profile = Profile.objects.get(handle__iexact=to_username)
            if to_profile.email:
                to_emails.append(to_profile.email)
            if to_profile.github_access_token:
                to_emails = get_github_emails(to_profile.github_access_token)
        except Profile.DoesNotExist:
            pass

        if params.get('email'):
            to_emails.append(params['email'])

        # If no primary email in session, try the POST data. If none, fetch from GH.
        if params.get('fromEmail'):
            primary_from_email = params['fromEmail']
        elif access_token and not primary_from_email:
            primary_from_email = get_github_primary_email(access_token)

        to_emails = list(set(to_emails))
        expires_date = timezone.now() + timezone.timedelta(
            seconds=params['expires_date'])

        # db mutations
        tip = Tip.objects.create(
            emails=to_emails,
            url=params['url'],
            tokenName=params['tokenName'],
            amount=params['amount'],
            comments_priv=params['comments_priv'],
            comments_public=params['comments_public'],
            ip=get_ip(request),
            expires_date=expires_date,
            github_url=params['github_url'],
            from_name=params['from_name'],
            from_email=params['from_email'],
            from_username=from_username,
            username=params['username'],
            network=params['network'],
            tokenAddress=params['tokenAddress'],
            txid=params['txid'],
            from_address=params['from_address'],
        )
        # notifications
        maybe_market_tip_to_github(tip)
        maybe_market_tip_to_slack(tip, 'new_tip')
        maybe_market_tip_to_email(tip, to_emails)
        if not to_emails:
            response['status'] = 'error'
            response[
                'message'] = 'Uh oh! No email addresses for this user were found via Github API.  Youll have to let the tipee know manually about their tip.'

        return JsonResponse(response)

    params = {
        'issueURL':
        request.GET.get('source'),
        'class':
        'send2',
        'title':
        'Send Tip',
        'recommend_gas_price':
        recommend_min_gas_price_to_confirm_in_time(
            confirm_time_minutes_target),
        'from_email':
        primary_from_email,
        'from_handle':
        from_username,
    }

    return TemplateResponse(request, 'yge/send2.html', params)
Beispiel #3
0
def send_tip_2(request):
    """Handle the second stage of sending a tip."""
    username = request.session.get('handle')
    primary_email = request.session.get('email', '')

    if request.body:
        # http response
        response = {
            'status': 'OK',
            'message': 'Notification has been sent',
        }
        params = json.loads(request.body)

        username = username or params['username']
        access_token = request.session.get('access_token')
        emails = get_github_emails(access_token) if access_token else [
            primary_email
        ]
        expires_date = timezone.now() + timezone.timedelta(
            seconds=params['expires_date'])

        # db mutations
        tip = Tip.objects.create(
            emails=emails,
            url=params['url'],
            tokenName=params['tokenName'],
            amount=params['amount'],
            comments_priv=params['comments_priv'],
            comments_public=params['comments_public'],
            ip=get_ip(request),
            expires_date=expires_date,
            github_url=params['github_url'],
            from_name=params['from_name'],
            from_email=params['from_email'],
            username=username,
            network=params['network'],
            tokenAddress=params['tokenAddress'],
            txid=params['txid'],
        )
        # notifications
        maybe_market_tip_to_github(tip)
        maybe_market_tip_to_slack(tip, 'new_tip', tip.txid)
        maybe_market_tip_to_email(tip, emails)
        if not emails:
            response['status'] = 'error'
            response[
                'message'] = 'Uh oh! No email addresses for this user were found via Github API.  Youll have to let the tipee know manually about their tip.'

        return JsonResponse(response)

    params = {
        'issueURL':
        request.GET.get('source'),
        'class':
        'send2',
        'title':
        'Send Tip',
        'recommend_gas_price':
        recommend_min_gas_price_to_confirm_in_time(
            confirm_time_minutes_target),
        'from_email':
        primary_email,
        'from_handle':
        username
    }

    return TemplateResponse(request, 'yge/send2.html', params)
Beispiel #4
0
def send_tip_3(request):
    """Handle the third stage of sending a tip (the POST)

    Returns:
        JsonResponse: response with success state.

    """
    response = {
        'status': 'OK',
        'message': _('Tip Created'),
    }

    is_user_authenticated = request.user.is_authenticated
    from_username = request.user.username if is_user_authenticated else ''
    primary_from_email = request.user.email if is_user_authenticated else ''
    access_token = request.user.profile.get_access_token(
    ) if is_user_authenticated else ''
    to_emails = []

    params = json.loads(request.body)

    to_username = params['username'].lstrip('@')
    try:
        to_profile = Profile.objects.get(handle__iexact=to_username)
    except Profile.MultipleObjectsReturned:
        to_profile = Profile.objects.filter(handle__iexact=to_username).first()
    except Profile.DoesNotExist:
        to_profile = None
    if to_profile:
        if to_profile.email:
            to_emails.append(to_profile.email)
        if to_profile.github_access_token:
            to_emails = get_github_emails(to_profile.github_access_token)

    if params.get('email'):
        to_emails.append(params['email'])

    # If no primary email in session, try the POST data. If none, fetch from GH.
    if params.get('fromEmail'):
        primary_from_email = params['fromEmail']
    elif access_token and not primary_from_email:
        primary_from_email = get_github_primary_email(access_token)

    to_emails = list(set(to_emails))
    expires_date = timezone.now() + timezone.timedelta(
        seconds=params['expires_date'])
    priv_key, pub_key, address = generate_pub_priv_keypair()

    # db mutations
    tip = Tip.objects.create(emails=to_emails,
                             tokenName=params['tokenName'],
                             amount=params['amount'],
                             comments_priv=params['comments_priv'],
                             comments_public=params['comments_public'],
                             ip=get_ip(request),
                             expires_date=expires_date,
                             github_url=params['github_url'],
                             from_name=params['from_name'],
                             from_email=params['from_email'],
                             from_username=from_username,
                             username=params['username'],
                             network=params['network'],
                             tokenAddress=params['tokenAddress'],
                             from_address=params['from_address'],
                             metadata={
                                 'priv_key': priv_key,
                                 'pub_key': pub_key,
                                 'address': address,
                             })
    response['payload'] = {
        'address': address,
    }
    return JsonResponse(response)