<commit_before>from flask import url_for

from bs4 import BeautifulSoup

import app

from app.notify_client.models import InvitedUser
from tests.conftest import sample_invite as create_sample_invite
from tests.conftest import mock_check_invite_token as mock_check_token_invite


def test_existing_user_accept_invite_calls_api_and_redirects_to_dashboard(app_,
                                                                          service_one,
                                                                          api_user_active,
                                                                          sample_invite,
                                                                          mock_check_invite_token,
                                                                          mock_get_user_by_email,
                                                                          mock_get_users_by_service,
                                                                          mock_add_user_to_service):

    expected_service = service_one['id']
    expected_redirect_location = 'http://localhost/services/{}/dashboard'.format(expected_service)
    expected_permissions = ['send_messages', 'manage_service', 'manage_api_keys']

    with app_.test_request_context():
        with app_.test_client() as client:

            response = client.get(url_for('main.accept_invite', token='<PASSWORD>'))

            mock_check_invite_token.assert_called_with('thisisnotarealtoken')
            mock_get_user_by_email.assert_called_with('<EMAIL>')
            mock_add_user_to_service.assert_called_with(expected_service, api_user_active.id, expected_permissions)

            assert response.status_code == 302
            assert response.location == expected_redirect_location


def test_existing_user_with_no_permissions_accept_invite(app_,
                                                         mocker,
                                                         service_one,
                                                         api_user_active,
                                                         sample_invite,
                                                         mock_check_invite_token,
                                                         mock_get_user_by_email,
                                                         mock_get_users_by_service,
                                                         mock_add_user_to_service):

    expected_service = service_one['id']
    sample_invite['permissions'] = ''
    expected_permissions = []
    mocker.patch('app.invite_api_client.accept_invite', return_value=sample_invite)

    with app_.test_request_context():
        with app_.test_client() as client:

            response = client.get(url_for('main.accept_invite', token='<PASSWORD>'))
            mock_add_user_to_service.assert_called_with(expected_service, api_user_active.id, expected_permissions)

            assert response.status_code == 302


def test_if_existing_user_accepts_twice_they_redirect_to_sign_in(app_,
                                                                 mocker,
                                                                 sample_invite):

    sample_invite['status'] = 'accepted'
    invite = InvitedUser(**sample_invite)
    mocker.patch('app.invite_api_client.check_token', return_value=invite)

    with app_.test_request_context():
        with app_.test_client() as client:
            response = client.get(url_for('main.accept_invite', token='<PASSWORD>'), follow_redirects=True)
            assert response.status_code == 200
            page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
            assert page.h1.string.strip() == 'Sign in'
            flash_banners = page.find_all('div', class_='banner-default')
            assert len(flash_banners) == 1
            assert flash_banners[0].text.strip() == 'Please log in to access this page.'


def test_existing_user_of_service_get_redirected_to_signin(app_,
                                                           mocker,
                                                           api_user_active,
                                                           sample_invite,
                                                           mock_get_user_by_email,
                                                           mock_accept_invite):
    sample_invite['email_address'] = api_user_active.email_address
    invite = InvitedUser(**sample_invite)
    mocker.patch('app.invite_api_client.check_token', return_value=invite)
    mocker.patch('app.user_api_client.get_users_for_service', return_value=[api_user_active])

    with app_.test_request_context():
        with app_.test_client() as client:
            response = client.get(url_for('main.accept_invite', token='<PASSWORD>token'), follow_redirects=True)
            assert response.status_code == 200
            page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
            assert page.h1.string.strip() == 'Sign in'
            flash_banners = page.find_all('div', class_='banner-default')
            assert len(flash_banners) == 1
            assert flash_banners[0].text.strip() == 'Please log in to access this page.'


def test_existing_signed_out_user_accept_invite_redirects_to_sign_in(app_,
                                                                     service_one,
                                                                     api_user_active,
                                                                     sample_invite,
                                                                     mock_check_invite_token,
                                                                     mock_get_user_by_email,
                                                                     mock_get_users_by_service,
                                                                     mock_add_user_to_service):

    expected_service = service_one['id']
    expected_permissions = ['send_messages', 'manage_service', 'manage_api_keys']
    with app_.test_request_context():
        with app_.test_client() as client:

            response = client.get(url_for('main.accept_invite', token='<PASSWORD>'), follow_redirects=True)

            mock_check_invite_token.assert_called_with('thisisnotarealtoken')
            mock_get_user_by_email.assert_called_with('<EMAIL>')
            mock_add_user_to_service.assert_called_with(expected_service, api_user_active.id, expected_permissions)

            assert response.status_code == 200
            page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
            assert page.h1.string.strip() == 'Sign in'
            flash_banners = page.find_all('div', class_='banner-default')
            assert len(flash_banners) == 1
            assert flash_banners[0].text.strip() == 'Please log in to access this page.'


def test_new_user_accept_invite_calls_api_and_redirects_to_registration(app_,
                                                                        service_one,
                                                                        mock_check_invite_token,
                                                                        mock_dont_get_user_by_email,
                                                                        mock_add_user_to_service,
                                                                        mock_get_users_by_service):

    expected_redirect_location = 'http://localhost/register-from-invite'

    with app_.test_request_context():
        with app_.test_client() as client:

            response = client.get(url_for('main.accept_invite', token='<PASSWORD>'))

            mock_check_invite_token.assert_called_with('thisisnotarealtoken')
            mock_dont_get_user_by_email.assert_called_with('<EMAIL>')
<commit_msg>Updated from git comments<commit_after>from flask import url_for

from bs4 import BeautifulSoup

import app

from app.notify_client.models import InvitedUser
from tests.conftest import sample_invite as create_sample_invite
from tests.conftest import mock_check_invite_token as mock_check_token_invite


def test_existing_user_accept_invite_calls_api_and_redirects_to_dashboard(app_,
                                                                          service_one,
                                                                          api_user_active,
                                                                          sample_invite,
                                                                          mock_check_invite_token,
                                                                          mock_get_user_by_email,
                                                                          mock_get_users_by_service,
                                                                          mock_add_user_to_service):

    expected_service = service_one['id']
    expected_redirect_location = 'http://localhost/services/{}/dashboard'.format(expected_service)
    expected_permissions = ['send_messages', 'manage_service', 'manage_api_keys']

    with app_.test_request_context():
        with app_.test_client() as client:

            response = client.get(url_for('main.accept_invite', token='<PASSWORD>'))

            mock_check_invite_token.assert_called_with('<PASSWORD>altoken')
            mock_get_user_by_email.assert_called_with('<EMAIL>')
            mock_add_user_to_service.assert_called_with(expected_service, api_user_active.id, expected_permissions)

            assert response.status_code == 302
            assert response.location == expected_redirect_location


def test_existing_user_with_no_permissions_accept_invite(app_,
                                                         mocker,
                                                         service_one,
                                                         api_user_active,
                                                         sample_invite,
                                                         mock_check_invite_token,
                                                         mock_get_user_by_email,
                                                         mock_get_users_by_service,
                                                         mock_add_user_to_service):

    expected_service = service_one['id']
    sample_invite['permissions'] = ''
    expected_permissions = []
    mocker.patch('app.invite_api_client.accept_invite', return_value=sample_invite)

    with app_.test_request_context():
        with app_.test_client() as client:

            response = client.get(url_for('main.accept_invite', token='<PASSWORD>'))
            mock_add_user_to_service.assert_called_with(expected_service, api_user_active.id, expected_permissions)

            assert response.status_code == 302


def test_if_existing_user_accepts_twice_they_redirect_to_sign_in(app_,
                                                                 mocker,
                                                                 sample_invite):

    sample_invite['status'] = 'accepted'
    invite = InvitedUser(**sample_invite)
    mocker.patch('app.invite_api_client.check_token', return_value=invite)

    with app_.test_request_context():
        with app_.test_client() as client:
            response = client.get(url_for('main.accept_invite', token='<PASSWORD>'), follow_redirects=True)
            assert response.status_code == 200
            page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
            assert page.h1.string.strip() == 'Sign in'
            flash_banners = page.find_all('div', class_='banner-default')
            assert len(flash_banners) == 1
            assert flash_banners[0].text.strip() == 'Please log in to access this page.'


def test_existing_user_of_service_get_redirected_to_signin(app_,
                                                           mocker,
                                                           api_user_active,
                                                           sample_invite,
                                                           mock_get_user_by_email,
                                                           mock_accept_invite):
    sample_invite['email_address'] = api_user_active.email_address
    invite = InvitedUser(**sample_invite)
    mocker.patch('app.invite_api_client.check_token', return_value=invite)
    mocker.patch('app.user_api_client.get_users_for_service', return_value=[api_user_active])

    with app_.test_request_context():
        with app_.test_client() as client:
            response = client.get(url_for('main.accept_invite', token='<PASSWORD>'), follow_redirects=True)
            assert response.status_code == 200
            page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
            assert page.h1.string.strip() == 'Sign in'
            flash_banners = page.find_all('div', class_='banner-default')
            assert len(flash_banners) == 1
            assert flash_banners[0].text.strip() == 'Please log in to access this page.'
            assert mock_accept_invite.call_count == 0


def test_existing_signed_out_user_accept_invite_redirects_to_sign_in(app_,
                                                                     service_one,
                                                                     api_user_active,
                                                                     sample_invite,
                                                                     mock_check_invite_token,
                                                                     mock_get_user_by_email,
                                                                     mock_get_users_by_service,
                                                                     mock_add_user_to_service,
                                                                     mock_accept_invite):

    expected_service = service_one['id']
    expected_permissions = ['send_messages', 'manage_service', 'manage_api_keys']
    with app_.test_request_context():
        with app_.test_client() as client:

            response = client.get(url_for('main.accept_invite', token='<PASSWORD>'), follow_redirects=True)

            mock_check_invite_token.assert_called_with('thisisnotarealtoken')
            mock_get_user_by_email.assert_called_with('<EMAIL>')
            mock_add_user_to_service.assert_called_with(expected_service, api_user_active.id, expected_permissions)
            assert mock_accept_invite.call_count == 0

            assert response.status_code == 200
            page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
            assert page.h1.string.strip() == 'Sign in'
            flash_banners = page.find_all('div', class_='banner-default')
            assert len(flash_banners) == 1
            assert flash_banners[0].text.strip() == 'Please log in to access this page.'


def test_new_user_accept_invite_calls_api_and_redirects_to_registration(app_,
                                                                        service_one,
                                                                        mock_check_invite_token,
                                                                        mock_dont_get_user_by_email,
                                                                        mock_add_user_to_service,
                                                                        mock_get_users_by_service):

    expected_redirect_location = 'http://localhost/register-from-invite'

    with app_.test_request_context():
        with app_.test_client() as client:

            response = client.get(url_for('main.accept_invite', token='<PASSWORD>'))

            mock_check_invite_token.assert_called_with('thisisnotarealtoken')
            mock_dont_get_user_by_email.assert_called_with('<EMAIL>')

{'fraction_non_alphanumeric': 0.0696962120265033, 'fraction_numerical': 0.003625453181647706, 'mean_word_length': 1.4131221719457014, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 177, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3422530', 'n_tokens_mistral': 3710, 'n_tokens_neox': 3613, 'n_words': 538}