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

Allow to configure the plugin using environment variables #8

Merged
merged 2 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 15 additions & 9 deletions ckanext/baepublisher/store_connector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

# Copyright (c) 2015 CoNWeT Lab., Universidad Politécnica de Madrid
# Copyright (c) 2018 Future Internet Consulting and Development Solutions S.L.

# This file is part of CKAN BAE Publisher Extension.

Expand All @@ -18,18 +19,18 @@

from __future__ import unicode_literals

import ckan.model as model
import ckan.plugins as plugins
from datetime import datetime
from decimal import Decimal
import logging
import os
import re

from datetime import datetime
from unicodedata import normalize
from decimal import Decimal
from requests_oauthlib import OAuth2Session
from urlparse import urlparse

import ckan.model as model
import ckan.plugins as plugins
from requests_oauthlib import OAuth2Session

log = logging.getLogger(__name__)


Expand Down Expand Up @@ -61,10 +62,15 @@ def __init__(self, config):
if not len(self.store_url):
raise StoreException('A store URL for the baepublisher has not been provided')

self.verify_https = not bool(os.environ.get('OAUTHLIB_INSECURE_TRANSPORT'))
self.verify_https = os.environ.get('OAUTHLIB_INSECURE_TRANSPORT', 'false').strip().lower() in ('', 'false', '0', 'off')

def _get_url(self, config, config_property):
url = config.get(config_property, '')
env_name = config_property.upper().replace('.', '_')
url = os.environ.get(env_name, '').strip()

if url == '':
url = config.get(config_property, '')

url = url[:-1] if url.endswith('/') else url
return url

Expand Down Expand Up @@ -405,7 +411,7 @@ def delete_attached_resources(self, dataset):

try:
products = self._get_existing_products(dataset)
except:
except Exception:
# An exeption accessing the BAE should not be propagated to avoid exeption on non published datasets
return

Expand Down
58 changes: 48 additions & 10 deletions ckanext/baepublisher/tests/test_store_connector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

# Copyright (C) 2015 - 2018 Conwet Lab., Universidad Politécnica de Madrid
# Copyright (c) 2018 Future Internet Consulting and Development Solutions S.L.

# This file is part of CKAN BAE Publisher Extension.

Expand All @@ -24,7 +25,7 @@
import requests
from decimal import Decimal

from mock import MagicMock, call
from mock import call, MagicMock, patch
from nose_parameterized import parameterized

# Need to be defined here, since it will be used as tests parameter
Expand Down Expand Up @@ -102,21 +103,58 @@ def tearDown(self):
self.instance._get_offering = self._get_offering

@parameterized.expand([
('%s' % BASE_SITE_URL, '%s' % BASE_STORE_URL),
('%s/' % BASE_SITE_URL, '%s' % BASE_STORE_URL),
('%s' % BASE_SITE_URL, '%s/' % BASE_STORE_URL),
('%s/' % BASE_SITE_URL, '%s/' % BASE_STORE_URL)
(
{'ckan.site_url': BASE_SITE_URL, 'ckan.baepublisher.store_url': BASE_STORE_URL},
{},
True,
),
(
{'ckan.site_url': BASE_SITE_URL + '/', 'ckan.baepublisher.store_url': BASE_STORE_URL + '/'},
{'OAUTHLIB_INSECURE_TRANSPORT': '1'},
False,
),
(
{'ckan.site_url': 'https://ckan.example.org', 'ckan.baepublisher.store_url': 'https://market.example.org'},
{'CKAN_SITE_URL': BASE_SITE_URL, 'CKAN_BAEPUBLISHER_STORE_URL': BASE_STORE_URL, 'OAUTHLIB_INSECURE_TRANSPORT': 'tRuE'},
False,
),
(
{'ckan.baepublisher.store_url': BASE_STORE_URL + '/'},
{'CKAN_SITE_URL': BASE_SITE_URL, 'OAUTHLIB_INSECURE_TRANSPORT': 'oN'},
False,
),
(
{},
{'CKAN_SITE_URL': BASE_SITE_URL + '/', 'CKAN_BAEPUBLISHER_STORE_URL': BASE_STORE_URL + '/', 'OAUTHLIB_INSECURE_TRANSPORT': '0'},
True,
),
(
{'ckan.site_url': BASE_SITE_URL},
{'CKAN_BAEPUBLISHER_STORE_URL': BASE_STORE_URL, 'OAUTHLIB_INSECURE_TRANSPORT': 'fAlSe'},
True,
),
(
{'ckan.site_url': BASE_SITE_URL},
{'CKAN_SITE_URL': BASE_SITE_URL, 'CKAN_BAEPUBLISHER_STORE_URL': BASE_STORE_URL, 'OAUTHLIB_INSECURE_TRANSPORT': 'oFf'},
True,
),
])
def test_init(self, site_url, store_url):
@patch('ckanext.baepublisher.store_connector.os')
def test_init(self, config, env, expected_verify, os):

config = {
'ckan.site_url': site_url,
'ckan.baepublisher.store_url': store_url,
}
os.environ = env

instance = store_connector.StoreConnector(config)

self.assertEquals(BASE_SITE_URL, instance.site_url)
self.assertEquals(BASE_STORE_URL, instance.store_url)
self.assertEquals(expected_verify, instance.verify_https)

@patch('ckanext.baepublisher.store_connector.os')
def test_init_missing_store_url(self, os):
os.environ = {}
with self.assertRaises(store_connector.StoreException):
store_connector.StoreConnector({})

@parameterized.expand([
(0,),
Expand Down