Beispiel #1
0
class ViewTestCase(APITestCase):
    """Test suite for the api views."""

    def setUp(self):
        """Define the test variables."""
        self.channel = Channel(name='test_channel')
        self.channel.save()
        self.categories_tree = {
            'Games': {
                'XBOX One': ['Console', 'Acessories', 'Games'],
                'XBOX 360': None,
                'Playstation 4': None
            },
            'Books': {
                'National Literature': ['Science Fiction', 'Fiction Fantastic'],
                'Foreign Language': None
            }
        }
        self.categories = []
        self.create_categories_recursive(self.categories_tree)
        self.responses = {
            'list_channels': self.client.get('/channels/'),
            'list_channel_categories': self.client.get('/channels/%s/' % self.channel.uuid),
            'list_category_relcategories': self.client.get('/categories/%s/' % (self.categories[1].uuid))
        }

    def create_categories_recursive(self, categories, parent=None):
        for name in categories:
            category = Category(name=name, parent=parent, channel=self.channel)
            category.save()
            self.categories.append(category)
            if isinstance(categories, dict):
                subcategories = categories[name]
                if subcategories:
                    self.create_categories_recursive(subcategories, category)

    def test_api_can_list_channels(self):
        """Test the api can list channels."""
        response = self.responses['list_channels']
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertContains(response, self.channel.name)

    def test_api_can_list_channel_categories(self):
        """Test the api can list a channel's categories."""
        response = self.responses['list_channel_categories']
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertContains(response, self.categories[0].name)

    def test_api_can_list_category_relcategories(self):
        """Test the api can return a category with parent and subcategories."""
        response = self.responses['list_category_relcategories']
        self.assertContains(response, self.categories[0].name)
        self.assertContains(response, self.categories[2].name)
Beispiel #2
0
    def test_call_delete(self):
        '''
        Test deleting of a registration of a device from a channel
        '''

        u = User()
        u.username = "******"
        u.first_name = "sample_del"
        u.email = "*****@*****.**"
        u.set_password("123")
        u.save()

        ch1 = Channel()

        ch1.owner = u
        ch1.name = "DelSub"
        ch1.image = 'http://www.google.com'
        ch1.description = "A channel description"
        ch1.kind = PUBLIC
        ch1.hidden = False
        ch1.subscriptions = 0

        ch1.save()

        sub1 = Subscriber()
        sub1.sub_type = 'type2'
        sub1.token = 'token2'
        sub1.device_id = 'devid6'
        sub1.save()

        resp = ask_subscribe_channel(ch1, sub1.device_id)

        self.assertEqual(resp, SubscribeResponse.SUBSCRIBED)
        test_user = User.objects.create_superuser('test_user',
                                                  '*****@*****.**',
                                                  'password')
        self.client.login(username='******', password='******')

        response = self.client.delete(
            reverse('browser-get-registration',
                    kwargs={
                        'device_id': sub1.device_id,
                        'channel_name': ch1.name
                    }))

        self.assertEqual(response.status_code, 200)

        channels = SubscriberManager().get_device_subscriptions(sub1.device_id)
        sub_channel = next((x for x in channels if x == ch1.name.lower()),
                           None)

        self.assertIsNone(sub_channel)
Beispiel #3
0
    def test_call_register_channel(self):
        '''
        Test registration to a channel
        '''

        u = User()
        u.username = "******"
        u.first_name = "sample_post"
        u.email = "*****@*****.**"
        u.set_password("123")
        u.save()

        ch1 = Channel()

        ch1.owner = u
        ch1.name = "PostSub"
        ch1.image = 'http://www.google.com'
        ch1.description = "A channel description"
        ch1.kind = PUBLIC
        ch1.hidden = False
        ch1.subscriptions = 0

        ch1.save()

        sub1 = Subscriber()
        sub1.sub_type = 'type2'
        sub1.token = 'token2'
        sub1.device_id = 'devid5'
        sub1.save()

        resp = ask_subscribe_channel(ch1, sub1.device_id)

        self.assertEqual(resp, SubscribeResponse.SUBSCRIBED)
        test_user = User.objects.create_superuser('test_user',
                                                  '*****@*****.**',
                                                  'password')
        self.client.login(username='******', password='******')

        data = {
            'channel': ch1.name,
            'token': sub1.token,
            'browser': 'chrome',
            'device_id': sub1.device_id
        }

        response = self.client.post(reverse('browser-registration'),
                                    json.dumps(data), sub1.token)

        self.assertTrue(response.status_code, 200)
Beispiel #4
0
 def post(self, request):
     # pass filled out HTML-Form from View to RegisterForm()
     form = ChannelForm(request.POST)
     if form.is_valid():
         # create a User account
         print(form.cleaned_data['channel_name'])
         channel_name = form.cleaned_data['channel_name']
         user = request.user
         subscribers = 0
         new_channel = Channel(channel_name=channel_name,
                               user=user,
                               subscribers=subscribers)
         new_channel.save()
         return HttpResponseRedirect('/')
     return HttpResponse('This is Register view. POST Request.')
Beispiel #5
0
 def handle(self, channel_name, categories_filename, *args, **options):
     if not isfile(categories_filename):
         raise CommandError('The specified filename does not exist.')
     try:
         channel = Channel.objects.get(name=channel_name)
         self.stdout.write('Overwriting channel %s...' % channel_name)
         Category.objects.filter(channel=channel).delete()
     except Channel.DoesNotExist:
         self.stdout.write('Creating channel %s...' % channel_name)
         channel = Channel(name=channel_name)
         channel.save()
     categories_paths = []
     created_categories = []
     with open(categories_filename, 'rt', encoding='utf8') as inp:
         # TODO Check the parent category is in the db if its not found in created_categories
         # (may happen if the file is not sorted)
         for line in inp:
             category_path = line.strip().replace('\n', '')
             if category_path != '':
                 categories_paths.append(category_path)
                 last_slash_pos = category_path.rfind('/')
                 parent = None
                 parent_path = ''
                 parent_name = ''
                 if last_slash_pos != -1:
                     parent_path = category_path[:last_slash_pos].strip()
                     parent_last_slash_pos = parent_path.rfind('/')
                     parent_name = parent_path[parent_last_slash_pos +
                                               1:].strip()
                     for created_category in reversed(created_categories):
                         if created_category.name == parent_name:
                             parent = created_category
                             break
                     if not parent:
                         self.stderr.write(
                             'Could not find the parent of category %s (check the file is sorted).'
                             % category_path)
                 category_name = category_path[last_slash_pos + 1:].strip()
                 self.stdout.write('Creating category %s...' %
                                   category_path)
                 category = Category(name=category_name,
                                     channel=channel,
                                     parent=parent)
                 category.save()
                 created_categories.append(category)
     n_categories = len(created_categories)
     self.stdout.write('Channel %s was imported with %i categories.' %
                       (channel_name, n_categories))
Beispiel #6
0
    def test_call_register_channel(self):
        '''
        Test registration to a channel
        '''

        u = User()
        u.username = "******"
        u.first_name = "sample_post"
        u.email = "*****@*****.**"
        u.set_password("123")
        u.save()

        ch1 = Channel()

        ch1.owner = u
        ch1.name = "PostSub"
        ch1.image = 'http://www.google.com'
        ch1.description = "A channel description"
        ch1.kind = PUBLIC
        ch1.hidden = False
        ch1.subscriptions = 0

        ch1.save()

        sub1 = Subscriber()
        sub1.sub_type = 'type2'
        sub1.token = 'token2'
        sub1.device_id = 'devid5'
        sub1.save()


        resp = ask_subscribe_channel(ch1, sub1.device_id)

        self.assertEqual(resp, SubscribeResponse.SUBSCRIBED)
        test_user = User.objects.create_superuser('test_user', '*****@*****.**', 'password')
        self.client.login(username='******', password='******')

        data = {
                'channel' : ch1.name,
                'token' : sub1.token,
                'browser' : 'chrome',
                'device_id' : sub1.device_id
        }

        response = self.client.post(reverse('browser-registration'), json.dumps(data), sub1.token)

        self.assertTrue(response.status_code, 200)
Beispiel #7
0
    def test_call_delete(self):
        '''
        Test deleting of a registration of a device from a channel
        '''

        u = User()
        u.username = "******"
        u.first_name = "sample_del"
        u.email = "*****@*****.**"
        u.set_password("123")
        u.save()

        ch1 = Channel()

        ch1.owner = u
        ch1.name = "DelSub"
        ch1.image = 'http://www.google.com'
        ch1.description = "A channel description"
        ch1.kind = PUBLIC
        ch1.hidden = False
        ch1.subscriptions = 0

        ch1.save()

        sub1 = Subscriber()
        sub1.sub_type = 'type2'
        sub1.token = 'token2'
        sub1.device_id = 'devid6'
        sub1.save()


        resp = ask_subscribe_channel(ch1, sub1.device_id)

        self.assertEqual(resp, SubscribeResponse.SUBSCRIBED)
        test_user = User.objects.create_superuser('test_user', '*****@*****.**', 'password')
        self.client.login(username='******', password='******')

        response = self.client.delete(reverse('browser-get-registration', kwargs={'device_id': sub1.device_id,
                                                                       'channel_name' : ch1.name}))

        self.assertEqual(response.status_code, 200)

        channels = SubscriberManager().get_device_subscriptions(sub1.device_id)
        sub_channel = next((x for x in channels if x == ch1.name.lower()), None)

        self.assertIsNone(sub_channel)
Beispiel #8
0
    def test_call_get(self):
        '''
        Test check if a device is registered to a channel
        '''

        u = User()
        u.username = "******"
        u.first_name = "Sample_un"
        u.email = "*****@*****.**"
        u.set_password("123")
        u.save()

        ch1 = Channel()

        ch1.owner = u
        ch1.name = "GetSub"
        ch1.image = 'http://www.google.com'
        ch1.description = "A channel description"
        ch1.kind = PUBLIC
        ch1.hidden = False
        ch1.subscriptions = 0

        ch1.save()

        sub1 = Subscriber()
        sub1.sub_type = 'type2'
        sub1.token = 'token2'
        sub1.device_id = 'devid4'
        sub1.save()

        resp = ask_subscribe_channel(ch1, 'devid4')

        self.assertEqual(resp, SubscribeResponse.SUBSCRIBED)
        test_user = User.objects.create_superuser('test_user',
                                                  '*****@*****.**',
                                                  'password')
        self.client.login(username='******', password='******')

        response = self.client.get(
            reverse('browser-get-registration',
                    kwargs={
                        'device_id': 'devid4',
                        'channel_name': 'GetSub'
                    }))

        self.assertTrue(response.status_code, 200)
Beispiel #9
0
    def test_call_get(self):
        '''
        Test check if a device is registered to a channel
        '''

        u = User()
        u.username = "******"
        u.first_name = "Sample_un"
        u.email = "*****@*****.**"
        u.set_password("123")
        u.save()

        ch1 = Channel()

        ch1.owner = u
        ch1.name = "GetSub"
        ch1.image = 'http://www.google.com'
        ch1.description = "A channel description"
        ch1.kind = PUBLIC
        ch1.hidden = False
        ch1.subscriptions = 0

        ch1.save()

        sub1 = Subscriber()
        sub1.sub_type = 'type2'
        sub1.token = 'token2'
        sub1.device_id = 'devid4'
        sub1.save()


        resp = ask_subscribe_channel(ch1, 'devid4')

        self.assertEqual(resp, SubscribeResponse.SUBSCRIBED)
        test_user = User.objects.create_superuser('test_user', '*****@*****.**', 'password')
        self.client.login(username='******', password='******')

        response = self.client.get(reverse('browser-get-registration', kwargs={'device_id': 'devid4', 'channel_name' : 'GetSub'}) )
        
        self.assertTrue(response.status_code, 200)
Beispiel #10
0
class ModelTestCase(TestCase):
    """Test suite for the channel and category models."""
    def setUp(self):
        """Define the test variables."""
        self.channel_name = "test"
        self.category_name = 'Games'
        self.subcategories_names = ['XBOX One', 'XBOX 360', 'Playstation 4']
        self.channel = Channel(name=self.channel_name)
        self.category = Category(name=self.category_name)
        self.subcategories = [
            Category(name=category_name)
            for category_name in self.subcategories_names
        ]

    def test_model_can_create_a_channel(self):
        """Test the channel model can create a channel."""
        old_count = Channel.objects.count()
        self.channel.save()
        new_count = Channel.objects.count()
        self.assertNotEqual(old_count, new_count)

    def test_model_can_create_categories(self):
        """Test the category model can create a category."""
        self.channel.save()
        self.category.channel = self.channel
        self.category.save()
        old_count = Category.objects.count()
        for category in self.subcategories:
            category.parent = self.category
            category.channel = self.channel
            category.save()
        new_count = Category.objects.count()
        self.assertNotEqual(old_count, new_count)

    def test_model_can_overwrite_a_channel_categories(self):
        """Test the category model can create a category."""
        self.channel.save()
        self.category.channel = self.channel
        self.category.save()
        old_count = Category.objects.count()
        for category in self.subcategories:
            category.parent = self.category
            category.channel = self.channel
            category.save()
        new_count = Category.objects.count()
        self.assertNotEqual(old_count, new_count)
        old_count = new_count
        Category.objects.filter(channel=self.channel).delete()
        new_count = Category.objects.count()
        self.assertNotEqual(old_count, new_count)