Skip to content

Commit

Permalink
Add available_request_authorization in repository serializer v2
Browse files Browse the repository at this point in the history
  • Loading branch information
dougppaz committed Oct 16, 2018
1 parent 1cd3b44 commit 007f930
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
20 changes: 20 additions & 0 deletions bothub/api/v2/repository/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from bothub.common.models import RepositoryCategory
from bothub.common.models import RepositoryEntityLabel
from bothub.common.models import RepositoryAuthorization
from bothub.common.models import RequestRepositoryAuthorization
from bothub.common.languages import LANGUAGE_CHOICES


Expand Down Expand Up @@ -107,6 +108,7 @@ class Meta:
'ready_for_train',
'requirements_to_train',
'languages_ready_for_train',
'available_request_authorization',
]
read_only = [
'uuid',
Expand Down Expand Up @@ -146,6 +148,7 @@ class Meta:
examples__count = serializers.SerializerMethodField()
absolute_url = serializers.SerializerMethodField()
authorization = serializers.SerializerMethodField()
available_request_authorization = serializers.SerializerMethodField()

def get_intents(self, obj):
return IntentSerializer(
Expand Down Expand Up @@ -180,3 +183,20 @@ def get_authorization(self, obj):
return None
return RepositoryAuthorizationSerializer(
obj.get_user_authorization(request.user)).data

def get_available_request_authorization(self, obj):
request = self.context.get('request')
if not request or not request.user.is_authenticated:
return False
authorization = obj.get_user_authorization(request.user)
if authorization.role is not RepositoryAuthorization.ROLE_NOT_SETTED:
return False
if authorization.is_owner:
return False
try:
RequestRepositoryAuthorization.objects.get(
user=request.user,
repository=obj)
return False
except RequestRepositoryAuthorization.DoesNotExist:
return True
59 changes: 59 additions & 0 deletions bothub/api/v2/repository/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from bothub.common.models import RepositoryCategory
from bothub.common.models import Repository
from bothub.common.models import RequestRepositoryAuthorization
from bothub.common import languages

from ..tests.utils import create_user_and_token
Expand Down Expand Up @@ -303,3 +304,61 @@ def test_authorization_with_user(self):
self.assertEqual(
authorization.get('uuid'),
str(repository.get_user_authorization(user).uuid))


class RepositoryAvailableRequestAuthorizationTestCase(TestCase):
def setUp(self):
self.factory = RequestFactory()

self.user, self.user_token = create_user_and_token()
self.owner, self.owner_token = create_user_and_token('owner')

self.repository = Repository.objects.create(
owner=self.owner,
name='Testing',
slug='test',
language=languages.LANGUAGE_EN)

def request(self, repository, token=None):
authorization_header = {
'HTTP_AUTHORIZATION': 'Token {}'.format(token.key),
} if token else {}

request = self.factory.get(
'/api/v2/repository/{}/'.format(repository.uuid),
**authorization_header)

response = RepositoryViewSet.as_view({'get': 'retrieve'})(
request,
uuid=repository.uuid)
response.render()
content_data = json.loads(response.content)
return (response, content_data,)

def test_owner_ever_false(self):
response, content_data = self.request(
self.repository,
self.owner_token)
available_request_authorization = content_data.get(
'available_request_authorization')
self.assertFalse(available_request_authorization)

def test_user_available(self):
response, content_data = self.request(
self.repository,
self.user_token)
available_request_authorization = content_data.get(
'available_request_authorization')
self.assertTrue(available_request_authorization)

def test_false_when_request(self):
RequestRepositoryAuthorization.objects.create(
user=self.user,
repository=self.repository,
text='r')
response, content_data = self.request(
self.repository,
self.user_token)
available_request_authorization = content_data.get(
'available_request_authorization')
self.assertFalse(available_request_authorization)

0 comments on commit 007f930

Please sign in to comment.