Esempio n. 1
0
def test_get_last_login_with_params(api_client):
    user1 = UserFactory(last_login=make_aware(datetime(2016, 12, 25)))
    user2 = UserFactory(last_login=make_aware(datetime(2017, 1, 1)))
    user3 = UserFactory(last_login=make_aware(datetime(2016, 12, 26)))
    # outside of filtered params, should not be in response
    UserFactory(last_login=make_aware(datetime(2016, 12, 24)))
    UserFactory(last_login=make_aware(datetime(2017, 1, 2)))
    setup_data()  # creates active user that should not be in response

    response = api_client.get(reverse('api:last-login'), {
        'start': '2016-12-25',
        'end': '2017-01-01'
    })

    assert response.status_code == status.HTTP_200_OK
    assert len(response.json()) == 3
    date_format = '%Y-%m-%dT%H:%M:%SZ'
    expected = [
        {
            'id': user1.id,
            'last_login': user1.last_login.strftime(date_format)
        },
        {
            'id': user2.id,
            'last_login': user2.last_login.strftime(date_format)
        },
        {
            'id': user3.id,
            'last_login': user3.last_login.strftime(date_format)
        },
    ]
    assert response.json() == expected
Esempio n. 2
0
def test_download_password_reset_links(settings, superuser_client):
    UserFactory.create()

    data = {
        'action': 'download_password_reset_links',
        '_selected_action': User.objects.all().values_list('pk', flat=True),
    }
    response = superuser_client.post(reverse('admin:user_user_changelist'), data, follow=True)

    assert '/accounts/password/reset/key/' in str(response.content)
Esempio n. 3
0
    def test_nonsuperuser_cannot_access_user_changelist(self):
        non_admin_user = UserFactory(is_staff=False)
        non_admin_user.save()

        settings.AUTHENTICATION_BACKENDS = self.AUTHENTICATION_BACKENDS_CLASSES
        settings.FEATURE_ENFORCE_STAFF_SSO_ENABLED = True
        reload_urlconf()
        self.client.force_login(non_admin_user)
        url = reverse('admin:user_user_changelist')

        response = self.client.get(url)
        assert response.status_code == 302
def test_activity_stream_list_users_endpoint(api_client):
    """If the Authorization and X-Forwarded-For headers are correct, then
    the correct, and authentic, data is returned
    """
    user_1 = UserFactory()
    UserProfileFactory(user=user_1, mobile_phone_number='824802648236868364')
    UserFactory.create_batch(4)
    sender = _auth_sender(url=_url_activity_stream_users)
    response = api_client.get(
        _url_activity_stream_users(),
        content_type='',
        HTTP_AUTHORIZATION=sender.request_header,
    )
    data = response.json()
    assert response.status_code == status.HTTP_200_OK
    assert len(data['orderedItems']) == 2
    assert set(data['orderedItems'][0]['object'].keys()) == {
        'id',
        'type',
        'dit:DirectorySSO:User:hashedUuid',
        'dit:DirectorySSO:User:email',
        'dit:DirectorySSO:User:telephone',
        'dit:DirectorySSO:User:dateJoined',
    }
    assert data['next'] is not None
    assert data['previous'] is None

    sender = _auth_sender(url=lambda: data['next'])
    response = api_client.get(
        data['next'],
        content_type='',
        HTTP_AUTHORIZATION=sender.request_header,
    )
    data = response.json()

    assert response.status_code == status.HTTP_200_OK
    assert len(data['orderedItems']) == 2
    assert data['next'] is not None
    assert data['previous'] is not None

    sender = _auth_sender(url=lambda: data['next'])
    response = api_client.get(
        data['next'],
        content_type='',
        HTTP_AUTHORIZATION=sender.request_header,
    )
    data = response.json()

    assert response.status_code == status.HTTP_200_OK
    assert len(data['orderedItems']) == 1
    assert data['next'] is None
    assert data['previous'] is not None
Esempio n. 5
0
def test_get_last_login_with_invalid_date_params(client):
    UserFactory(last_login=make_aware(datetime(2016, 12, 25)))
    UserFactory(last_login=make_aware(datetime(2017, 1, 1)))
    UserFactory(last_login=make_aware(datetime(2016, 12, 26)))
    setup_data()

    response = client.get(reverse('api:last-login'), {
        'start': '2016-A-25',
        'end': '2017-B-01'
    })

    assert response.status_code == status.HTTP_400_BAD_REQUEST
    assert response.json() == 'not enough arguments for format string'
Esempio n. 6
0
def test_get_last_login(api_client):
    users = UserFactory.create_batch(5)
    setup_data()  # creates active user that should not be in response

    response = api_client.get(reverse('api:last-login'))

    assert response.status_code == status.HTTP_200_OK
    assert len(response.json()) == 5
    date_format = '%Y-%m-%dT%H:%M:%S.%fZ'
    expected = [
        {
            'id': users[0].id,
            'last_login': users[0].last_login.strftime(date_format)
        },
        {
            'id': users[1].id,
            'last_login': users[1].last_login.strftime(date_format)
        },
        {
            'id': users[2].id,
            'last_login': users[2].last_login.strftime(date_format)
        },
        {
            'id': users[3].id,
            'last_login': users[3].last_login.strftime(date_format)
        },
        {
            'id': users[4].id,
            'last_login': users[4].last_login.strftime(date_format)
        },
    ]
    assert response.json() == expected
Esempio n. 7
0
def test_user_data_object():
    user = UserFactory()
    data = {'user': user, 'name': 'data-object-name'}

    user_data = UserData.objects.create(**data)

    assert str(user_data) == str(f'{user} : {data["name"]}')
Esempio n. 8
0
def test_user_answer_object():
    user = UserFactory()
    question = QuestionFactory()
    data = {'user': user, 'question': question}

    expected = UserAnswer.objects.create(**data)

    assert str(expected) == str(f'{user} : {question}')
    assert expected.to_dict()['question_id'] == question.id
Esempio n. 9
0
def test_download_email_verification_links(settings, superuser_client):
    user = UserFactory.create()
    EmailAddress(user=user, email=user.email).save()

    data = {
        'action': 'download_email_verification_links',
        '_selected_action': User.objects.all().values_list('pk', flat=True),
    }
    response = superuser_client.post(reverse('admin:user_user_changelist'), data, follow=True)

    assert '/accounts/confirm-email/' in str(response.content)
Esempio n. 10
0
def test_verify_no_verification_code(api_client):
    user = UserFactory()
    api_client.force_authenticate(user=user)

    url = reverse('api:verification-code-verify')
    response = api_client.post(
        url,
        {
            'code': 'my-name-is-jeff',
            'email': user.email,
        },
        format='json',
    )

    assert response.status_code == 404
Esempio n. 11
0
def test_create_user_profile():
    user = UserFactory()
    data = {
        'first_name': 'john',
        'last_name': 'smith',
        'mobile_phone_number': '0203044213',
        'job_title': 'Director',
        'user': user,
    }

    expected = UserProfile.objects.create(**data)
    assert expected.first_name == data['first_name']
    assert expected.last_name == data['last_name']
    assert expected.job_title == data['job_title']
    assert expected.mobile_phone_number == data['mobile_phone_number']
    assert str(expected) == str(user)
Esempio n. 12
0
def test_set_questionnaire_answer():
    user = UserFactory()
    service = ServiceFactory()
    question = QuestionFactory(service=service, name='in-progress')

    UserAnswerFactory(question=question, user=user)

    assert utils.set_questionnaire_answer(user, service.name, question.id,
                                          'answer') is None

    question.name = 'continue'
    question.question_choices = {'options': [{'value': 'a', 'jump': 'end'}]}
    question.save()

    assert utils.set_questionnaire_answer(user, service.name, question.id,
                                          'a') is None
Esempio n. 13
0
def test_get_questionnaire():
    user = UserFactory()
    service = ServiceFactory()
    first_question = QuestionFactory(service=service,
                                     id=0,
                                     name='in-progress',
                                     is_active=False)
    second_question = QuestionFactory(service=service,
                                      predefined_choices='TURNOVER_CHOICES')
    utils.set_questionnaire_answer(user, service.name, first_question.id,
                                   'in-progress')

    questionnaire = utils.get_questionnaire(user, service.name)
    assert len(questionnaire['answers']) == 0
    assert len(questionnaire['questions']) == 1
    assert len(questionnaire['questions'][0]['choices']['options']) == 8

    utils.set_questionnaire_answer(user, service.name, second_question.id,
                                   'answer')
    questionnaire = utils.get_questionnaire(user, service.name)
    assert len(questionnaire['answers']) == 1
Esempio n. 14
0
def test_set_questionnaire_answer_invalid():
    user = UserFactory()

    assert utils.set_questionnaire_answer(user, 'service', 999,
                                          'user_answer') is None
    assert utils.set_questionnaire_answer(user, 'service', 999, '') is None
Esempio n. 15
0
def test_inactivity_filter(rf, superuser):
    three_years_ago = timezone.now() - relativedelta(years=3)

    with freeze_time(three_years_ago):
        user_one = UserFactory()
        user_one.last_login = three_years_ago - timedelta(days=5)
        user_one.save()

    with freeze_time(three_years_ago):
        user_two = UserFactory(is_superuser=True)
        user_two.last_login = three_years_ago
        user_two.save()

    with freeze_time(three_years_ago):
        user_three = UserFactory()
        user_three.last_login = three_years_ago + timedelta(days=5)
        user_three.save()

    modeladmin = admin.UserAdmin(User, site)
    request = rf.get('/', {'inactive': True})
    request.user = superuser
    changelist = modeladmin.get_changelist_instance(request)
    queryset = changelist.get_queryset(request)

    assert queryset.count() == 1
    assert user_one in queryset
    assert user_two not in queryset
    assert user_three not in queryset
Esempio n. 16
0
def user():
    return UserFactory()
Esempio n. 17
0
def test_get_questionnaire_no_questions():
    user = UserFactory()
    questionnaire = utils.get_questionnaire(user, 'service')

    assert questionnaire is None