2121import mock
2222import unittest
2323
24+ import pytest
25+
2426from st2auth_ldap import ldap_backend
2527
2628
3032LDAP_BIND_DN = 'cn=Administrator,cn=users,dc=stackstorm,dc=net'
3133LDAP_BIND_PASSWORD = uuid .uuid4 ().hex
3234LDAP_GROUP_DNS = ['cn=testers,dc=stackstorm,dc=net' ]
35+ LDAP_GROUP_DNS_CASES = (
36+ pytest .param (LDAP_GROUP_DNS , id = "group_fqdn" ),
37+ pytest .param (['cn=testers' ], id = "group_rdn" ),
38+ )
3339LDAP_CACERT = '../fixtures/certs/cacert.pem'
3440LDAP_CACERT_REAL_PATH = os .path .join (os .path .dirname (os .path .abspath (__file__ )), LDAP_CACERT )
3541LDAP_BASE_OU = 'dc=stackstorm,dc=net'
@@ -102,12 +108,13 @@ def test_instantaite_no_group_dns_provided(self):
102108 @mock .patch .object (
103109 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
104110 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT , LDAP_GROUP_SEARCH_RESULT ]))
105- def test_authenticate (self ):
111+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
112+ def test_authenticate (self , required_group_dns ):
106113 backend = ldap_backend .LDAPAuthenticationBackend (
107114 LDAP_BIND_DN ,
108115 LDAP_BIND_PASSWORD ,
109116 LDAP_BASE_OU ,
110- LDAP_GROUP_DNS ,
117+ required_group_dns ,
111118 LDAP_HOST ,
112119 id_attr = LDAP_ID_ATTR
113120 )
@@ -121,12 +128,13 @@ def test_authenticate(self):
121128 @mock .patch .object (
122129 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
123130 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT , LDAP_GROUP_SEARCH_RESULT ]))
124- def test_authenticate_with_multiple_ldap_hosts (self ):
131+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
132+ def test_authenticate_with_multiple_ldap_hosts (self , required_group_dns ):
125133 backend = ldap_backend .LDAPAuthenticationBackend (
126134 LDAP_BIND_DN ,
127135 LDAP_BIND_PASSWORD ,
128136 LDAP_BASE_OU ,
129- LDAP_GROUP_DNS ,
137+ required_group_dns ,
130138 LDAP_MULTIPLE_HOSTS ,
131139 id_attr = LDAP_ID_ATTR
132140 )
@@ -140,12 +148,13 @@ def test_authenticate_with_multiple_ldap_hosts(self):
140148 @mock .patch .object (
141149 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
142150 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT , LDAP_GROUP_SEARCH_RESULT ]))
143- def test_authenticate_without_password (self ):
151+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
152+ def test_authenticate_without_password (self , required_group_dns ):
144153 backend = ldap_backend .LDAPAuthenticationBackend (
145154 LDAP_BIND_DN ,
146155 LDAP_BIND_PASSWORD ,
147156 LDAP_BASE_OU ,
148- LDAP_GROUP_DNS ,
157+ required_group_dns ,
149158 LDAP_HOST ,
150159 id_attr = LDAP_ID_ATTR
151160 )
@@ -156,12 +165,13 @@ def test_authenticate_without_password(self):
156165 @mock .patch .object (
157166 ldap .ldapobject .SimpleLDAPObject , 'simple_bind_s' ,
158167 mock .MagicMock (side_effect = Exception ()))
159- def test_authenticate_failure_bad_bind_cred (self ):
168+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
169+ def test_authenticate_failure_bad_bind_cred (self , required_group_dns ):
160170 backend = ldap_backend .LDAPAuthenticationBackend (
161171 LDAP_BIND_DN ,
162172 LDAP_BIND_PASSWORD ,
163173 LDAP_BASE_OU ,
164- LDAP_GROUP_DNS ,
174+ required_group_dns ,
165175 LDAP_HOST ,
166176 id_attr = LDAP_ID_ATTR
167177 )
@@ -175,12 +185,13 @@ def test_authenticate_failure_bad_bind_cred(self):
175185 @mock .patch .object (
176186 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
177187 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT , LDAP_GROUP_SEARCH_RESULT ]))
178- def test_authenticate_failure_bad_user_password (self ):
188+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
189+ def test_authenticate_failure_bad_user_password (self , required_group_dns ):
179190 backend = ldap_backend .LDAPAuthenticationBackend (
180191 LDAP_BIND_DN ,
181192 LDAP_BIND_PASSWORD ,
182193 LDAP_BASE_OU ,
183- LDAP_GROUP_DNS ,
194+ required_group_dns ,
184195 LDAP_HOST ,
185196 id_attr = LDAP_ID_ATTR
186197 )
@@ -194,13 +205,14 @@ def test_authenticate_failure_bad_user_password(self):
194205 @mock .patch .object (
195206 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
196207 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT , []]))
197- def test_authenticate_failure_non_group_member_no_groups (self ):
208+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
209+ def test_authenticate_failure_non_group_member_no_groups (self , required_group_dns ):
198210 # User is not member of any of the required group
199211 backend = ldap_backend .LDAPAuthenticationBackend (
200212 LDAP_BIND_DN ,
201213 LDAP_BIND_PASSWORD ,
202214 LDAP_BASE_OU ,
203- LDAP_GROUP_DNS ,
215+ required_group_dns ,
204216 LDAP_HOST ,
205217 id_attr = LDAP_ID_ATTR ,
206218 group_dns_check = 'and'
@@ -213,7 +225,7 @@ def test_authenticate_failure_non_group_member_no_groups(self):
213225 LDAP_BIND_DN ,
214226 LDAP_BIND_PASSWORD ,
215227 LDAP_BASE_OU ,
216- LDAP_GROUP_DNS ,
228+ required_group_dns ,
217229 LDAP_HOST ,
218230 id_attr = LDAP_ID_ATTR ,
219231 group_dns_check = 'or'
@@ -229,13 +241,14 @@ def test_authenticate_failure_non_group_member_no_groups(self):
229241 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
230242 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT ,
231243 [('cn=group1,dc=stackstorm,dc=net' , ())]]))
232- def test_authenticatefailure_non_group_member_non_required_group (self ):
244+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
245+ def test_authenticatefailure_non_group_member_non_required_group (self , required_group_dns ):
233246 # User is member of a group which is not required
234247 backend = ldap_backend .LDAPAuthenticationBackend (
235248 LDAP_BIND_DN ,
236249 LDAP_BIND_PASSWORD ,
237250 LDAP_BASE_OU ,
238- LDAP_GROUP_DNS ,
251+ required_group_dns ,
239252 LDAP_HOST ,
240253 id_attr = LDAP_ID_ATTR ,
241254 group_dns_check = 'and'
@@ -248,7 +261,7 @@ def test_authenticatefailure_non_group_member_non_required_group(self):
248261 LDAP_BIND_DN ,
249262 LDAP_BIND_PASSWORD ,
250263 LDAP_BASE_OU ,
251- LDAP_GROUP_DNS ,
264+ required_group_dns ,
252265 LDAP_HOST ,
253266 id_attr = LDAP_ID_ATTR ,
254267 group_dns_check = 'or'
@@ -576,12 +589,13 @@ def test_authenticate_or_behavior_success_member_of_multiple_groups_3b(self):
576589 @mock .patch .object (
577590 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
578591 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT , LDAP_GROUP_SEARCH_RESULT ]))
579- def test_ssl_authenticate (self ):
592+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
593+ def test_ssl_authenticate (self , required_group_dns ):
580594 backend = ldap_backend .LDAPAuthenticationBackend (
581595 LDAP_BIND_DN ,
582596 LDAP_BIND_PASSWORD ,
583597 LDAP_BASE_OU ,
584- LDAP_GROUP_DNS ,
598+ required_group_dns ,
585599 LDAP_HOST ,
586600 port = LDAPS_PORT ,
587601 use_ssl = True ,
@@ -597,12 +611,13 @@ def test_ssl_authenticate(self):
597611 @mock .patch .object (
598612 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
599613 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT , LDAP_GROUP_SEARCH_RESULT ]))
600- def test_ssl_authenticate_failure (self ):
614+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
615+ def test_ssl_authenticate_failure (self , required_group_dns ):
601616 backend = ldap_backend .LDAPAuthenticationBackend (
602617 LDAP_BIND_DN ,
603618 LDAP_BIND_PASSWORD ,
604619 LDAP_BASE_OU ,
605- LDAP_GROUP_DNS ,
620+ required_group_dns ,
606621 LDAP_HOST ,
607622 port = LDAPS_PORT ,
608623 use_ssl = True ,
@@ -618,12 +633,13 @@ def test_ssl_authenticate_failure(self):
618633 @mock .patch .object (
619634 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
620635 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT , LDAP_GROUP_SEARCH_RESULT ]))
621- def test_ssl_authenticate_validate_cert (self ):
636+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
637+ def test_ssl_authenticate_validate_cert (self , required_group_dns ):
622638 backend = ldap_backend .LDAPAuthenticationBackend (
623639 LDAP_BIND_DN ,
624640 LDAP_BIND_PASSWORD ,
625641 LDAP_BASE_OU ,
626- LDAP_GROUP_DNS ,
642+ required_group_dns ,
627643 LDAP_HOST ,
628644 port = LDAPS_PORT ,
629645 use_ssl = True ,
@@ -643,12 +659,13 @@ def test_ssl_authenticate_validate_cert(self):
643659 @mock .patch .object (
644660 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
645661 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT , LDAP_GROUP_SEARCH_RESULT ]))
646- def test_tls_authenticate (self ):
662+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
663+ def test_tls_authenticate (self , required_group_dns ):
647664 backend = ldap_backend .LDAPAuthenticationBackend (
648665 LDAP_BIND_DN ,
649666 LDAP_BIND_PASSWORD ,
650667 LDAP_BASE_OU ,
651- LDAP_GROUP_DNS ,
668+ required_group_dns ,
652669 LDAP_HOST ,
653670 use_tls = True ,
654671 id_attr = LDAP_ID_ATTR
@@ -666,12 +683,13 @@ def test_tls_authenticate(self):
666683 @mock .patch .object (
667684 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
668685 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT , LDAP_GROUP_SEARCH_RESULT ]))
669- def test_tls_authenticate_failure (self ):
686+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
687+ def test_tls_authenticate_failure (self , required_group_dns ):
670688 backend = ldap_backend .LDAPAuthenticationBackend (
671689 LDAP_BIND_DN ,
672690 LDAP_BIND_PASSWORD ,
673691 LDAP_BASE_OU ,
674- LDAP_GROUP_DNS ,
692+ required_group_dns ,
675693 LDAP_HOST ,
676694 use_tls = True ,
677695 id_attr = LDAP_ID_ATTR
@@ -689,12 +707,13 @@ def test_tls_authenticate_failure(self):
689707 @mock .patch .object (
690708 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
691709 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT , LDAP_GROUP_SEARCH_RESULT ]))
692- def test_tls_authenticate_validate_cert (self ):
710+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
711+ def test_tls_authenticate_validate_cert (self , required_group_dns ):
693712 backend = ldap_backend .LDAPAuthenticationBackend (
694713 LDAP_BIND_DN ,
695714 LDAP_BIND_PASSWORD ,
696715 LDAP_BASE_OU ,
697- LDAP_GROUP_DNS ,
716+ required_group_dns ,
698717 LDAP_HOST ,
699718 use_tls = True ,
700719 cacert = LDAP_CACERT_REAL_PATH ,
@@ -710,13 +729,14 @@ def test_tls_authenticate_validate_cert(self):
710729 @mock .patch .object (
711730 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
712731 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT , []]))
713- def test_special_characters_in_username_are_escaped (self ):
732+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
733+ def test_special_characters_in_username_are_escaped (self , required_group_dns ):
714734 # User is not member of any of the required group
715735 backend = ldap_backend .LDAPAuthenticationBackend (
716736 LDAP_BIND_DN ,
717737 LDAP_BIND_PASSWORD ,
718738 LDAP_BASE_OU ,
719- LDAP_GROUP_DNS ,
739+ required_group_dns ,
720740 LDAP_HOST ,
721741 id_attr = LDAP_ID_ATTR
722742 )
@@ -753,12 +773,13 @@ def test_special_characters_in_username_are_escaped(self):
753773 @mock .patch .object (
754774 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
755775 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT , LDAP_GROUP_SEARCH_RESULT ]))
756- def test_get_user (self ):
776+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
777+ def test_get_user (self , required_group_dns ):
757778 backend = ldap_backend .LDAPAuthenticationBackend (
758779 LDAP_BIND_DN ,
759780 LDAP_BIND_PASSWORD ,
760781 LDAP_BASE_OU ,
761- LDAP_GROUP_DNS ,
782+ required_group_dns ,
762783 LDAP_HOST ,
763784 id_attr = LDAP_ID_ATTR
764785 )
@@ -775,12 +796,13 @@ def test_get_user(self):
775796 @mock .patch .object (
776797 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
777798 mock .MagicMock (side_effect = [2 * LDAP_USER_SEARCH_RESULT , LDAP_GROUP_SEARCH_RESULT ]))
778- def test_get_user_multiple_results (self ):
799+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
800+ def test_get_user_multiple_results (self , required_group_dns ):
779801 backend = ldap_backend .LDAPAuthenticationBackend (
780802 LDAP_BIND_DN ,
781803 LDAP_BIND_PASSWORD ,
782804 LDAP_BASE_OU ,
783- LDAP_GROUP_DNS ,
805+ required_group_dns ,
784806 LDAP_HOST ,
785807 id_attr = LDAP_ID_ATTR
786808 )
@@ -794,12 +816,13 @@ def test_get_user_multiple_results(self):
794816 @mock .patch .object (
795817 ldap .ldapobject .SimpleLDAPObject , 'search_s' ,
796818 mock .MagicMock (side_effect = [LDAP_USER_SEARCH_RESULT , LDAP_GROUP_SEARCH_RESULT ]))
797- def test_get_user_groups (self ):
819+ @pytest .mark .parametrize ("required_group_dns" , LDAP_GROUP_DNS_CASES )
820+ def test_get_user_groups (self , required_group_dns ):
798821 backend = ldap_backend .LDAPAuthenticationBackend (
799822 LDAP_BIND_DN ,
800823 LDAP_BIND_PASSWORD ,
801824 LDAP_BASE_OU ,
802- LDAP_GROUP_DNS ,
825+ required_group_dns ,
803826 LDAP_HOST ,
804827 id_attr = LDAP_ID_ATTR
805828 )
0 commit comments