def test_alerts(self): """ Unlike the test_alerts() definition in the users package this just tests that TaggingAlert and CommentingAlert are generated in the right situation """ # Create 3 test users user1 = create_account('user1', '*****@*****.**', 'Password') user2 = create_account('user2', '*****@*****.**', 'Password') user3 = create_account('user3', '*****@*****.**', 'Password') # No need to activate the accounts activate(user1) activate(user2) activate(user3) # User1 tag user2 in a post post1 = create_post(user1, 'user1', 'Hello @user2') # Get alerts for test2 and check that it is correct alert = get_alerts(user2).items[0] self.assertTrue(isinstance(alert, TaggingAlert)) self.assertEqual(alert.user['username'], 'user1') self.assertEqual(alert.user['email'], '*****@*****.**') self.assertIn('tagged you in a', alert.prettify()) # Have user2 comment on a the post and check that user1 has the alert create_post(user2, 'user2', 'Hello', post1) # User 1 should now have a commenting alert from user2 alert = get_alerts(user1).items[0] self.assertTrue(isinstance(alert, CommentingAlert)) self.assertEqual(alert.user['username'], 'user2') self.assertEqual(alert.user['email'], '*****@*****.**') # Please remember that prettify requires a uid to format it to self.assertIn('commented on a', alert.prettify(user1)) self.assertIn('you posted', alert.prettify(user1)) # Create a comment as user3 so they become subscribed create_post(user3, 'user3', 'Hello', post1) # Check that if user1 posts a comment user2 get a tagging alert create_post(user1, 'user1', 'Hello', post1) # Check alerts for user2 alert = get_alerts(user2).items[0] self.assertTrue(isinstance(alert, CommentingAlert)) self.assertIn('you were tagged in', alert.prettify(user2)) # Check alerts for user3 alert = get_alerts(user3).items[0] self.assertTrue(isinstance(alert, CommentingAlert)) self.assertIn('you commented on', alert.prettify(user3)) # To check that an invalid subscription reason returns the generic # reason. This should not happen on the site. # Manually change the score in Redis to a high number r.zincrby(K.POST_SUBSCRIBERS.format(post1), user3, 100) # Check the new prettify message alert = get_alerts(user3).items[0] self.assertTrue(isinstance(alert, CommentingAlert)) self.assertIn('you are subscribed to', alert.prettify(user3))
def test_alerts_pagination_sizes(self): """Check that the correct number of alerts are generated""" # Create 2 test users user1 = create_account('user1', '*****@*****.**', 'Password') user2 = create_account('user2', '*****@*****.**', 'Password') # Generate lots of following alerts for i in xrange(101): follow_user(user2, user1) unfollow_user(user2, user1) ALERT_ITEMS_PER_PAGE = app.config.get('ALERT_ITEMS_PER_PAGE') self.assertEqual(len(get_alerts(user1).items), ALERT_ITEMS_PER_PAGE) self.assertEqual(len(get_alerts(user1, per_page=25).items), 25) self.assertEqual(len(get_alerts(user1, per_page=50).items), 50) self.assertEqual(len(get_alerts(user1, per_page=100).items), 100)
def alerts(): """Display a users alerts (notifications) to them on the site.""" uid = current_user.get('_id') # Pagination page = handle_page(request) _results = get_alerts(uid, page) return render_template('alerts.html', pagination=_results)
def alerts(): """Display a users alerts (notifications) to them on the site.""" uid = current_user.get('_id') # Pagination page = handle_page(request) _results = get_alerts(uid, page, current_user.get('alerts_pagination_size')) return render_template('alerts.html', pagination=_results)
def test_alerts(self): """ Tests for the 2 functions which are used on the side to get alerts and also test FollowAlert from here. """ # Create 2 test users user1 = create_account('user1', '*****@*****.**', 'Password') user2 = create_account('user2', '*****@*****.**', 'Password') # Ensure that get_alerts pagination object is empty self.assertEqual(get_alerts(user1).total, 0) self.assertEqual(len(get_alerts(user1).items), 0) # Get user 2 to follow user 1 follow_user(user2, user1) # Check that i_has_alerts is True self.assertTrue(new_alerts(user1)) # Ensure that there is an alert in the get_alerts self.assertEqual(get_alerts(user1).total, 1) self.assertEqual(len(get_alerts(user1).items), 1) # Check that i_has_alerts is False, we have read them with get_alerts self.assertFalse(new_alerts(user1)) # Get the alert and check that the alert is the follow alert alert = get_alerts(user1).items[0] self.assertTrue(isinstance(alert, FollowAlert)) # Also check it's still a BaseAlert self.assertTrue(isinstance(alert, BaseAlert)) # Check its from test2 self.assertEqual(alert.user.get('username'), 'user2') self.assertEqual(alert.user.get('email'), '*****@*****.**') self.assertIn('has started following you', alert.prettify()) # Delete test2 and ensure we get no alerts delete_account(user2) # Ensure the alert is still inside Redis self.assertEqual(r.zcard(k.USER_ALERTS.format(user1)), 1) # Get the alerts, should be none and should also clear the alert from # Redis self.assertEqual(get_alerts(user1).total, 0) self.assertEqual(len(get_alerts(user1).items), 0) self.assertEqual(r.zcard(k.USER_ALERTS.format(user1)), 0) # Do the same as above to ensure we can delete an alert ourselves # Create another user user3 = create_account('user3', '*****@*****.**', 'Password') follow_user(user1, user3) # Check the alerts are there alert = get_alerts(user3).items[0] self.assertTrue(isinstance(alert, FollowAlert)) # Also check it's still a BaseAlert self.assertTrue(isinstance(alert, BaseAlert)) # Check its from test2 self.assertEqual(alert.user.get('username'), 'user1') self.assertEqual(alert.user.get('email'), '*****@*****.**') self.assertIn('has started following you', alert.prettify()) # Delete the alert with aid from the alert delete_alert(user3, alert.alert_id) # Get the alerts and ensure the list is empty self.assertEqual(get_alerts(user3).total, 0) self.assertEqual(len(get_alerts(user3).items), 0) self.assertEqual(r.zcard(k.USER_ALERTS.format(user3)), 0) # Unfollow the user3 and then follow them again unfollow_user(user1, user3) follow_user(user1, user3) alert = get_alerts(user3).items[0] self.assertIn('has started following you', alert.prettify()) # Manually delete the alert r.delete(k.ALERT.format(alert.alert_id)) # Get the alerts again and ensure the length is 0 # Ensure that the alert is not pulled down alerts = get_alerts(user3) self.assertEqual(len(alerts.items), 0) # Get alerts for a non-existant user # This will not fail but will have an empty pagination alerts = get_alerts(k.NIL_VALUE) self.assertEqual(len(alerts.items), 0)
def test_alerts(self): """Check that alerts are displayed properly in the frontend.""" # Create two test users user1 = create_account('user1', '*****@*****.**', 'Password') user2 = create_account('user2', '*****@*****.**', 'Password') user3 = create_account('user3', '*****@*****.**', 'Password') # Activate activate(user1) activate(user2) activate(user3) # Try an visit i-has-alerts when not logged in resp = self.client.get(url_for('users.new_alerts')) self.assertEqual(resp.status_code, 403) # Login as user1 self.client.post(url_for('auth.signin'), data={ 'username': '******', 'password': '******' }) # Get I has alerts and check that it is false resp = self.client.get(url_for('users.new_alerts')) # Check the JSON response self.assertEqual(resp.status_code, 200) self.assertEqual(json.loads(resp.data).get('new_alerts'), 0) # Ensure that /alerts returns nothing resp = self.client.get(url_for('users.alerts')) self.assertNotIn('list:alert', resp.data) self.assertIn('Empty', resp.data) # Get user2 to follow user1 follow_user(user2, user1) # Ensure that /i-has-alerts is correct resp = self.client.get(url_for('users.new_alerts')) # Check the JSON response self.assertEqual(resp.status_code, 200) self.assertEqual(json.loads(resp.data).get('new_alerts'), 1) # Ensure the count goes up correctly follow_user(user3, user1) resp = self.client.get(url_for('users.new_alerts')) # Check the JSON response self.assertEqual(resp.status_code, 200) self.assertEqual(json.loads(resp.data).get('new_alerts'), 2) resp = self.client.get(url_for('users.alerts')) # We don't know the alert ID but we can check that one is there by # looking for the comment in test mode self.assertIn('list:alert:', resp.data) self.assertNotIn('Empty', resp.data) # Check test2's name is there self.assertIn('user2', resp.data) # Check that the prettify message from FollowAlert is there self.assertIn('has started following you', resp.data) # We have now checked the alerts, ensure that i-has-alerts is False resp = self.client.get(url_for('users.new_alerts')) # Check the JSON response self.assertEqual(resp.status_code, 200) self.assertEqual(json.loads(resp.data).get('new_alerts'), 0) # Check that we can delete the alert # Get the both alert ids from the backend function alert1 = get_alerts(user1).items[0].alert_id alert2 = get_alerts(user1).items[1].alert_id # Check that we don't get a message if there was no alert to delete resp = self.client.get(url_for('users.delete_alert', alert_id=k.NIL_VALUE), follow_redirects=True) self.assertNotIn('Alert has been hidden', resp.data) # Delete both alerts resp = self.client.get(url_for('users.delete_alert', alert_id=alert1), follow_redirects=True) self.assertIn('Alert has been hidden', resp.data) self.assertNotIn('<!-- list:alert:{} -->'.format(alert1), resp.data) self.assertIn('<!-- list:alert:{} -->'.format(alert2), resp.data) # Check when the last alert is deleted we get an empty list resp = self.client.get(url_for('users.delete_alert', alert_id=alert2), follow_redirects=True) self.assertIn('Alert has been hidden', resp.data) # Check that there are also no alerts now self.assertIn('Empty', resp.data)