Skip to content

Commit dedb00b

Browse files
committed
Control the tests of adding user passwords to the htpasswd file better
fixing a bug in the tests and add separate tests of the Python-based approach and the htpasswd binary - Legacy-Id: 11170
1 parent 793bc3c commit dedb00b

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

ietf/ietfauth/htpasswd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.conf import settings
44

5-
def save_htpasswd_file(username, password):
5+
def update_htpasswd_file(username, password):
66
if getattr(settings, 'USE_PYTHON_HTDIGEST', None):
77
pass_file = settings.HTPASSWD_FILE
88
realm = settings.HTDIGEST_REALM

ietf/ietfauth/tests.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,25 @@
1313
from ietf.utils.mail import outbox, empty_outbox
1414
from ietf.person.models import Person, Email
1515
from ietf.group.models import Group, Role, RoleName
16+
from ietf.ietfauth.htpasswd import update_htpasswd_file
1617

1718
class IetfAuthTests(TestCase):
1819
def setUp(self):
20+
self.saved_use_python_htdigest = getattr(settings, "USE_PYTHON_HTDIGEST", None)
21+
settings.USE_PYTHON_HTDIGEST = True
22+
1923
self.saved_htpasswd_file = settings.HTPASSWD_FILE
2024
self.htpasswd_dir = os.path.abspath("tmp-htpasswd-dir")
2125
os.mkdir(self.htpasswd_dir)
2226
settings.HTPASSWD_FILE = os.path.join(self.htpasswd_dir, "htpasswd")
2327
open(settings.HTPASSWD_FILE, 'a').close() # create empty file
28+
2429
self.saved_htdigest_realm = getattr(settings, "HTDIGEST_REALM", None)
2530
settings.HTDIGEST_REALM = "test-realm"
2631

2732
def tearDown(self):
2833
shutil.rmtree(self.htpasswd_dir)
34+
settings.USE_PYTHON_HTDIGEST = self.saved_use_python_htdigest
2935
settings.HTPASSWD_FILE = self.saved_htpasswd_file
3036
settings.HTDIGEST_REALM = self.saved_htdigest_realm
3137

@@ -71,8 +77,11 @@ def extract_confirm_url(self, confirm_email):
7177
def username_in_htpasswd_file(self, username):
7278
with open(settings.HTPASSWD_FILE) as f:
7379
for l in f:
74-
if l.startswith(username + ":" + settings.HTDIGEST_REALM):
80+
if l.startswith(username + ":"):
7581
return True
82+
with open(settings.HTPASSWD_FILE) as f:
83+
print f.read()
84+
7685
return False
7786

7887
def test_create_account(self):
@@ -260,3 +269,18 @@ def test_reset_password(self):
260269
q = PyQuery(r.content)
261270
self.assertEqual(len(q("form .has-error")), 0)
262271
self.assertTrue(self.username_in_htpasswd_file(user.username))
272+
273+
def test_htpasswd_file_with_python(self):
274+
# make sure we test both Python and call-out to binary
275+
settings.USE_PYTHON_HTDIGEST = True
276+
277+
update_htpasswd_file("foo", "passwd")
278+
self.assertTrue(self.username_in_htpasswd_file("foo"))
279+
280+
def test_htpasswd_file_with_htpasswd_binary(self):
281+
# make sure we test both Python and call-out to binary
282+
settings.USE_PYTHON_HTDIGEST = False
283+
284+
update_htpasswd_file("foo", "passwd")
285+
self.assertTrue(self.username_in_htpasswd_file("foo"))
286+

ietf/ietfauth/views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from ietf.group.models import Role
4646
from ietf.ietfauth.forms import RegistrationForm, PasswordForm, ResetPasswordForm, TestEmailForm
4747
from ietf.ietfauth.forms import PersonForm, RoleEmailForm, NewEmailForm
48-
from ietf.ietfauth.htpasswd import save_htpasswd_file
48+
from ietf.ietfauth.htpasswd import update_htpasswd_file
4949
from ietf.person.models import Person, Email, Alias
5050
from ietf.utils.mail import send_mail
5151

@@ -126,7 +126,7 @@ def confirm_account(request, auth):
126126
user.set_password(password)
127127
user.save()
128128
# password is also stored in htpasswd file
129-
save_htpasswd_file(email, password)
129+
update_htpasswd_file(email, password)
130130

131131
# make sure the rest of the person infrastructure is
132132
# well-connected
@@ -317,7 +317,7 @@ def confirm_password_reset(request, auth):
317317
user.set_password(password)
318318
user.save()
319319
# password is also stored in htpasswd file
320-
save_htpasswd_file(user.username, password)
320+
update_htpasswd_file(user.username, password)
321321

322322
success = True
323323
else:

0 commit comments

Comments
 (0)