<gh_stars>1-10
import json
import datetime

from project import db
from project.api.models import User
from project.tests.utils import add_user
from project.tests.base import BaseTestCase


class TestUserService(BaseTestCase):
    """Test for the User Service """

    def test_users(self):
        """Ensure the /ping route behaves correctly"""
        response = self.client.get('/ping')
        data = json.loads(response.data.decode())
        self.assertEqual(response.status_code, 200)
        self.assertIn('pong!', data['message'])
        self.assertIn('success', data['status'])

    def test_add_user(self):
        """Ensure a new user can be added to the database."""
        add_user('user', '<EMAIL>', '1234')
        user = User.query.filter_by(email='<EMAIL>').first()
        user.admin = True
        db.session.commit()
        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps(dict(
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json'
            )
            response = self.client.post(
                '/users',
                data=json.dumps(dict(
                    username='repodevs',
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_login.data.decode()
                        )['auth_token']
                )
            )
            data = json.loads(response.data)
            self.assertEqual(response.status_code, 201)
            self.assertIn('<EMAIL> was added!', data['message'])
            self.assertIn('success', data['status'])

    def test_add_user_invalid_json(self):
        """Ensure error is thrown if the JSON object is empty."""
        add_user('user', '<EMAIL>', '1234')
        user = User.query.filter_by(email='<EMAIL>').first()
        user.admin = True
        db.session.commit()
        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps(dict(
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json'
            )
            response = self.client.post(
                '/users',
                data=json.dumps(dict()),
                content_type='application/json',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_login.data.decode()
                        )['auth_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(self):
        """Ensure error is thrown if the JSON object doest not hava a username keys."""
        add_user('user', '<EMAIL>', '<PASSWORD>')
        user = User.query.filter_by(email='<EMAIL>').first()
        user.admin = True
        db.session.commit()
        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps(dict(
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json'
            )
            response = self.client.post(
                '/users',
                data=json.dumps(dict(
                        email='<EMAIL>',
                        password='password')),
                content_type='application/json',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_login.data.decode()
                        )['auth_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):
        """Ensure error is thrown if the JSON object doest not hava a password keys."""
        add_user('user', '<EMAIL>', '1234')
        user = User.query.filter_by(email='<EMAIL>').first()
        user.admin = True
        db.session.commit()
        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps(dict(
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json'
            )
            response = self.client.post(
                '/users',
                data=json.dumps(dict(
                        username='repodevs',
                        email='<EMAIL>')),
                content_type='application/json',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_login.data.decode()
                        )['auth_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_duplicate_email(self):
        """Ensure error is thrown if the email already exists."""
        add_user('user', '<EMAIL>', '1234')
        user = User.query.filter_by(email='<EMAIL>').first()
        user.admin = True
        db.session.commit()
        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps(dict(
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json'
            )
            self.client.post(
                '/users',
                data=json.dumps(dict(
                    username='repodevs',
                    email='<EMAIL>',
                    password='password'
                )),
                content_type='application/json',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_login.data.decode()
                        )['auth_token']
                )
            )
            response = self.client.post(
                '/users',
                data=json.dumps(dict(
                    username='repodevs',
                    email='<EMAIL>',
                    password='password'
                )),
                content_type='application/json',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_login.data.decode()
                        )['auth_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_singe_user(self):
        """Ensure get single user behaves correctly."""
        user = add_user(
                username='repodevs',
                email='<EMAIL>',
                password='password'
            )
        with self.client:
            response = self.client.get(f'/users/{user.id}')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 200)
            self.assertTrue('created_at' in  data['data'])
            self.assertIn('repodevs', data['data']['username'])
            self.assertIn('<EMAIL>', data['data']['email'])
            self.assertIn('success', data['status'])


    def test_single_user_no_id(self):
        """Ensure error is thrown if an id is not provided."""
        with self.client:
            response = self.client.get('/users/blah')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 404)
            self.assertIn('User does not exist', data['message'])
            self.assertIn('fail', data['status'])


    def test_single_user_incorrect_id(self):
        """Ensure error is thrown if the id does not exist."""
        with self.client:
            response = self.client.get('/users/999')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 404)
            self.assertIn('User does not exist', data['message'])
            self.assertIn('fail', data['status'])


    def test_all_users(self):
        """Ensure get all users behaves correctly."""
        created = datetime.datetime.utcnow() + datetime.timedelta(-30)
        add_user('edi', '<EMAIL>', 'password', created)
        add_user('santoso', '<EMAIL>', 'password')
        with self.client:
            response = self.client.get('/users')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 200)
            self.assertEqual(len(data['data']['users']), 2)
            self.assertTrue('created_at' in  data['data']['users'][0])
            self.assertTrue('created_at' in  data['data']['users'][1])
            self.assertIn('edi', data['data']['users'][1]['username'])
            self.assertIn('<EMAIL>', data['data']['users'][1]['email'])
            self.assertIn('santoso', data['data']['users'][0]['username'])
            self.assertIn('<EMAIL>', data['data']['users'][0]['email'])
            self.assertIn('success', data['status'])

    def test_add_user_not_admin(self):
        add_user('user', '<EMAIL>', '1234')
        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps(dict(
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json'
            )
            response = self.client.post(
                '/users',
                data=json.dumps(dict(
                    username='repodevs',
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_login.data.decode()
                        )['auth_token']
                )
            )
            data = json.loads(response.data)
            self.assertTrue(data['status'] == 'error')
            self.assertTrue(
                data['message'] == 'You do not have permission to do that.')
            self.assertEqual(response.status_code, 401)

import json
import datetime

from project import db
from project.api.models import User
from project.tests.utils import add_user
from project.tests.base import BaseTestCase


class TestUserService(BaseTestCase):
    """Test for the User Service """

    def test_users(self):
        """Ensure the /ping route behaves correctly"""
        response = self.client.get('/ping')
        data = json.loads(response.data.decode())
        self.assertEqual(response.status_code, 200)
        self.assertIn('pong!', data['message'])
        self.assertIn('success', data['status'])

    def test_add_user(self):
        """Ensure a new user can be added to the database."""
        add_user('user', '<EMAIL>', '1234')
        user = User.query.filter_by(email='<EMAIL>').first()
        user.admin = True
        db.session.commit()
        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps(dict(
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json'
            )
            response = self.client.post(
                '/users',
                data=json.dumps(dict(
                    username='repodevs',
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_login.data.decode()
                        )['auth_token']
                )
            )
            data = json.loads(response.data)
            self.assertEqual(response.status_code, 201)
            self.assertIn('<EMAIL> was added!', data['message'])
            self.assertIn('success', data['status'])

    def test_add_user_invalid_json(self):
        """Ensure error is thrown if the JSON object is empty."""
        add_user('user', '<EMAIL>', '1234')
        user = User.query.filter_by(email='<EMAIL>').first()
        user.admin = True
        db.session.commit()
        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps(dict(
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json'
            )
            response = self.client.post(
                '/users',
                data=json.dumps(dict()),
                content_type='application/json',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_login.data.decode()
                        )['auth_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(self):
        """Ensure error is thrown if the JSON object doest not hava a username keys."""
        add_user('user', '<EMAIL>', '<PASSWORD>')
        user = User.query.filter_by(email='<EMAIL>').first()
        user.admin = True
        db.session.commit()
        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps(dict(
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json'
            )
            response = self.client.post(
                '/users',
                data=json.dumps(dict(
                        email='<EMAIL>',
                        password='password')),
                content_type='application/json',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_login.data.decode()
                        )['auth_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):
        """Ensure error is thrown if the JSON object doest not hava a password keys."""
        add_user('user', '<EMAIL>', '1234')
        user = User.query.filter_by(email='<EMAIL>').first()
        user.admin = True
        db.session.commit()
        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps(dict(
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json'
            )
            response = self.client.post(
                '/users',
                data=json.dumps(dict(
                        username='repodevs',
                        email='<EMAIL>')),
                content_type='application/json',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_login.data.decode()
                        )['auth_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_duplicate_email(self):
        """Ensure error is thrown if the email already exists."""
        add_user('user', '<EMAIL>', '1234')
        user = User.query.filter_by(email='<EMAIL>').first()
        user.admin = True
        db.session.commit()
        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps(dict(
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json'
            )
            self.client.post(
                '/users',
                data=json.dumps(dict(
                    username='repodevs',
                    email='<EMAIL>',
                    password='password'
                )),
                content_type='application/json',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_login.data.decode()
                        )['auth_token']
                )
            )
            response = self.client.post(
                '/users',
                data=json.dumps(dict(
                    username='repodevs',
                    email='<EMAIL>',
                    password='password'
                )),
                content_type='application/json',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_login.data.decode()
                        )['auth_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_singe_user(self):
        """Ensure get single user behaves correctly."""
        user = add_user(
                username='repodevs',
                email='<EMAIL>',
                password='password'
            )
        with self.client:
            response = self.client.get(f'/users/{user.id}')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 200)
            self.assertTrue('created_at' in  data['data'])
            self.assertIn('repodevs', data['data']['username'])
            self.assertIn('<EMAIL>', data['data']['email'])
            self.assertIn('success', data['status'])


    def test_single_user_no_id(self):
        """Ensure error is thrown if an id is not provided."""
        with self.client:
            response = self.client.get('/users/blah')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 404)
            self.assertIn('User does not exist', data['message'])
            self.assertIn('fail', data['status'])


    def test_single_user_incorrect_id(self):
        """Ensure error is thrown if the id does not exist."""
        with self.client:
            response = self.client.get('/users/999')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 404)
            self.assertIn('User does not exist', data['message'])
            self.assertIn('fail', data['status'])


    def test_all_users(self):
        """Ensure get all users behaves correctly."""
        created = datetime.datetime.utcnow() + datetime.timedelta(-30)
        add_user('edi', '<EMAIL>', 'password', created)
        add_user('santoso', '<EMAIL>', 'password')
        with self.client:
            response = self.client.get('/users')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 200)
            self.assertEqual(len(data['data']['users']), 2)
            self.assertTrue('created_at' in  data['data']['users'][0])
            self.assertTrue('created_at' in  data['data']['users'][1])
            self.assertIn('edi', data['data']['users'][1]['username'])
            self.assertIn('<EMAIL>', data['data']['users'][1]['email'])
            self.assertIn('santoso', data['data']['users'][0]['username'])
            self.assertIn('<EMAIL>', data['data']['users'][0]['email'])
            self.assertIn('success', data['status'])

    def test_add_user_not_admin(self):
        add_user('user', '<EMAIL>', '1234')
        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps(dict(
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json'
            )
            response = self.client.post(
                '/users',
                data=json.dumps(dict(
                    username='repodevs',
                    email='<EMAIL>',
                    password='<PASSWORD>'
                )),
                content_type='application/json',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_login.data.decode()
                        )['auth_token']
                )
            )
            data = json.loads(response.data)
            self.assertTrue(data['status'] == 'error')
            self.assertTrue(
                data['message'] == 'You do not have permission to do that.')
            self.assertEqual(response.status_code, 401)
Test for the User Service Ensure the /ping route behaves correctly Ensure a new user can be added to the database. Ensure error is thrown if the JSON object is empty. Ensure error is thrown if the JSON object doest not hava a username keys. Ensure error is thrown if the JSON object doest not hava a password keys. Ensure error is thrown if the email already exists. Ensure get single user behaves correctly. Ensure error is thrown if an id is not provided. Ensure error is thrown if the id does not exist. Ensure get all users behaves correctly.