def test_root_url_staff_logged_in(self): home_url = reverse('visitas') found = resolve(home_url) # expected response for a user that IS logged in and # IS staff request = self.factory.get(home_url) logged_in_staff = User() logged_in_staff.is_staff = True request.user = logged_in_staff response_expected_logged_in_staff = VisitRecordTableView.as_view()( request) response_found_logged_in_staff = found.func(request) self.assertEqual( response_expected_logged_in_staff.template_name, ['historias/visitas.html', 'historias/visitrecord_list.html']) self.assertEqual(response_expected_logged_in_staff.template_name, response_found_logged_in_staff.template_name)
def test_root_url_user_not_logged_in(self): home_url = reverse('visitas') found = resolve(home_url) # expected response for a user that IS NOT logged in request = self.factory.get(home_url) request.user = AnonymousUser() response_expected_not_logged_in = VisitRecordTableView.as_view()( request) response_found_not_logged_in = found.func(request) self.assertTrue( isinstance( response_found_not_logged_in, HttpResponseRedirect)) self.assertEqual( response_expected_not_logged_in.url, "/accounts/login/?next=/historias/visitas") self.assertEqual(response_expected_not_logged_in.url, response_found_not_logged_in.url)
def test_root_page_view_html(self): # generate response for logged in staff request = self.factory.get(reverse('visitas')) logged_in_staff = User() logged_in_staff.is_staff = True request.user = logged_in_staff response = VisitRecordTableView.as_view()( request) response.render() # test the html contents self.assertTrue(response.content.startswith(b'<!DOCTYPE html>')) # test table header should be present self.assertIn( bytearray( '<thead> <tr> <th class="id orderable ' 'sortable"><a href="?sort=id">ID</a></th> <th class="ordera' 'ble patient sortable"><a href="?sort=patient">Paciente</a></th' '> <th class="orderable sortable staff_in_charge"><a href="' '?sort=staff_in_charge">Personal a cargo de la visita</a></th>&' '#32;<th class="created desc orderable sortable"><a href="?sort' '=created">Creado</a></th> <th class="modified orderable so' 'rtable"><a href="?sort=modified">Modificado</a></th> </tr>' ' </thead>', 'utf-8'), response.content) self.assertTrue(response.content.strip().endswith(b'</html>')) # make a list of the VisitRecords fixtures created by mixer DATE_FORMAT = "%d/%m/%Y %H:%M" visit_tuples = [] visits = self.visitsA + self.visitsB for v in visits: visit_tuples.append( (str(v.patient), str(v.staff_in_charge), v.created.strftime(DATE_FORMAT), v.modified.strftime(DATE_FORMAT))) soup = BeautifulSoup(response.content.decode('utf-8'), 'html.parser') # find all rows, skip the header for row in soup.find_all('tr')[1:]: patient = row.find('td', {"class": "patient"}).text staff_in_charge = row.find('td', {"class": "staff_in_charge"}).text modified = row.find('td', {"class": "modified"}).text created = row.find('td', {"class": "created"}).text # Each visit record that appears in the table is part of the visit # records that were created for the test self.assertIn( (patient, staff_in_charge, modified, created), visit_tuples) idx = visit_tuples.index( (patient, staff_in_charge, modified, created)) # The url link, present in the id column of the table, links to the # proper VisitRecord slug idcol = row.find('td', {"class": "id"}).find('a')['href'] self.assertEquals(resolve(idcol).kwargs['slug'], visits[idx].slug)