| 1 |  |  | """Test framework for running tests of PPP modules.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 | 1 |  | __all__ = ['PPPTestCase'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 | 1 |  | import os | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 | 1 |  | import json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 | 1 |  | import tempfile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 | 1 |  | from webtest import TestApp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 | 1 |  | from unittest import TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 | 1 |  | from ppp_datamodel.communication import Request, Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 | 1 |  | def arg_to_dict(f): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     """Converts the first argument to the function to a dict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     (assuming it's either a json-encoding string or a Request object).""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 1 |  |     def newf(self, obj, *args, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 1 |  |         if isinstance(obj, Request): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             obj = obj.as_dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 1 |  |         elif isinstance(obj, str): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             obj = json.loads(obj) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 1 |  |         return f(self, obj, *args, **kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 1 |  |     return newf | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 1 |  | def PPPTestCase(app): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 1 |  |     class _PPPTestCase(TestCase): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 1 |  |         config_var = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 1 |  |         config = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 1 |  |         def setUp(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 1 |  |             super(_PPPTestCase, self).setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  |             self.app = TestApp(app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 1 |  |             if self.config_var or self.config is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 assert self.config_var and self.config is not None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 self.config_file = tempfile.NamedTemporaryFile('w+') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 os.environ[self.config_var] = self.config_file.name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 self.config_file.write(self.config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 self.config_file.seek(0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |         def tearDown(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |             if self.config_var or self.config is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 assert self.config_var and self.config is not None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 self.config_file.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 del os.environ[self.config_var] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 del self.config_file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |             super(_PPPTestCase, self).tearDown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |         @arg_to_dict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         def request(self, obj): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |             j = self.app.post_json('/', obj).json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             """Make a request and return the answers.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |             return list(map(Response.from_dict, j)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         @arg_to_dict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |         def assertResponses(self, request, expected_responses, *, remove_time=True): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             """Makes a request and asserts the responses are the expected one.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |             responses = self.request(request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |             if remove_time: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |                 for response in responses: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |                     for trace_item in response.trace: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |                         trace_item.times.clear() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |             self.assertEqual(responses, expected_responses) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         assertResponse = assertResponses # Alias for old code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         @arg_to_dict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         def assertResponsesIn(self, request, expected, *, remove_time=True): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             """Makes a request and asserts the response is among a set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             of expected ones.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             responses = self.request(request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             if remove_time: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 for response in responses: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     for trace_item in response.trace: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                         trace_item.times.clear() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             self.assertTrue(all(x in expected for x in responses), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                             'Not all of:\n%r\n are in:\n%r' % (responses, expected)) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 1 |  |         @arg_to_dict | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         def assertResponsesCount(self, request, count): | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             """Makes a request and asserts the number of responses is | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             a given number.""" | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             self.assertEqual(len(self.request(request)), count) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |         @arg_to_dict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         def assertStatusInt(self, obj, status): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             """Makes a request and asserts the HTTP status code is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             the one that is expected.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |             res = self.app.post_json('/', obj, status='*') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |             self.assertEqual(res.status_int, status) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |     return _PPPTestCase | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  |