Commit c1e26749 by Will Daly

Merge pull request #5621 from edx/will/remember-me

Add remember me checkbox to the login page
parents 52051df4 e65fe4ed
...@@ -94,7 +94,7 @@ class InvalidFieldError(Exception): ...@@ -94,7 +94,7 @@ class InvalidFieldError(Exception):
class FormDescription(object): class FormDescription(object):
"""Generate a JSON representation of a form. """ """Generate a JSON representation of a form. """
ALLOWED_TYPES = ["text", "select", "textarea"] ALLOWED_TYPES = ["text", "select", "textarea", "checkbox"]
ALLOWED_RESTRICTIONS = { ALLOWED_RESTRICTIONS = {
"text": ["min_length", "max_length"], "text": ["min_length", "max_length"],
......
...@@ -544,6 +544,7 @@ class PreferenceUsersListViewTest(UserApiTestCase): ...@@ -544,6 +544,7 @@ class PreferenceUsersListViewTest(UserApiTestCase):
self.assertEqual(len(set(all_user_uris)), 2) self.assertEqual(len(set(all_user_uris)), 2)
@ddt.ddt
class LoginSessionViewTest(ApiTestCase): class LoginSessionViewTest(ApiTestCase):
"""Tests for the login end-points of the user API. """ """Tests for the login end-points of the user API. """
...@@ -580,31 +581,41 @@ class LoginSessionViewTest(ApiTestCase): ...@@ -580,31 +581,41 @@ class LoginSessionViewTest(ApiTestCase):
self.assertEqual(form_desc["submit_url"], self.url) self.assertEqual(form_desc["submit_url"], self.url)
self.assertEqual(form_desc["fields"], [ self.assertEqual(form_desc["fields"], [
{ {
u"name": u"email", "name": "email",
u"default": u"", "default": "",
u"type": u"text", "type": "text",
u"required": True, "required": True,
u"label": u"E-mail", "label": "E-mail",
u"placeholder": u"example: username@domain.com", "placeholder": "example: username@domain.com",
u"instructions": u"This is the e-mail address you used to register with edX", "instructions": "This is the e-mail address you used to register with edX",
u"restrictions": { "restrictions": {
u"min_length": 3, "min_length": 3,
u"max_length": 254 "max_length": 254
}, },
}, },
{ {
u"name": u"password", "name": "password",
u"default": u"", "default": "",
u"type": u"text", "type": "text",
u"required": True, "required": True,
u"label": u"Password", "label": "Password",
u"placeholder": u"", "placeholder": "",
u"instructions": u"", "instructions": "",
u"restrictions": { "restrictions": {
u"min_length": 2, "min_length": 2,
u"max_length": 75 "max_length": 75
}, },
}, },
{
"name": "remember",
"default": False,
"type": "checkbox",
"required": False,
"label": "Remember me",
"placeholder": "",
"instructions": "",
"restrictions": {},
}
]) ])
def test_login(self): def test_login(self):
...@@ -623,6 +634,34 @@ class LoginSessionViewTest(ApiTestCase): ...@@ -623,6 +634,34 @@ class LoginSessionViewTest(ApiTestCase):
response = self.client.get(reverse("dashboard")) response = self.client.get(reverse("dashboard"))
self.assertHttpOK(response) self.assertHttpOK(response)
@ddt.data(
(json.dumps(True), False),
(json.dumps(False), True),
(None, True),
)
@ddt.unpack
def test_login_remember_me(self, remember_value, expire_at_browser_close):
# Create a test user
UserFactory.create(username=self.USERNAME, email=self.EMAIL, password=self.PASSWORD)
# Login and remember me
data = {
"email": self.EMAIL,
"password": self.PASSWORD,
}
if remember_value is not None:
data["remember"] = remember_value
response = self.client.post(self.url, data)
self.assertHttpOK(response)
# Verify that the session expiration was set correctly
self.assertEqual(
self.client.session.get_expire_at_browser_close(),
expire_at_browser_close
)
def test_invalid_credentials(self): def test_invalid_credentials(self):
# Create a test user # Create a test user
UserFactory.create(username=self.USERNAME, email=self.EMAIL, password=self.PASSWORD) UserFactory.create(username=self.USERNAME, email=self.EMAIL, password=self.PASSWORD)
......
...@@ -87,6 +87,14 @@ class LoginSessionView(APIView): ...@@ -87,6 +87,14 @@ class LoginSessionView(APIView):
} }
) )
form_desc.add_field(
"remember",
field_type="checkbox",
label=_("Remember me"),
default=False,
required=False,
)
return HttpResponse(form_desc.to_json(), content_type="application/json") return HttpResponse(form_desc.to_json(), content_type="application/json")
@method_decorator(ensure_csrf_cookie) @method_decorator(ensure_csrf_cookie)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment