# -*- coding: utf-8 -*-
#
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
#
# Created on 2019-08-07
# @author: chrigu <christian.cueni@iterativ.ch>
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 RoomFactory, ModuleRoomSlugFactory
from users.factories import SchoolClassFactory


class AdminRoomQueryPermission(TestCase):

    def setUp(self):
        self.user = UserFactory(username='aschi')
        self.another_user = UserFactory(username='pesche')
        self.sc1 = SchoolClassFactory(users=[self.user])
        sc2 = SchoolClassFactory(users=[self.another_user])
        self.room1 = RoomFactory(school_class=self.sc1)
        self.room2 = RoomFactory(school_class=sc2)

        self.module_room_slug = ModuleRoomSlugFactory(title='some title')

        self.sc1_id = to_global_id('SchoolClass', self.sc1.pk)
        self.sc2_id = to_global_id('SchoolClass', sc2.pk)

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

        self.query = '''
            query ModuleRoomEntriesQuery($slug: String, $classId: ID!) {
              moduleRoom(slug: $slug, classId: $classId) {
                title
              }
            }
        '''

    def test_should_return_none_if_slug_does_not_exist(self):

        result = self.client.execute(self.query, variables={
            'slug': 'no-slug',
            'classId': 'norealId'
        })
        self.assertIsNone(result.get('errors'))
        self.assertIsNone(result.get('data').get('moduleRoom'))

    def test_should_return_none_if_class_id_does_not_exist(self):

        result = self.client.execute(self.query, variables={
            'slug': 'no-slug',
            'classId': 'norealId'
        })
        self.assertIsNone(result.get('errors'))
        self.assertIsNone(result.get('data').get('moduleRoom'))

    def test_user_should_not_be_able_to_create_room_for_other_class(self):

        result = self.client.execute(self.query, variables={
            'slug': self.module_room_slug.slug,
            'classId': self.sc2_id
        })

        self.assertIsNone(result.get('errors'))
        self.assertIsNone(result.get('data').get('moduleRoom'))

    def test_should_create_room_if_none_exists(self):

        result = self.client.execute(self.query, variables={
            'slug': self.module_room_slug.slug,
            'classId': self.sc1_id
        })

        self.assertIsNone(result.get('errors'))
        self.assertEqual(result.get('data').get('moduleRoom').get('title'), self.module_room_slug.title)

    def test_should_return_room_if_one_exists(self):

        existing_room = RoomFactory(school_class=self.sc1, user_created=False)
        admin_slug = ModuleRoomSlugFactory(slug=existing_room.slug, title=existing_room.title)

        result = self.client.execute(self.query, variables={
            'slug': admin_slug.slug,
            'classId': self.sc1_id
        })

        self.assertIsNone(result.get('errors'))
        self.assertEqual(result.get('data').get('moduleRoom').get('title'), existing_room.title)
