Skip to content

Commit 003691f

Browse files
committed
Merged in [7549] from rcross@amsl.com:
fix tests to work with secauth middleware changes - Legacy-Id: 7570 Note: SVN reference [7549] has been migrated to Git commit 0b5fc98
2 parents a83c139 + 0b5fc98 commit 003691f

3 files changed

Lines changed: 27 additions & 31 deletions

File tree

ietf/secr/groups/tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_view(self):
7777
make_test_data()
7878
group = Group.objects.all()[0]
7979
url = reverse('groups_view', kwargs={'acronym':group.acronym})
80-
response = self.client.get(url)
80+
response = self.client.get(url, REMOTE_USER=SECR_USER)
8181
self.assertEqual(response.status_code, 200)
8282

8383
# ------- Test Edit -------- #

ietf/secr/middleware/secauth.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
import re
22

33
from django.conf import settings
4-
from django.shortcuts import render_to_response
4+
from django.contrib.auth.decorators import login_required
5+
from ietf.ietfauth.utils import has_role, role_required
56

6-
from ietf.ietfauth.utils import has_role
7-
87

98

109
class SecAuthMiddleware(object):
1110
"""
12-
Middleware component that performs custom auth check for every
13-
request except those excluded by SECR_AUTH_UNRESTRICTED_URLS.
14-
15-
Since authentication is performed externally at the apache level
16-
REMOTE_USER should contain the name of the authenticated
17-
user. If the user is a secretariat than access is granted.
18-
Otherwise return a 401 error page.
11+
Middleware component that performs custom auth check for secretariat
12+
apps. request except those excluded by SECR_AUTH_UNRESTRICTED_URLS.
1913
2014
To use, add the class to MIDDLEWARE_CLASSES and define
2115
SECR_AUTH_UNRESTRICTED_URLS in your settings.py.
@@ -28,30 +22,32 @@ class SecAuthMiddleware(object):
2822
2923
Also sets custom request attributes:
3024
user_is_secretariat
31-
user_is_chair
32-
user_is_ad
33-
)
34-
3525
"""
3626

3727
def __init__(self):
3828
self.unrestricted = [re.compile(pattern) for pattern in
3929
settings.SECR_AUTH_UNRESTRICTED_URLS]
4030

31+
def is_unrestricted_url(self,path):
32+
for pattern in self.unrestricted:
33+
if pattern.match(path):
34+
return True
35+
return False
36+
4137
def process_view(self, request, view_func, view_args, view_kwargs):
42-
# need to initialize user, it doesn't get set when running tests for example
43-
4438
if request.path.startswith('/secr/'):
45-
request.user_is_secretariat = False
46-
47-
if request.user.is_anonymous():
48-
return render_to_response('401.html')
49-
50-
# do custom check
39+
# set custom request attribute
5140
if has_role(request.user, 'Secretariat'):
5241
request.user_is_secretariat = True
53-
42+
else:
43+
request.user_is_secretariat = False
44+
45+
if request.path.startswith('/secr/announcement/'):
46+
return login_required(view_func)(request,*view_args,**view_kwargs)
47+
elif self.is_unrestricted_url(request.path):
48+
return role_required('WG Chair','Secretariat')(view_func)(request,*view_args,**view_kwargs)
49+
else:
50+
return role_required('Secretariat')(view_func)(request,*view_args,**view_kwargs)
51+
else:
5452
return None
55-
56-
return None
5753

ietf/secr/sreq/tests.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ class SreqUrlTests(TestCase):
1212
def test_urls(self):
1313
draft = make_test_data()
1414

15-
r = self.client.get("/secr/")
15+
r = self.client.get("/secr/",REMOTE_USER=SECR_USER)
1616
self.assertEqual(r.status_code, 200)
1717

18-
r = self.client.get("/secr/sreq/")
18+
r = self.client.get("/secr/sreq/",REMOTE_USER=SECR_USER)
1919
self.assertEqual(r.status_code, 200)
2020

21-
r = self.client.get("/secr/sreq/%s/new/" % draft.group.acronym)
21+
r = self.client.get("/secr/sreq/%s/new/" % draft.group.acronym, REMOTE_USER=SECR_USER)
2222
self.assertEqual(r.status_code, 200)
2323

2424
class MainTestCase(TestCase):
@@ -42,8 +42,8 @@ def test_submit_request(self):
4242
'id_attendees':'10',
4343
'id_conflict1':'',
4444
'id_comments':'need projector'}
45-
self.client.login( REMOTE_USER=SECR_USER)
46-
r = self.client.post(url,post_data)
45+
self.client.login(REMOTE_USER=SECR_USER)
46+
r = self.client.post(url,post_data,REMOTE_USER=SECR_USER)
4747
self.assertEqual(r.status_code, 200)
4848
"""
4949
#assert False, self.client.session..__dict__

0 commit comments

Comments
 (0)