<commit_before>from __future__ import unicode_literals

import collections
import datetime
import unittest

from django.contrib.auth import get_user_model
from django.utils import timezone
from rest_framework import status
from rest_framework import test
from rest_framework.reverse import reverse

from nodeconductor.core.tests.helpers import override_nodeconductor_settings
from nodeconductor.structure import serializers
from nodeconductor.structure import tasks
from nodeconductor.structure import views
from nodeconductor.structure.models import CustomerRole, ProjectRole, CustomerPermission
from nodeconductor.structure.tests import factories

User = get_user_model()

TestRole = collections.namedtuple('TestRole', ['user', 'customer', 'role'])


class CustomerPermissionViewSetTest(unittest.TestCase):
    def setUp(self):
        self.view_set = views.CustomerPermissionViewSet()

    def test_cannot_modify_permission_in_place(self):
        self.assertNotIn('PUT', self.view_set.allowed_methods)
        self.assertNotIn('PATCH', self.view_set.allowed_methods)

    def test_customer_permission_serializer_is_used(self):
        self.assertIs(
            serializers.CustomerPermissionSerializer,
            self.view_set.get_serializer_class(),
        )


class CustomerPermissionSerializerTest(unittest.TestCase):
    def setUp(self):
        self.serializer = serializers.CustomerPermissionSerializer()

    def test_payload_has_required_fields(self):
        expected_fields = [
            'url', 'role', 'pk', 'created', 'expiration_time', 'created_by',
            'customer', 'customer_name', 'customer_native_name', 'customer_abbreviation', 'customer_uuid',
            'user', 'user_full_name', 'user_native_name', 'user_username', 'user_uuid', 'user_email',

        ]
        self.assertItemsEqual(expected_fields, self.serializer.fields.keys())


class CustomerPermissionApiPermissionTest(test.APITransactionTestCase):
    all_roles = (
        # user customer role
        TestRole('first', 'first', 'owner'),
        TestRole('second', 'second', 'owner'),
    )

    role_map = {<commit_msg>Include is_expert_provider field into permissions tests<commit_after>from __future__ import unicode_literals

import collections
import datetime
import unittest

from django.contrib.auth import get_user_model
from django.utils import timezone
from rest_framework import status
from rest_framework import test
from rest_framework.reverse import reverse

from nodeconductor.core.tests.helpers import override_nodeconductor_settings
from nodeconductor.structure import serializers
from nodeconductor.structure import tasks
from nodeconductor.structure import views
from nodeconductor.structure.models import CustomerRole, ProjectRole, CustomerPermission
from nodeconductor.structure.tests import factories

User = get_user_model()

TestRole = collections.namedtuple('TestRole', ['user', 'customer', 'role'])


class CustomerPermissionViewSetTest(unittest.TestCase):
    def setUp(self):
        self.view_set = views.CustomerPermissionViewSet()

    def test_cannot_modify_permission_in_place(self):
        self.assertNotIn('PUT', self.view_set.allowed_methods)
        self.assertNotIn('PATCH', self.view_set.allowed_methods)

    def test_customer_permission_serializer_is_used(self):
        self.assertIs(
            serializers.CustomerPermissionSerializer,
            self.view_set.get_serializer_class(),
        )


class CustomerPermissionSerializerTest(unittest.TestCase):
    def setUp(self):
        self.serializer = serializers.CustomerPermissionSerializer()

    def test_payload_has_required_fields(self):
        expected_fields = [
            'url', 'role', 'pk', 'created', 'expiration_time', 'created_by',
            'customer', 'customer_name', 'customer_native_name', 'customer_abbreviation', 'customer_uuid',
            'user', 'user_full_name', 'user_native_name', 'user_username', 'user_uuid', 'user_email',
            'is_expert_provider',
        ]
        self.assertItemsEqual(expected_fields, self.serializer.fields.keys())


class CustomerPermissionApiPermissionTest(test.APITransactionTestCase):
    all_roles = (
        # user customer role
        TestRole('first', 'first', 'owner'),
        TestRole('second', 'second', 'owner'),
    )

    role_map = {
{'fraction_non_alphanumeric': 0.09019244145606306, 'fraction_numerical': 0.0, 'mean_word_length': 4.631853785900783, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7484968', 'n_tokens_mistral': 1192, 'n_tokens_neox': 1140, 'n_words': 248}