def setUp(self): """Executed prior to each test.""" app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['DEBUG'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' self.app = app.test_client() db.drop_all() db.create_all()
def test_book_detail_logged_in(self): """Test that the book appears on its detail page.""" # creates books, authors, user create_books() create_user() login(self.app, 'me1', 'password') # a GET request to the URL /book/1, check to see that the # status code is 200 result = app.test_client().get('/book/1', follow_redirects=True) self.assertEqual(result.status_code, 200) # Checks that the response contains the book's title, publish date, # and author's name book = Book.query.get(1) self.assertEqual(book.title, 'To Kill a Mockingbird') self.assertEqual(book.publish_date, date(1960, 7, 11)) self.assertEqual(book.author.name, "Harper Lee")
def test_book_detail_logged_out(self): """Test that the book appears on its detail page.""" # creates books, authors, user create_books() create_user() # a GET request to the URL /book/1, check to see that the # status code is 200 result = app.test_client().get('/book/1', follow_redirects=True) self.assertEqual(result.status_code, 200) result_text = result.get_data(as_text=True) # Checks that the response contains the book's title, publish date, # and author's name book = Book.query.get(1) self.assertEqual(book.title, 'To Kill a Mockingbird') self.assertEqual(book.publish_date, date(1960, 7, 11)) self.assertEqual(book.author.name, "Harper Lee") # Checks that the response does NOT contain the 'Favorite' button # (it should only be shown to logged in users) self.assertNotIn('Favorite', result_text)
def test_logout(self): # test logout page tester = app.test_client(self) response = tester.get('/logout', content_type = 'html/text', follow_redirects=True) self.assertTrue(b'logged out' in response.data) # check for flash message text self.assertTrue(b'Please login' in response.data) # back to login page ?
def test_bad_login(self): # test an invalid login tester = app.test_client(self) response = tester.post('/login', data =dict(username="******", password="******"), follow_redirects=True) self.assertTrue(b'User name or password not found. Please try again' in response.data) # check for flash message text self.assertEqual(response.status_code, 200)
def test_good_login(self): # test the login with correct credentials tester = app.test_client(self) response = tester.post('/login', data =dict(username="******", password="******"), follow_redirects=True) self.assertTrue(b'logged in' in response.data) # check for flash message of logged in self.assertEqual(response.status_code, 200)
def test_index_requires_login(self): # test the login route function with app.test_client() as client: # NOTE: Check text on login page in response response = client.get('/', follow_redirects = True) self.assertTrue(b'Please login first' in response.data) # test for request to login
def test_index(self): # test the login route function with app.test_client() as client: # NOTE: Check text on login page in response response = client.get('/', content_type = 'html/text') self.assertEqual(response.status_code, 302) # test for redirect to login page (not logged in))
def test_login_loads(self): # test the login route function with app.test_client() as client: # NOTE: URL response code 200 means OK response = client.get('/login', content_type = 'html/text') self.assertEqual(response.status_code, 200) self.assertTrue(b'Please login' in response.data)