Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:theonion/django-bulbs into thumbn…
Browse files Browse the repository at this point in the history
…ail-override-fix
  • Loading branch information
Andrew Kos committed Sep 15, 2014
2 parents 3355cbc + 31da4dc commit 38a7085
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bulbs/__init__.py
@@ -1 +1 @@
__version__ = "0.3.4"
__version__ = "0.3.6"
4 changes: 2 additions & 2 deletions bulbs/api/urls.py
Expand Up @@ -3,7 +3,7 @@
from .views import api_v1_router, MeViewSet

urlpatterns = (
url(r"^me/logout$", "django.contrib.auth.views.logout", name="logout"),
url(r'^me', MeViewSet.as_view({'get': 'retrieve'}), name='me'),
url(r"^me/logout/?$", "django.contrib.auth.views.logout", name="logout"),
url(r'^me/?$', MeViewSet.as_view({'get': 'retrieve'}), name='me'),
url(r"^", include(api_v1_router.urls)) # noqa
)
17 changes: 16 additions & 1 deletion bulbs/api/views.py
@@ -1,11 +1,12 @@
"""API Views and ViewSets"""

from django.conf import settings
from django.contrib.auth import get_user_model
from django.http import Http404
from django.template.defaultfilters import slugify
from django.utils import timezone
from django.utils.dateparse import parse_datetime
from firebase_token_generator import create_token
from django.conf import settings

import elasticsearch

Expand Down Expand Up @@ -269,6 +270,19 @@ class MeViewSet(UncachedResponse, viewsets.ReadOnlyModelViewSet):
def retrieve(self, request, *args, **kwargs):

data = UserSerializer().to_native(request.user)

# attempt to add a firebase token if we have a firebase secret
secret = getattr(settings, 'FIREBASE_SECRET', None)
if secret:
# use firebase auth to provide auth variables to firebase security api
firebase_auth_payload = {
'id': request.user.pk,
'username': request.user.username,
'email': request.user.email,
'is_staff': request.user.is_staff
}
data['firebase_token'] = create_token(secret, firebase_auth_payload)

return Response(data)


Expand All @@ -278,3 +292,4 @@ def retrieve(self, request, *args, **kwargs):
api_v1_router.register(r"tag", TagViewSet, base_name="tag")
api_v1_router.register(r"log", LogEntryViewSet, base_name="logentry")
api_v1_router.register(r"user", UserViewSet, base_name="user")
# note: me view is registered in urls.py
15 changes: 0 additions & 15 deletions bulbs/content/serializers.py
Expand Up @@ -2,9 +2,6 @@
from django.core.exceptions import ValidationError
from django.db import transaction
from django.template.defaultfilters import slugify
from firebase_token_generator import create_token
from django.conf import settings


from rest_framework import serializers
from rest_framework import relations
Expand Down Expand Up @@ -147,18 +144,6 @@ def to_native(self, obj):
"short_name": obj.get_short_name()
}

# ensure this variable exists
secret = getattr(settings, 'FIREBASE_SECRET', None)
if secret:
# use firebase auth to provide auth variables to firebase security api
firebase_auth_payload = {
'id': obj.pk,
'username': obj.username,
'email': obj.email,
'is_staff': obj.is_staff
}
json['firebase_token'] = create_token(secret, firebase_auth_payload)

return json


Expand Down
18 changes: 18 additions & 0 deletions tests/test_content_api.py
Expand Up @@ -401,6 +401,7 @@ def test_image_serializer(self):


class TestMeApi(ContentAPITestCase):

def test_me(self):
client = Client()
client.login(username="admin", password="secret")
Expand All @@ -410,6 +411,23 @@ def test_me(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.get("username"), "admin")

def test_me_has_firebase_token(self):
"""Test that firebase token is put into me view if settings contains a FIREBASE_SECRET variable."""

# login user
client = Client()
client.login(username="admin", password="secret")
me_endpoint = reverse("me")

# set the firebase secret needed to get the firebase_token property in the me view
from django.conf import settings as mock_settings
mock_settings.FIREBASE_SECRET = 'abc'

# check that me view has the firebase token
response = client.get(me_endpoint, content_type="application/json")
self.assertEqual(response.status_code, 200)
self.assertTrue("firebase_token" in response.data)


class TestTrashContentAPI(ContentAPITestCase):
def test_trash(self):
Expand Down

0 comments on commit 38a7085

Please sign in to comment.