11import os
22import tempfile
33
4- from django .conf import settings
54from django .test import TestCase
65from django .db import IntegrityError
76from django .core .urlresolvers import reverse
87from django .core .files import File
98from django .contrib .formtools .preview import security_hash
109
1110from ietf .utils .test_utils import login_testing_unauthorized
12- from ietf . utils . pipe import pipe
11+
1312
1413from 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
1818from 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