Skip to content

Commit

Permalink
added verification permission to mentor and to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
marktai committed Jan 24, 2018
1 parent 4f9fe1b commit 14b82fa
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
25 changes: 16 additions & 9 deletions src/email_requests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def test_make_request(self):
create_url = reverse('email_requests:send_email', kwargs={'mentor_id': self.mentor.id})

request_params = {
#'phone': '12345678910',
'preferred_mentee_email': '[email protected]',
'message': 'Hi this is test message',
}
Expand All @@ -46,7 +45,6 @@ def test_make_request(self):

self.assertEqual(request.mentor, self.mentor)
self.assertEqual(request.mentee, self.mentee)
#self.assertEqual(request.phone, request_params['phone'])
self.assertEqual(request.preferred_mentee_email, request_params['preferred_mentee_email'])

def test_make_request_no_phone(self):
Expand All @@ -71,6 +69,22 @@ def test_make_request_no_phone(self):
self.assertEqual(request.phone, '')
self.assertEqual(request.preferred_mentee_email, request_params['preferred_mentee_email'])

def test_make_request_unverified(self):
self.mentee = users_factories.ProfileFactory(verified=False)
create_url = reverse('email_requests:send_email', kwargs={'mentor_id': self.mentor.id})

request_params = {
'preferred_mentee_email': '[email protected]',
'message': 'Hi this is test message',
}

resp = self.client.post(
create_url,
data=request_params,
)

self.assertFalse(Request.objects.filter(mentor=self.mentor, mentee=self.mentee).exists())

class ListRequestsTest(APITestCase):

get_url = reverse('email_requests:requests_list')
Expand Down Expand Up @@ -166,17 +180,10 @@ def test_mentee_only_requests(self):


def test_list_reqests_empty(self):


resp = self.client.get(
self.get_url,
)

self.assertEqual(resp.data['count'], 0)
self.assertEqual(len(resp.data['results']), 0)






2 changes: 2 additions & 0 deletions src/email_requests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

import sendgrid
from sendgrid.helpers.mail import Email, Content, Substitution, Mail
from users.permissions import VerifiedUser

# Create your views here.
class EmailRequestView(generics.CreateAPIView):
permission_classes = (VerifiedUser,)
serializer_class = RequestSerializer

def get_object(self):
Expand Down
1 change: 1 addition & 0 deletions src/users/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Meta:
model = models.Profile
user = factory.SubFactory(UserFactory)
verification_code = factory.LazyAttribute(lambda a: models.Profile.generate_verification_code())
verified = True

class MajorFactory(factory.django.DjangoModelFactory):
class Meta:
Expand Down
10 changes: 10 additions & 0 deletions src/users/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from rest_framework import permissions


class VerifiedUser(permissions.BasePermission):
"""
Custom permission to only allow verified users
"""

def has_object_permission(self, request, view, obj):
return obj.verified
11 changes: 10 additions & 1 deletion src/users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class VerifyUserTest(APITestCase):
verify_url = reverse('users:verify')

def setUp(self):
self.profile = factories.ProfileFactory()
self.profile = factories.ProfileFactory(verified=False)
self.client.force_authenticate(user=self.profile.user)

def tearDown(self):
Expand Down Expand Up @@ -240,6 +240,15 @@ def test_makes_existing_mentor_active(self):
mentor.refresh_from_db()
self.assertTrue(mentor.active)

def test_requires_verification(self):
self.profile = factories.ProfileFactory(verified=False)
self.assertEqual(Mentor.objects.filter(profile = self.profile).exists(), False)
resp = self.client.post(
self.mentors_create_url,
)
self.assertEqual(Mentor.objects.filter(profile = self.profile).exists(), False)


class FindMentorByIDTest(APITestCase):
def setUp(self):
self.mentor = factories.MentorFactory()
Expand Down
6 changes: 4 additions & 2 deletions src/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
UserSerializer, GroupSerializer, ProfileSerializer, MajorSerializer,
MentorSerializer, CourseSerializer,
)
from .permissions import VerifiedUser

import sendgrid
from sendgrid.helpers.mail import Email, Content, Substitution, Mail
Expand Down Expand Up @@ -169,12 +170,13 @@ class OwnMentorView(generics.RetrieveUpdateDestroyAPIView):
"""
View for turning mentor status on (post) and modifying all mentor fields
"""
permission_classes = tuple()
serializer_class = MentorSerializer

def get_object(self):
return get_object_or_404(Mentor, profile__user=self.request.user)
serializer_class = MentorSerializer
def post (self,request):

def post (self, request):
profile_id = self.request.user.profile.id
profile = Profile.objects.get(id=profile_id)
mentor_request = Mentor.objects.filter(profile__user=self.request.user)
Expand Down

0 comments on commit 14b82fa

Please sign in to comment.