@@ -192,57 +192,73 @@ def _validate_authorization_header(self, request_target, actual_headers, authori
192
192
193
193
class PetApiTests (unittest .TestCase ):
194
194
195
- def setUp (self ):
196
- self .setUpModels ()
197
- self .setUpFiles ()
198
-
199
-
200
- def setUpModels (self ):
201
- self .category = petstore_api .Category ()
202
- self .category .id = id_gen ()
203
- self .category .name = "dog"
204
- self .tag = petstore_api .Tag ()
205
- self .tag .id = id_gen ()
206
- self .tag .name = "python-pet-tag"
207
- self .pet = petstore_api .Pet (name = "hello kity" , photo_urls = ["http://foo.bar.com/1" , "http://foo.bar.com/2" ])
208
- self .pet .id = id_gen ()
209
- self .pet .status = "sold"
210
- self .pet .category = self .category
211
- self .pet .tags = [self .tag ]
212
-
213
- def setUpFiles (self ):
214
- self .test_file_dir = os .path .join (os .path .dirname (__file__ ), ".." , "testfiles" )
215
- self .test_file_dir = os .path .realpath (self .test_file_dir )
216
- if not os .path .exists (self .test_file_dir ):
217
- os .mkdir (self .test_file_dir )
218
-
219
- self .private_key_passphrase = 'test-passphrase'
220
- self .rsa_key_path = os .path .join (self .test_file_dir , 'rsa.pem' )
221
- self .rsa4096_key_path = os .path .join (self .test_file_dir , 'rsa4096.pem' )
222
- self .ec_p521_key_path = os .path .join (self .test_file_dir , 'ecP521.pem' )
223
-
224
- if not os .path .exists (self .rsa_key_path ):
225
- with open (self .rsa_key_path , 'w' ) as f :
195
+ @classmethod
196
+ def setUpClass (cls ):
197
+ cls .setUpModels ()
198
+ cls .setUpFiles ()
199
+
200
+ @classmethod
201
+ def tearDownClass (cls ):
202
+ file_paths = [
203
+ cls .rsa_key_path ,
204
+ cls .rsa4096_key_path ,
205
+ cls .ec_p521_key_path ,
206
+ ]
207
+ for file_path in file_paths :
208
+ os .unlink (file_path )
209
+
210
+ @classmethod
211
+ def setUpModels (cls ):
212
+ cls .category = petstore_api .Category ()
213
+ cls .category .id = id_gen ()
214
+ cls .category .name = "dog"
215
+ cls .tag = petstore_api .Tag ()
216
+ cls .tag .id = id_gen ()
217
+ cls .tag .name = "python-pet-tag"
218
+ cls .pet = petstore_api .Pet (
219
+ name = "hello kity" ,
220
+ photo_urls = ["http://foo.bar.com/1" , "http://foo.bar.com/2" ]
221
+ )
222
+ cls .pet .id = id_gen ()
223
+ cls .pet .status = "sold"
224
+ cls .pet .category = cls .category
225
+ cls .pet .tags = [cls .tag ]
226
+
227
+ @classmethod
228
+ def setUpFiles (cls ):
229
+ cls .test_file_dir = os .path .join (
230
+ os .path .dirname (__file__ ), ".." , "testfiles" )
231
+ cls .test_file_dir = os .path .realpath (cls .test_file_dir )
232
+ if not os .path .exists (cls .test_file_dir ):
233
+ os .mkdir (cls .test_file_dir )
234
+
235
+ cls .private_key_passphrase = 'test-passphrase'
236
+ cls .rsa_key_path = os .path .join (cls .test_file_dir , 'rsa.pem' )
237
+ cls .rsa4096_key_path = os .path .join (cls .test_file_dir , 'rsa4096.pem' )
238
+ cls .ec_p521_key_path = os .path .join (cls .test_file_dir , 'ecP521.pem' )
239
+
240
+ if not os .path .exists (cls .rsa_key_path ):
241
+ with open (cls .rsa_key_path , 'w' ) as f :
226
242
f .write (RSA_TEST_PRIVATE_KEY )
227
243
228
- if not os .path .exists (self .rsa4096_key_path ):
244
+ if not os .path .exists (cls .rsa4096_key_path ):
229
245
key = RSA .generate (4096 )
230
246
private_key = key .export_key (
231
- passphrase = self .private_key_passphrase ,
247
+ passphrase = cls .private_key_passphrase ,
232
248
protection = 'PEM'
233
249
)
234
- with open (self .rsa4096_key_path , "wb" ) as f :
250
+ with open (cls .rsa4096_key_path , "wb" ) as f :
235
251
f .write (private_key )
236
252
237
- if not os .path .exists (self .ec_p521_key_path ):
253
+ if not os .path .exists (cls .ec_p521_key_path ):
238
254
key = ECC .generate (curve = 'P-521' )
239
255
private_key = key .export_key (
240
256
format = 'PEM' ,
241
- passphrase = self .private_key_passphrase ,
257
+ passphrase = cls .private_key_passphrase ,
242
258
use_pkcs8 = True ,
243
259
protection = 'PBKDF2WithHMAC-SHA1AndAES128-CBC'
244
260
)
245
- with open (self .ec_p521_key_path , "wt" ) as f :
261
+ with open (cls .ec_p521_key_path , "wt" ) as f :
246
262
f .write (private_key )
247
263
248
264
def test_valid_http_signature (self ):
0 commit comments