-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from pennlabs/feature/settings
Feature/settings
- Loading branch information
Showing
15 changed files
with
270 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
version: 2 | ||
jobs: | ||
build: | ||
working_directory: ~/django-labs-accounts | ||
docker: | ||
- image: themattrix/tox | ||
steps: | ||
- checkout | ||
- run: | ||
name: Install dependencies | ||
command: | | ||
pip install tox coveralls | ||
- run: | ||
name: Run tests | ||
command: | | ||
tox | ||
coveralls | ||
workflows: | ||
version: 2 | ||
build: | ||
jobs: | ||
- build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[run] | ||
source = labs_accounts | ||
source = accounts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
Changelog | ||
========= | ||
|
||
0.2.0 (2019-03-24) | ||
------------------ | ||
* New feature: Provide an easier way to access settings through a new `accounts_settings` object | ||
|
||
0.1.0 (2019-03-17) | ||
------------------ | ||
* Initial Release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,36 @@ | ||
import os | ||
from django.conf import settings | ||
|
||
CLIENT_ID = getattr(settings, "OAUTH2_CLIENT_ID", os.environ.get('OAUTH2_CLIENT_ID')) | ||
CLIENT_SECRET = getattr(settings, "OAUTH2_CLIENT_SECRET", os.environ.get('OAUTH2_CLIENT_ID')) | ||
REDIRECT_URI = getattr(settings, "OAUTH2_REDIRECT_URI", os.environ.get('OAUTH2_REDIRECT_URI')) | ||
SCOPE = getattr(settings, "OAUTH2_SCOPES", ['read', 'introspection']) | ||
PLATFORM_URL = getattr(settings, "OAUTH2_PLATFORM_URL", 'https://platform.pennlabs.org') | ||
|
||
USER_SETTINGS = getattr(settings, "PLATFORM_ACCOUNTS", {}) | ||
|
||
DEFAULTS = { | ||
'CLIENT_ID': os.environ.get('LABS_CLIENT_ID'), | ||
'CLIENT_SECRET': os.environ.get('LABS_CLIENT_SECRET'), | ||
'REDIRECT_URI': os.environ.get('LABS_REDIRECT_URI'), | ||
'SCOPE': ['read', 'introspection'], | ||
'PLATFORM_URL': 'https://platform.pennlabs.org', | ||
} | ||
|
||
|
||
class AccountsSettings(object): | ||
""" | ||
Based on https://github.com/encode/django-rest-framework/blob/master/rest_framework/settings.py | ||
""" | ||
def __init__(self, settings=None, defaults=None): | ||
self.settings = settings or {} | ||
self.defaults = defaults or {} | ||
|
||
def __getattr__(self, attr): | ||
if attr not in self.defaults.keys(): | ||
raise AttributeError("Invalid Penn Labs accounts setting: %s" % attr) | ||
|
||
try: | ||
val = self.settings[attr] | ||
except KeyError: | ||
val = self.defaults[attr] | ||
|
||
setattr(self, attr, val) | ||
return val | ||
|
||
|
||
accounts_settings = AccountsSettings(USER_SETTINGS, DEFAULTS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import sys | ||
import django | ||
from django.test.runner import DiscoverRunner | ||
|
||
django.setup() | ||
test_runner = DiscoverRunner(verbosity=1) | ||
|
||
failures = test_runner.run_tests(['tests']) | ||
if failures: | ||
sys.exit(failures) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
SECRET_KEY = 'supersecret' | ||
|
||
ALLOWED_HOSTS = [] | ||
|
||
INSTALLED_APPS = ( | ||
'django.contrib.auth', | ||
'django.contrib.contenttypes', | ||
'django.contrib.sessions', | ||
'django.contrib.sites', | ||
'django.contrib.admin', | ||
'django.contrib.messages', | ||
'accounts', | ||
'tests' | ||
) | ||
|
||
MIDDLEWARE = [ | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.middleware.common.CommonMiddleware', | ||
'django.middleware.csrf.CsrfViewMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'django.contrib.messages.middleware.MessageMiddleware', | ||
] | ||
|
||
ROOT_URLCONF = 'tests.urls' | ||
|
||
TEMPLATES = [ | ||
{ | ||
'BACKEND': 'django.template.backends.django.DjangoTemplates', | ||
'DIRS': [], | ||
'APP_DIRS': True, | ||
'OPTIONS': { | ||
'debug': True, | ||
'context_processors': [ | ||
'django.contrib.auth.context_processors.auth', | ||
'django.template.context_processors.debug', | ||
'django.template.context_processors.i18n', | ||
'django.template.context_processors.media', | ||
'django.template.context_processors.static', | ||
'django.template.context_processors.tz', | ||
'django.contrib.messages.context_processors.messages', | ||
], | ||
}, | ||
}, | ||
] | ||
|
||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.sqlite3', | ||
'NAME': 'example.sqlite', | ||
} | ||
} | ||
|
||
|
||
LANGUAGE_CODE = 'en-us' | ||
|
||
TIME_ZONE = 'UTC' | ||
|
||
SITE_ID = 1 | ||
|
||
USE_I18N = True | ||
|
||
USE_L10N = True | ||
|
||
USE_TZ = True | ||
|
||
STATIC_URL = '/static/' | ||
|
||
AUTH_USER_MODEL = 'accounts.User' | ||
|
||
AUTHENTICATION_BACKENDS = ( | ||
'accounts.backends.LabsUserBackend', | ||
'django.contrib.auth.backends.ModelBackend', | ||
) | ||
|
||
PLATFORM_ACCOUNTS = { | ||
'CLIENT_ID': 'id', | ||
'CLIENT_SECRET': 'secret', | ||
'REDIRECT_URI': 'example', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.test import TestCase | ||
from accounts.apps import AccountsConfig | ||
|
||
|
||
class AppsTestCase(TestCase): | ||
def test_apps(self): | ||
self.assertEqual(AccountsConfig.name, 'accounts') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django.test import TestCase | ||
from tests.settings import PLATFORM_ACCOUNTS | ||
from accounts.settings import accounts_settings, DEFAULTS | ||
|
||
|
||
class SettingsTestCase(TestCase): | ||
def test_invalid_setting(self): | ||
with self.assertRaises(AttributeError): | ||
accounts_settings.INVALID_SETTING | ||
|
||
def test_defined_setting(self): | ||
self.assertEqual(accounts_settings.CLIENT_ID, PLATFORM_ACCOUNTS['CLIENT_ID']) | ||
|
||
def test_default_setting(self): | ||
self.assertEqual(accounts_settings.SCOPE, DEFAULTS['SCOPE']) |
Oops, something went wrong.