Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
F174 auth ldap (#29)
Browse files Browse the repository at this point in the history
* Adding a new module that allows for new users to be associated with the hospital and given HCA role when first logging in via LDAP

* Renaming module for Odoo install GUI

* Adding unit tests and exceptions

* Linting fixes and adding LDAP to tests to run

* Adding LDAP to requirements as turns out isn't installed by default by auth_ldap module :S
  • Loading branch information
colinfwren authored Jun 28, 2016
1 parent 8f6ad45 commit f1a5e6e
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ env:
EXCLUDE="xml_test_output"
matrix:
- LINT_CHECK="1"
- ODOO_REPO="valdecdev/odoo" RUN_PIPELINE="1" TESTS="1" INCLUDE="nh_odoo_fixes,nh_activity,nh_clinical"
- ODOO_REPO="valdecdev/odoo" RUN_PIPELINE="1" TESTS="1" INCLUDE="nh_odoo_fixes,nh_activity,nh_clinical,nh_clinical_ldap"
VERSION="openeobs-8-12"
virtualenv:
system_site_packages: true
Expand Down
3 changes: 3 additions & 0 deletions nh_clinical_ldap/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Part of NHClinical. See LICENSE file for full copyright and licensing details
# -*- coding: utf-8 -*-
from . import auth_ldap_extension
21 changes: 21 additions & 0 deletions nh_clinical_ldap/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Part of NHClinical. See LICENSE file for full copyright and licensing details
# -*- coding: utf-8 -*-
{
'name': 'NH Clinical LDAP',
'version': '0.1',
'category': 'Base',
'license': 'AGPL-3',
'summary': 'NH LDAP Extension',
'description': """ LDAP for NH Clinical """,
'author': 'Neova Health',
'website': 'http://www.neovahealth.co.uk/',
'depends': ['auth_ldap', 'nh_clinical'],
'data': [],
'demo': [],
'images': [],
'css': [],
'test': [],
'application': True,
'installable': True,
'active': False,
}
50 changes: 50 additions & 0 deletions nh_clinical_ldap/auth_ldap_extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Part of NHClinical. See LICENSE file for full copyright and licensing details
# -*- coding: utf-8 -*-
from openerp.osv import orm
import logging

_logger = logging.getLogger(__name__)


class NHClinicalLDAPExtension(orm.Model):

_name = 'res.company.ldap'
_inherit = 'res.company.ldap'

def map_ldap_attributes(self, cr, uid, conf, login, ldap_entry,
context=None):
"""
Compose values for a new resource of model res_users,
based upon the retrieved ldap entry and the LDAP settings.
:param dict conf: LDAP configuration
:param login: the new user's login
:param tuple ldap_entry: single LDAP result (dn, attrs)
:return: parameters for a new resource of model res_users
:rtype: dict
"""

category_pool = self.pool['res.partner.category']
location_pool = self.pool['nh.clinical.location']
pos_pool = self.pool['nh.clinical.pos']

hospital = location_pool.search(cr, uid, [['usage', '=', 'hospital']],
context=context)
pos = pos_pool.search(cr, uid, [['location_id', 'in', hospital]],
context=context)
hca_group = category_pool.search(cr, uid, [['name', '=', 'HCA']],
context=context)

if len(ldap_entry) < 2:
raise ValueError('LDAP Entry does not contain second element')
if len(ldap_entry[1].get('cn')) < 1:
raise ValueError('LDAP Entry CN does not contain elements')

values = {'name': ldap_entry[1]['cn'][0],
'login': login,
'company_id': conf.get('company'),
'ward_ids': [[6, 0, []]],
'pos_ids': [[6, 0, pos]],
'category_id': [[6, 0, hca_group]]
}
return values
1 change: 1 addition & 0 deletions nh_clinical_ldap/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_map_ldap_extension
113 changes: 113 additions & 0 deletions nh_clinical_ldap/tests/test_map_ldap_extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
from openerp.tests.common import SingleTransactionCase


class TestMapLDAPExtension(SingleTransactionCase):

@classmethod
def setUpClass(cls):
super(TestMapLDAPExtension, cls).setUpClass()
cls.category_pool = cls.registry('res.partner.category')
cls.location_pool = cls.registry('nh.clinical.location')
cls.pos_pool = cls.registry('nh.clinical.pos')
cls.ldap_pool = cls.registry('res.company.ldap')

cls.ldap_entry = [
None,
{
'cn': [
'Test User'
]
}
]

cls.conf = {
'company': 1
}

def mock_category_search(*args, **kwargs):
return [1]

def mock_location_search(*args, **kwargs):
context = kwargs.get('context')
test = context.get('test') if context else ''
if test == 'test_empty_location':
return []
return [666]

def mock_pos_search(*args, **kwargs):
context = kwargs.get('context')
test = context.get('test') if context else ''
if test == 'test_location':
global location_passed
location_passed = args[3]
if test == 'test_empty_location':
global empty_location
empty_location = args[3]
return [1]

cls.category_pool._patch_method('search', mock_category_search)
cls.location_pool._patch_method('search', mock_location_search)
cls.pos_pool._patch_method('search', mock_pos_search)

@classmethod
def tearDownClass(cls):
cls.category_pool._revert_method('search')
cls.location_pool._revert_method('search')
cls.pos_pool._revert_method('search')
super(TestMapLDAPExtension, cls).tearDownClass()

def test_hospital_passed_to_pos(self):
"""
TEst that when a hospital is found it is then used to find the POS for
that hospital
"""
cr, uid = self.cr, self.uid
self.ldap_pool.map_ldap_attributes(
cr, uid, self.conf, 'test', self.ldap_entry,
context={'test': 'test_location'})
self.assertEqual(location_passed[0][2], [666])

def test_empty_hospital_passed_to_pos(self):
"""
TEst that when a hospital is not found it is then used to find the POS
for that hospital
"""
cr, uid = self.cr, self.uid
self.ldap_pool.map_ldap_attributes(
cr, uid, self.conf, 'test', self.ldap_entry,
context={'test': 'test_empty_location'})
self.assertEqual(empty_location, [['location_id', 'in', []]])

def test_pos(self):
"""
TEst that on POS found it then in the returned dict
"""
cr, uid = self.cr, self.uid
vals = self.ldap_pool.map_ldap_attributes(
cr, uid, self.conf, 'test', self.ldap_entry,
context={'test': 'test_pos'})
self.assertEqual(vals.get('pos_ids'), [[6, 0, [1]]])

def test_raises_on_invalid_ldap_entry(self):
"""
TEst it raises value error when trying to map a non-conformant LDAP
entry
"""
cr, uid = self.cr, self.uid
with self.assertRaises(ValueError) as ldap_err:
self.ldap_pool.map_ldap_attributes(
cr, uid, self.conf, 'test', [0])
self.assertEqual(ldap_err.exception.message,
'LDAP Entry does not contain second element')

def test_raises_on_invalid_ldap_entry_cn(self):
"""
TEst it raises value error when trying to map a non-conformant LDAP
entry
"""
cr, uid = self.cr, self.uid
with self.assertRaises(ValueError) as ldap_err:
self.ldap_pool.map_ldap_attributes(
cr, uid, self.conf, 'test', [None, {'cn': []}])
self.assertEqual(ldap_err.exception.message,
'LDAP Entry CN does not contain elements')
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ mock==1.3.0
pylint==1.4.4
coverage==4.0.3
fake-factory==0.5.1
python-ldap==2.4.19

0 comments on commit f1a5e6e

Please sign in to comment.