Skip to content

Commit

Permalink
Redirect anonymous users to login page for /profile/ (fixes #39)
Browse files Browse the repository at this point in the history
* modify tests to check for redirect instead of bad request
* modify ProfileView to redirect anonymous users to the login page
  • Loading branch information
jandd committed Nov 13, 2014
1 parent a2bff04 commit 48f4804
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions coffeestats/caffeine/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ class ProfileViewsTest(CaffeineViewTest):
"""

def test_bad_request_for_anonymous(self):
def test_redirect_for_anonymous(self):
response = self.client.get('/profile/')
self.assertEqual(response.status_code, 400)
self.assertRedirects(response, '/auth/login/?next=/profile/')

def test_notfound_for_missing_user(self):
response = self.client.get('/profile/testuser/')
Expand Down
3 changes: 2 additions & 1 deletion coffeestats/caffeine/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ def get(self, request, *args, **kwargs):
return HttpResponseRedirect(reverse('public', kwargs={
'username': request.GET['u']}))
if not request.user.is_authenticated():
return HttpResponseBadRequest(_('Invalid request!'))
return HttpResponseRedirect(
"%s?next=%s" % (settings.LOGIN_URL, request.path))
return super(ProfileView, self).get(request, *args, **kwargs)

def get_context_data(self, **kwargs):
Expand Down
19 changes: 19 additions & 0 deletions coffeestats/functional_tests/test_base_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,22 @@ def test_overall_elements(self):
self.assertEqual(len(box.find_elements_by_tag_name('canvas')), 1)

self.check_page_footer()


class ProfilePageTest(BaseCoffeeStatsPageTestMixin, SeleniumTest):

def test_redirect_profile_for_anonymous(self):
self.selenium.get(self.server_url + '/profile/')

self.check_page_header()

# He finds out that he was redirected to a login page
self.assertRegexpMatches(self.selenium.current_url,
r'/auth/login/\?next=/profile/$')

# there is a navigation in the page header
header = self.selenium.find_element_by_id('header')
nav = header.find_element_by_tag_name('nav')
self.assertEquals("Login\nRegister", nav.text)

self.check_page_footer()

0 comments on commit 48f4804

Please sign in to comment.