<commit_before>from datetime import datetime, timedelta

import mock
import pytest
from sqlalchemy.exc import IntegrityError

from app import db, create_app
from app.models import (
    User, Lot, Framework, Service,
    Supplier, SupplierFramework, FrameworkAgreement,
    Brief, BriefResponse,
    ValidationError,
    BriefClarificationQuestion,
    DraftService,
    FrameworkLot,
    ContactInformation
)
from tests.bases import BaseApplicationTest
from tests.helpers import FixtureMixin


class TestUser(BaseApplicationTest, FixtureMixin):
    def test_should_not_return_password_on_user(self):
        with self.app.app_context():
            self.setup_default_buyer_domain()

            now = datetime.utcnow()
            user = User(
                email_address='<EMAIL>',
                name='name',
                role='buyer',
                password='password',
                active=True,
                failed_login_count=0,
                created_at=now,
                updated_at=now,
                password_changed_at=now
            )

            assert user.serialize()['emailAddress'] == "<EMAIL>"
            assert user.serialize()['name'] == "name"
            assert user.serialize()['role'] == "buyer"
            assert 'password' not in user.serialize()

    def test_buyer_user_requires_valid_email_domain(self):
        with self.app.app_context(), pytest.raises(ValidationError) as exc:
            self.setup_default_buyer_domain()
            valid_buyer_user = User(
                email_address='<EMAIL>',
                name='name',
                role='buyer',
                password='password',
                active=True,
            )
            # Changing email to invalid domain raises an error
            valid_buyer_user.email_address = '<EMAIL>'

        assert str(exc.value) == 'invalid_buyer_domain'

    @pytest.mark.parametrize('role', User.ADMIN_ROLES)
    def test_admin_user_requires_whitelisted_email_domain(self, role):
        with self.app.app_context(), pytest.raises(ValidationError) as exc:
            valid_admin_user = User(
                email_address='<EMAIL>',
                name='name',
                role=role,
                password='password',
                active=True,
            )
            # Changing email to invalid domain raises an error
            valid_admin_user.email_address = '<EMAIL>'
        assert str(exc.value) == 'invalid_admin_domain'

    def test_user_with_invalid_email_domain_cannot_be_changed_to_buyer_role(self):
        with self.app.app_context(), pytest.raises(ValidationError) as exc:
            self.setup_default_buyer_domain()
            valid_admin_user = User(
                email_address='<EMAIL>',
                name='name',
                role='admin',
                password='password',
                active=True,
            )
            # Changing role to buyer raises an error
            valid_admin_user.role = 'buyer'

        assert str(exc.value) == 'invalid_buyer_domain'

    @pytest.mark.parametrize('role', User.ADMIN_ROLES)
    def test_user_with_invalid_email_domain_cannot_be_changed_to_admin_role(self, role):
        with self.app.app_context(), pytest.raises(ValidationError) as exc:
            self.setup_default_buyer_domain()
            valid_buyer_user = User(
                email_address='<EMAIL>',
                name='name',
                role='buyer',
                password='password',
                active=True,
            )
            # Changing role to admin raises an error
            valid_buyer_user.role = role
        assert str(exc.value) == 'invalid_admin_domain'


def test_framework_should_not_accept_invalid_status():
    app = create_app('test')
    with app.app_context(), pytest.raises(ValidationError):
        f = Framework(
            name='foo',
            slug='foo',
            framework='g-cloud',
            status='invalid',
        )
        db.session.add(f)
        db.session.commit()


def test_framework_should_accept_valid_statuses():
    app = create_app('test')
    with app.app_context():
        for i, status in enumerate(Framework.STATUSES):
            f = Framework(
                name='foo',<commit_msg>New `TestFrameworks` class inheriting BaseApplicationTest<commit_after>from datetime import datetime, timedelta

import mock
import pytest
from sqlalchemy.exc import IntegrityError

from app import db
from app.models import (
    User, Lot, Framework, Service,
    Supplier, SupplierFramework, FrameworkAgreement,
    Brief, BriefResponse,
    ValidationError,
    BriefClarificationQuestion,
    DraftService,
    FrameworkLot,
    ContactInformation
)
from tests.bases import BaseApplicationTest
from tests.helpers import FixtureMixin


class TestUser(BaseApplicationTest, FixtureMixin):
    def test_should_not_return_password_on_user(self):
        with self.app.app_context():
            self.setup_default_buyer_domain()

            now = datetime.utcnow()
            user = User(
                email_address='<EMAIL>',
                name='name',
                role='buyer',
                password='password',
                active=True,
                failed_login_count=0,
                created_at=now,
                updated_at=now,
                password_changed_at=now
            )

            assert user.serialize()['emailAddress'] == "<EMAIL>"
            assert user.serialize()['name'] == "name"
            assert user.serialize()['role'] == "buyer"
            assert 'password' not in user.serialize()

    def test_buyer_user_requires_valid_email_domain(self):
        with self.app.app_context(), pytest.raises(ValidationError) as exc:
            self.setup_default_buyer_domain()
            valid_buyer_user = User(
                email_address='<EMAIL>',
                name='name',
                role='buyer',
                password='password',
                active=True,
            )
            # Changing email to invalid domain raises an error
            valid_buyer_user.email_address = '<EMAIL>'

        assert str(exc.value) == 'invalid_buyer_domain'

    @pytest.mark.parametrize('role', User.ADMIN_ROLES)
    def test_admin_user_requires_whitelisted_email_domain(self, role):
        with self.app.app_context(), pytest.raises(ValidationError) as exc:
            valid_admin_user = User(
                email_address='<EMAIL>',
                name='name',
                role=role,
                password='password',
                active=True,
            )
            # Changing email to invalid domain raises an error
            valid_admin_user.email_address = '<EMAIL>'
        assert str(exc.value) == 'invalid_admin_domain'

    def test_user_with_invalid_email_domain_cannot_be_changed_to_buyer_role(self):
        with self.app.app_context(), pytest.raises(ValidationError) as exc:
            self.setup_default_buyer_domain()
            valid_admin_user = User(
                email_address='<EMAIL>',
                name='name',
                role='admin',
                password='password',
                active=True,
            )
            # Changing role to buyer raises an error
            valid_admin_user.role = 'buyer'

        assert str(exc.value) == 'invalid_buyer_domain'

    @pytest.mark.parametrize('role', User.ADMIN_ROLES)
    def test_user_with_invalid_email_domain_cannot_be_changed_to_admin_role(self, role):
        with self.app.app_context(), pytest.raises(ValidationError) as exc:
            self.setup_default_buyer_domain()
            valid_buyer_user = User(
                email_address='<EMAIL>',
                name='name',
                role='buyer',
                password='password',
                active=True,
            )
            # Changing role to admin raises an error
            valid_buyer_user.role = role
        assert str(exc.value) == 'invalid_admin_domain'


class TestFrameworks(BaseApplicationTest):

    def test_framework_should_not_accept_invalid_status(self):
        with pytest.raises(ValidationError):
            f = Framework(
                name='foo',
                slug='foo',
                framework='g-cloud',
                status='invalid',
            )
            db.session.add(f)
            db.session.commit()

    def test_framework_should_accept_valid_statuses(self):
        for i, status in enumerate(Framework.STATUSES):
            f = Framework(
                name='foo',
{'fraction_non_alphanumeric': 0.09154929577464789, 'fraction_numerical': 0.00023474178403755868, 'mean_word_length': 2.0750631540959943, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '88719', 'n_tokens_mistral': 2433, 'n_tokens_neox': 2303, 'n_words': 441}