def test_post_users_empty(self): """ Verify adding an empty user throws an error. """ admin = add_admin() with self.client: token = get_jwt(self.client, admin.email) response = self.client.post( '/users', data=json.dumps({}), content_type='application/json', headers={'Authorization': 'Bearer ' + token}) data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'error') self.assertEqual(data['message'], 'Invalid payload.') self.assertEqual(response.content_type, 'application/json') self.assert400(response)
def test_post_users(self): """ Verify POST request to /users adds a new user to the database. """ admin = add_admin() with self.client: token = get_jwt(self.client, admin.email) response = self.client.post( '/users', data=json.dumps({ 'username': USERNAME, 'email': EMAIL, 'password': PASSWORD }), content_type='application/json', headers={'Authorization': 'Bearer ' + token}) data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'success') self.assertEqual(data['message'], '{email} was added!'.format(email=EMAIL)) self.assertEqual(response.content_type, 'application/json') self.assertEqual(response.status_code, 201)
def test_post_users_duplicate_email(self): """ Verify adding a user with a duplicate email throws an error. """ admin = add_admin() user = add_user(USERNAME, EMAIL, PASSWORD) with self.client: token = get_jwt(self.client, admin.email) response = self.client.post( '/users', data=json.dumps({ 'username': USERNAME2, 'email': user.email, 'password': PASSWORD }), content_type='application/json', headers={'Authorization': 'Bearer ' + token}) data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'error') self.assertEqual(data['message'], 'User already exists.') self.assertEqual(response.content_type, 'application/json') self.assert400(response)