text: from __future__ import absolute_import
  
  import uuid
  
  from django.core.urlresolvers import reverse
  
  from contentcuration import models
  from contentcuration.tests.base import StudioAPITestCase
  from contentcuration.tests import testdata
  from contentcuration.viewsets.sync.constants import CHANNEL
  from contentcuration.viewsets.sync.utils import generate_create_event
  from contentcuration.viewsets.sync.utils import generate_update_event
  from contentcuration.viewsets.sync.utils import generate_delete_event
  
  
  class SyncTestCase(StudioAPITestCase):
      @property
      def sync_url(self):
          return reverse("sync")
  
      @property
      def channel_metadata(self):
          return {
              "name": "Aron's cool channel",
              "id": uuid.uuid4().hex,
              "description": "coolest channel this side of the Pacific",
          }
  
      def test_create_channel(self):
          user = testdata.user()
          self.client.force_authenticate(user=user)
          channel = self.channel_metadata
          response = self.client.post(
              self.sync_url,
              [generate_create_event(channel["id"], CHANNEL, channel)],
              format="json",
          )
          self.assertEqual(response.status_code, 200, response.content)
          try:
              models.Channel.objects.get(id=channel["id"])
          except models.Channel.DoesNotExist:
              self.fail("Channel was not created")
  
      def test_create_channels(self):
          user = testdata.user()
          self.client.force_authenticate(user=user)
          channel1 = self.channel_metadata
          channel2 = self.channel_metadata
          response = self.client.post(
              self.sync_url,
              [
                  generate_create_event(channel1["id"], CHANNEL, channel1),
                  generate_create_event(channel2["id"], CHANNEL, channel2),
              ],
              format="json",
          )
          self.assertEqual(response.status_code, 200, response.content)
          try:
              models.Channel.objects.get(id=channel1["id"])
          except models.Channel.DoesNotExist:
              self.fail("Channel 1 was not created")
  
          try:
              models.Channel.objects.get(id=channel2["id"])
          except models.Channel.DoesNotExist:
              self.fail("Channel 2 was not created")
  
      def test_update_channel(self):
          user = testdata.user()
          channel = models.Channel.objects.create(**self.channel_metadata)
          channel.editors.add(user)
          new_name = "This is not the old name"
  
          self.client.force_authenticate(user=user)
          response = self.client.post(
              self.sync_url,
              [generate_update_event(channel.id, CHANNEL, {"name": new_name})],
              format="json",
          )
          self.assertEqual(response.status_code, 200, response.content)
          self.assertEqual(models.Channel.objects.get(id=channel.id).name, new_name)
  
      def test_update_channels(self):
          user = testdata.user()
          channel1 = models.Channel.objects.create(**self.channel_metadata)
          channel1.editors.add(user)
          channel2 = models.Channel.objects.create(**self.channel_metadata)
          channel2.editors.add(user)
          new_name = "This is not the old name"
  
          self.client.force_authenticate(user=user)
          response = self.client.post(
              self.sync_url,
              [
                  generate_update_event(channel1.id, CHANNEL, {"name": new_name}),
                  generate_update_event(channel2.id, CHANNEL, {"name": new_name}),
              ],
              format="json",
          )
          self.assertEqual(response.status_code, 200, response.content)
          self.assertEqual(models.Channel.objects.get(id=channel1.id).name, new_name)
          self.assertEqual(models.Channel.objects.get(id=channel2.id).name, new_name)
  
      def test_delete_channel(self):
          user = testdata.user()
          channel = models.Channel.objects.create(**self.channel_metadata)
          channel.editors.add(user)
  
          self.client.force_authenticate(user=user)
          response = self.client.post(
              self.sync_url, [generate_delete_event(channel.id, CHANNEL)], format="json",
          )
          self.assertEqual(response.status_code, 200, response.content)
          try:
              models.Channel.objects.get(id=channel.id)
              self.fail("Channel was not deleted")
          except models.Channel.DoesNotExist:
              pass
  
      def test_delete_channels(self):
          user = testdata.user()
          channel1 = models.Channel.objects.create(**self.channel_metadata)
          channel1.editors.add(user)
  
          channel2 = models.Channel.objects.create(**self.channel_metadata)
          channel2.editors.add(user)
  
          self.client.force_authenticate(user=user)
          response = self.client.post(
              self.sync_url,
              [
                  generate_delete_event(channel1.id, CHANNEL),
                  generate_delete_event(channel2.id, CHANNEL),
              ],
              format="json",
          )
          self.assertEqual(response.status_code, 200, response.content)
          try:
              models.Channel.objects.get(id=channel1.id)
              self.fail("Channel 1 was not deleted")
          except models.Channel.DoesNotExist:
              pass
  
          try:
              models.Channel.objects.get(id=channel2.id)
              self.fail("Channel 2 was not deleted")
          except models.Channel.DoesNotExist:
              pass
  
  
  class CRUDTestCase(StudioAPITestCase):
      @property
      def channel_metadata(self):
          return {
              "name": "Aron's cool channel",
              "id": uuid.uuid4().hex,
              "description": "coolest channel this side of the Pacific",
          }
  
      def test_create_channel(self):
          user = testdata.user()
          self.client.force_authenticate(user=user)
          channel = self.channel_metadata
          response = self.client.post(reverse("channel-list"), channel, format="json",)
          self.assertEqual(response.status_code, 201, response.content)
          try:
              models.Channel.objects.get(id=channel["id"])
          except models.Channel.DoesNotExist:
              self.fail("Channel was not created")
  
      def test_update_channel(self):
          user = testdata.user()
          channel = models.Channel.objects.create(**self.channel_metadata)
          channel.editors.add(user)
          new_name = "This is not the old name"
  
          self.client.force_authenticate(user=user)
          response = self.client.patch(
              reverse("channel-detail", kwargs={"pk": channel.id}),
              {"name": new_name},
              format="json",
          )
          self.assertEqual(response.status_code, 200, response.content)
          self.assertEqual(models.Channel.objects.get(id=channel.id).name, new_name)
  
      def test_delete_channel(self):
          user = testdata.user()
          channel = models.Channel.objects.create(**self.channel_metadata)
          channel.editors.add(user)
  
          self.client.force_authenticate(user=user)
          response = self.client.delete(
              reverse("channel-detail", kwargs={"pk": channel.id})
          )
          self.assertEqual(response.status_code, 204, response.content)
          try:
              models.Channel.objects.get(id=channel.id)
              self.fail("Channel was not deleted")
          except models.Channel.DoesNotExist:
              pass
  