Skip to content

Commit

Permalink
[fix] add tests
Browse files Browse the repository at this point in the history
- add tests for the gallery view
- remove code sections for anonymous users since
  all users have to be logged in
  • Loading branch information
collinmutembei committed Mar 29, 2016
1 parent 2c1209e commit be3a1e4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 27 deletions.
7 changes: 0 additions & 7 deletions api/models/phedited.py
Expand Up @@ -14,13 +14,6 @@ def set_upload_file_path(instance, filename):
instance.effects.append('')
effects = "_".join(instance.effects)
# all edited images are stored in the directory phedited
if user.username == "anonymous":
file_path = "{0}/phedited/{1}{2}".format(
'anonymous',
effects,
filename
)
return file_path
random_hex = user.uuid.hex
file_path = "{0}/phedited/{1}{2}".format(
random_hex[-20:],
Expand Down
20 changes: 5 additions & 15 deletions api/viewsets.py
Expand Up @@ -45,19 +45,9 @@ class PheditedImageViewset(viewsets.ModelViewSet):
def perform_create(self, serializer):
"""
creates image under the users upload directory slash phedited
or under anonymous upload directory slash phedited if user is
is not logged in
"""
if self.request.user.is_active:
serializer.save(
original_image=self.request.data.get('original_image_url'),
phedited_by=self.request.user,
effects=self.request.data.get('effects')
)
elif self.request.user.is_anonymous:
anonymous_user = User.objects.get(username="anonymous")
serializer.save(
original_image=self.request.data.get('original_image_url'),
phedited_by=anonymous_user,
effects=self.request.data.get('effects')
)
serializer.save(
original_image=self.request.data.get('original_image_url'),
phedited_by=self.request.user,
effects=self.request.data.get('effects')
)
8 changes: 8 additions & 0 deletions tests/test_api.py
Expand Up @@ -126,3 +126,11 @@ def test_redirect_when_accessing_webapp_unauthenticated(self):
)
authenticated_response = self.client.get('/app/')
self.assertEqual(authenticated_response.status_code, 200)

def test_accessing_navigating_to_gallery_page(self):
self.client.login(
username='testuser',
password='ncvsN809ibkj!*HJ2612J'
)
response = self.client.get('/gallery/')
self.assertContains(response, 'Gallery')
5 changes: 0 additions & 5 deletions tests/test_models.py
Expand Up @@ -21,11 +21,6 @@ def setUp(self):
def test_upload_path(self):
"""test for the directory where uploaded image is saved
"""
# for anonymous user
self.image_being_edited = phedited.PheditedImage(phedited_by=self.anonymous_user)
self.image_being_edited.effects = ['BLUR']
anon_upload_path = phedited.set_upload_file_path(self.image_being_edited, self.image_filename)
self.assertEqual(anon_upload_path, 'anonymous/phedited/BLUR_hashtag.jpg')

# for logged in user
self.image_being_edited = phedited.PheditedImage(phedited_by=self.user)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_views.py
Expand Up @@ -17,3 +17,9 @@ def test_landing_page_view(self):
"""
resp = self.client.get('/xyzabcd')
self.assertEqual(resp.status_code, 404)

def test_gallery_page_view(self):
"""test behaviour on accessing the gallery when unauthenticated
"""
resp = self.client.get('/gallery/')
self.assertEqual(resp.status_code, 302)

0 comments on commit be3a1e4

Please sign in to comment.