Exemple #1
0
def test_callback_requests_sso_profile_valid_non_staff_user(
    get_sso_user_profile,
    get_access_token,
    flags,
    caplog,
):
    """
    Test that if SSO user has a matching SSO email user ID,
    but Data Hub user has `is_staff` or `is_active`
    flag not set, then the access is forbidden.
    """
    AdviserFactory(sso_email_user_id='some-123@email', **flags)

    get_access_token.return_value = {'access_token': 'access-token'}
    get_sso_user_profile.return_value = {'email_user_id': 'some-123@email'}

    request = get_request_with_session('/oauth/callback/?state=original&code=code')
    request.session['oauth.state'] = 'original'
    response = callback(request)
    response.render()

    assert response.status_code == status.HTTP_403_FORBIDDEN
    assert 'Forbidden.' in str(response.content)
    assert not request.user.is_authenticated
    assert len(caplog.records) == 1
    assert 'Django Admin OAuth2 authentication failed: User not found.' in caplog.text
Exemple #2
0
def test_callback_without_state():
    """Test that a callback without provided state will restart login process."""
    request = get_request_with_session('/oauth/callback')

    response = callback(request)

    assert response.status_code == status.HTTP_302_FOUND
    assert response.url == request.build_absolute_uri(reverse('admin:login'))
Exemple #3
0
def test_callback_without_state_includes_next_url():
    """
    Test that a callback without provided state will restart login process including next URL.
    """
    request = get_request_with_session('/oauth/callback/?next=/protected-area')

    response = callback(request)

    assert response.status_code == status.HTTP_302_FOUND
    next_url = extract_next_url_from_url(response.url)
    assert next_url == '/protected-area'
Exemple #4
0
def test_callback_without_access_code():
    """Test that a callback without a code will return an error page."""
    fake_state_id = token_urlsafe(settings.ADMIN_OAUTH2_TOKEN_BYTE_LENGTH)

    request = get_request_with_session(f'/oauth/callback/?state={fake_state_id}')
    request.session['oauth.state'] = fake_state_id
    response = callback(request)

    assert response.status_code == status.HTTP_403_FORBIDDEN

    response.render()
    response_content = str(response.content)
    assert 'Forbidden.' in response_content
    assert not request.user.is_authenticated
Exemple #5
0
def test_callback_validates_next_url(get_sso_user_profile, get_access_token, dangerous_redirect):
    """Test that successful login redirects user to `next_url`."""
    fake_state_id = token_urlsafe(settings.ADMIN_OAUTH2_TOKEN_BYTE_LENGTH)
    AdviserFactory(sso_email_user_id='some-123@email', is_staff=True, is_active=True)

    get_access_token.return_value = {'access_token': 'access-token', 'expires_in': 3600}
    get_sso_user_profile.return_value = {'email_user_id': 'some-123@email'}

    request = get_request_with_session(
        f'/oauth/callback/?next={dangerous_redirect}&state={fake_state_id}&code=code',
    )
    request.session['oauth.state'] = fake_state_id

    response = callback(request)

    assert response.status_code == status.HTTP_302_FOUND
    assert response.url == reverse('admin:index')
Exemple #6
0
def test_callback_requests_sso_profile_no_user(get_sso_user_profile, get_access_token):
    """Test that if SSO user is not found then no access is granted."""
    get_access_token.return_value = {'access_token': 'access-token', 'expires_in': 3600}
    get_sso_user_profile.return_value = {'email_user_id': 'some-123@email'}

    fake_state_id = token_urlsafe(settings.ADMIN_OAUTH2_TOKEN_BYTE_LENGTH)

    request = get_request_with_session(f'/oauth/callback/?state={fake_state_id}&code=code')
    request.session['oauth.state'] = fake_state_id
    response = callback(request)

    assert response.status_code == status.HTTP_403_FORBIDDEN

    response.render()
    response_content = str(response.content)
    assert 'Forbidden.' in response_content
    assert not request.user.is_authenticated
Exemple #7
0
def test_callback_requests_valid_sso_profile(get_sso_user_profile, get_access_token):
    """
    Test that if SSO user has a matching SSO email user id (and relevant flags),
    then the access is granted.
    """
    fake_state_id = token_urlsafe(settings.ADMIN_OAUTH2_TOKEN_BYTE_LENGTH)
    adviser = AdviserFactory(sso_email_user_id='some-123@email', is_staff=True, is_active=True)

    get_access_token.return_value = {'access_token': 'access-token', 'expires_in': 3600}
    get_sso_user_profile.return_value = {'email_user_id': 'some-123@email'}

    request = get_request_with_session(f'/oauth/callback/?state={fake_state_id}&code=code')

    request.session['oauth.state'] = fake_state_id

    response = callback(request)

    assert response.status_code == status.HTTP_302_FOUND
    assert response.url == reverse('admin:index')
    assert request.user.is_authenticated
    assert request.user == adviser