Ejemplo n.º 1
0
    def test_avatar_urls_service_not_found(self):
        """Testing {% avatar_urls %} template tag with an invalid service"""
        service = avatar_services.default_service

        self.assertIsNotNone(service)
        self.assertIsNone(avatar_services.get_avatar_service(
            DummyAvatarService.avatar_service_id))

        t = Template(
            '{% load avatars %}'
            '{% avatar_urls u 32 service_id %}'
        )

        self.assertEqual(
            t.render(RequestContext(self.request, {
                'u': self.user,
                'service_id': DummyAvatarService.avatar_service_id,
            })),
            ('{'
             '"1x": "%(1x)s", '
             '"3x": "%(3x)s", '
             '"2x": "%(2x)s"'
             '}'
             % service.get_avatar_urls_uncached(self.user, 32))
        )
Ejemplo n.º 2
0
    def test_avatar_urls_service_not_found(self):
        """Testing {% avatar_urls %} template tag with an invalid service"""
        service = avatar_services.default_service

        self.assertIsNotNone(service)
        self.assertIsNone(avatar_services.get_avatar_service(
            DummyAvatarService.avatar_service_id))

        t = Template(
            '{% load avatars %}'
            '{% avatar_urls u 32 service_id %}'
        )

        self.assertEqual(
            t.render(RequestContext(self.request, {
                'u': self.user,
                'service_id': DummyAvatarService.avatar_service_id,
            })),
            '{'
            '"1x": "%(1x)s", '
            '"3x": "%(3x)s", '
            '"2x": "%(2x)s"'
            '}'
            % service.get_avatar_urls_uncached(self.user, 32)
        )
Ejemplo n.º 3
0
 def _reset_avatar_services():
     avatar_services.reset()
     gravatar_service = avatar_services.get_avatar_service(
         GravatarService.avatar_service_id)
     avatar_services.enable_service(gravatar_service.avatar_service_id,
                                    save=False)
     avatar_services.set_default_service(gravatar_service, save=False)
Ejemplo n.º 4
0
    def _get_or_create_user(self):
        """Return a user to use for CircleCI.

        Returns:
            django.contrib.auth.models.User:
            A user instance.
        """
        try:
            return User.objects.get(username='******')
        except User.DoesNotExist:
            logger.info('Creating new user for CircleCI')
            siteconfig = SiteConfiguration.objects.get_current()
            noreply_email = siteconfig.get('mail_default_from')

            with transaction.atomic():
                try:
                    user = User.objects.create(username='******',
                                               email=noreply_email,
                                               first_name='Circle',
                                               last_name='CI')
                except IntegrityError:
                    # Another process/thread beat us to it.
                    return User.objects.get(username='******')

                profile = user.get_profile()
                profile.should_send_email = False
                profile.save()

                avatar_service = avatar_services.get_avatar_service(
                    URLAvatarService.avatar_service_id)
                # TODO: make somewhat higher-res versions for the main avatar.
                avatar_service.setup(user, self.icon_static_urls)

                return user
Ejemplo n.º 5
0
 def _reset_avatar_services():
     avatar_services.reset()
     gravatar_service = avatar_services.get_avatar_service(
         GravatarService.avatar_service_id)
     avatar_services.enable_service(gravatar_service.avatar_service_id,
                                    save=False)
     avatar_services.set_default_service(gravatar_service, save=False)
Ejemplo n.º 6
0
    def setUp(self):
        super(TemplateTagTests, self).setUp()

        gravatar_service = avatar_services.get_avatar_service(
            GravatarService.avatar_service_id)
        avatar_services.enable_service(gravatar_service.avatar_service_id,
                                       save=False)
        avatar_services.set_default_service(gravatar_service, save=False)

        self.user = User.objects.get(username='******')
        self.request = HttpRequest()
        self.request.user = AnonymousUser()
Ejemplo n.º 7
0
    def post(self, request):
        """Create a new user for Review Bot.

        Args:
            request (django.http.HttpRequest):
                The HTTP request.

        Returns:
            django.http.HttpResponse:
            A response containing the newly-configured user.
        """
        if not request.user.is_superuser:
            return self.get_no_access_error(request)

        siteconfig = SiteConfiguration.objects.get_current()
        noreply_email = siteconfig.get('mail_default_from')

        extension = ReviewBotExtension.instance

        try:
            with transaction.atomic():
                user = User.objects.create(username='******',
                                           email=noreply_email,
                                           first_name='Review',
                                           last_name='Bot')

                profile = user.get_profile()
                profile.should_send_email = False
                profile.save()

                avatar_service = avatar_services.get_avatar_service(
                    URLAvatarService.avatar_service_id)
                extension = ReviewBotExtension.instance
                avatar_service.setup(
                    user,
                    {
                        '1x': extension.get_static_url(
                            'images/reviewbot.png'),
                        '2x': extension.get_static_url(
                            'images/[email protected]'),
                    })
        except IntegrityError:
            return HttpResponseBadRequest()

        extension.settings['user'] = user.pk
        extension.settings.save()

        return HttpResponse(json.dumps(_serialize_user(request, user)),
                            content_type='application/json')
Ejemplo n.º 8
0
    def test_avatar_urls_with_service(self):
        """Testing {% avatar_urls %} template tag with avatar_service_id"""
        avatar_services.register(DummyHighDPIAvatarService)
        avatar_services.enable_service(DummyHighDPIAvatarService)

        service = avatar_services.get_avatar_service(
            DummyHighDPIAvatarService.avatar_service_id)

        t = Template('{% load avatars %}' '{% avatar_urls u 32 service_id %}')

        self.assertEqual(
            t.render(
                RequestContext(
                    self.request, {
                        'u': self.user,
                        'service_id':
                        DummyHighDPIAvatarService.avatar_service_id,
                    })),
            ('{'
             '"1x": "%(1x)s", '
             '"2x": "%(2x)s"'
             '}' % service.get_avatar_urls_uncached(self.user, 32)))
Ejemplo n.º 9
0
    def test_absolute_urls(self):
        """Testing FileUploadService.get_avatar_urls_uncached returns absolute
        URLs
        """
        user = User.objects.get(username='******')
        avatar = SimpleUploadedFile('filename.png',
                                    content=b' ',
                                    content_type='image/png')

        service = avatar_services.get_avatar_service(
            FileUploadService.avatar_service_id)
        storage_cls = get_storage_class()

        self.spy_on(storage_cls.save,
                    owner=storage_cls,
                    call_fake=lambda self, name, *args, **kwargs: name)

        form = AvatarSettingsForm(None,
                                  self.request_factory.post('/'),
                                  user,
                                  data={
                                      'avatar_service_id':
                                      FileUploadService.avatar_service_id,
                                  })

        service_form = \
            form.avatar_service_forms[FileUploadService.avatar_service_id]

        form.files = service_form.files = {
            service_form.add_prefix('avatar_upload'): avatar,
        }

        self.assertTrue(form.is_valid())
        form.save()

        file_path = storage_cls.save.spy.last_call.args[0]

        self.assertEqual(service.get_avatar_urls_uncached(user, None),
                         {'1x': 'http://example.com/media/%s' % file_path})
Ejemplo n.º 10
0
def get_or_create_jenkins_user():
    """Return a user to use for Jenkins CI.

    Returns:
        django.contrib.auth.models.User:
        A user instance.
    """
    try:
        return User.objects.get(username='******')
    except User.DoesNotExist:
        logger.info('Creating new user for Jenkins CI')
        siteconfig = SiteConfiguration.objects.get_current()
        noreply_email = siteconfig.get('mail_default_from')

        with transaction.atomic():
            try:
                user = User.objects.create(username='******',
                                           email=noreply_email,
                                           first_name='Jenkins',
                                           last_name='CI')
            except IntegrityError:
                # Another process/thread beat us to it.
                return User.objects.get(username='******')

            profile = user.get_profile()
            profile.should_send_email = False
            profile.save()

            if avatar_services.is_enabled(
                URLAvatarService.avatar_service_id):
                avatar_service = avatar_services.get_avatar_service(
                    URLAvatarService.avatar_service_id)
                # TODO: make somewhat higher-res versions for the main
                # avatar.
                avatar_service.setup(user, get_icon_static_urls())

            return user
Ejemplo n.º 11
0
    def test_absolute_urls(self):
        """Testing FileUploadService.get_avatar_urls_uncached returns absolute
        URLs
        """
        user = User.objects.get(username='******')
        avatar = SimpleUploadedFile('filename.png', content=b' ',
                                    content_type='image/png')

        service = avatar_services.get_avatar_service(
            FileUploadService.avatar_service_id)
        storage_cls = get_storage_class()

        self.spy_on(storage_cls.save,
                    call_fake=lambda self, filename, data: filename)

        form = AvatarSettingsForm(
            None,
            self.request_factory.post('/'),
            user,
            data={
                'avatar_service_id': FileUploadService.avatar_service_id,
            })

        service_form = \
            form.avatar_service_forms[FileUploadService.avatar_service_id]

        form.files = service_form.files = {
            service_form.add_prefix('avatar_upload'): avatar,
        }

        self.assertTrue(form.is_valid())
        form.save()

        file_path = storage_cls.save.spy.last_call.args[0]

        self.assertEqual(service.get_avatar_urls_uncached(user, None),
                         {'1x': 'http://example.com/media/%s' % file_path})
Ejemplo n.º 12
0
    def test_avatar_urls_with_service(self):
        """Testing {% avatar_urls %} template tag with avatar_service_id"""
        avatar_services.register(DummyHighDPIAvatarService)
        avatar_services.enable_service(DummyHighDPIAvatarService)

        service = avatar_services.get_avatar_service(
            DummyHighDPIAvatarService.avatar_service_id)

        t = Template(
            '{% load avatars %}'
            '{% avatar_urls u 32 service_id %}'
        )

        self.assertEqual(
            t.render(RequestContext(self.request, {
                'u': self.user,
                'service_id': DummyHighDPIAvatarService.avatar_service_id,
            })),
            ('{'
             '"1x": "%(1x)s", '
             '"2x": "%(2x)s"'
             '}'
             % service.get_avatar_urls_uncached(self.user, 32))
        )