# Path: api_v3/factories/profile.py
# class ProfileFactory(DjangoModelFactory):
# 
#     class Meta:
#         model = Profile
# 
#     email = Faker('email')
#     first_name = Faker('first_name')
#     last_name = Faker('last_name')
#     bio = Faker('job')
#     last_login = Faker('past_date', start_date='-30d')
# 
# Path: api_v3/serializers/profile.py
# class ProfileSerializer(serializers.ModelSerializer):
# 
#     tickets_count = fields.SerializerMethodField()
# 
#     class Meta:
#         model = Profile
#         read_only_fields = (
#             'id',
#             'email',
#             'first_name',
#             'last_name',
#             'is_staff',
#             'is_superuser',
#             'locale'
#         )
#         fields = (
#             'id',
#             'email',
#             'first_name',
#             'last_name',
#             'is_staff',
#             'is_superuser',
#             'bio',
#             'locale',
#             'tickets_count'
#         )
# 
#     def get_tickets_count(self, obj):
#         if obj.is_superuser:
#             return Ticket.objects.count()
#         else:
#             return Ticket.filter_by_user(obj).count()
# 
#     def to_representation(self, obj):
#         request = self.context.get('request', None)
# 
#         data = super(ProfileSerializer, self).to_representation(obj)
# 
#         if request and request.user and (
#             request.user.is_staff or request.user.is_superuser
#         ):
#             return data
# 
#         # For regular users, make sure others email is not displayed
#         if request and request.user != obj:
#             data.pop('email')
# 
#         return data
# 
#     # Adds extra application related metas.
#     def get_root_meta(self, resource, many):
#         if not self.context.get('add_misc', None):
#             return {}
# 
#         return {
#             'member_centers': sorted(settings.MEMBER_CENTERS),
#             'expense_scopes': sorted(settings.EXPENSE_SCOPES)
#         }
# 
# Path: api_v3/tests/views/support.py
# class ApiTestCase(TestCase):
#     JSON_API_CONTENT_TYPE = 'application/vnd.api+json'
#     def as_jsonapi_payload(self, serializer_class, obj, update={}):


import json
from api_v3.factories import ProfileFactory
from api_v3.serializers import ProfileSerializer
from .support import ApiTestCase, APIClient, reverse
    def test_update_authenticated_not_owned_profile(self):
        self.client.force_authenticate(self.users[0])

        new_data = self.as_jsonapi_payload(
            ProfileSerializer, self.users[1], {'bio': 'Short Bio'})

        response = self.client.patch(
            reverse('profile-detail', args=[self.users[1].id]),
            data=json.dumps(new_data),
            content_type=ApiTestCase.JSON_API_CONTENT_TYPE
        )

        self.assertEqual(response.status_code, 404)

    def test_update_authenticated(self):
        self.client.force_authenticate(self.users[0])

        email = 'ignored@email.address'
        new_data = self.as_jsonapi_payload(
            ProfileSerializer, self.users[0], {
                'email': email, 'bio': 'Short Bio', 'is_staff': True})

        response = self.client.patch(
            reverse('profile-detail', args=[self.users[0].id]),
            data=json.dumps(new_data),
            content_type=ApiTestCase.JSON_API_CONTENT_TYPE
        )

        self.assertEqual(response.status_code, 200)


# Path: api_v3/factories/profile.py
# class ProfileFactory(DjangoModelFactory):
# 
#     class Meta:
#         model = Profile
# 
#     email = Faker('email')
#     first_name = Faker('first_name')
#     last_name = Faker('last_name')
#     bio = Faker('job')
#     last_login = Faker('past_date', start_date='-30d')
# 
# Path: api_v3/serializers/profile.py
# class ProfileSerializer(serializers.ModelSerializer):
# 
#     tickets_count = fields.SerializerMethodField()
# 
#     class Meta:
#         model = Profile
#         read_only_fields = (
#             'id',
#             'email',
#             'first_name',
#             'last_name',
#             'is_staff',
#             'is_superuser',
#             'locale'
#         )
#         fields = (
#             'id',
#             'email',
#             'first_name',
#             'last_name',
#             'is_staff',
#             'is_superuser',
#             'bio',
#             'locale',
#             'tickets_count'
#         )
# 
#     def get_tickets_count(self, obj):
#         if obj.is_superuser:
#             return Ticket.objects.count()
#         else:
#             return Ticket.filter_by_user(obj).count()
# 
#     def to_representation(self, obj):
#         request = self.context.get('request', None)
# 
#         data = super(ProfileSerializer, self).to_representation(obj)
# 
#         if request and request.user and (
#             request.user.is_staff or request.user.is_superuser
#         ):
#             return data
# 
#         # For regular users, make sure others email is not displayed
#         if request and request.user != obj:
#             data.pop('email')
# 
#         return data
# 
#     # Adds extra application related metas.
#     def get_root_meta(self, resource, many):
#         if not self.context.get('add_misc', None):
#             return {}
# 
#         return {
#             'member_centers': sorted(settings.MEMBER_CENTERS),
#             'expense_scopes': sorted(settings.EXPENSE_SCOPES)
#         }
# 
# Path: api_v3/tests/views/support.py
# class ApiTestCase(TestCase):
#     JSON_API_CONTENT_TYPE = 'application/vnd.api+json'
#     def as_jsonapi_payload(self, serializer_class, obj, update={}):


# Path: api_v3/tests/views/test_profiles.py
import json
from api_v3.factories import ProfileFactory
from api_v3.serializers import ProfileSerializer
from .support import ApiTestCase, APIClient, reverse

    def test_update_authenticated_not_owned_profile(self):
        self.client.force_authenticate(self.users[0])

        new_data = self.as_jsonapi_payload(
            ProfileSerializer, self.users[1], {'bio': 'Short Bio'})

        response = self.client.patch(
            reverse('profile-detail', args=[self.users[1].id]),
            data=json.dumps(new_data),
            content_type=ApiTestCase.JSON_API_CONTENT_TYPE
        )

        self.assertEqual(response.status_code, 404)

    def test_update_authenticated(self):
        self.client.force_authenticate(self.users[0])

        email = 'ignored@email.address'
        new_data = self.as_jsonapi_payload(
            ProfileSerializer, self.users[0], {
                'email': email, 'bio': 'Short Bio', 'is_staff': True})

        response = self.client.patch(
            reverse('profile-detail', args=[self.users[0].id]),
            data=json.dumps(new_data),
            content_type=ApiTestCase.JSON_API_CONTENT_TYPE
        )

        self.assertEqual(response.status_code, 200)


        data = json.loads(response.content)['data']