def test_category_page_using_template(self): #Create categories in database test_utils.create_categories() # Access category page response = self.client.get(reverse('show_category', args=['category-1'])) # check was used the right template self.assertTemplateUsed(response, 'rango/category.html')
def test_index_displays_five_most_liked_categories(self): #Create categories test_utils.create_categories() # Access index response = self.client.get(reverse('index')) # Check if the 5 pages with most likes are displayed for i in range(10, 5, -1): self.assertIn("Category " + str(i), response.content.decode('ascii'))
def test_url_reference_in_category_page(self): # Create user and log in test_utils.create_user() self.client.login(username='******', password='******') # Create categories test_utils.create_categories() # Check for add_page in category page response = self.client.get( reverse('show_category', args=['category-1'])) self.assertIn(reverse('add_page', args=['category-1']), response.content.decode('ascii'))
def test_cleaned_data_from_add_page(self): #Create categories and pages categories = test_utils.create_categories() i = 0 for category in categories: i = i + 1 # Access link to add page for the category url = self.live_server_url url = url.replace('localhost', '127.0.0.1') self.browser.get(url + '/rango/category/' + category.slug + '/add_page/') # Types new page name username_field = self.browser.find_element_by_name('title') username_field.send_keys('New Page ' + str(i)) # Types url for the page username_field = self.browser.find_element_by_name('url') username_field.send_keys('http://www.newpage' + str(1) + '.com') # Click on Create Page self.browser.find_element_by_css_selector( "input[type='submit']").click() body = self.browser.find_element_by_tag_name('body') # Check if New Page appears in the category page self.assertIn('New Page'.lower(), body.text.lower())
def test_pages_using_templates(self): # Create user and log in test_utils.create_user() self.client.login(username='******', password='******') # Create categories categories = test_utils.create_categories() # Create a list of pages to access pages = [ reverse('index'), reverse('about'), reverse('add_category'), reverse('register'), reverse('login'), reverse('show_category', args=[categories[0].slug]), reverse('add_page', args=[categories[0].slug]) ] #, reverse('restricted')] # Create a list of pages to access templates = [ 'rango/index.html', 'rango/about.html', 'rango/add_category.html', 'rango/register.html', 'rango/login.html', 'rango/category.html', 'rango/add_page.html' ] #, 'rango/restricted.html'] # For each page in the page list, check if it extends from base template for template, page in zip(templates, pages): response = self.client.get(page) self.assertTemplateUsed(response, template)
def test_add_page_form_is_displayed_correctly(self): # Create categories categories = test_utils.create_categories() for category in categories: # Access add category page try: response = self.client.get(reverse('index')) response = self.client.get(reverse('add_page', args=[category.slug])) except: try: response = self.client.get(reverse('rango:index')) response = self.client.get(reverse('rango:add_page', args=[category.slug])) except: return False # Check form in response context is instance of CategoryForm self.assertTrue(isinstance(response.context['form'], PageForm)) # Check form is displayed correctly # Label 1 self.assertIn('Please enter the title of the page.'.lower(), response.content.decode('ascii').lower()) # Label 2 self.assertIn('Please enter the URL of the page.'.lower(), response.content.decode('ascii').lower()) # Text input 1 self.assertIn('id="id_title" maxlength="128" name="title" type="text"'.lower(), response.content.decode('ascii').lower()) # Text input 2 self.assertIn('id="id_url" maxlength="200" name="url" type="url"'.lower(), response.content.decode('ascii').lower()) # Button self.assertIn('type="submit" name="submit" value="Add Page"'.lower(), response.content.decode('ascii').lower())
def test_category_page_displays_empty_message(self): #Create categories in database categories = test_utils.create_categories() # For each category, access its page and check there are no pages associated with it for category in categories: # Access category page response = self.client.get(reverse('show_category', args=[category.slug])) self.assertIn("No pages currently in category.".lower(), response.content.decode('ascii').lower())
def test_add_page_when_logged(self): #Create categories test_utils.create_categories() # Access login page url = self.live_server_url url = url.replace('localhost', '127.0.0.1') try: self.browser.get(url + reverse('login')) except: try: self.browser.get(url + reverse('rango:login')) except: return False # Log in test_utils.user_login(self) # Click category self.browser.find_element_by_partial_link_text('Category').click() # Click add page try: self.browser.find_element_by_partial_link_text("Add").click() except: self.browser.find_element_by_partial_link_text("add").click() # Types new page name username_field = self.browser.find_element_by_name('title') username_field.send_keys('New Page') # Types url for the page username_field = self.browser.find_element_by_name('url') username_field.send_keys('http://www.newpage.com') # Click on Create Page self.browser.find_element_by_css_selector( "input[type='submit']").click() body = self.browser.find_element_by_tag_name('body') # Check if New Page appears in the category page self.assertIn('New Page', body.text)
def test_index_contains_link_to_categories(self): #Create categories categories = test_utils.create_categories() # Access index response = self.client.get(reverse('index')) # Check if the 5 pages with most likes are displayed for i in range(10, 5, -1): category = categories[i - 1] self.assertIn(reverse('show_category', args=[category.slug])[:-1], response.content.decode('ascii'))
def test_category_context(self): #Create categories and pages for categories categories = test_utils.create_categories() pages = test_utils.create_pages(categories) # For each category check the context dictionary passed via render() function for category in categories: response = self.client.get(reverse('show_category', args=[category.slug])) pages = Page.objects.filter(category=category) self.assertCountEqual(response.context['pages'], pages) self.assertEquals(response.context['category'], category)
def test_add_page_when_not_logged(self): #Create categories test_utils.create_categories() # Access index url = self.live_server_url url = url.replace('localhost', '127.0.0.1') try: self.browser.get(url + reverse('index')) except: try: self.browser.get(url + reverse('rango:index')) except: return False # Click category self.browser.find_element_by_partial_link_text('Category').click() # Check it does not have a link to add page body = self.browser.find_element_by_tag_name('body') self.assertNotIn('Add a Page', body.text)
def test_index_displays_five_most_viewed_pages(self): #Create categories categories = test_utils.create_categories() #Create pages for categories test_utils.create_pages(categories) # Access index response = self.client.get(reverse('index')) # Check if the 5 pages with most views are displayed for i in range(20, 15, -1): self.assertIn("Page " + str(i), response.content.decode('ascii'))
def test_titles_displayed(self): # Create user and log in test_utils.create_user() self.client.login(username='******', password='******') # Create categories categories = test_utils.create_categories() # Access index and check the title displayed response = self.client.get(reverse('index')) self.assertIn('Rango -'.lower(), response.content.decode('ascii').lower()) # Access category page and check the title displayed response = self.client.get( reverse('show_category', args=[categories[0].slug])) self.assertIn(categories[0].name.lower(), response.content.decode('ascii').lower()) # Access about page and check the title displayed response = self.client.get(reverse('about')) self.assertIn('About'.lower(), response.content.decode('ascii').lower()) # Access login page and check the title displayed response = self.client.get(reverse('login')) self.assertIn('Login'.lower(), response.content.decode('ascii').lower()) # Access register page and check the title displayed response = self.client.get(reverse('register')) self.assertIn('Register'.lower(), response.content.decode('ascii').lower()) # Access restricted page and check the title displayed response = self.client.get(reverse('restricted')) self.assertIn("Since you're logged in".lower(), response.content.decode('ascii').lower()) # Access add page and check the title displayed response = self.client.get( reverse('add_page', args=[categories[0].slug])) self.assertIn('Add Page'.lower(), response.content.decode('ascii').lower()) # Access add new category page and check the title displayed response = self.client.get(reverse('add_category')) self.assertIn('Add Category'.lower(), response.content.decode('ascii').lower())
def test_category_contains_link_to_add_page(self): # Crete categories categories = test_utils.create_categories() # For each category in the database check if contains link to add page for category in categories: try: response = self.client.get(reverse('show_category', args=[category.slug])) self.assertIn(reverse('add_page', args=[category.slug]), response.content.decode('ascii')) except: try: response = self.client.get(reverse('rango:show_category', args=[category.slug])) self.assertIn(reverse('rango:add_page', args=[category.slug]), response.content.decode('ascii')) except: return False
def test_category_page_displays_pages(self): #Create categories in database categories = test_utils.create_categories() # Create pages for categories test_utils.create_pages(categories) # For each category, access its page and check for the pages associated with it for category in categories: # Access category page response = self.client.get(reverse('show_category', args=[category.slug])) # Retrieve pages for that category pages = Page.objects.filter(category=category) # Check pages are displayed and they have a link for page in pages: self.assertIn(page.title, response.content.decode('ascii')) self.assertIn(page.url, response.content.decode('ascii'))
def test_index_context(self): # Access index with empty database response = self.client.get(reverse('index')) # Context dictionary is then empty self.assertCountEqual(response.context['categories'], []) self.assertCountEqual(response.context['pages'], []) categories = test_utils.create_categories() test_utils.create_pages(categories) #Access index with database filled response = self.client.get(reverse('index')) #Retrieve categories and pages from database categories = Category.objects.order_by('-likes')[:5] pages = Page.objects.order_by('-views')[:5] # Check context dictionary filled self.assertCountEqual(response.context['categories'], categories) self.assertCountEqual(response.context['pages'], pages)