diff --git a/common/djangoapps/user_api/helpers.py b/common/djangoapps/user_api/helpers.py index 0621278074..b591e41783 100644 --- a/common/djangoapps/user_api/helpers.py +++ b/common/djangoapps/user_api/helpers.py @@ -94,7 +94,7 @@ class InvalidFieldError(Exception): class FormDescription(object): """Generate a JSON representation of a form. """ - ALLOWED_TYPES = ["text", "select", "textarea"] + ALLOWED_TYPES = ["text", "select", "textarea", "checkbox"] ALLOWED_RESTRICTIONS = { "text": ["min_length", "max_length"], diff --git a/common/djangoapps/user_api/tests/test_views.py b/common/djangoapps/user_api/tests/test_views.py index 5677c0b99f..3954505428 100644 --- a/common/djangoapps/user_api/tests/test_views.py +++ b/common/djangoapps/user_api/tests/test_views.py @@ -544,6 +544,7 @@ class PreferenceUsersListViewTest(UserApiTestCase): self.assertEqual(len(set(all_user_uris)), 2) +@ddt.ddt class LoginSessionViewTest(ApiTestCase): """Tests for the login end-points of the user API. """ @@ -580,31 +581,41 @@ class LoginSessionViewTest(ApiTestCase): self.assertEqual(form_desc["submit_url"], self.url) self.assertEqual(form_desc["fields"], [ { - u"name": u"email", - u"default": u"", - u"type": u"text", - u"required": True, - u"label": u"E-mail", - u"placeholder": u"example: username@domain.com", - u"instructions": u"This is the e-mail address you used to register with edX", - u"restrictions": { - u"min_length": 3, - u"max_length": 254 + "name": "email", + "default": "", + "type": "text", + "required": True, + "label": "E-mail", + "placeholder": "example: username@domain.com", + "instructions": "This is the e-mail address you used to register with edX", + "restrictions": { + "min_length": 3, + "max_length": 254 }, }, { - u"name": u"password", - u"default": u"", - u"type": u"text", - u"required": True, - u"label": u"Password", - u"placeholder": u"", - u"instructions": u"", - u"restrictions": { - u"min_length": 2, - u"max_length": 75 + "name": "password", + "default": "", + "type": "text", + "required": True, + "label": "Password", + "placeholder": "", + "instructions": "", + "restrictions": { + "min_length": 2, + "max_length": 75 }, }, + { + "name": "remember", + "default": False, + "type": "checkbox", + "required": False, + "label": "Remember me", + "placeholder": "", + "instructions": "", + "restrictions": {}, + } ]) def test_login(self): @@ -623,6 +634,34 @@ class LoginSessionViewTest(ApiTestCase): response = self.client.get(reverse("dashboard")) 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): # Create a test user UserFactory.create(username=self.USERNAME, email=self.EMAIL, password=self.PASSWORD) diff --git a/common/djangoapps/user_api/views.py b/common/djangoapps/user_api/views.py index b25814a748..dde98e092e 100644 --- a/common/djangoapps/user_api/views.py +++ b/common/djangoapps/user_api/views.py @@ -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") @method_decorator(ensure_csrf_cookie)