Selaa lähdekoodia

implement get sites api

pull/2/head
Guillaume Vincent 9 vuotta sitten
vanhempi
commit
66fb0aa82c
8 muutettua tiedostoa jossa 80 lisäystä ja 4 poistoa
  1. +6
    -0
      lesspass/api/permissions.py
  2. +0
    -0
      lesspass/api/tests/__init__.py
  3. +30
    -0
      lesspass/api/tests/factories.py
  4. +33
    -0
      lesspass/api/tests/tests_site.py
  5. +1
    -1
      lesspass/api/urls.py
  6. +5
    -1
      lesspass/api/views.py
  7. +3
    -2
      lesspass/lesspass/settings.py
  8. +2
    -0
      lesspass/requirements.dev.txt

+ 6
- 0
lesspass/api/permissions.py Näytä tiedosto

@@ -0,0 +1,6 @@
from rest_framework import permissions


class IsOwner(permissions.IsAuthenticated):
def has_object_permission(self, request, view, obj):
return obj.user == request.user

lesspass/api/tests.py → lesspass/api/tests/__init__.py Näytä tiedosto


+ 30
- 0
lesspass/api/tests/factories.py Näytä tiedosto

@@ -0,0 +1,30 @@
import factory

from api import models


class UserFactory(factory.DjangoModelFactory):
class Meta:
model = models.User

username = factory.Sequence(lambda n: 'username{0}'.format(n))
first_name = factory.Faker('first_name')
last_name = factory.Faker('last_name')
email = factory.LazyAttribute(lambda a: '{0}.{1}@oslab.fr'.format(a.first_name, a.last_name).lower())
password = factory.PostGenerationMethodCall('set_password', 'password')
is_staff = False


class AdminFactory(UserFactory):
is_staff = True


class SiteFactory(factory.DjangoModelFactory):
class Meta:
model = models.Site

user = factory.SubFactory(UserFactory)
name = 'facebook'
password_length = 12
password_type = 'strong'
counter = 1

+ 33
- 0
lesspass/api/tests/tests_site.py Näytä tiedosto

@@ -0,0 +1,33 @@
from rest_framework.test import APITestCase, APIClient

from api.tests import factories


class LogoutApiTestCase(APITestCase):
def test_get_sites_403(self):
response = self.client.get('/api/sites/')
self.assertEqual(403, response.status_code)


class LoginApiTestCase(APITestCase):
def setUp(self):
self.user = factories.UserFactory()
self.client = APIClient()
self.client.force_authenticate(user=self.user)

def test_get_empty_sites(self):
request = self.client.get('/api/sites/')
self.assertEqual(0, len(request.data['results']))

def test_retrieve_its_own_sites(self):
site = factories.SiteFactory(user=self.user)
not_my_site = factories.SiteFactory(user=factories.UserFactory())

request = self.client.get('/api/sites/%s/' % not_my_site.id)
self.assertEqual(404, request.status_code)

request = self.client.get('/api/sites/')
self.assertEqual(1, len(request.data['results']))
self.assertEqual(site.name, request.data['results'][0]['name'])



+ 1
- 1
lesspass/api/urls.py Näytä tiedosto

@@ -4,7 +4,7 @@ from rest_framework import routers
from api import views

router = routers.DefaultRouter()
router.register(r'sites', views.SiteViewSet)
router.register(r'sites', views.SiteViewSet, base_name='sites')

urlpatterns = [
url(r'^', include(router.urls)),


+ 5
- 1
lesspass/api/views.py Näytä tiedosto

@@ -1,9 +1,13 @@
from rest_framework import viewsets

from api import models
from api.permissions import IsOwner
from api.serializers import SiteSerializer


class SiteViewSet(viewsets.ModelViewSet):
queryset = models.Site.objects.all()
serializer_class = SiteSerializer
permission_classes = (IsOwner,)

def get_queryset(self):
return models.Site.objects.filter(user=self.request.user)

+ 3
- 2
lesspass/lesspass/settings.py Näytä tiedosto

@@ -107,6 +107,7 @@ USE_TZ = True
STATIC_URL = '/static/'

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),
'PAGE_SIZE': 10
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
'PAGE_SIZE': 100,
'TEST_REQUEST_DEFAULT_FORMAT': 'json'
}

+ 2
- 0
lesspass/requirements.dev.txt Näytä tiedosto

@@ -0,0 +1,2 @@
-r requirements.txt
factory-boy==2.6.0

Ladataan…
Peruuta
Tallenna