| 1 |  |  | # -*- coding: utf-8 -*- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | Functional test suite for the root controller. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | This is an example of how functional tests can be written for controllers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | As opposed to a unit-test, which test a small unit of functionality, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | functional tests exercise the whole application and its WSGI stack. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | Please read http://pythonpaste.org/webtest/ for more information. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | from nose.tools import ok_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | from pyjobsweb.tests import TestController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 19 |  |  | class TestRootController(TestController): | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |     """Tests for the method in the root controller.""" | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |     def test_index(self): | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |         """The front page is working properly""" | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |         response = self.app.get('/') | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |         msg = 'TurboGears 2 is rapid web application development toolkit '\ | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |               'designed to make your life easier.' | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |         # You can look for specific strings: | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |         ok_(msg in response) | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |         # You can also access a BeautifulSoup'ed response in your tests | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |         # (First run $ easy_install BeautifulSoup | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |         # and then uncomment the next two lines) | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |         # links = response.html.findAll('a') | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |         # print(links) | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |         # ok_(links, "Mummy, there are no links here!") | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |     def test_environ(self): | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |         """Displaying the wsgi environ works""" | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |         response = self.app.get('/environ.html') | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |         ok_('The keys in the environment are:' in response) | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |     def test_data(self): | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |         """The data display demo works with HTML""" | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |         response = self.app.get('/data.html?a=1&b=2') | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |         response.mustcontain("<td>a", "<td>1", | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |                              "<td>b", "<td>2") | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |     def test_data_json(self): | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         """The data display demo works with JSON""" | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |         resp = self.app.get('/data.json?a=1&b=2') | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |         ok_( | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |             dict(page='data', params={'a': '1', 'b': '2'}) == resp.json, | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |             resp.json | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         ) | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |     def test_secc_with_manager(self): | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         """The manager can access the secure controller""" | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         # Note how authentication is forged: | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         environ = {'REMOTE_USER': 'manager'} | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         resp = self.app.get('/secc', extra_environ=environ, status=200) | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |         ok_('Secure Controller here' in resp.text, resp.text) | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |     def test_secc_with_editor(self): | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         """The editor cannot access the secure controller""" | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         environ = {'REMOTE_USER': 'editor'} | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |         self.app.get('/secc', extra_environ=environ, status=403) | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |         # It's enough to know that authorization was denied with a 403 status | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     def test_secc_with_anonymous(self): | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |         """Anonymous users must not access the secure controller""" | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |         self.app.get('/secc', status=401) | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |         # It's enough to know that authorization was denied with a 401 status | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  |