示例#1
0
def django_db_setup(django_db_setup, django_db_blocker):
    """Populate DB with users.

    Create a couple of users we can use for testing different parts of the codebase
    """
    with django_db_blocker.unblock():
       # Create required users
       User.objects.create(username='******')
       # Currently created at top level of project
       # User.objects.create(username='******', email='*****@*****.**', first_name='Karl')
       User.objects.create(username='******', email='*****@*****.**')
       User.objects.create(username='******', email='*****@*****.**')
       User.objects.create(username='******', email='foo@bar')
       # no user [email protected], listed here as a warning

       User.objects.create(username='******')

       # Instantiate an instance of VipUser, this user is pro so we'll need one that isn't too
       vu = VipUser()
       vu.user = User.objects.get(username='******')
       # Use now, NotNull is on this but the date doesn't matter atm
       vu.vip_created_at = datetime.utcnow().replace(tzinfo=pytz.utc)
       vu.save()

       credential_models.update_user_credentials(api_data.get_user_info_credentials_json())
示例#2
0
def update_vip_user_records(info_from_api):
    """Update both user and credential DB records.

  This accepts output from query_user_info()
  """
    # Update the users "personal information"
    user_result = utils.update_user_record(info_from_api)

    # Update all credential records in DB
    credential_result = credential_models.update_user_credentials(
        info_from_api)

    if user_result and credential_result:
        return True

    return False
示例#3
0
def test_update_user_credentials_receives_empty_list():
    """If no data is passed to update_user_credentials it should return True as
  there is nothing to update.
  """
    assert credential_models.update_user_credentials([]) is False
示例#4
0
def manage_two_factor(request, *args, **kwargs):
    """Stub credential management page for users.

  This is a basic page to 'get things going', its expected to be replaced by
  something nicer per site.
  Template needs to be supplied
  """
    add_vip_token_credentials = None
    remove_vip_credentials = None
    try:
        users_vip_record = models.VipUser.objects.get(user=request.user)
    except models.VipUser.DoesNotExist as dne:
        logger.debug(
            '{0} does not have a VipUser object. Are they permitted here?'.
            format(request.user))
        raise Http404

    # Only process posts if _something_ was included (CSRF token is always included)
    if request.method == "POST" and len(request.POST) > 1:
        if request.POST.has_key('credential_id') or request.POST.has_key(
                'name'):
            print 'credential_id code path'  # string
            add_vip_token_credentials = forms.AddTokenCredential(request.POST)
            if add_vip_token_credentials.is_valid():
                added_cred = utils.add_credential_to_vip(
                    request.user.email,
                    add_vip_token_credentials.cleaned_data['credential_id'],
                    add_vip_token_credentials.cleaned_data['name'],
                )
                if not added_cred.status == '0000':
                    add_vip_token_credentials.add_error(
                        None,
                        'An error occurred ({1} - {0}) while adding {2} via the API.'
                        .format(
                            added_cred.statusMessage, added_cred.status,
                            add_vip_token_credentials.
                            cleaned_data['credential_id']))
                # Attempt to update the DB based on API data now they have changed their tokens
                user_record_updated = utils.update_user_record(
                    utils.query_user_info(request.user.email))
                token_credential_updated = credential_models.update_user_credentials(
                    utils.query_user_info(request.user.email))

        if request.POST.has_key('credentials_list'):
            remove_vip_credentials = forms.RemoveCredentials(request.POST,
                                                             user=request.user)
            if remove_vip_credentials.is_valid():
                for removing_device in remove_vip_credentials.cleaned_data[
                        'credentials_list']:
                    logger.debug('removing {0}'.format(removing_device))
                    # Remove single credential
                    removed_cred = utils.remove_credential_from_vip(
                        request.user.email, removing_device.credential_id)
                    if not removed_cred.status == '0000':
                        remove_vip_credentials.add_error(
                            None,
                            'An error occurred ({1} - {0}) while removing {2} from {3} via the API.'
                            .format(removed_cred.statusMessage,
                                    removed_cred.status,
                                    removing_device.credential_id,
                                    request.user.email))
                    else:
                        # Only remove from DB if successfully removed from API.
                        # update db with new credential data, including removing this one
                        token_credential_updated = credential_models.update_user_credentials(
                            utils.query_user_info(request.user.email))

    if not add_vip_token_credentials:
        add_vip_token_credentials = forms.AddTokenCredential()
    if not remove_vip_credentials:
        remove_vip_credentials = forms.RemoveCredentials(user=request.user)


# TODO: include update_vip_user_records here?

    return render(
        request, kwargs['template'], {
            'remove_credentials': remove_vip_credentials,
            'add_token_credentials': add_vip_token_credentials,
        })