Skip to content

Commit 4b00c32

Browse files
committed
Add nominate test.
See ietf-tools#913 - Legacy-Id: 5169
1 parent 7e3a6a3 commit 4b00c32

3 files changed

Lines changed: 96 additions & 75 deletions

File tree

ietf/nomcom/forms.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ def save(self, commit=True):
225225
comments=comments,
226226
type=FeedbackType.objects.get(slug='nomina'))
227227
if author:
228+
nomination.nominator_email = author.address
228229
feedback.author = author
229230
feedback.save()
230231

ietf/nomcom/test_data.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import tempfile
2+
import os
3+
14
from django.contrib.auth.models import User
5+
from django.conf import settings
26

7+
from ietf.utils.pipe import pipe
38
from ietf.group.models import Group, Role
49
from ietf.person.models import Email, Person
510
from ietf.name.models import RoleName
@@ -28,6 +33,66 @@
2833
}
2934

3035

36+
def generate_cert():
37+
"""Function to generate cert"""
38+
config = """
39+
[ req ]
40+
distinguished_name = req_distinguished_name
41+
string_mask = utf8only
42+
x509_extensions = ss_v3_ca
43+
44+
[ req_distinguished_name ]
45+
commonName = Common Name (e.g., NomComYY)
46+
commonName_default = NomCom12
47+
48+
[ ss_v3_ca ]
49+
50+
subjectKeyIdentifier = hash
51+
keyUsage = critical, digitalSignature, keyEncipherment, dataEncipherment
52+
basicConstraints = critical, CA:true
53+
subjectAltName = email:nomcom12@ietf.org
54+
extendedKeyUsage= emailProtection"""
55+
56+
config_file = tempfile.NamedTemporaryFile(delete=False)
57+
privatekey_file = tempfile.NamedTemporaryFile(delete=False)
58+
cert_file = tempfile.NamedTemporaryFile(delete=False)
59+
60+
config_file.write(config)
61+
config_file.close()
62+
63+
command = "%s req -config %s -x509 -new -newkey rsa:2048 -sha256 -days 730 -nodes \
64+
-keyout %s -out %s -batch"
65+
code, out, error = pipe(command % (settings.OPENSSL_COMMAND,
66+
config_file.name,
67+
privatekey_file.name,
68+
cert_file.name))
69+
privatekey_file.close()
70+
cert_file.close()
71+
return cert_file, privatekey_file
72+
73+
74+
def check_comments(encryped, plain, privatekey_file):
75+
encrypted_file = tempfile.NamedTemporaryFile(delete=False)
76+
encrypted_file.write(encryped)
77+
encrypted_file.close()
78+
79+
# to decrypt comments was encryped and check they are equal to the plain comments
80+
decrypted_file = tempfile.NamedTemporaryFile(delete=False)
81+
command = "%s smime -decrypt -in %s -out %s -inkey %s"
82+
code, out, error = pipe(command % (settings.OPENSSL_COMMAND,
83+
encrypted_file.name,
84+
decrypted_file.name,
85+
privatekey_file.name))
86+
87+
decrypted_file.close()
88+
encrypted_file.close()
89+
decrypted_comments = open(decrypted_file.name, 'r').read()
90+
os.unlink(encrypted_file.name)
91+
os.unlink(decrypted_file.name)
92+
93+
return decrypted_comments == plain
94+
95+
3196
def nomcom_test_data():
3297
# groups
3398
group, created = Group.objects.get_or_create(name='IAB/IESG Nominating Committee 2013/2014',

ietf/nomcom/tests.py

Lines changed: 30 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import os
22
import tempfile
33

4-
from django.conf import settings
54
from django.test import TestCase
65
from django.db import IntegrityError
76
from django.core.urlresolvers import reverse
87
from django.core.files import File
98
from django.contrib.formtools.preview import security_hash
109

1110
from ietf.utils.test_utils import login_testing_unauthorized
12-
from ietf.utils.pipe import pipe
11+
1312

1413
from ietf.person.models import Email, Person
1514

16-
from ietf.nomcom.test_data import nomcom_test_data, COMMUNITY_USER, CHAIR_USER, \
15+
from ietf.nomcom.test_data import nomcom_test_data, generate_cert, check_comments, \
16+
COMMUNITY_USER, CHAIR_USER, \
1717
MEMBER_USER, SECRETARIAT_USER, EMAIL_DOMAIN
1818
from ietf.nomcom.models import NomineePosition, Position, Nominee, \
1919
NomineePositionState, Feedback, FeedbackType, \
@@ -32,6 +32,7 @@ def check_url_status(self, url, status):
3232

3333
def setUp(self):
3434
nomcom_test_data()
35+
self.cert_file, self.privatekey_file = generate_cert()
3536
self.year = 2013
3637

3738
# private urls
@@ -169,17 +170,22 @@ def test_nominate_view(self):
169170
response = self.client.get(self.nominate_url)
170171
self.assertEqual(response.status_code, 200)
171172
nomcom = get_nomcom_by_year(self.year)
172-
has_publickey = nomcom.public_key and True or False
173-
self.assertEqual(response.context['has_publickey'], has_publickey)
173+
if not nomcom.public_key:
174+
self.assertNotContains(response, "nominateform")
174175

175-
if not has_publickey:
176-
return
176+
# save the cert file in tmp
177+
nomcom.public_key.storage.location = tempfile.gettempdir()
178+
nomcom.public_key.save('cert', File(open(self.cert_file.name, 'r')))
179+
180+
response = self.client.get(self.nominate_url)
181+
self.assertEqual(response.status_code, 200)
182+
self.assertContains(response, "nominateform")
177183

178184
position = Position.objects.get(name='IAOC')
179-
candidate_email = 'nominee@example.com',
180-
candidate_name = 'nominee'
185+
candidate_email = u'nominee@example.com'
186+
candidate_name = u'nominee'
181187
comments = 'test nominate view'
182-
candidate_phone = '123456'
188+
candidate_phone = u'123456'
183189

184190
test_data = {'candidate_name': candidate_name,
185191
'candidate_email': candidate_email,
@@ -198,17 +204,19 @@ def test_nominate_view(self):
198204
feedback = Feedback.objects.get(position=position,
199205
nominee=nominee,
200206
type=FeedbackType.objects.get(slug='nomina'),
201-
author__person__name=COMMUNITY_USER)
207+
author="%s%s" % (COMMUNITY_USER, EMAIL_DOMAIN))
208+
202209
# to check feedback comments are saved like enrypted data
203210
self.assertNotEqual(feedback.comments, comments)
204211

212+
self.assertEqual(check_comments(feedback.comments, comments, self.privatekey_file), True)
205213
Nomination.objects.get(position=position,
206-
candidate_name=candidate_name,
207-
candidate_mail=candidate_email,
208-
candidate_phone=candidate_phone,
209-
nominee=nominee,
210-
comments=feedback,
211-
nominator_email="%s%s" % (COMMUNITY_USER, EMAIL_DOMAIN))
214+
candidate_name=candidate_name,
215+
candidate_email=candidate_email,
216+
candidate_phone=candidate_phone,
217+
nominee=nominee,
218+
comments=feedback,
219+
nominator_email="%s%s" % (COMMUNITY_USER, EMAIL_DOMAIN))
212220
self.client.logout()
213221

214222

@@ -224,7 +232,7 @@ def test_state_autoset(self):
224232
"""Verify state is autoset correctly"""
225233
position = Position.objects.get(name='APP')
226234
nominee_position = NomineePosition.objects.create(position=position,
227-
nominee=self.nominee)
235+
nominee=self.nominee)
228236
self.assertEqual(nominee_position.state.slug, 'pending')
229237

230238
def test_state_specified(self):
@@ -250,43 +258,7 @@ class FeedbackTest(TestCase):
250258

251259
def setUp(self):
252260
nomcom_test_data()
253-
self.generate_cert()
254-
255-
def generate_cert(self):
256-
"""Function to generate cert"""
257-
config = """
258-
[ req ]
259-
distinguished_name = req_distinguished_name
260-
string_mask = utf8only
261-
x509_extensions = ss_v3_ca
262-
263-
[ req_distinguished_name ]
264-
commonName = Common Name (e.g., NomComYY)
265-
commonName_default = NomCom12
266-
267-
[ ss_v3_ca ]
268-
269-
subjectKeyIdentifier = hash
270-
keyUsage = critical, digitalSignature, keyEncipherment, dataEncipherment
271-
basicConstraints = critical, CA:true
272-
subjectAltName = email:nomcom12@ietf.org
273-
extendedKeyUsage= emailProtection"""
274-
275-
self.config_file = tempfile.NamedTemporaryFile(delete=False)
276-
self.privatekey_file = tempfile.NamedTemporaryFile(delete=False)
277-
self.cert_file = tempfile.NamedTemporaryFile(delete=False)
278-
279-
self.config_file.write(config)
280-
self.config_file.close()
281-
282-
command = "%s req -config %s -x509 -new -newkey rsa:2048 -sha256 -days 730 -nodes \
283-
-keyout %s -out %s -batch"
284-
code, out, error = pipe(command % (settings.OPENSSL_COMMAND,
285-
self.config_file.name,
286-
self.privatekey_file.name,
287-
self.cert_file.name))
288-
self.privatekey_file.close()
289-
self.cert_file.close()
261+
self.cert_file, self.privatekey_file = generate_cert()
290262

291263
def test_encrypted_comments(self):
292264

@@ -307,26 +279,9 @@ def test_encrypted_comments(self):
307279
# to check feedback comments are saved like enrypted data
308280
self.assertNotEqual(feedback.comments, comments)
309281

310-
encrypted_file = tempfile.NamedTemporaryFile(delete=False)
311-
encrypted_file.write(feedback.comments)
312-
encrypted_file.close()
313-
314-
# to decrypt comments was encryped and check they are equal to the plain comments
315-
decrypted_file = tempfile.NamedTemporaryFile(delete=False)
316-
command = "%s smime -decrypt -in %s -out %s -inkey %s"
317-
code, out, error = pipe(command % (settings.OPENSSL_COMMAND,
318-
encrypted_file.name,
319-
decrypted_file.name,
320-
self.privatekey_file.name))
321-
322-
decrypted_file.close()
323-
encrypted_file.close()
324-
325-
self.assertEqual(open(decrypted_file.name, 'r').read(), comments)
282+
self.assertEqual(check_comments(feedback.comments,
283+
comments,
284+
self.privatekey_file), True)
326285

327-
# delete tmps
328-
os.unlink(self.config_file.name)
329286
os.unlink(self.privatekey_file.name)
330287
os.unlink(self.cert_file.name)
331-
os.unlink(encrypted_file.name)
332-
os.unlink(decrypted_file.name)

0 commit comments

Comments
 (0)