def test_add_user_duplicate_email(self):
     """Ensure error is thrown if the email already exists."""
     token = add_user_login(self.client, 'test', '*****@*****.**', 'test')
     update_admin(email='*****@*****.**')
     with self.client:
         self.client.post('/users',
                          data=json.dumps({
                              'username': '******',
                              'email': '*****@*****.**',
                              'password': '******'
                          }),
                          content_type='application/json',
                          headers={'Authorization': f'Bearer {token}'})
         response = self.client.post(
             '/users',
             data=json.dumps({
                 'username': '******',
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token}'})
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn('Sorry. That email already exists.', data['message'])
         self.assertIn('fail', data['status'])
 def test_add_user_invalid_json(self):
     "Ensure error is thrown if the JSON object is empty"
     token = add_user_login(self.client, 'test', '*****@*****.**', 'test')
     update_admin(email='*****@*****.**')
     with self.client:
         response = self.client.post(
             '/users',
             data=json.dumps({}),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token}'})
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn('Invalid payload.', data['message'])
         self.assertIn('fail', data['status'])
 def test_add_user_invalid_json_keys_no_password(self):
     token = add_user_login(self.client, 'test', '*****@*****.**', 'test')
     update_admin(email='*****@*****.**')
     with self.client:
         response = self.client.post(
             '/users',
             data=json.dumps(
                 dict(username='******',
                      email='*****@*****.**')),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token}'})
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn('Invalid payload', data['message'])
         self.assertIn('fail', data['status'])
 def test_add_users(self):
     token = add_user_login(self.client, 'test', '*****@*****.**', 'test')
     update_admin(email='*****@*****.**')
     with self.client:
         response = self.client.post(
             '/users',
             data=json.dumps({
                 'username': '******',
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token}'})
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 201)
         self.assertIn('[email protected] was added!', data['message'])
         self.assertIn('success', data['status'])
 def test_add_user_invalid_json_keys(self):
     """
     Ensure error is thrown if the JSON object does not have a username key
     """
     token = add_user_login(self.client, 'test', '*****@*****.**', 'test')
     update_admin(email='*****@*****.**')
     with self.client:
         response = self.client.post(
             '/users',
             data=json.dumps({
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token}'})
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn('Invalid payload.', data['message'])
         self.assertIn('fail', data['status'])