from graphql_relay import to_global_id

from core.tests.base_test import SkillboxTestCase
from core.tests.helpers import GQLResult
from rooms.factories import RoomFactory


class TestRoomVisibilityMutationTestCase(SkillboxTestCase):
    def setUp(self) -> None:
        self.createDefault()
        self.room = RoomFactory(school_class=self.school_class)
        self.room_id = to_global_id('RoomNode', self.room.id)
        self.mutation = """
mutation UpdateRoomVisibility($input: UpdateRoomVisibilityInput!) {
  updateRoomVisibility(input: $input) {
    success
    room {
      id
      restricted
    }
  }
}        
        """
        self.query = """
query RoomQuery ($slug: String!) {
    room(slug: $slug) {
        restricted
    }
}
        """

    def test_restricted_query(self):
        res = self.get_client().execute(self.query, variables={
            'slug': self.room.slug
        })
        result = GQLResult(res)
        self.assertIsNone(result.errors)
        self.assertFalse(result.data.get('room').get('restricted'))

    def test_successful_mutation(self):
        res = self.get_client().execute(self.mutation, variables={
            'input': {
                'id': self.room_id,
                'restricted': True
            }
        })
        result = GQLResult(res)
        self.assertIsNone(result.errors)
        self.assertEqual(result.data.get('updateRoomVisibility').get('room').get('restricted'), True)

    def test_permission_denied(self):
        res = self.get_client(self.student1).execute(self.mutation, variables={
            'input': {
                'id': self.room_id,
                'restricted': True
            }
        })
        result = GQLResult(res)
        self.assertIsNotNone(result.errors)

        res = self.get_client(self.teacher2).execute(self.mutation, variables={
            'input': {
                'id': self.room_id,
                'restricted': True
            }
        })
        result = GQLResult(res)
        self.assertIsNotNone(result.errors)


