Пример #1
0
    def test_upload_view(self):
        """upload view renders on get"""
        handler_link = reverse('misago:usercp_upload_avatar_handler')

        response = self.client.get(handler_link)
        self.assertEqual(response.status_code, 405)

        ajax_header = {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
        response = self.client.post(handler_link,
                                    data={'baww': 'nope'},
                                    **ajax_header)

        self.assertEqual(response.status_code, 406)
        self.assertIn('No file was sent.', response.content)

        with open('%s/%s' % (settings.MEDIA_ROOT, 'misago.png')) as avatar:
            response = self.client.post(handler_link,
                                        data={'new-avatar': avatar},
                                        **ajax_header)
            self.assertEqual(response.status_code, 200)

            avatar_dir = store.get_existing_avatars_dir(self.user)
            avatar = path('%s/%s_tmp.png' % (avatar_dir, self.user.pk))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())
Пример #2
0
    def test_crop_view(self):
        """avatar gets cropped"""
        with open('%s/%s' % (settings.MEDIA_ROOT, 'misago.png')) as avatar:
            handler_link = reverse('misago:usercp_upload_avatar_handler')
            ajax_header = {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
            response = self.client.post(handler_link,
                                        data={'new-avatar': avatar},
                                        **ajax_header)
            self.assertEqual(response.status_code, 200)

            crop_link = reverse('misago:usercp_crop_new_avatar')
            response = self.client.post(crop_link, data={'crop': '1245'})
            self.assertEqual(response.status_code, 200)

            test_crop = '619,201,150,150,0,0,150,150'
            response = self.client.post(crop_link, data={'crop': test_crop})
            self.assertEqual(response.status_code, 302)

            avatar_dir = store.get_existing_avatars_dir(self.user)
            avatar = path('%s/%s_tmp.png' % (avatar_dir, self.user.pk))
            self.assertFalse(avatar.exists())

            avatar = path('%s/%s_org.png' % (avatar_dir, self.user.pk))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())
Пример #3
0
    def test_store(self):
        """store successfully stores and deletes avatar"""
        User = get_user_model()
        test_user = User.objects.create_user('Bob', '*****@*****.**', 'pass123')

        test_image = Image.new("RGBA", (100, 100), 0)
        store.store_avatar(test_user, test_image)

        # Assert that avatar was stored
        avatar_dir = store.get_existing_avatars_dir(test_user)
        for size in settings.MISAGO_AVATARS_SIZES:
            avatar = Path('%s/%s_%s.png' % (avatar_dir, test_user.pk, size))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())

        # Delete avatar and assert its gone
        store.delete_avatar(test_user)
        for size in settings.MISAGO_AVATARS_SIZES:
            avatar = Path('%s/%s_%s.png' % (avatar_dir, test_user.pk, size))
            self.assertFalse(avatar.exists())

        # Override new avatar and test that it was changed
        store.store_avatar(test_user, test_image)
        store.store_new_avatar(test_user, test_image)
        for size in settings.MISAGO_AVATARS_SIZES:
            avatar = Path('%s/%s_%s.png' % (avatar_dir, test_user.pk, size))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())

        # Delete avatar
        store.delete_avatar(test_user)
Пример #4
0
    def test_store(self):
        """store successfully stores and deletes avatar"""
        User = get_user_model()
        test_user = User.objects.create_user('Bob', '*****@*****.**', 'pass123')

        test_image = Image.new("RGBA", (100, 100), 0)
        store.store_avatar(test_user, test_image)

        # Assert that avatar was stored
        avatar_dir = store.get_existing_avatars_dir(test_user)
        for size in settings.MISAGO_AVATARS_SIZES:
            avatar = path('%s/%s_%s.png' % (avatar_dir, test_user.pk, size))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())

        # Delete avatar and assert its gone
        store.delete_avatar(test_user)
        for size in settings.MISAGO_AVATARS_SIZES:
            avatar = path('%s/%s_%s.png' % (avatar_dir, test_user.pk, size))
            self.assertFalse(avatar.exists())

        # Override new avatar and test that it was changed
        store.store_avatar(test_user, test_image)
        store.store_new_avatar(test_user, test_image)
        for size in settings.MISAGO_AVATARS_SIZES:
            avatar = path('%s/%s_%s.png' % (avatar_dir, test_user.pk, size))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())

        # Delete avatar
        store.delete_avatar(test_user)
Пример #5
0
    def test_crop_view(self):
        """avatar gets cropped"""
        with open('%s/%s' % (settings.MEDIA_ROOT, 'misago.png')) as avatar:
            handler_link = reverse('misago:usercp_upload_avatar_handler')
            ajax_header = {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
            response = self.client.post(handler_link,
                                        data={'new-avatar': avatar},
                                        **ajax_header)
            self.assertEqual(response.status_code, 200)

            crop_link = reverse('misago:usercp_crop_new_avatar')
            response = self.client.post(crop_link, data={'crop': '1245'})
            self.assertEqual(response.status_code, 200)

            test_crop = '619,201,150,150,0,0,150,150'
            response = self.client.post(crop_link, data={'crop': test_crop})
            self.assertEqual(response.status_code, 302)

            avatar_dir = store.get_existing_avatars_dir(self.user)
            avatar = path('%s/%s_tmp.png' % (avatar_dir, self.user.pk))
            self.assertFalse(avatar.exists())

            avatar = path('%s/%s_org.png' % (avatar_dir, self.user.pk))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())
Пример #6
0
    def test_upload_view(self):
        """upload view renders on get"""
        handler_link = reverse('misago:usercp_upload_avatar_handler')

        response = self.client.get(handler_link)
        self.assertEqual(response.status_code, 405)

        ajax_header = {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
        response = self.client.post(handler_link,
                                    data={'baww': 'nope'},
                                    **ajax_header)

        self.assertEqual(response.status_code, 406)
        self.assertIn('No file was sent.', response.content)

        with open('%s/%s' % (settings.MEDIA_ROOT, 'misago.png')) as avatar:
            response = self.client.post(handler_link,
                                        data={'new-avatar': avatar},
                                        **ajax_header)
            self.assertEqual(response.status_code, 200)

            avatar_dir = store.get_existing_avatars_dir(self.user)
            avatar = path('%s/%s_tmp.png' % (avatar_dir, self.user.pk))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())
Пример #7
0
    def test_store(self):
        """store successfully stores and deletes avatar"""
        User = get_user_model()
        test_user = User.objects.create_user('Bob', '*****@*****.**', 'pass123')

        test_image = Image.new("RGBA", (100, 100), 0)
        store.store_avatar(test_user, test_image)

        # Assert that avatar was stored
        avatar_dir = store.get_existing_avatars_dir(test_user)
        for size in settings.MISAGO_AVATARS_SIZES:
            avatar = Path('%s/%s_%s.png' % (avatar_dir, test_user.pk, size))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())

        # Delete avatar and assert its gone
        store.delete_avatar(test_user)
        for size in settings.MISAGO_AVATARS_SIZES:
            avatar = Path('%s/%s_%s.png' % (avatar_dir, test_user.pk, size))
            self.assertFalse(avatar.exists())

        # Override new avatar and test that it was changed
        store.store_avatar(test_user, test_image)
        store.store_new_avatar(test_user, test_image)
        for size in settings.MISAGO_AVATARS_SIZES:
            avatar = Path('%s/%s_%s.png' % (avatar_dir, test_user.pk, size))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())

        # Compute avatar hash
        test_user.avatar_hash = store.get_avatar_hash(test_user)
        self.assertEqual(len(test_user.avatar_hash), 8)
        test_user.save(update_fields=['avatar_hash'])

        # Get avatar tokens
        tokens = store.get_user_avatar_tokens(test_user)
        self.assertEqual(tokens[tokens['org']], 'org')
        self.assertEqual(tokens[tokens['tmp']], 'tmp')

        # Delete avatar
        store.delete_avatar(test_user)
        for size in settings.MISAGO_AVATARS_SIZES:
            avatar = Path('%s/%s_%s.png' % (avatar_dir, test_user.pk, size))
            self.assertFalse(avatar.exists())
Пример #8
0
    def test_store(self):
        """store successfully stores and deletes avatar"""
        User = get_user_model()
        test_user = User.objects.create_user('Bob', '*****@*****.**', 'pass123')

        test_image = Image.new("RGBA", (100, 100), 0)
        store.store_avatar(test_user, test_image)

        # Assert that avatar was stored
        avatar_dir = store.get_existing_avatars_dir(test_user)
        for size in settings.MISAGO_AVATARS_SIZES:
            avatar = Path('%s/%s_%s.png' % (avatar_dir, test_user.pk, size))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())

        # Delete avatar and assert its gone
        store.delete_avatar(test_user)
        for size in settings.MISAGO_AVATARS_SIZES:
            avatar = Path('%s/%s_%s.png' % (avatar_dir, test_user.pk, size))
            self.assertFalse(avatar.exists())

        # Override new avatar and test that it was changed
        store.store_avatar(test_user, test_image)
        store.store_new_avatar(test_user, test_image)
        for size in settings.MISAGO_AVATARS_SIZES:
            avatar = Path('%s/%s_%s.png' % (avatar_dir, test_user.pk, size))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())

        # Compute avatar hash
        test_user.avatar_hash = store.get_avatar_hash(test_user)
        self.assertEqual(len(test_user.avatar_hash), 8)
        test_user.save(update_fields=['avatar_hash'])

        # Get avatar tokens
        tokens = store.get_user_avatar_tokens(test_user)
        self.assertEqual(tokens[tokens['org']], 'org')
        self.assertEqual(tokens[tokens['tmp']], 'tmp')

        # Delete avatar
        store.delete_avatar(test_user)
        for size in settings.MISAGO_AVATARS_SIZES:
            avatar = Path('%s/%s_%s.png' % (avatar_dir, test_user.pk, size))
            self.assertFalse(avatar.exists())
Пример #9
0
    def test_avatar_upload_and_crop(self):
        """avatar can be uploaded and cropped"""
        response = self.client.post(self.link, data={'avatar': 'generated'})
        self.assertEqual(response.status_code, 200)

        response = self.client.post(self.link, data={'avatar': 'upload'})
        self.assertEqual(response.status_code, 400)
        self.assertIn('No file was sent.', response.content)

        avatar_path = (settings.MEDIA_ROOT, 'avatars', 'blank.png')
        with open('/'.join(avatar_path)) as avatar:
            response = self.client.post(self.link,
                                        data={
                                            'avatar': 'upload',
                                            'image': avatar
                                        })
            self.assertEqual(response.status_code, 200)

            response_json = json.loads(response.content)
            self.assertTrue(response_json['options']['crop_tmp'])

            avatar_dir = store.get_existing_avatars_dir(self.user)
            avatar = Path('%s/%s_tmp.png' % (avatar_dir, self.user.pk))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())

            tmp_avatar_kwargs = {
                'secret': response_json['options']['crop_tmp']['secret'],
                'hash': response_json['avatar_hash'],
                'user_id': self.user.pk
            }
            tmp_avatar_path = reverse('misago:user_avatar_source',
                                      kwargs=tmp_avatar_kwargs)
            response = self.client.get(tmp_avatar_path)
            self.assertEqual(response.status_code, 200)

            response = self.client.post(self.link, json.dumps({
                    'avatar': 'crop_tmp',
                    'crop': {
                        'offset': {
                            'x': 0, 'y': 0
                        },
                        'zoom': 1
                    }
                }),
                content_type="application/json")
            response_json = json.loads(response.content)

            self.assertEqual(response.status_code, 200)
            self.assertIn('Uploaded avatar was set.', response.content)

            avatar_dir = store.get_existing_avatars_dir(self.user)
            avatar = Path('%s/%s_tmp.png' % (avatar_dir, self.user.pk))
            self.assertFalse(avatar.exists())

            avatar = Path('%s/%s_org.png' % (avatar_dir, self.user.pk))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())

            org_avatar_kwargs = {
                'secret': response_json['options']['crop_org']['secret'],
                'hash': response_json['avatar_hash'],
                'user_id': self.user.pk
            }
            org_avatar_path = reverse('misago:user_avatar_source',
                                      kwargs=tmp_avatar_kwargs)
            response = self.client.get(org_avatar_path)
            self.assertEqual(response.status_code, 200)

            response = self.client.post(self.link, json.dumps({
                    'avatar': 'crop_tmp',
                    'crop': {
                        'offset': {
                            'x': 0, 'y': 0
                        },
                        'zoom': 1
                    }
                }),
                content_type="application/json")
            self.assertEqual(response.status_code, 400)
            self.assertIn('This avatar type is not allowed.', response.content)

            response = self.client.post(self.link, json.dumps({
                    'avatar': 'crop_org',
                    'crop': {
                        'offset': {
                            'x': 0, 'y': 0
                        },
                        'zoom': 1
                    }
                }),
                content_type="application/json")
            self.assertEqual(response.status_code, 200)
            self.assertIn('Avatar was re-cropped.', response.content)
Пример #10
0
 def assertAvatarWasSet(self):
     avatar_dir = store.get_existing_avatars_dir(self.user)
     for size in settings.MISAGO_AVATARS_SIZES:
         avatar = Path('%s/%s_%s.png' % (avatar_dir, self.user.pk, size))
         self.assertTrue(avatar.exists())
         self.assertTrue(avatar.isfile())
Пример #11
0
 def assertAvatarWasSet(self):
     avatar_dir = store.get_existing_avatars_dir(self.user)
     for size in settings.MISAGO_AVATARS_SIZES:
         avatar = Path('%s/%s_%s.png' % (avatar_dir, self.user.pk, size))
         self.assertTrue(avatar.exists())
         self.assertTrue(avatar.isfile())
Пример #12
0
    def test_avatar_upload_and_crop(self):
        """avatar can be uploaded and cropped"""
        response = self.client.post(self.link, data={'avatar': 'generated'})
        self.assertEqual(response.status_code, 200)

        response = self.client.post(self.link, data={'avatar': 'upload'})
        self.assertEqual(response.status_code, 400)
        self.assertIn('No file was sent.', response.content)

        with open('%s/%s' % (settings.MEDIA_ROOT, 'misago.png')) as avatar:
            response = self.client.post(self.link,
                                        data={
                                            'avatar': 'upload',
                                            'image': avatar
                                        })
            self.assertEqual(response.status_code, 200)

            response_json = json.loads(response.content)
            self.assertTrue(response_json['options']['crop_tmp'])

            avatar_dir = store.get_existing_avatars_dir(self.user)
            avatar = Path('%s/%s_tmp.png' % (avatar_dir, self.user.pk))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())

            tmp_avatar_kwargs = {
                'secret': response_json['options']['crop_tmp']['secret'],
                'hash': response_json['avatar_hash'],
                'user_id': self.user.pk
            }
            tmp_avatar_path = reverse('misago:user_avatar_source',
                                      kwargs=tmp_avatar_kwargs)
            response = self.client.get(tmp_avatar_path)
            self.assertEqual(response.status_code, 200)

            response = self.client.post(self.link,
                                        json.dumps({
                                            'avatar': 'crop_tmp',
                                            'crop': {
                                                'offset': {
                                                    'x': 0,
                                                    'y': 0
                                                },
                                                'zoom': 1
                                            }
                                        }),
                                        content_type="application/json")
            response_json = json.loads(response.content)

            self.assertEqual(response.status_code, 200)
            self.assertIn('Uploaded avatar was set.', response.content)

            avatar_dir = store.get_existing_avatars_dir(self.user)
            avatar = Path('%s/%s_tmp.png' % (avatar_dir, self.user.pk))
            self.assertFalse(avatar.exists())

            avatar = Path('%s/%s_org.png' % (avatar_dir, self.user.pk))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())

            org_avatar_kwargs = {
                'secret': response_json['options']['crop_org']['secret'],
                'hash': response_json['avatar_hash'],
                'user_id': self.user.pk
            }
            org_avatar_path = reverse('misago:user_avatar_source',
                                      kwargs=tmp_avatar_kwargs)
            response = self.client.get(org_avatar_path)
            self.assertEqual(response.status_code, 200)

            response = self.client.post(self.link,
                                        json.dumps({
                                            'avatar': 'crop_tmp',
                                            'crop': {
                                                'offset': {
                                                    'x': 0,
                                                    'y': 0
                                                },
                                                'zoom': 1
                                            }
                                        }),
                                        content_type="application/json")
            self.assertEqual(response.status_code, 400)
            self.assertIn('This avatar type is not allowed.', response.content)

            response = self.client.post(self.link,
                                        json.dumps({
                                            'avatar': 'crop_org',
                                            'crop': {
                                                'offset': {
                                                    'x': 0,
                                                    'y': 0
                                                },
                                                'zoom': 1
                                            }
                                        }),
                                        content_type="application/json")
            self.assertEqual(response.status_code, 200)
            self.assertIn('Avatar was re-cropped.', response.content)
Пример #13
0
 def assertNoAvatarIsSet(self):
     avatar_dir = store.get_existing_avatars_dir(self.user)
     for size in settings.MISAGO_AVATARS_SIZES:
         avatar = path('%s/%s_%s.png' % (avatar_dir, self.user.pk, size))
         self.assertFalse(avatar.exists())
Пример #14
0
    def test_avatar_upload_and_crop(self):
        """avatar can be uploaded and cropped"""
        response = self.client.post(self.link, data={'avatar': 'generated'})
        self.assertEqual(response.status_code, 200)

        response = self.client.post(self.link, data={'avatar': 'upload'})
        self.assertEqual(response.status_code, 400)
        self.assertIn('No file was sent.', response.content)

        with open('%s/%s' % (settings.MEDIA_ROOT, 'misago.png')) as avatar:
            response = self.client.post(self.link,
                                        data={
                                            'avatar': 'upload',
                                            'image': avatar
                                        })
            self.assertEqual(response.status_code, 200)

            response_json = json.loads(response.content)
            self.assertTrue(response_json['options']['crop_tmp'])

            avatar_dir = store.get_existing_avatars_dir(self.user)
            avatar = Path('%s/%s_tmp.png' % (avatar_dir, self.user.pk))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())

            response = self.client.post(self.link, json.dumps({
                    'avatar': 'crop_tmp',
                    'crop': {
                        'offset': {
                            'x': 0, 'y': 0
                        },
                        'zoom': 1
                    }
                }),
                content_type="application/json")

            self.assertEqual(response.status_code, 200)
            self.assertIn('Uploaded avatar was set.', response.content)

            avatar_dir = store.get_existing_avatars_dir(self.user)
            avatar = Path('%s/%s_tmp.png' % (avatar_dir, self.user.pk))
            self.assertFalse(avatar.exists())

            avatar = Path('%s/%s_org.png' % (avatar_dir, self.user.pk))
            self.assertTrue(avatar.exists())
            self.assertTrue(avatar.isfile())

            response = self.client.post(self.link, json.dumps({
                    'avatar': 'crop_tmp',
                    'crop': {
                        'offset': {
                            'x': 0, 'y': 0
                        },
                        'zoom': 1
                    }
                }),
                content_type="application/json")
            self.assertEqual(response.status_code, 400)
            self.assertIn('This avatar type is not allowed.', response.content)

            response = self.client.post(self.link, json.dumps({
                    'avatar': 'crop_org',
                    'crop': {
                        'offset': {
                            'x': 0, 'y': 0
                        },
                        'zoom': 1
                    }
                }),
                content_type="application/json")
            self.assertEqual(response.status_code, 200)
            self.assertIn('Avatar was re-cropped.', response.content)