Updated CredentialsApiConfig to pull URLs from settings

Pulling URLs from settings allows us to rely on site configuration overrides in order to support multi-tenancy.

LEARNER-1103
This commit is contained in:
Clinton Blackburn
2017-05-24 15:46:00 -04:00
committed by Clinton Blackburn
parent 8a4bb2e670
commit 800bcd8e20
9 changed files with 129 additions and 91 deletions

View File

@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('credentials', '0002_auto_20160325_0631'),
]
operations = [
migrations.AlterField(
model_name='credentialsapiconfig',
name='internal_service_url',
field=models.URLField(help_text=b'DEPRECATED: Use the setting CREDENTIALS_INTERNAL_SERVICE_URL.', verbose_name='Internal Service URL'),
),
migrations.AlterField(
model_name='credentialsapiconfig',
name='public_service_url',
field=models.URLField(help_text=b'DEPRECATED: Use the setting CREDENTIALS_PUBLIC_SERVICE_URL.', verbose_name='Public Service URL'),
),
]

View File

@@ -4,10 +4,12 @@ Models for credentials support for the LMS and Studio.
from urlparse import urljoin
from django.utils.translation import ugettext_lazy as _
from django.db import models
from config_models.models import ConfigurationModel
from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
from openedx.core.djangoapps.site_configuration import helpers
API_VERSION = 'v2'
@@ -17,35 +19,42 @@ class CredentialsApiConfig(ConfigurationModel):
Manages configuration for connecting to the Credential service and using its
API.
"""
class Meta(object):
app_label = "credentials"
app_label = 'credentials'
OAUTH2_CLIENT_NAME = 'credentials'
API_NAME = 'credentials'
CACHE_KEY = 'credentials.api.data'
internal_service_url = models.URLField(verbose_name=_("Internal Service URL"))
public_service_url = models.URLField(verbose_name=_("Public Service URL"))
internal_service_url = models.URLField(
verbose_name=_('Internal Service URL'),
help_text='DEPRECATED: Use the setting CREDENTIALS_INTERNAL_SERVICE_URL.'
)
public_service_url = models.URLField(
verbose_name=_('Public Service URL'),
help_text='DEPRECATED: Use the setting CREDENTIALS_PUBLIC_SERVICE_URL.'
)
enable_learner_issuance = models.BooleanField(
verbose_name=_("Enable Learner Issuance"),
verbose_name=_('Enable Learner Issuance'),
default=False,
help_text=_(
"Enable issuance of credentials via Credential Service."
'Enable issuance of credentials via Credential Service.'
)
)
enable_studio_authoring = models.BooleanField(
verbose_name=_("Enable Authoring of Credential in Studio"),
verbose_name=_('Enable Authoring of Credential in Studio'),
default=False,
help_text=_(
"Enable authoring of Credential Service credentials in Studio."
'Enable authoring of Credential Service credentials in Studio.'
)
)
cache_ttl = models.PositiveIntegerField(
verbose_name=_("Cache Time To Live"),
verbose_name=_('Cache Time To Live'),
default=0,
help_text=_(
"Specified in seconds. Enable caching by setting this to a value greater than 0."
'Specified in seconds. Enable caching by setting this to a value greater than 0.'
)
)
@@ -55,32 +64,26 @@ class CredentialsApiConfig(ConfigurationModel):
@property
def internal_api_url(self):
"""
Generate a URL based on internal service URL and API version number.
Internally-accessible API URL root.
"""
return urljoin(self.internal_service_url, '/api/{}/'.format(API_VERSION))
root = helpers.get_value('CREDENTIALS_INTERNAL_SERVICE_URL', settings.CREDENTIALS_INTERNAL_SERVICE_URL)
return urljoin(root, '/api/{}/'.format(API_VERSION))
@property
def public_api_url(self):
"""
Generate a URL based on public service URL and API version number.
Publicly-accessible API URL root.
"""
return urljoin(self.public_service_url, '/api/{}/'.format(API_VERSION))
root = helpers.get_value('CREDENTIALS_PUBLIC_SERVICE_URL', settings.CREDENTIALS_PUBLIC_SERVICE_URL)
return urljoin(root, '/api/{}/'.format(API_VERSION))
@property
def is_learner_issuance_enabled(self):
"""
Indicates whether the learner credential should be enabled or not.
Returns boolean indicating if credentials should be issued.
"""
return self.enabled and self.enable_learner_issuance
@property
def is_studio_authoring_enabled(self):
"""
Indicates whether Studio functionality related to Credential should
be enabled or not.
"""
return self.enabled and self.enable_studio_authoring
@property
def is_cache_enabled(self):
"""Whether responses from the Credentials API will be cached."""

View File

@@ -1,27 +1,34 @@
"""Tests for models supporting Credentials-related functionality."""
from django.test import TestCase
from django.test import TestCase, override_settings
from nose.plugins.attrib import attr
from openedx.core.djangoapps.credentials.models import API_VERSION
from openedx.core.djangoapps.credentials.tests.mixins import CredentialsApiConfigMixin
from openedx.core.djangolib.testing.utils import skip_unless_lms
CREDENTIALS_INTERNAL_SERVICE_URL = 'https://credentials.example.com'
CREDENTIALS_PUBLIC_SERVICE_URL = 'https://credentials.example.com'
@skip_unless_lms
@attr(shard=2)
class TestCredentialsApiConfig(CredentialsApiConfigMixin, TestCase):
"""Tests covering the CredentialsApiConfig model."""
@override_settings(
CREDENTIALS_INTERNAL_SERVICE_URL=CREDENTIALS_INTERNAL_SERVICE_URL,
CREDENTIALS_PUBLIC_SERVICE_URL=CREDENTIALS_PUBLIC_SERVICE_URL
)
def test_url_construction(self):
"""Verify that URLs returned by the model are constructed correctly."""
credentials_config = self.create_credentials_config()
self.assertEqual(
credentials_config.internal_api_url,
credentials_config.internal_service_url.strip('/') + '/api/v2/')
expected = '{root}/api/{version}/'.format(root=CREDENTIALS_INTERNAL_SERVICE_URL.strip('/'), version=API_VERSION)
self.assertEqual(credentials_config.internal_api_url, expected)
self.assertEqual(
credentials_config.public_api_url,
credentials_config.public_service_url.strip('/') + '/api/v2/')
expected = '{root}/api/{version}/'.format(root=CREDENTIALS_PUBLIC_SERVICE_URL.strip('/'), version=API_VERSION)
self.assertEqual(credentials_config.public_api_url, expected)
def test_is_learner_issuance_enabled(self):
"""
@@ -36,17 +43,3 @@ class TestCredentialsApiConfig(CredentialsApiConfigMixin, TestCase):
credentials_config = self.create_credentials_config()
self.assertTrue(credentials_config.is_learner_issuance_enabled)
def test_is_studio_authoring_enabled(self):
"""
Verify that the property controlling display in the Studio authoring is only True
when configuration is enabled and all required configuration is provided.
"""
credentials_config = self.create_credentials_config(enabled=False)
self.assertFalse(credentials_config.is_studio_authoring_enabled)
credentials_config = self.create_credentials_config(enable_studio_authoring=False)
self.assertFalse(credentials_config.is_studio_authoring_enabled)
credentials_config = self.create_credentials_config()
self.assertTrue(credentials_config.is_studio_authoring_enabled)