def test_add_user_duplicate_email(self): add_user('justatest', '*****@*****.**', 'greaterthaneight') duplicate_user = User(username='******', email='*****@*****.**', password='******') db.session.add(duplicate_user) self.assertRaises(IntegrityError, db.session.commit)
def test_main_with_users(self): """Ensure the main route behaves correctly when users have been added to the database.""" add_user('michael', '*****@*****.**', 'greaterthaneight') add_user('fletcher', '*****@*****.**', 'greaterthaneight') with self.client: response = self.client.get('/') self.assertEqual(response.status_code, 200) self.assertIn(b'<h1>All Users</h1>', response.data) self.assertNotIn(b'<p>No users!</p>', response.data) self.assertIn(b'michael', response.data) self.assertIn(b'fletcher', response.data)
def test_all_users(self): """Ensure get all users behaves correctly.""" add_user('michael', '*****@*****.**', 'greaterthaneight') add_user('fletcher', '*****@*****.**', 'greaterthaneight') with self.client: response = self.client.get(f'/users') data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertEqual(len(data['data']['users']), 2) self.assertIn('michael', data['data']['users'][0]['username']) self.assertIn('*****@*****.**', data['data']['users'][0]['email']) self.assertIn('fletcher', data['data']['users'][1]['username']) self.assertIn('*****@*****.**', data['data']['users'][1]['email']) self.assertIn('success', data['status'])
def test_add_user(self): user = add_user('justatest', '*****@*****.**', 'greaterthaneight') self.assertTrue(user.id) self.assertEqual(user.username, 'justatest') self.assertEqual(user.email, '*****@*****.**') self.assertTrue(user.active)
def test_single_user(self): """Ensure get single user behaves correctly.""" user = add_user('michael', '*****@*****.**', 'greaterthaneight') with self.client: response = self.client.get(f'/users/{user.id}') data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertIn('michael', data['data']['username']) self.assertIn('*****@*****.**', data['data']['email']) self.assertIn('success', data['status'])
def test_decode_auth_token(self): user = add_user('justatest', '*****@*****.**', 'test') auth_token = User.encode_auth_token(user.id) self.assertEqual(User.decode_auth_token(auth_token), user.id)
def test_encode_auth_token(self): user = add_user('justatest', '*****@*****.**', 'test') auth_token = User.encode_auth_token(user.id) self.assertTrue(isinstance(auth_token, bytes))
def test_passwords_are_random(self): user_one = add_user('justatest', '*****@*****.**', 'greaterthaneight') user_two = add_user('justatest2', '*****@*****.**', 'greaterthaneight') self.assertNotEqual(user_one.password, user_two.password)
def test_to_json(self): user = add_user('justatest', '*****@*****.**', 'greaterthaneight') self.assertTrue(isinstance(user.to_json(), dict))