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

feat: renamed DeprecatedRestApiClient from EdxRestApiClient #33603

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
30d8c1a
feat: renamed DeprecatedRestApiClient from EdxRestApiClient
Oct 26, 2023
2c562b7
Merge branch 'master' into edx-depr218
Yagnesh1998 Nov 1, 2023
c907655
feat: renamed DeprecatedRestApiClient
Nov 1, 2023
933c922
feat: add undefined-variable
Nov 1, 2023
7583935
Merge branch 'master' into edx-depr218
Yagnesh1998 Nov 1, 2023
ea643df
feat: add undefined-variable
Nov 1, 2023
a1b9bd8
feat: update utils.py
Nov 1, 2023
2f0833e
feat: add test file for DeprecatedRestApiClient
Nov 2, 2023
a99e8c1
feat: update __init__.py
Nov 2, 2023
bb34690
feat: update __init__.py and undefined-variable
Nov 2, 2023
4cd7f38
feat: Update __init__.py
Yagnesh1998 Nov 2, 2023
f614c8c
feat: Renamed functions and add undefined-variable
Nov 2, 2023
4a78e2e
Merge branch 'master' into edx-depr218
Yagnesh1998 Nov 2, 2023
9ec5bd2
feat: add new test class
Nov 2, 2023
e149fb3
feat: Update function name
Yagnesh1998 Nov 2, 2023
7b97592
feat: Update __init__.py
Yagnesh1998 Nov 2, 2023
dc452a2
Merge branch 'master' into edx-depr218
Yagnesh1998 Nov 8, 2023
6f29776
feat: add JwtAuth and BearerAuth
Nov 8, 2023
2fe5b5b
feat: update __init__.py
Nov 8, 2023
ba94b3d
feat: update utils.py
Nov 8, 2023
55d6550
feat: update utils.py
Nov 8, 2023
9d0ceda
feat: update patch
Nov 8, 2023
059da11
feat: update changes
Nov 8, 2023
03eb270
feat: add version
Nov 8, 2023
a996ded
Merge branch 'master' into edx-depr218
Yagnesh1998 Dec 6, 2023
f63c06c
Merge branch 'master' into edx-depr218
Yagnesh1998 Dec 6, 2023
5cbf590
Merge branch 'master' into edx-depr218
Yagnesh1998 Dec 7, 2023
9032fe8
Merge branch 'master' into edx-depr218
Yagnesh1998 Dec 12, 2023
9625159
feat: add slumber
Dec 12, 2023
d9ec06d
feat: update kernel.in
Dec 12, 2023
a317fdb
feat:update kernel.in
Dec 12, 2023
001e363
feat: update alphabetical order
Dec 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 95 additions & 1 deletion lms/djangoapps/commerce/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,28 @@
from urllib.parse import urljoin

import httpretty
import ddt
import os
import requests
from django.conf import settings
from django.test import TestCase
from freezegun import freeze_time
from edx_rest_api_client.auth import JwtAuth
from openedx.core.djangoapps.commerce.utils import DeprecatedRestApiClient, user_agent

from common.djangoapps.student.tests.factories import UserFactory
from openedx.core.djangoapps.commerce.utils import get_ecommerce_api_base_url, get_ecommerce_api_client
from openedx.core.djangoapps.oauth_dispatch.jwt import create_jwt_for_user

__version__ = '5.6.1'
URL = 'http://example.com/api/v2'
SIGNING_KEY = 'edx'
USERNAME = 'edx'
FULL_NAME = 'édx äpp'
EMAIL = 'edx@example.com'
TRACKING_CONTEXT = {'foo': 'bar'}
ACCESS_TOKEN = 'abc123'
JWT = 'abc.123.doremi'
JSON = 'application/json'
TEST_PUBLIC_URL_ROOT = 'http://www.example.com'
TEST_API_URL = 'http://www-internal.example.com/api'
Expand All @@ -25,7 +39,87 @@
}


class EdxRestApiClientTest(TestCase):
@ddt.ddt
class DeprecatedRestApiClientTests(TestCase):
"""
Tests for the edX Rest API client.
"""

@ddt.unpack
@ddt.data(
({'url': URL, 'signing_key': SIGNING_KEY, 'username': USERNAME,
'full_name': FULL_NAME, 'email': EMAIL}, JwtAuth),
({'url': URL, 'signing_key': SIGNING_KEY, 'username': USERNAME, 'full_name': None, 'email': EMAIL}, JwtAuth),
({'url': URL, 'signing_key': SIGNING_KEY, 'username': USERNAME,
'full_name': FULL_NAME, 'email': None}, JwtAuth),
({'url': URL, 'signing_key': SIGNING_KEY, 'username': USERNAME, 'full_name': None, 'email': None}, JwtAuth),
({'url': URL, 'signing_key': SIGNING_KEY, 'username': USERNAME}, JwtAuth),
({'url': URL, 'signing_key': None, 'username': USERNAME}, type(None)),
({'url': URL, 'signing_key': SIGNING_KEY, 'username': None}, type(None)),
({'url': URL, 'signing_key': None, 'username': None, 'oauth_access_token': None}, type(None))
)
def test_valid_configuration(self, kwargs, auth_type):
"""
The constructor should return successfully if all arguments are valid.
We also check that the auth type of the api is what we expect.
"""
api = DeprecatedRestApiClient(**kwargs)
self.assertIsInstance(api._store['session'].auth, auth_type) # pylint: disable=protected-access

@ddt.data(
{'url': None, 'signing_key': SIGNING_KEY, 'username': USERNAME},
{'url': None, 'signing_key': None, 'username': None, 'oauth_access_token': None},
)
def test_invalid_configuration(self, kwargs):
"""
If the constructor arguments are invalid, an InvalidConfigurationError should be raised.
"""
self.assertRaises(ValueError, DeprecatedRestApiClient, **kwargs)

@mock.patch('edx_rest_api_client.auth.JwtAuth.__init__', return_value=None)
def test_tracking_context(self, mock_auth):
"""
Ensure the tracking context is included with API requests if specified.
"""
DeprecatedRestApiClient(URL, SIGNING_KEY, USERNAME, FULL_NAME, EMAIL, tracking_context=TRACKING_CONTEXT)
self.assertIn(TRACKING_CONTEXT, mock_auth.call_args[1].values())

def test_oauth2(self):
"""
Ensure OAuth2 authentication is used when an access token is supplied to the constructor.
"""

with mock.patch('openedx.core.djangoapps.commerce.utils.BearerAuth.__init__', return_value=None) as mock_auth:
DeprecatedRestApiClient(URL, oauth_access_token=ACCESS_TOKEN)
mock_auth.assert_called_with(ACCESS_TOKEN)

def test_supplied_jwt(self):
"""Ensure JWT authentication is used when a JWT is supplied to the constructor."""
with mock.patch('edx_rest_api_client.auth.SuppliedJwtAuth.__init__', return_value=None) as mock_auth:
DeprecatedRestApiClient(URL, jwt=JWT)
mock_auth.assert_called_with(JWT)

def test_user_agent(self):
"""Make sure our custom User-Agent is getting built correctly."""
with mock.patch('socket.gethostbyname', return_value='test_hostname'):
default_user_agent = user_agent()
self.assertIn('python-requests', default_user_agent)
self.assertIn(f'edx-rest-api-client/{__version__}', default_user_agent)
self.assertIn('test_hostname', default_user_agent)

with mock.patch('socket.gethostbyname') as mock_gethostbyname:
mock_gethostbyname.side_effect = ValueError()
default_user_agent = user_agent()
self.assertIn('unknown_client_name', default_user_agent)

with mock.patch.dict(os.environ, {'EDX_REST_API_CLIENT_NAME': "awesome_app"}):
uagent = user_agent()
self.assertIn('awesome_app', uagent)

self.assertEqual(user_agent(), DeprecatedRestApiClient.user_agent())


class DeprecatedRestApiClientTest(TestCase):
"""
Tests to ensure the client is initialized properly.
"""
Expand Down
Loading
Loading