Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 3040, Redirect to login page in case of Unauthorized #3092

Merged
merged 8 commits into from
Feb 25, 2015
21 changes: 20 additions & 1 deletion kalite/distributed/tests/browser_tests/quiz.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
"""
These use a web-browser, along selenium, to simulate user actions.
"""

import urllib

from django.conf import settings
logging = settings.LOG
from django.core.urlresolvers import reverse
from django.test.utils import override_settings

from kalite.testing.base import KALiteBrowserTestCase
from kalite.testing.mixins import BrowserActionMixins, FacilityMixins
from selenium.webdriver.common.keys import Keys

PLAYLIST_ID = "g3_p1"
PLAYLIST_ID = "g4_u400_ap1"


class QuizTest(BrowserActionMixins, FacilityMixins, KALiteBrowserTestCase):
Expand Down Expand Up @@ -53,3 +57,18 @@ def test_quiz_first_answer_correct_not_registered(self):
self.browser.execute_script("quizlog.add_response_log_item({correct: true});")
self.assertEqual(self.browser.execute_script("return quizlog.get('total_correct')"), 1)
self.assertEqual(self.browser.execute_script("return quizlog._response_log_cache[0]"), 1)


def test_unauthorized_request_redirect_to_login(self):

self.browse_to(
self.live_server_url +
reverse("view_playlist", kwargs={"playlist_id": PLAYLIST_ID}))
hash_value = urllib.urlparse(self.browser.current_url).fragment
self.browser.delete_all_cookies()
self.browser.implicitly_wait(30)
self.browser.find_element_by_id('solutionarea').find_element_by_css_selector('input[type=text]').click()
self.browser_send_keys(unicode("Anurag"))
self.browser_send_keys(Keys.RETURN)
#Not using urlencode for next param as its hassle to use escape character for params only.
self.assertEqual(self.browser.current_url, self.reverse('login') + "?next=" + reverse("view_playlist", kwargs={"playlist_id": PLAYLIST_ID}) + "#" + hash_value)

This comment was marked as spam.

3 changes: 3 additions & 0 deletions static-libraries/js/khan-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ function handleFailedAPI(resp, error_prefix) {
console.log(e);
}
break;
case 401:
case 403:
// Redirect to Login Page and add the current url as next
window.location.href = setGetParam(USER_LOGIN_URL, "next", window.location.pathname + window.location.hash)

This comment was marked as spam.

messages = {error: sprintf(gettext("You are not authorized to complete the request. Please <a href='%(login_url)s'>login</a> as an authorized user, then retry the request."), {
login_url: USER_LOGIN_URL
})};
Expand Down