def test_create_rss_feed_url_entry_with_empty_data(self): # When: a blank RSS feed URL is added response = app.test_client().post("/config", data=dict(url=""), follow_redirects=True) # Then: no entry will be added self.assertEqual(HTTP_SUCCESS, response.status_code) self.assertIn(b"Please provide a valid URL", response.data)
def test_register_with_new_user(self, mock_db): # When: a new user is registered response = app.test_client().post("/register", data=dict(email="*****@*****.**", password="******", repeat_password="******", register=True), follow_redirects=True) # Then: the registration is successful assert mock_db.session.commit.called self.assertEqual(HTTP_SUCCESS, response.status_code) self.assertIn(b"Sign In", response.data)
def test_fetch_feed_content_invalid_url(self, mock_response): # Given: an invalid RSS Feed config rss_feed_url = "https://www.blahblah.com/rss/xml" mock_response.return_value = { 'entries': [], 'bozo': 1, 'bozo_exception': TypeError("a bytes-like object is required, not 'RssFeedUrl'", ), 'feed': {} } # When: the root path is loaded response = app.test_client().get("/content?url=" + rss_feed_url) # Then: no RSS feed content is returned self.assertEqual(HTTP_SUCCESS, response.status_code) self.assertIn(b"No RSS feed content to display", response.data)
def test_load_home_with_valid_url(self, mock_rss_feed_url): # Given: a valid RSS Feed config rss_feed_url = RssFeedUrl() rss_feed_url.url = "https://www.fiercewireless.com/rss/xml" mock_rss_feed_url.query.all.return_value = [rss_feed_url] # When: the root path is loaded response = app.test_client().get("/") # Then: the RSS feed content is successfully returned self.assertEqual(HTTP_SUCCESS, response.status_code) self.assertIn(b"https://www.fiercewireless.com/rss/xml", response.data) self.assertIn(b"FeedMe - Home", response.data)
def test_delete_rss_feed_url_entry(self, mock_rss_feed_url, mock_db): # Given: an RSS feel URL entry rss_feed_url = RssFeedUrl() rss_feed_url.url = "https://www.fiercewireless.com/rss/xml" mock_rss_feed_url.query.filter_by.first.return_value = rss_feed_url # When: the RSS feed URL entry is deleted response = app.test_client().post( "/delete", data=dict(url="https://www.fiercewireless.com/rss/xml"), follow_redirects=True) # Then: the database has been updated with the removed entry assert mock_db.session.commit.called self.assertEqual(HTTP_SUCCESS, response.status_code)
def test_create_rss_feed_url_entry(self, mock_rss_feed_url): # Given: an new RSS feed URL rss_feed_url = RssFeedUrl() rss_feed_url.url = "https://www.fiercewireless.com/rss/xml" mock_rss_feed_url.query.all.return_value = [rss_feed_url] # When: a new RSS feed URL is added response = app.test_client().post( "/config", data=dict(url="https://www.fiercewireless.com/rss/xml"), follow_redirects=True) # Then: the config page is reloaded with new entry self.assertEqual(HTTP_SUCCESS, response.status_code) self.assertIn(b"https://www.fiercewireless.com/rss/xml", response.data)
def test_register_with_incorrect_repeat_password(self, mock_db): # When: a new user is registered response = app.test_client().post("/register", data=dict( email="*****@*****.**", password="******", repeat_password="******", register=True), follow_redirects=True) # Then: the registration is not successful assert not mock_db.session.commit.called self.assertEqual(HTTP_SUCCESS, response.status_code) self.assertIn(b"Register", response.data) self.assertIn(b"Field must be equal to password", response.data)
def test_register_with_invalid_email(self, mock_db, _, invalid_email, expected_error): # When: a new user is registered with invalid email response = app.test_client().post("/register", data=dict(email=invalid_email, password="******", repeat_password="******", register=True), follow_redirects=True) # Then: the registration is not successful assert not mock_db.session.commit.called self.assertEqual(HTTP_SUCCESS, response.status_code) self.assertIn(b"Register", response.data) self.assertIn(expected_error, response.data)
def test_fetch_feed_content_valid_url(self, mock_response): # Given a valid RSS feed URL rss_feed_url = "https://www.fiercewireless.com/rss/xml" mock_response.return_value = { "feed": {"title": "FierceWireless", "link": "some link", "description": "blah"}, "entries": [{"title": "Some Entry Title", "link": "some link", "author": "Joe Bloggs", "summary": "blah", "published": "123"}] } # When the RSS feed content is fetched for URL response = app.test_client().get("/content?url=" + rss_feed_url) # Then the feed content for URL is returned self.assertEqual(HTTP_SUCCESS, response.status_code) self.assertIn(b"FierceWireless", response.data)
def test_register_with_no_password_provided(self, mock_db, _, invalid_password): # When: a new user is registered with no password provided response = app.test_client().post("/register", data=dict( email="*****@*****.**", password=invalid_password, repeat_password=invalid_password, register=True), follow_redirects=True) # Then: the registration is not successful assert not mock_db.session.commit.called self.assertEqual(HTTP_SUCCESS, response.status_code) self.assertIn(b"Register", response.data) self.assertIn(b"This field is required", response.data)
def test_create_rss_feed_url_entry_duplicate(self, mock_rss_feed_url): # Given: an already added RSS feed URL rss_feed_url = RssFeedUrl() rss_feed_url.url = "https://www.fiercewireless.com/rss/xml" mock_rss_feed_url.query.filter_by.first.return_value = rss_feed_url mock_rss_feed_url.query.all.return_value = [rss_feed_url] # When: the RSS feed URL is added again response = app.test_client().post( "/config", data=dict(url="https://www.fiercewireless.com/rss/xml"), follow_redirects=True) # Then: the entry will not be added again self.assertEqual(HTTP_SUCCESS, response.status_code) self.assertIn(b"URL already added. Please provide a unique URL", response.data)
def test_login_with_invalid_user(self, _, invalid_email, mock_user, mock_db): # Given: the user is not registered mock_user.query.get.return_value = None # When: login credentials are provided with invalid user response = app.test_client().post("/login", data=dict(email=invalid_email, password="******", remember_me=False, login=True), follow_redirects=True) # Then: the user is not logged in assert not mock_db.session.commit.called self.assertEqual(HTTP_SUCCESS, response.status_code) self.assertIn(b"FeedMe - Login", response.data)
def test_login_with_valid_credentials(self, mock_user, mock_db): # Given: a registered user registered_user = User(email="*****@*****.**", authenticated=True) registered_user.set_password("test") mock_user.query.get.return_value = registered_user # When: valid login credentials are provided response = app.test_client().post("/login", data=dict(email="*****@*****.**", password="******", remember_me=False, login=True), follow_redirects=True) # Then: the user is successfully logged in assert mock_db.session.commit.called self.assertEqual(HTTP_SUCCESS, response.status_code) self.assertIn(b"FeedMe - Home", response.data)
def test_register_with_already_used_email(self, mock_user, mock_db): # Given: a user already existing with email mock_user.query.filter_by.first.return_value = User( email="*****@*****.**", password="******", authenticated=True) # When: a new user is registered with same email response = app.test_client().post("/register", data=dict(email="*****@*****.**", password="******", repeat_password="******", register=True), follow_redirects=True) # Then: the registration is not successful assert not mock_db.session.commit.called self.assertEqual(HTTP_SUCCESS, response.status_code) self.assertIn(b"Register", response.data) self.assertIn(b"Email already in use. Please choose a different email", response.data)