Ejemplo n.º 1
0
class TestAPI(unittest.TestCase):
    default_username = '******'
    default_password = '******'

    def setUp(self):
        self.app = app
        self.ctx = self.app.app_context()
        self.ctx.push()
        db.drop_all()
        db.create_all()
        u = User(username=self.default_username)
        u.set_password(self.default_password)
        db.session.add(u)
        db.session.commit()
        self.client = TestClient(self.app, u.generate_auth_token(), '')

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.ctx.pop()

    def test_customers(self):
        # get empty list of customers
        rv, json = self.client.get('/customers/')
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['customers'] == [])

        # add the customer
        rv, json = self.client.post('/customers/', data={'name': 'john'})
        self.assertTrue(rv.status_code == 201)
        location = rv.headers['Location']
        rv, json = self.client.get(location)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['name'] == 'john')
        rv, json = self.client.get('/customers/')
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['customers'] == [location])

        # edit the customer
        rv, json = self.client.put(location, data={'name': 'John Smith'})
        self.assertTrue(rv.status_code == 200)
        rv, json = self.client.get(location)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['name'] == 'John Smith')
Ejemplo n.º 2
0
class TestAPI(unittest.TestCase):
    default_username = '******'
    default_password = '******'

    def setUp(self):
        self.app = app
        self.ctx = self.app.app_context()
        self.ctx.push()
        db.drop_all()
        db.create_all()
        u = User(username=self.default_username)
        u.set_password(self.default_password)
        db.session.add(u)
        db.session.commit()
        self.client = TestClient(self.app, u.generate_auth_token(), '')

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.ctx.pop()

    def test_customers(self):
        # get list of customers
        rv, json = self.client.get('/customers/')
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['customers'] == [])

        # add a customer
        rv, json = self.client.post('/customers/', data={'name': 'john'})
        self.assertTrue(rv.status_code == 201)
        location = rv.headers['Location']
        rv, json = self.client.get(location)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['name'] == 'john')
        rv, json = self.client.get('/customers/')
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['customers'] == [location])

        # edit the customer
        rv, json = self.client.put(location, data={'name': 'John Smith'})
        self.assertTrue(rv.status_code == 200)
        rv, json = self.client.get(location)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['name'] == 'John Smith')
Ejemplo n.º 3
0
class TestProposalsAPI(unittest.TestCase):
    default_username = '******'
    default_password = '******'

    def setUp(self):
        self.app = app
        self.ctx = self.app.app_context()
        self.ctx.push()
        db.drop_all()
        db.create_all()

        # Creating the 1st user. This user will issue a request
        user = User(username=self.default_username)
        user.set_password_hash(self.default_password)
        db.session.add(user)
        db.session.commit()
        self.client = TestClient(self.app, user.generate_auth_token(), '')

        # Create a request for 1st user
        request_data = {
            'meal_type': 'Vietnamese',
            'meal_time': 'Dinner',
            'location_string': 'San Francisco'
        }
        rv, json = self.client.post(API_VERSION + '/requests/',
                                    data=request_data)
        self.request_location = rv.headers['Location']

        # Create the 2nd user. This user will make proposal for the request by
        # 1st user
        user_2 = User(username='******')
        user_2.set_password_hash('123456')
        db.session.add(user_2)
        db.session.commit()
        self.client = TestClient(self.app, user_2.generate_auth_token(), '')

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.ctx.pop()

    def test_misc(self):
        root_endpoint = API_VERSION + '/'
        rv, json = self.client.get(root_endpoint)
        self.assertTrue(rv.status_code == 200)

    def test_proposals(self):
        proposals_endpoint = API_VERSION + '/proposals/'
        rv, json = self.client.get(proposals_endpoint)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(len(json['proposals']) == 0)

        # Add new proposal
        rv, json = self.client.get(self.request_location)
        self.assertTrue(rv.status_code == 200)

        proposals_url = json['proposals_url']
        rv, json = self.client.post(proposals_url, data={'a': 'b'})
        self.assertTrue(rv.status_code == 201)

        proposal_location = rv.headers['Location']
        rv, json = self.client.get(proposal_location)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['request_url'] == self.request_location)
        self.assertTrue(json['self_url'] == proposal_location)
        self.assertTrue(json['user_url'].split('/')[-1] == str(g.user.id))

        rv, json = self.client.get(proposals_url)
        self.assertTrue(rv.status_code == 200)
        self.assertIn(proposal_location, json['proposals'])

        # Update proposal
        rv, json = self.client.put(proposal_location, data={'accepted': True})
        self.assertTrue(rv.status_code == 200)

        rv, json = self.client.get(proposal_location)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['accepted'] == True)

        # Delete proposal
        rv, json = self.client.delete(proposal_location)
        self.assertTrue(rv.status_code == 200)

        with self.assertRaises(NotFound):
            rv, json = self.client.get(proposal_location)
Ejemplo n.º 4
0
class APITestCase(unittest.TestCase):
    default_username = '******'
    default_password = '******'

    def setUp(self):
        self.app = create_app('testing')
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()
        u = User(username=self.default_username,
                 password=self.default_password)
        db.session.add(u)
        db.session.commit()

        self.client = TestClient(self.app, u.generate_auth_token(), '')

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.app_context.pop()

    def test_404(self):
        response, json_response = self.client.get('/wrong/url')
        self.assertTrue(response.status_code == 404)
        self.assertTrue(json_response['error'] == 'not found')

    def test_task(self):
        test_task = {'title': 'Smash Patriarchy'}

        #create task
        response, json_response = self.client.post(url_for('api.create_task'),
                                                   data=test_task)
        self.assertEquals(response.status_code, 201)
        self.assertTrue(json_response['title'], test_task['title'])
        inputted_id = json_response['id']

        #get task
        response, json_response = self.client.get(
            url_for('api.get_task', task_id=inputted_id, _external=True))
        self.assertTrue(json_response['title'], test_task['title'])

        #update task
        response, json_response = self.client.put(url_for('api.update_task',
                                                          task_id=inputted_id),
                                                  data={'done': True})
        self.assertEquals(response.status_code, 201)
        self.assertTrue(json_response['done'], True)

        #delete task
        response, json_response = self.client.delete(
            url_for('api.delete_task', task_id=inputted_id))
        self.assertEquals(response.status_code, 201)

    def test_user(self):
        test_user = {'username': '******'}

        #create user
        response, json_response = self.client.post(url_for('api.create_user'),
                                                   data=test_user)
        self.assertEquals(response.status_code, 201)
        self.assertTrue(json_response['username'], test_user['username'])
        inputted_id = json_response['id']

        #get user
        respose, json_response = self.client.get(
            url_for('api.get_user', user_id=inputted_id, _external=True))
        self.assertTrue(json_response['username'], test_user['username'])

        #delete user
        response, json_response = self.client.delete(
            url_for('api.delete_user', user_id=inputted_id))
        self.assertEquals(response.status_code, 201)

    def test_relations(self):
        user2_json = {'username': '******', 'password': '******'}

        user2 = User.from_json(user2_json)
        db.session.add(user2)
        db.session.commit()

        user2_tasks = [{
            'title': 'destroy the state'
        }, {
            'title': 'form cooperatives'
        }]
        user1_tasks = [{
            'title': 'form a proletarian dictatorship'
        }, {
            'title': 'implement the 5 year plan'
        }, {
            'title': 'decide on a successor'
        }]

        # we put the tasks 'manually' in order to not login in and out of user1
        for task_json in user2_tasks:
            task = Task.from_json(task_json)
            task.user_id = user2.id
            db.session.add(task)
        db.session.commit()

        # create task of user1
        for task in user1_tasks:
            response, json_response = self.client.post(
                url_for('api.create_task'), data=task)

        # get tasks of user1

        response, json_response = self.client.get(url_for('api.get_tasks'))
        self.assertEquals(response.status_code, 200)

        # check if only the task of the user is given
        self.assertEquals(len(json_response['tasks']), 3)

        response_titles = [task['title'] for task in json_response['tasks']]
        input_titles = [task['title'] for task in user1_tasks]

        self.assertEquals(set(input_titles), set(response_titles))

        # check if access to the tasks of user2 allowed
        user2_task = user2.tasks.first()

        response, json_response = self.client.get(
            url_for('api.get_task', task_id=user2_task.id))
        self.assertEquals(response.status_code, 403)
Ejemplo n.º 5
0
class TestAPI(unittest.TestCase):
    default_username = '******'
    default_password = '******'

    def setUp(self):
        self.app = app
        self.ctx = self.app.app_context()
        self.ctx.push()
        db.drop_all()
        db.create_all()
        u = User(username=self.default_username)
        u.set_password(self.default_password)
        db.session.add(u)
        db.session.commit()
        self.client = TestClient(self.app, u.generate_auth_token(), '')

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.ctx.pop()

    def test_customers(self):
        # get list of customers
        rv, json = self.client.get('/customers/')
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['customers'] == [])

        # add a customer
        rv, json = self.client.post('/customers/', data={'name': 'john'})
        self.assertTrue(rv.status_code == 201)
        location = rv.headers['Location']
        rv, json = self.client.get(location)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['name'] == 'john')
        rv, json = self.client.get('/customers/')
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['customers'] == [location])

        # edit the customer
        rv, json = self.client.put(location, data={'name': 'John Smith'})
        self.assertTrue(rv.status_code == 200)
        rv, json = self.client.get(location)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['name'] == 'John Smith')

    def test_products(self):
        # get list of products
        rv, json = self.client.get('/products/')
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['products'] == [])

        # add a customer
        rv, json = self.client.post('/products/', data={'name': 'prod1'})
        self.assertTrue(rv.status_code == 201)
        location = rv.headers['Location']
        rv, json = self.client.get(location)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['name'] == 'prod1')
        rv, json = self.client.get('/products/')
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['products'] == [location])

        # edit the customer
        rv, json = self.client.put(location, data={'name': 'product1'})
        self.assertTrue(rv.status_code == 200)
        rv, json = self.client.get(location)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['name'] == 'product1')

    def test_orders_and_items(self):
        # define a customer
        rv, json = self.client.post('/customers/', data={'name': 'john'})
        self.assertTrue(rv.status_code == 201)
        customer = rv.headers['Location']
        rv, json = self.client.get(customer)
        orders_url = json['orders_url']
        rv, json = self.client.get(orders_url)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['orders'] == [])

        # define two products
        rv, json = self.client.post('/products/', data={'name': 'prod1'})
        self.assertTrue(rv.status_code == 201)
        prod1 = rv.headers['Location']
        rv, json = self.client.post('/products/', data={'name': 'prod2'})
        self.assertTrue(rv.status_code == 201)
        prod2 = rv.headers['Location']

        # create an order
        rv, json = self.client.post(orders_url,
                                    data={'date': '2014-01-01T00:00:00Z'})
        self.assertTrue(rv.status_code == 201)
        order = rv.headers['Location']
        rv, json = self.client.get(order)
        items_url = json['items_url']
        rv, json = self.client.get(items_url)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['items'] == [])
        rv, json = self.client.get('/orders/')
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(len(json['orders']) == 1)
        self.assertTrue(order in json['orders'])

        # edit the order
        rv, json = self.client.put(order,
                                   data={'date': '2014-02-02T00:00:00Z'})
        self.assertTrue(rv.status_code == 200)
        rv, json = self.client.get(order)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['date'] == '2014-02-02T00:00:00Z')

        # add two items to order
        rv, json = self.client.post(items_url,
                                    data={
                                        'product_url': prod1,
                                        'quantity': 2
                                    })
        self.assertTrue(rv.status_code == 201)
        item1 = rv.headers['Location']
        rv, json = self.client.post(items_url,
                                    data={
                                        'product_url': prod2,
                                        'quantity': 1
                                    })
        self.assertTrue(rv.status_code == 201)
        item2 = rv.headers['Location']
        rv, json = self.client.get(items_url)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(len(json['items']) == 2)
        self.assertTrue(item1 in json['items'])
        self.assertTrue(item2 in json['items'])
        rv, json = self.client.get(item1)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['product_url'] == prod1)
        self.assertTrue(json['quantity'] == 2)
        self.assertTrue(json['order_url'] == order)
        rv, json = self.client.get(item2)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['product_url'] == prod2)
        self.assertTrue(json['quantity'] == 1)
        self.assertTrue(json['order_url'] == order)

        # edit the second item
        rv, json = self.client.put(item2,
                                   data={
                                       'product_url': prod2,
                                       'quantity': 3
                                   })
        self.assertTrue(rv.status_code == 200)
        rv, json = self.client.get(item2)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['product_url'] == prod2)
        self.assertTrue(json['quantity'] == 3)
        self.assertTrue(json['order_url'] == order)

        # delete first item
        rv, json = self.client.delete(item1)
        self.assertTrue(rv.status_code == 200)
        rv, json = self.client.get(items_url)
        self.assertFalse(item1 in json['items'])
        self.assertTrue(item2 in json['items'])

        # delete order
        rv, json = self.client.delete(order)
        self.assertTrue(rv.status_code == 200)
        with self.assertRaises(NotFound):
            rv, json = self.client.get(item2)
        rv, json = self.client.get('/orders/')
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(len(json['orders']) == 0)
Ejemplo n.º 6
0
class TestUsersAPI(unittest.TestCase):
    default_username = '******'
    default_password = '******'

    def setUp(self):
        self.app = app
        self.ctx = self.app.app_context()
        self.ctx.push()
        db.drop_all()
        db.create_all()
        user = User(username=self.default_username)
        user.set_password_hash(self.default_password)
        db.session.add(user)
        db.session.commit()
        self.client = TestClient(self.app, user.generate_auth_token(), '')

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.ctx.pop()

    def test_misc(self):
        root_endpoint = API_VERSION + '/'
        rv, json = self.client.get(root_endpoint)
        self.assertTrue(rv.status_code == 200)

    def test_users(self):
        # Get list of all users
        users_endpoint = API_VERSION + '/users/'
        rv, json = self.client.get(users_endpoint)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(len(json['users']) == 1)

        # Add a new user
        user_data = {
            'username': '******',
            'password': '******',
            'email': '*****@*****.**',
            'photo_url': 'http://www.gotitapp.co/user.jpg'
        }

        rv, json = self.client.post(users_endpoint, data=user_data)
        self.assertTrue(rv.status_code == 201)
        location = rv.headers['Location']
        rv, json = self.client.get(location)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['username'] == user_data['username'])
        self.assertTrue(json['email'] == user_data['email'])
        self.assertTrue(json['photo_url'] == user_data['photo_url'])

        # Add a duplicated user
        duplicated_user_data = {
            'username': '******',
            'password': '******',
            'email': '*****@*****.**',
            'photo_url': 'http://www.gotitapp.co/user_2.jpg'
        }
        with self.assertRaises(ValidationError) as context:
            rv, json = self.client.post(users_endpoint,
                                        data=duplicated_user_data)
            self.assertTrue('Username already existed!' in context.exception)

        # Edit a user
        updated_user_data = {
            'username': '******',
            'email': '*****@*****.**',
            'photo_url': 'http://www.gotitapp.co/user2.jpg'
        }

        rv, json = self.client.put(location, data=updated_user_data)
        self.assertTrue(rv.status_code == 200)
        rv, json = self.client.get(location)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['username'] == updated_user_data['username'])
        self.assertTrue(json['email'] == updated_user_data['email'])
        self.assertTrue(json['photo_url'] == updated_user_data['photo_url'])

        # Delete a user
        rv, json = self.client.delete(location)
        self.assertTrue(rv.status_code == 200)

        with self.assertRaises(NotFound) as context:
            rv, json = self.client.get(location)
            self.assertTrue('Not Found' in context.exception)
Ejemplo n.º 7
0
class TestDatesAPI(unittest.TestCase):
    def setUp(self):
        self.app = app
        self.ctx = self.app.app_context()
        self.ctx.push()
        db.drop_all()
        db.create_all()

        # Creating the 1st user. This user will issue a request
        requester = User(username='******')
        requester.set_password_hash('password_1')
        db.session.add(requester)
        db.session.commit()
        self.requester = requester
        self.client_1 = TestClient(self.app, requester.generate_auth_token(),
                                   '')

        # Create a request for 1st user
        request_data = {
            'meal_type': 'Vietnamese',
            'meal_time': 'Dinner',
            'location_string': 'San Francisco'
        }
        rv, json = self.client_1.post(API_VERSION + '/requests/',
                                      data=request_data)
        self.request_location = rv.headers['Location']

        # Create the 2nd user. This user will make proposal for the request by
        # 1st user
        proposer = User(username='******')
        proposer.set_password_hash('password_1')
        db.session.add(proposer)
        db.session.commit()
        self.client_2 = TestClient(self.app, proposer.generate_auth_token(),
                                   '')

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.ctx.pop()

    def test_misc(self):
        root_endpoint = API_VERSION + '/'
        rv, json = self.client_1.get(root_endpoint)
        self.assertTrue(rv.status_code == 200)

    def test_dates(self):
        proposals_endpoint = API_VERSION + '/dates/'
        rv, json = self.client_1.get(proposals_endpoint)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(len(json['dates']) == 0)

        # Proposer add a proposal for the request by Requester
        rv, json = self.client_2.get(self.request_location)
        self.assertTrue(rv.status_code == 200)

        proposals_url = json['proposals_url']
        rv, json = self.client_2.post(proposals_url, data={'foo': 'bar'})
        self.assertTrue(rv.status_code == 201)
        proposal_location = rv.headers['Location']
        rv, json = self.client_2.get(proposal_location)
        date_url = json['date_url']
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['request_url'] == self.request_location)
        self.assertTrue(json['self_url'] == proposal_location)
        self.assertTrue(json['user_url'].split('/')[-1] == str(g.user.id))

        # Requester accept a proposal
        rv, json = self.client_1.post(date_url, data={'foo': 'bar'})
        self.assertTrue(rv.status_code == 201)
        date_location = rv.headers['Location']
        rv, json = self.client_1.get(date_location)
        self.assertTrue(rv.status_code == 200)
        rv, json = self.client_2.get(date_location)
        self.assertTrue(rv.status_code == 200)

        # Requester update a date
        rv, json = self.client_1.put(date_location,
                                     data={'restaurant_name': 'Japanese'})
        self.assertTrue(rv.status_code == 200)
        rv, json = self.client_1.get(date_location)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['restaurant_name'] == 'Japanese')

        # Requester delete a date
        rv, json = self.client_1.delete(date_location)
        self.assertTrue(rv.status_code == 200)
        with self.assertRaises(NotFound):
            rv, json = self.client_1.get(date_location)