def test_follows(self): u1 = User(email='*****@*****.**', password='******') u2 = User(email='*****@*****.**', password='******') db.session.add(u1) db.session.add(u2) db.session.commit() self.assertFalse(u1.is_following(u2)) self.assertFalse(u1.is_followed_by(u2)) timestamp_before = datetime.utcnow() u1.follow(u2) timestamp_after = datetime.utcnow() self.assertTrue(u1.is_following(u2)) self.assertFalse(u1.is_followed_by(u2)) self.assertTrue(u2.is_followed_by(u1)) self.assertTrue(u1.followed.count() == 2) self.assertTrue(u2.followers.count() == 2) f = u1.followed.all()[-1] self.assertTrue(f.followed == u2) self.assertTrue(timestamp_before <= f.timestamp <= timestamp_after) f = u2.followers.all()[-1] self.assertTrue(f.follower == u1) u1.unfollow(u2) self.assertTrue(u1.followed.count() == 1) self.assertTrue(u2.followers.count() == 1) self.assertTrue(Follow.query.count() == 2) u2.follow(u1) db.session.delete(u2) db.session.commit() self.assertTrue(Follow.query.count() == 1)
class UserTest(unittest.TestCase): def setUp(self): self.app = create_app(config['testDB']) self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.user = User(username='******', email='*****@*****.**', password='******', image_file='Night view.png') db.session.add(self.user) db.session.commit() def tearDown(self): db.session.remove() db.drop_all() self.app_context.pop() def test_model_repr(self): self.assertEqual(repr(self.user), "User: Haruka, [email protected], Night view.png") def test_token_valid(self): token = self.user.generate_confirmation_token() self.assertTrue(self.user.verify_token(token)) def test_token_invalid(self): token = self.user.generate_confirmation_token(expires_sec=0.2) sleep(1) self.assertFalse(self.user.verify_token(token)) def test_token_difference(self): user_test = User(username='******', email='*****@*****.**', password='******', image_file='Day view.jpg') db.session.add(user_test) db.session.commit() token1 = self.user.get_reset_token() token2 = user_test.get_reset_token() self.assertNotEqual(token1, token2) def test_token_from_another_user(self): user_test = User(username='******', email='*****@*****.**', password='******', image_file='Day view.jpg') db.session.add(user_test) db.session.commit() token = user_test.get_reset_token() self.assertFalse(self.user.verify_token(token)) def test_follow_unfollow_feature(self): user_test = User(username='******', email='*****@*****.**', password='******', image_file='Day view.jpg') db.session.add(user_test) db.session.commit() self.assertFalse(self.user.is_following(user_test)) self.user.follow(user_test) self.assertTrue(self.user.is_following(user_test)) self.user.unfollow(user_test) self.assertFalse(self.user.is_following(user_test)) user_test.follow(self.user) self.assertTrue(self.user.is_followed_by(user_test)) user_test.unfollow(self.user) self.assertFalse(self.user.is_followed_by(user_test)) @patch('blog.models.User.to_json') def test_json(self, mock_obj): mock_obj.return_value = { 'url': '/api/v1/users/', 'username': '******', 'posts_url': None } expected_keys = ['url', 'username', 'posts_url'] self.assertEqual(sorted(mock_obj.return_value.keys()), sorted(expected_keys)) self.assertEqual(sorted((self.user.to_json())), sorted(expected_keys))