예제 #1
0
    def test_category_redirect_to_desired_page(self):
        # Populate database
        populate_rango.populate()

        # Access index page
        url = self.live_server_url
        url = url.replace('localhost', '127.0.0.1')
        self.browser.get(url + reverse('index'))

        #Access Python category page
        category_link = self.browser.find_elements_by_link_text('Python')
        category_link[0].click()

        # Check it is in the correct page
        self.assertEquals(self.browser.current_url, url + reverse('show_category', args=['python']))

        # Access index page
        self.browser.get(url + reverse('index'))

        #Access Django category page
        category_link = self.browser.find_elements_by_link_text('Django')
        category_link[0].click()

        # Check it is in the correct page
        self.assertEquals(self.browser.current_url, url + reverse('show_category', args=['django']))

        # Access index page
        self.browser.get(url + reverse('index'))

        #Access Other Frameworks category page
        category_link = self.browser.find_elements_by_link_text('Other Frameworks')
        category_link[0].click()

        # Check it is in the correct page
        self.assertEquals(self.browser.current_url, url + reverse('show_category', args=['other-frameworks']))
예제 #2
0
    def setUp(self):
        """

        Imports and runs the population script, calling the populate() method.

        """

        try:

            import populate_rango

        except ImportError:

            raise ImportError(
                f"{FAILURE_HEADER}The Chapter 5 tests could not import the populate_rango. Check it's in the right location (the first tango_with_django_project directory).{FAILURE_FOOTER}"
            )

        if 'populate' not in dir(populate_rango):

            raise NameError(
                f"{FAILURE_HEADER}The populate() function does not exist in the populate_rango module. This is required.{FAILURE_FOOTER}"
            )

        # Call the population script -- any exceptions raised here do not have fancy error messages to help readers.

        populate_rango.populate()
예제 #3
0
    def test_add_page_link(self):
        """
        Tests to see if the Add Page link only appears when logged in.
        """
        populate()
        content = self.client.get(
            reverse('rango:show_category',
                    kwargs={'category_name_slug': 'python'})).content.decode()

        self.assertTrue(
            reverse('rango:add_page', kwargs={'category_name_slug':
                                              'python'}) not in content,
            f"{FAILURE_HEADER}The Add Page link was present in the show_category() response when a user was not logged in. It shouldn't be there. Did you do the exercises?{FAILURE_FOOTER}"
        )

        user_object = create_user_object()
        self.client.login(username='******', password='******')
        content = self.client.get(
            reverse('rango:show_category',
                    kwargs={'category_name_slug': 'python'})).content.decode()

        self.assertTrue(
            reverse('rango:add_page', kwargs={'category_name_slug':
                                              'python'}) in content,
            f"{FAILURE_HEADER}The Add Page link was not present when a user was logged in, and looking at the show_category() view. Did you make a mistake in your category.html template?{FAILURE_FOOTER}"
        )
예제 #4
0
    def test_add_page_form_response(self):
        """

        Checks whether the template rendering add_page() contains a form, and whether it points to the add_page view.

        """

        populate()

        response = self.client.get(
            reverse('rango:add_page', kwargs={'category_name_slug': 'django'}))

        context = response.context

        content = response.content.decode()

        self.assertTrue(
            '<form' in content,
            f"{FAILURE_HEADER}We couldn't find a <form> element in the response for adding a page.{FAILURE_FOOTER}"
        )

        self.assertTrue(
            'action="/rango/category/django/add_page/"' in content,
            f"{FAILURE_HEADER}We couldn't find the correct action URL for adding a page in your add_page.html template. We expected to see 'action=\"/rango/django/add_page/\"' when adding a page to the 'python' category.{FAILURE_FOOTER}"
        )
예제 #5
0
 def test_add_page_template(self):
     """
     Checks whether a template was used for the add_page() view.
     """
     populate()
     response = self.client.get(reverse('rango:add_page', kwargs={'category_name_slug': 'python'}))
     self.assertTemplateUsed(response, 'rango/add_page.html', f"{FAILURE_HEADER}The add_page.html template is not used for the add_page() view. The specification requires this.{FAILURE_FOOTER}")
예제 #6
0
    def test_admin_page_contains_title_url_and_category(self):
        #Populate database
        populate_rango.populate()

        url = self.live_server_url
        url = url.replace('localhost', '127.0.0.1')
        self.browser.get(url + reverse('admin:index'))

        # Log in the admin page
        test_utils.login(self)

        # Click in Pages
        pages_link = self.browser.find_element_by_link_text('Pages')
        pages_link.click()

        body = self.browser.find_element_by_tag_name('body')

        # Get all pages
        pages = Page.objects.all()

        # Check all pages title, category and url are displayed
        for page in pages:
            self.assertIn(page.title, body.text)
            # self.assertIn(page.category.name, body.text)
            self.assertIn(page.url, body.text)

        # Check for the Github account and PythonAnywhere account
        body = self.browser.find_element_by_tag_name('body')
        self.assertIn('http://www.djangorocks.com/', body.text)
        self.assertIn('http://flask.pocoo.org', body.text)
    def test_add_page_functionality(self):
        """
        Given a category and a new page, tests whether the functionality implemented works as expected.
        """
        populate()

        response = self.client.post(
            reverse('rango:add_page', kwargs={'category_name_slug': 'python'}),
            {
                'title': 'New webpage',
                'url': 'www.google.com',
                'views': 0
            })

        python_pages = Page.objects.filter(title='New webpage')
        self.assertEqual(
            len(python_pages), 1,
            f"{FAILURE_HEADER}When adding a new page to a category with the add_page form, the new Page object that we were expecting wasn't created. Check your add_page() view for mistakes, and try again. You need to call .save() on the page you create!{FAILURE_FOOTER}"
        )

        page = python_pages[0]
        self.assertEqual(
            page.url, 'http://www.google.com',
            f"{FAILURE_HEADER}We created a new page with a URL of 'www.google.com'. The saved object is expected to have a URL of 'http://www.google.com'. Is your clean() method in PageForm working correctly?{FAILURE_FOOTER}"
        )
        self.assertEqual(
            page.title, 'New webpage',
            f"{FAILURE_HEADER}The new page we created didn't have the title we specified in the add_page form. Are you missing something in your PageForm implementation?{FAILURE_FOOTER}"
        )
예제 #8
0
    def test_good_add_page(self):
        """

        Tests to see if a page can be added when logged in.

        """

        populate()

        user_object = create_user_object()

        self.client.login(username='******', password='******')

        response = self.client.get(
            reverse('rango:add_page', kwargs={'category_name_slug': 'python'}))

        self.assertEqual(
            response.status_code, 200,
            f"{FAILURE_HEADER}We weren't greeted with a HTTP status code when attempting to add a page when logged in. Check your add_page() view.{FAILURE_FOOTER}"
        )

        content = response.content.decode()

        self.assertTrue(
            'Add a Page' in content,
            f"{FAILURE_HEADER}When adding a page (when logged in), we didn't see the expected page. Please check your add_page() view.{FAILURE_FOOTER}"
        )
예제 #9
0
    def test_template_usage(self):
        """
        Check that each view uses the correct template.
        """
        populate()

        urls = [
            reverse('rango:about'),
            reverse('rango:add_category'),
            reverse('rango:add_page', kwargs={'category_name_slug': 'python'}),
            reverse('rango:show_category',
                    kwargs={'category_name_slug': 'python'}),
            reverse('rango:index'),
        ]

        templates = [
            'rango/about.html',
            'rango/add_category.html',
            'rango/add_page.html',
            'rango/category.html',
            'rango/index.html',
        ]

        for url, template in zip(urls, templates):
            response = self.client.get(url)
            self.assertTemplateUsed(response, template)
예제 #10
0
    def test_admin_page_contains_title_url_and_category(self):
        #Populate database
        populate_rango.populate()

        self.browser.get(self.live_server_url + reverse('admin:index'))

        # Log in the admin page
        test_utils.login(self)

        # Click in Pages
        pages_link = self.browser.find_element_by_link_text('Pages')
        pages_link.click()

        body = self.browser.find_element_by_tag_name('body')

        # Get all pages
        pages = Page.objects.all()

        # Check all pages title, category and url are displayed
        for page in pages:
            self.assertIn(page.title, body.text)
            self.assertIn(page.category.name, body.text)
            self.assertIn(page.url, body.text)

        # Check for the Github account and PythonAnywhere account
        body = self.browser.find_element_by_tag_name('body')
        self.assertIn('pythonanywhere.com', body.text)
        self.assertIn('github.com', body.text)
예제 #11
0
    def test_population_script(self):
        #Populate database
        populate_rango.populate()

        self.browser.get(self.live_server_url + reverse('admin:index'))

        # Log in the admin page
        test_utils.login(self)

        # Check if is there link to categories
        category_link = self.browser.find_elements_by_partial_link_text('Categor')
        category_link[0].click()

        # Check for the categories saved by the population script
        self.browser.find_elements_by_partial_link_text('Other Frameworks')
        self.browser.find_elements_by_partial_link_text('Django')
        self.browser.find_elements_by_partial_link_text('Python')

        # Check the pages saved by the population script
        self.browser.get(self.live_server_url + reverse('admin:rango_page_changelist'))
        self.browser.find_elements_by_partial_link_text('Flask')
        self.browser.find_elements_by_partial_link_text('Bottle')
        self.browser.find_elements_by_partial_link_text('How to Tango with Django')
        self.browser.find_elements_by_partial_link_text('Official Django Tutorial')
        self.browser.find_elements_by_partial_link_text('Django Rocks')
        self.browser.find_elements_by_partial_link_text('Learn Python in 10 Minutes')
        self.browser.find_elements_by_partial_link_text('How to Think like a Computer Scientist')
        self.browser.find_elements_by_partial_link_text('Official Python Tutorial')
예제 #12
0
    def test_population_script(self):
        #Populate database
        populate_rango.populate()
        url = self.live_server_url
        #url = url.replace('localhost', '127.0.0.1')
        self.browser.get(url + reverse('admin:index'))

        # Log in the admin page
        test_utils.login(self)

        # # Check if is there link to categories
        # category_link = self.browser.find_elements_by_partial_link_text('Categor')
        # print(category_link[0].text)
        # category_link[0].click()

        # Check for the categories saved by the population script
        # self.browser.find_elements_by_partial_link_text('Other Frameworks')
        # self.browser.find_elements_by_partial_link_text('Django')
        # self.browser.find_elements_by_partial_link_text('Python')

        # Check the pages saved by the population script
        self.browser.get(url + reverse('admin:rango_page_changelist'))
        self.browser.find_elements_by_partial_link_text('Flask')
        self.browser.find_elements_by_partial_link_text('Bottle')
        self.browser.find_elements_by_partial_link_text(
            'How to Tango with Django')
        self.browser.find_elements_by_partial_link_text(
            'Official Django Tutorial')
        self.browser.find_elements_by_partial_link_text('Django Rocks')
        self.browser.find_elements_by_partial_link_text(
            'Learn Python in 10 Minutes')
        self.browser.find_elements_by_partial_link_text(
            'How to Think like a Computer Scientist')
        self.browser.find_elements_by_partial_link_text(
            'Official Python Tutorial')
예제 #13
0
 def setUp(self):
     try:
         populate()
     except ImportError:
         print('The module populate_rango does not exist')
     except NameError:
         print('The function populate() does not exist or is not correct')
     except Exception:
         print('Something went wrong in the populate() function :-(')
예제 #14
0
 def test_bad_add_page(self):
     """
     Tests to see if a page cannot be added when not logged in.
     """
     populate()
     response = self.client.get(reverse('rango:add_page', kwargs={'category_name_slug': 'python'}))
     
     self.assertEqual(response.status_code, 302, f"{FAILURE_HEADER}When not logged in and attempting to add a page, we should be redirected. But we weren't. Check your add_page() implementation.{FAILURE_FOOTER}")
     self.assertTrue(response.url.startswith(reverse('rango:login')), f"{FAILURE_HEADER}When not logged in and attempting to add a page, we should be redirected to the login page. But we weren't. Check your add_page() implementation.{FAILURE_FOOTER}")
예제 #15
0
    def test_category_exists(self):
        """
        Attempts to add a category that already exists.
        """
        populate()

        response = self.client.post(reverse('rango:add_category'),
                                            {'name': 'Python', 'views': 0, 'likes': 0})
        
        self.assertTrue('Category with this Name already exists.' in response.content.decode(), f"{FAILURE_HEADER}When attempting to add a category that already exists, we didn't get the error message we were expecting. Please check your add_category() view and add_category.html template.{FAILURE_FOOTER}")
예제 #16
0
파일: tests.py 프로젝트: sigbikes/rango
 def setUp(self):
     try:
         from populate_rango import populate
         populate()
     except ImportError:
         print('The module populate_rango does not exist')
     except NameError:
         print('The function populate() does not exist or is not correct')
     except:
         print('Something went wrong in the populate() function :-(')
예제 #17
0
 def setUp(self):
     try:
         from populate_rango import populate
         populate()
     except ImportError:
         print("The module populate_rango does not exist")
     except NameError:
         print("The function populate() does not exist or is not correct")
     except Exception as ex:
         print("Unexpected exception in populate() function - %s" % ex)
예제 #18
0
def populate():
    """  auxiliary function to populate database
    """
    try:
        from populate_rango import populate
        populate()
    except ImportError:
        print('The module populate_rango does not exist')
    except NameError:
        print('The function populate() does not exist or is not correct')
    except Exception:
        print('Something went wrong in the populate() function :-(')
        raise
예제 #19
0
    def test_population_script_changes(self):
        #Populate database
        populate_rango.populate()

        # Check if the category has correct number of views and likes
        cat = Category.objects.get(name='Python')
        self.assertEquals(cat.views, 128)
        self.assertEquals(cat.likes, 64)

        # Check if the category has correct number of views and likes
        cat = Category.objects.get(name='Django')
        self.assertEquals(cat.views, 64)
        self.assertEquals(cat.likes, 32)

        # Check if the category has correct number of views and likes
        cat = Category.objects.get(name='Other Frameworks')
        self.assertEquals(cat.views, 32)
        self.assertEquals(cat.likes, 16)
예제 #20
0
    def test_population_script_changes(self):
        #Populate database
        populate_rango.populate()

        # Check if the category has correct number of views and likes
        cat = Category.objects.get(name='Python')
        self.assertEquals(cat.views, 128)
        self.assertEquals(cat.likes, 64)

        # Check if the category has correct number of views and likes
        cat = Category.objects.get(name='Django')
        self.assertEquals(cat.views, 64)
        self.assertEquals(cat.likes, 32)

        # Check if the category has correct number of views and likes
        cat = Category.objects.get(name='Other Frameworks')
        self.assertEquals(cat.views, 32)
        self.assertEquals(cat.likes, 16)
예제 #21
0
    def test_title_blocks(self):
        """
        Tests whether the title blocks in each page are the expected values.
        This is probably the easiest way to check for blocks.
        """
        populate()
        template_base_path = os.path.join(settings.TEMPLATE_DIR, 'rango')

        mappings = {
            reverse('rango:about'): {
                'full_title_pattern': r'<title>(\s*|\n*)Rango(\s*|\n*)-(\s*|\n*)About Rango(\s*|\n*)</title>',
                'block_title_pattern': r'{% block title_block %}(\s*|\n*)About Rango(\s*|\n*){% endblock %}',
                'template_filename': 'about.html'},
            reverse('rango:add_category'): {
                'full_title_pattern': r'<title>(\s*|\n*)Rango(\s*|\n*)-(\s*|\n*)Add a Category(\s*|\n*)</title>',
                'block_title_pattern': r'{% block title_block %}(\s*|\n*)Add a Category(\s*|\n*){% endblock %}',
                'template_filename': 'add_category.html'},
            reverse('rango:add_page', kwargs={'category_name_slug': 'python'}): {
                'full_title_pattern': r'<title>(\s*|\n*)Rango(\s*|\n*)-(\s*|\n*)Add a Page(\s*|\n*)</title>',
                'block_title_pattern': r'{% block title_block %}(\s*|\n*)Add a Page(\s*|\n*){% endblock %}',
                'template_filename': 'add_page.html'},
            reverse('rango:show_category', kwargs={'category_name_slug': 'python'}): {
                'full_title_pattern': r'<title>(\s*|\n*)Rango(\s*|\n*)-(\s*|\n*)Python(\s*|\n*)</title>',
                'block_title_pattern': r'{% block title_block %}(\s*|\n*){% if category %}(\s*|\n*){{ category.name }}(\s*|\n*){% else %}(\s*|\n*)Unknown Category(\s*|\n*){% endif %}(\s*|\n*){% endblock %}',
                'template_filename': 'category.html'},
            reverse('rango:index'): {
                'full_title_pattern': r'<title>(\s*|\n*)Rango(\s*|\n*)-(\s*|\n*)Homepage(\s*|\n*)</title>',
                'block_title_pattern': r'{% block title_block %}(\s*|\n*)Homepage(\s*|\n*){% endblock %}',
                'template_filename': 'index.html'},
        }

        for url in mappings.keys():
            full_title_pattern = mappings[url]['full_title_pattern']
            template_filename = mappings[url]['template_filename']
            block_title_pattern = mappings[url]['block_title_pattern']

            request = self.client.get(url)
            content = request.content.decode('utf-8')
            template_str = self.get_template(os.path.join(template_base_path, template_filename))

            self.assertTrue(re.search(full_title_pattern, content),
                            f"{FAILURE_HEADER}When looking at the response of GET '{url}', we couldn't find the correct <title> block. Check the exercises on Chapter 8 for the expected title.{FAILURE_FOOTER}")
            self.assertTrue(re.search(block_title_pattern, template_str),
                            f"{FAILURE_HEADER}When looking at the source of template '{template_filename}', we couldn't find the correct template block. Are you using template inheritence correctly, and did you spell the title as in the book? Check the exercises on Chapter 8 for the expected title.{FAILURE_FOOTER}")
    def setUp(self):
        self.client = Client()
        try:
            populate()
        except ImportError:
            print('The module populate_rango does not exist')
        except NameError:
            print('The function populate() does not exist or is not correct')
        except:
            print('Something went wrong in the populate() function :-(')

        # Check forms are defined
        try:
            from forms import PageForm
            from forms import CategoryForm
            PageForm()
            CategoryForm()
        except ImportError:
            print('The module forms does not exist')
        except NameError:
            print('The class PageForm does not exist or is not correct')
        except Exception:
            print('Something else went wrong :-(')
 def setUp(self):
     populate()
 def setUp(self):
     populate()
     self.response = self.client.get(reverse('rango:index'))
     self.content = self.response.content.decode()
 def setUp(self):
     populate()
     self.response = self.client.get(reverse('rango:show_category', kwargs={'category_name_slug': 'other-frameworks'}))
     self.content = self.response.content.decode()