Skip to content

Commit

Permalink
Add tests and docs for config classes
Browse files Browse the repository at this point in the history
This commit adds a few basic tests to check things work as expected.
  • Loading branch information
maiksprenger committed Jul 8, 2015
1 parent 9df0ce6 commit b62b46e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 26 deletions.
41 changes: 22 additions & 19 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,34 @@ Add ``'adyen'`` to ``INSTALLED_APPS`` and run::
to create the appropriate database tables.

Configuration
-------------
=============

Edit your ``settings.py`` to set the following settings:

.. code-block:: python
You have two approaches to configure `django-oscar-adyen`.

ADYEN_IDENTIFIER = 'YourAdyenAccountName'
ADYEN_SECRET_KEY = 'YourAdyenSkinSecretKey'
ADYEN_ACTION_URL = 'https://test.adyen.com/hpp/select.shtml'
Settings-based configuration
----------------------------
For simple deployments, setting the required values in the settings will suffice.

Obviously, you'll need to specify different settings in your test environment
as opposed to your production environment.
Edit your ``settings.py`` to set the following settings:

* ``ADYEN_IDENTIFIER`` - The identifier of your Adyen account.
* ``ADYEN_SKIN_CODE`` - The code for your Adyen skin.
* ``ADYEN_SECRET_KEY`` - The secret key defined in your Adyen skin.
* ``ADYEN_ACTION_URL`` -
The URL towards which the Adyen form should be POSTed to initiate the payment process
(e.g. 'https://test.adyen.com/hpp/select.shtml').
* ``ADYEN_IP_ADDRESS_HTTP_HEADER`` - Optional. The header in `META` to inspect to determine
the IP address of the request. Defaults to `REMOTE_ADDR`.

Settings
========
You will likely need to specify different settings in your test environment
as opposed to your production environment.

====================== =========================================================
Setting Description
---------------------- ---------------------------------------------------------
``ADYEN_IDENTIFIER`` The identifier of your Adyen account
``ADYEN_SECRET_KEY`` The secret key defined in your Adyen skin
``ADYEN_ACTION_URL`` The URL towards which the Adyen form should be POSTed
to initiate the payment process
====================== =========================================================
Class-based configuration
-------------------------
In more complex deployments, you will want to e.g. alter the Adyen identifier based on
the request. That is not easily implemented with Django settings, so you can alternatively
set ``ADYEN_CONFIG_CLASS`` to a config class of your own.
See `adyen.settings_config.FromSettingsConfig` for an example.

License
=======
Expand Down
7 changes: 0 additions & 7 deletions tests/test_adyen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import six
from unittest.mock import Mock

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
from django.test.utils import override_settings

Expand Down Expand Up @@ -140,11 +138,6 @@ def test_form_action(self):
action_url = self.scaffold.get_form_action()
self.assertEqual(action_url, TEST_ACTION_URL)

# If the setting is missing, a proper exception is raised
del settings.ADYEN_ACTION_URL
with self.assertRaises(ImproperlyConfigured):
self.scaffold.get_form_action()

def test_form_fields_ok(self):
"""
Test that the payment form fields are properly built.
Expand Down
61 changes: 61 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from django.test import TestCase
from django.test.utils import override_settings

from adyen.config import get_config


@override_settings(
ADYEN_IDENTIFIER='foo',
ADYEN_SECRET_KEY='foo',
ADYEN_ACTION_URL='foo',
ADYEN_SKIN_CODE='foo',
)
class FromSettingsTestCase(TestCase):
"""
This test case tests the FromSettings confic class, which just fetches its
values from the Django settings.
"""

def test_is_default(self):
assert 'FromSettingsConfig' in get_config().__class__.__name__

This comment has been minimized.

Copy link
@aaugustin

aaugustin Jul 8, 2015

assert isinstance(get_config(), FromSettingsConfig) would be semantically more appropriate, wouldn't it?

This comment has been minimized.

Copy link
@maiksprenger

maiksprenger Jul 8, 2015

Author Member

True. For some reason I wanted to avoid the import, but I forgot why.


def test_value_passing_works(self):
assert get_config().get_action_url() == 'foo'

This comment has been minimized.

Copy link
@aaugustin

aaugustin Jul 8, 2015

This test would still pass if get_action_url() returned ADYEN_IDENTIFIER.

This comment has been minimized.

Copy link
@maiksprenger

maiksprenger Jul 8, 2015

Author Member

Okay, I'll switch to using the more life-like test constants from test_adyen.py


# https://docs.djangoproject.com/en/1.8/topics/testing/tools/#django.test.modify_settings
# Override settings is needed to let us delete settings on a per-test basis.
@override_settings()
def test_complains_when_not_fully_configured(self):
# If the setting is missing, a proper exception is raised
del settings.ADYEN_ACTION_URL
with self.assertRaises(ImproperlyConfigured):
get_config()


class DummyConfigClass:

def get_action_url(self):
return 'foo'


@override_settings(ADYEN_CONFIG_CLASS='tests.test_config.DummyConfigClass')
class CustomConfigClassTestCase(TestCase):
"""
This test case checks that it's possible to replace the FromSettings confic class
by one's own, and that it is used to fetch values as expected.
"""

def test_class_gets_picked_up(self):
assert 'DummyConfigClass' in get_config().__class__.__name__

@override_settings(ADYEN_ACTION_URL='bar')
def test_settings_ignored(self):
"""
Check that we indeed ignore Django settings (apart from the config class).
"""
assert get_config().get_action_url() == 'foo'


0 comments on commit b62b46e

Please sign in to comment.