Skip to content

Commit

Permalink
Merge pull request #108 from edly-io/rehan/EDLY-1474
Browse files Browse the repository at this point in the history
Adds edly cookie set method for social auth
  • Loading branch information
rehan99000 authored Jun 17, 2020
2 parents 343621a + a7c03c1 commit 6977126
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 11 deletions.
2 changes: 0 additions & 2 deletions openedx/core/djangoapps/user_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from openedx.core.djangoapps.user_authn.cookies import set_logged_in_cookies
from openedx.core.djangoapps.user_authn.views.register import create_account_with_params
from openedx.core.lib.api.permissions import ApiKeyHeaderPermission
from openedx.features.edly.cookies import set_logged_in_edly_cookies
from openedx.features.edly.utils import create_user_link_with_edly_sub_organization
from student.helpers import AccountValidationError
from util.json_request import JsonResponse
Expand Down Expand Up @@ -174,7 +173,6 @@ def post(self, request):

response = JsonResponse({"success": True})
set_logged_in_cookies(request, response, user)
set_logged_in_edly_cookies(request, response, user)
return response

@method_decorator(transaction.non_atomic_requests)
Expand Down
2 changes: 2 additions & 0 deletions openedx/core/djangoapps/user_authn/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from openedx.core.djangoapps.oauth_dispatch.jwt import create_jwt_from_token
from openedx.core.djangoapps.user_api.accounts.utils import retrieve_last_sitewide_block_completed
from openedx.core.djangoapps.user_authn.exceptions import AuthFailedError
from openedx.features.edly.cookies import set_logged_in_edly_cookies
from student.models import CourseEnrollment


Expand Down Expand Up @@ -146,6 +147,7 @@ def set_logged_in_cookies(request, response, user):
_set_deprecated_logged_in_cookie(response, cookie_settings)
_set_deprecated_user_info_cookie(response, request, user, cookie_settings)
_create_and_set_jwt_cookies(response, request, cookie_settings, user=user)
set_logged_in_edly_cookies(request, response, user, cookie_settings)
CREATE_LOGON_COOKIE.send(sender=None, user=user, response=response)

return response
Expand Down
2 changes: 0 additions & 2 deletions openedx/core/djangoapps/user_authn/views/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
from openedx.core.djangoapps.util.user_messages import PageLevelMessages
from openedx.core.djangolib.markup import HTML, Text
from openedx.features.edly.cookies import set_logged_in_edly_cookies
from student.models import LoginFailures
from student.views import send_reactivation_email_for_user
from student.forms import send_password_reset_email_for_user
Expand Down Expand Up @@ -378,7 +377,6 @@ def login_user(request):

# Ensure that the external marketing site can
# detect that the user is logged in.
set_logged_in_edly_cookies(request, response, possibly_authenticated_user)
return set_logged_in_cookies(request, response, possibly_authenticated_user)
except AuthFailedError as error:
log.exception(error.get_response())
Expand Down
4 changes: 1 addition & 3 deletions openedx/features/edly/cookies.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from django.conf import settings

from openedx.core.djangoapps.user_authn.cookies import standard_cookie_settings
from openedx.features.edly.models import EdlySubOrganization
from openedx.features.edly.utils import encode_edly_user_info_cookie
from student import auth
from student.roles import CourseCreatorRole


def set_logged_in_edly_cookies(request, response, user):
def set_logged_in_edly_cookies(request, response, user, cookie_settings):
"""
Set cookies for edly users at the time of login.
Expand All @@ -22,7 +21,6 @@ def set_logged_in_edly_cookies(request, response, user):
"""
if user.is_authenticated and not user.is_anonymous:
cookie_settings = standard_cookie_settings(request)
edly_cookie_string = _get_edly_user_info_cookie_string(request)

response.set_cookie(
Expand Down
8 changes: 5 additions & 3 deletions openedx/features/edly/tests/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.http import HttpResponse
from django.test import RequestFactory, TestCase

from openedx.core.djangoapps.user_authn.cookies import standard_cookie_settings as cookie_settings
from openedx.features.edly.tests.factories import SiteFactory, EdlySubOrganizationFactory
from openedx.features.edly import cookies as cookies_api
from student import auth
Expand All @@ -27,6 +28,7 @@ def setUp(self):
self.request.user = self.user
self.request.session = self._get_stub_session()
self.request.site = SiteFactory()
self.cookie_settings = cookie_settings(self.request)

def _get_stub_session(self, expire_at_browser_close=False, max_age=604800):
return MagicMock(
Expand Down Expand Up @@ -64,14 +66,14 @@ def test_set_logged_in_edly_cookies(self):
Tests that Edly user info cookie is set correctly.
"""
self._create_edly_sub_organization()
response = cookies_api.set_logged_in_edly_cookies(self.request, HttpResponse(), self.user)
response = cookies_api.set_logged_in_edly_cookies(self.request, HttpResponse(), self.user, self.cookie_settings)
assert settings.EDLY_USER_INFO_COOKIE_NAME in response.cookies

def test_set_logged_in_edly_cookies_with_no_edly_sub_organiztion(self):
"""
Tests that Edly user info cookie is set to empty in absence of edly sub-organization.
"""
response = cookies_api.set_logged_in_edly_cookies(self.request, HttpResponse(), self.user)
response = cookies_api.set_logged_in_edly_cookies(self.request, HttpResponse(), self.user, self.cookie_settings)
assert settings.EDLY_USER_INFO_COOKIE_NAME in response.cookies
assert response.cookies.get(settings.EDLY_USER_INFO_COOKIE_NAME).value == ''

Expand All @@ -80,7 +82,7 @@ def test_delete_logged_in_edly_cookies(self):
Tests that Edly user info cookie is deleted correctly.
"""
self._create_edly_sub_organization()
response = cookies_api.set_logged_in_edly_cookies(self.request, HttpResponse(), self.user)
response = cookies_api.set_logged_in_edly_cookies(self.request, HttpResponse(), self.user, self.cookie_settings)
assert settings.EDLY_USER_INFO_COOKIE_NAME in response.cookies

cookies_api.delete_logged_in_edly_cookies(response)
Expand Down
7 changes: 6 additions & 1 deletion openedx/features/edly/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.test import TestCase
from django.test.client import RequestFactory

from openedx.core.djangoapps.user_authn.cookies import standard_cookie_settings as cookie_settings
from openedx.core.djangolib.testing.utils import skip_unless_cms
from openedx.features.edly import cookies as cookies_api
from openedx.features.edly.tests.factories import EdlySubOrganizationFactory, EdlyUserProfileFactory, SiteFactory
Expand Down Expand Up @@ -114,7 +115,11 @@ def test_user_with_organization_access(self):
Test user have access to a valid site URL which is linked to that user.
"""
self._create_edly_sub_organization()
response = cookies_api.set_logged_in_edly_cookies(self.request, HttpResponse(), self.user)
response = cookies_api.set_logged_in_edly_cookies(
self.request, HttpResponse(),
self.user,
cookie_settings(self.request)
)
self._copy_cookies_to_request(response, self.request)

user_has_access = user_has_edly_organization_access(self.request)
Expand Down

0 comments on commit 6977126

Please sign in to comment.