from django.test import TestCase, RequestFactory
from graphene.test import Client
from graphql_relay import to_global_id

from api.schema import schema
from core.factories import UserFactory
from rooms.factories import RoomEntryFactory, RoomFactory
from rooms.models import RoomEntry
from users.factories import SchoolClassFactory


class RoomEntryMutationsTestCase(TestCase):
    def setUp(self):
        self.user = UserFactory(username='aschi')
        self.another_user = UserFactory(username='pesche')
        self.yet_another_user = UserFactory(username='hansueli')
        s = SchoolClassFactory(users=[self.user, self.another_user])
        s2 = SchoolClassFactory(users=[self.yet_another_user])
        self.room_entry = RoomEntryFactory(author=self.user, room=RoomFactory(school_class=s))

        request = RequestFactory().get('/')
        request.user = self.user
        self.client = Client(schema=schema, context_value=request)

    def test_delete_room_entry(self):
        self.assertEqual(RoomEntry.objects.count(), 1)

        mutation = '''
        mutation DeleteRoomEntry($input: DeleteRoomEntryInput!) {
              deleteRoomEntry(input: $input) {
                success
                errors
              }
            }
        '''

        result = self.client.execute(mutation, variables={
            'input': {
                'id': to_global_id('RoomEntryNode', self.room_entry.pk)
            }
        })
        self.assertIsNone(result.get('errors'))
        self.assertEqual(RoomEntry.objects.count(), 0)

    def test_delete_room_not_owner(self):
        self.assertEqual(RoomEntry.objects.count(), 1)
        mutation = '''
                mutation DeleteRoomEntry($input: DeleteRoomEntryInput!) {
                      deleteRoomEntry(input: $input) {
                        success
                        errors
                      }
                    }
                '''

        request = RequestFactory().get('/')
        request.user = self.another_user
        client = Client(schema=schema, context_value=request)

        result = client.execute(mutation, variables={
            'input': {
                'id': to_global_id('RoomEntryNode', self.room_entry.pk)
            }
        })

        self.assertIsNotNone(result.get('errors'))
        self.assertEqual(RoomEntry.objects.count(), 1)

    def test_update_room_entry_not_owner_but_same_class(self):
        self.assertEqual(RoomEntry.objects.count(), 1)
        mutation = '''
                mutation UpdateRoomEntry($input: UpdateRoomEntryInput!){
                  updateRoomEntry(input: $input) {
                    roomEntry {
                      title
                      author {
                        firstName
                      }
                    }
                    errors
                  }
                }
                '''

        request = RequestFactory().get('/')
        request.user = self.another_user
        client = Client(schema=schema, context_value=request)

        new_title = 'new title, Alte!'

        result = client.execute(mutation, variables={
            'input': {
                'roomEntry': {
                    'id': to_global_id('RoomEntryNode', self.room_entry.pk),
                    'title': new_title
                }
            }
        })

        entry = RoomEntry.objects.get(pk=self.room_entry.pk)
        self.assertIsNotNone(result.get('errors'))
        self.assertEqual(entry.title, self.room_entry.title)

    def test_update_room_entry_not_owner_from_other_class(self):
        self.assertEqual(RoomEntry.objects.count(), 1)
        mutation = '''
                mutation UpdateRoomEntry($input: UpdateRoomEntryInput!){
                  updateRoomEntry(input: $input) {
                    roomEntry {
                      title
                      author {
                        firstName
                      }
                    }
                    errors
                  }
                }
                '''

        request = RequestFactory().get('/')
        request.user = self.yet_another_user
        client = Client(schema=schema, context_value=request)

        new_title = 'new title, Alte!'

        result = client.execute(mutation, variables={
            'input': {
                'roomEntry': {
                    'id': to_global_id('RoomEntryNode', self.room_entry.pk),
                    'title': new_title
                }
            }
        })

        entry = RoomEntry.objects.get(pk=self.room_entry.pk)
        self.assertIsNotNone(result.get('errors'))
        self.assertEqual(entry.title, self.room_entry.title)
