Пример #1
0
class TransportTestCase(unittest.TestCase):
    def setUp(self):
        os.environ['espa_api_testing'] = 'True'
        # create a user
        self.mock_user = MockUser()
        self.mock_order = MockOrder()
        self.user = User.find(self.mock_user.add_testing_user())
        self.order_id = self.mock_order.generate_testing_order(self.user.id)

        self.app = http.app.test_client()
        self.app.testing = True

        self.sceneids = self.mock_order.scene_names_list(self.order_id)[0:2]

        token = ''.format(self.user.username, 'foo')
        auth_string = "Basic {}".format(base64.b64encode(token))
        self.headers = {"Authorization": auth_string}

        with db_instance() as db:
            uidsql = "select user_id, orderid from ordering_order limit 1;"
            db.select(uidsql)
            self.userid = db[0]['user_id']
            self.orderid = db[0]['orderid']

            itemsql = "select name, order_id from ordering_scene limit 1;"
            db.select(itemsql)
            self.itemid = db[0][0]
            itemorderid = db[0][1]

            ordersql = "select orderid from ordering_order where id = {};".format(
                itemorderid)
            db.select(ordersql)
            self.itemorderid = db[0][0]

        self.base_order = lowercase_all(testorders.build_base_order())

    def tearDown(self):
        # clean up orders
        self.mock_order.tear_down_testing_orders()
        # clean up users
        self.mock_user.cleanup()
        os.environ['espa_api_testing'] = ''

    def test_get_api_response_type(self):
        response = self.app.get('/api',
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        assert response.content_type == 'application/json'

    def test_get_api_response_content(self):
        response = self.app.get('/api',
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertEqual(set(['1', '0']), set(resp_json.keys()))

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_api_info_response_content(self):
        response = self.app.get('/api/v1',
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert "Version 1" in resp_json['description']

    @patch('api.domain.user.User.get', MockUser.get)
    @patch(
        'api.providers.ordering.ordering_provider.OrderingProvider.available_products',
        mock_api.available_products)
    def test_get_available_prods(self):
        url = '/api/v1/available-products/' + ",".join(self.sceneids)
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert "etm" in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    @patch(
        'api.providers.ordering.ordering_provider.OrderingProvider.available_products',
        mock_api.available_products)
    def test_post_available_prods(self):
        url = '/api/v1/available-products'
        data_dict = {'inputs': self.sceneids}
        response = self.app.post(url,
                                 data=json.dumps(data_dict),
                                 headers=self.headers,
                                 environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert "etm" in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    @patch(
        'api.providers.ordering.ordering_provider.OrderingProvider.fetch_user_orders',
        mock_ordering_provider.fetch_user_orders)
    def test_get_available_orders_user(self):
        url = "/api/v1/list-orders"
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert resp_json.keys() == ['orders']

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_available_orders_email(self):
        # email param comes in as unicode
        url = "/api/v1/list-orders/" + str(self.user.email)
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert resp_json.keys() == ['orders']

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_order_by_ordernum(self):
        url = "/api/v1/order/" + str(self.orderid)
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'orderid' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_order_status_by_ordernum(self):
        url = "/api/v1/order-status/" + str(self.orderid)
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'orderid' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_item_status_by_ordernum(self):
        url = "/api/v1/item-status/%s" % self.itemorderid
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'orderid' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_item_status_by_ordernum_itemnum(self):
        url = "/api/v1/item-status/%s/%s" % (self.itemorderid, self.itemid)
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'orderid' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_current_user(self):
        url = "/api/v1/user"
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'username' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_projections(self):
        url = '/api/v1/projections'
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'aea' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_formats(self):
        url = '/api/v1/formats'
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'formats' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_resampling(self):
        url = '/api/v1/resampling-methods'
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'resampling_methods' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_order_schema(self):
        url = '/api/v1/order-schema'
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'properties' in resp_json.keys()

    # Waiting for DB mock-ups to be finished
    def test_post_order(self):
        pass
Пример #2
0
class ProductionTransportTestCase(unittest.TestCase):
    def setUp(self):
        os.environ['espa_api_testing'] = 'True'
        # create a user
        self.mock_user = MockUser()
        self.mock_order = MockOrder()
        self.user = User.find(self.mock_user.add_testing_user())
        self.order_id = self.mock_order.generate_testing_order(self.user.id)

        self.app = http.app.test_client()
        self.app.testing = True

        self.sceneids = self.mock_order.scene_names_list(self.order_id)[0:2]

        with db_instance() as db:
            uidsql = "select user_id, orderid from ordering_order limit 1;"
            db.select(uidsql)
            self.userid = db[0]['user_id']
            self.orderid = db[0]['orderid']

            itemsql = "select name, order_id from ordering_scene limit 1;"
            db.select(itemsql)
            self.itemid = db[0][0]
            itemorderid = db[0][1]

            ordersql = "select orderid from ordering_order where id = {};".format(
                itemorderid)
            db.select(ordersql)
            self.itemorderid = db[0][0]

        self.base_order = lowercase_all(testorders.build_base_order())

    def tearDown(self):
        # clean up orders
        self.mock_order.tear_down_testing_orders()
        # clean up users
        self.mock_user.cleanup()
        os.environ['espa_api_testing'] = ''

    @patch('api.interfaces.production.version1.API.get_production_whitelist',
           api.get_production_whitelist)
    def test_get_production_api(self):
        response = self.app.get('/production-api',
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response.content_type == 'application/json'
        assert set(response_data.keys()) == set(['0', '1'])

    @patch('api.interfaces.production.version1.API.get_production_whitelist',
           api.get_production_whitelist)
    def test_get_production_api_v1(self):
        response = self.app.get('/production-api/v1',
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert set(response_data.keys()) == set(["operations", "description"])
        assert "ESPA Production" in response_data['description']

    @patch(
        'api.providers.production.production_provider.ProductionProvider.get_products_to_process',
        production_provider.get_products_to_process_inputs)
    @patch('api.interfaces.production.version1.API.get_production_whitelist',
           api.get_production_whitelist)
    def test_get_production_api_products_modis(self):
        url = "/production-api/v1/products?for_user=bilbo&product_types=modis"
        response = self.app.get(url, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        correct_resp = {
            'encode_urls': False,
            'for_user': '******',
            'record_limit': 500,
            'product_types': 'modis',
            'priority': None
        }
        assert response_data == correct_resp

    @patch(
        'api.providers.production.production_provider.ProductionProvider.get_products_to_process',
        production_provider.get_products_to_process_inputs)
    @patch('api.interfaces.production.version1.API.get_production_whitelist',
           api.get_production_whitelist)
    def test_get_production_api_products_landsat(self):
        url = "/production-api/v1/products?for_user=bilbo&product_types=landsat"
        response = self.app.get(url, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        correct_resp = {
            'encode_urls': False,
            'for_user': '******',
            'record_limit': 500,
            'product_types': 'landsat',
            'priority': None
        }
        assert response_data == correct_resp

    @patch('api.interfaces.production.version1.API.get_production_whitelist',
           api.get_production_whitelist)
    @patch(
        'api.providers.production.production_provider.ProductionProvider.update_status',
        production_provider.update_status_inputs)
    def test_post_production_api_update_status(self):
        url = "/production-api/v1/update_status"
        data_dict = {
            'name': 't10000xyz401',
            'orderid': '[email protected]',
            'processing_loc': 'update_status',
            'status': 'updated'
        }
        response = self.app.post(url,
                                 data=json.dumps(data_dict),
                                 environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data == data_dict

    @patch(
        'api.providers.production.production_provider.ProductionProvider.set_product_error',
        production_provider.set_product_error_inputs)
    @patch('api.interfaces.production.version1.API.get_production_whitelist',
           api.get_production_whitelist)
    def test_post_production_api_set_product_error(self):
        url = "/production-api/v1/set_product_error"
        data_dict = {
            'name': 't10000xyz401',
            'orderid': '[email protected]',
            'processing_loc': 'xyz',
            'error': 'oopsie'
        }
        response = self.app.post(url,
                                 data=json.dumps(data_dict),
                                 environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data == data_dict

    @patch(
        'api.providers.production.production_provider.ProductionProvider.set_product_unavailable',
        production_provider.set_product_unavailable_inputs)
    @patch('api.interfaces.production.version1.API.get_production_whitelist',
           api.get_production_whitelist)
    def test_post_production_api_set_product_unavailable(self):
        url = "/production-api/v1/set_product_unavailable"
        data_dict = {
            'name': 't10000xyz401',
            'orderid': '[email protected]',
            'processing_loc': 'xyz',
            'error': 'oopsie',
            'note': 'them notes'
        }
        response = self.app.post(url,
                                 data=json.dumps(data_dict),
                                 environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data == data_dict

    @patch(
        'api.providers.production.production_provider.ProductionProvider.mark_product_complete',
        production_provider.set_mark_product_complete_inputs)
    @patch('api.interfaces.production.version1.API.get_production_whitelist',
           api.get_production_whitelist)
    def test_post_production_api_mark_product_complete(self):
        url = "/production-api/v1/mark_product_complete"
        data_dict = {
            'name': 't10000xyz401',
            'orderid': '[email protected]',
            'processing_loc': 'xyz',
            'completed_file_location': '/tmp',
            'cksum_file_location': '/tmp/txt.txt',
            'log_file_contents': 'details'
        }
        response = self.app.post(url,
                                 data=json.dumps(data_dict),
                                 environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data == data_dict

    @patch(
        'api.providers.production.production_provider.ProductionProvider.handle_orders',
        production_provider.respond_true)
    @patch('api.interfaces.production.version1.API.get_production_whitelist',
           api.get_production_whitelist)
    def test_post_production_api_handle_orders(self):
        url = "/production-api/v1/handle-orders"
        response = self.app.get(url,
                                data=json.dumps({}),
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data is True

    @patch(
        'api.providers.production.production_provider.ProductionProvider.queue_products',
        production_provider.queue_products_inputs)
    @patch('api.interfaces.production.version1.API.get_production_whitelist',
           api.get_production_whitelist)
    def test_post_production_api_queue_products(self):
        url = "/production-api/v1/queue-products"
        data_dict = {
            'order_name_tuple_list': 'order_name_tuple_list',
            'processing_location': 'processing_location',
            'job_name': 'job_name'
        }
        response = self.app.post(url,
                                 data=json.dumps(data_dict),
                                 environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data == data_dict

    @patch('api.interfaces.production.version1.API.get_production_whitelist',
           api.get_production_whitelist)
    def test_get_production_api_configurations(self):
        url = "/production-api/v1/configuration/system_message_title"
        response = self.app.get(url, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data.keys() == ['system_message_title']

    @patch('api.interfaces.admin.version1.API.get_stat_whitelist',
           api.get_stat_whitelist)
    def test_get_production_api_stat_products_complete_24_hrs(self):
        url = "/production-api/v1/statistics/stat_products_complete_24_hrs"
        response = self.app.get(url, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data == 0
Пример #3
0
class TransportTestCase(unittest.TestCase):
    def setUp(self):
        os.environ['espa_api_testing'] = 'True'
        # create a user
        self.mock_user = MockUser()
        self.mock_order = MockOrder()
        self.user = User.find(self.mock_user.add_testing_user())
        self.order_id = self.mock_order.generate_testing_order(self.user.id)

        self.app = http.app.test_client()
        self.app.testing = True

        self.sceneids = self.mock_order.scene_names_list(self.order_id)[0:2]

        token = ':'.join((self.user.username, 'foo'))
        auth_string = "Basic {}".format(base64.b64encode(token))
        self.headers = {"Authorization": auth_string}

        with db_instance() as db:
            uidsql = "select user_id, orderid from ordering_order limit 1;"
            db.select(uidsql)
            self.userid = db[0]['user_id']
            self.orderid = db[0]['orderid']

            itemsql = "select name, order_id from ordering_scene limit 1;"
            db.select(itemsql)
            self.itemid = db[0][0]
            itemorderid = db[0][1]

            ordersql = "select orderid from ordering_order where id = {};".format(
                itemorderid)
            db.select(ordersql)
            self.itemorderid = db[0][0]

        self.base_order = lowercase_all(testorders.build_base_order())
        self.sensors = [
            k for k in self.base_order.keys()
            if isinstance(self.base_order[k], dict)
            and 'inputs' in self.base_order[k]
        ]
        self.inputs = {s: self.base_order[s]['inputs'] for s in self.sensors}
        self.input_names_all = set([
            i for sublist in [s for k, s in self.inputs.items()]
            for i in sublist
        ])

    def tearDown(self):
        # clean up orders
        self.mock_order.tear_down_testing_orders()
        # clean up users
        self.mock_user.cleanup()
        os.environ['espa_api_testing'] = ''

    def test_get_api_response_type(self):
        response = self.app.get('/api',
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        self.assertEqual(response.content_type, 'application/json')

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_api_response_content(self):
        response = self.app.get('/api',
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertEqual(set(['v1', 'v0']), set(resp_json.keys()))
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_api_info_response_content(self):
        response = self.app.get('/api/v1',
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertIn("Version 1", resp_json['description'])
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    @patch(
        'api.providers.ordering.ordering_provider.OrderingProvider.available_products',
        mock_api.available_products)
    def test_get_available_prods(self):
        url = '/api/v1/available-products/' + ",".join(self.sceneids)
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertIn("etm", resp_json.keys())
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    @patch(
        'api.providers.ordering.ordering_provider.OrderingProvider.available_products',
        mock_api.available_products)
    def test_get_available_prods_json(self):
        url = '/api/v1/available-products'
        data_dict = {'inputs': self.sceneids}
        response = self.app.get(url,
                                data=json.dumps(data_dict),
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertIn('etm', resp_json.keys())
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_available_orders_user(self):
        url = "/api/v1/list-orders"
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertIsInstance(resp_json, list)
        self.assertListEqual(resp_json, [self.orderid])
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_available_orders_email(self):
        # email param comes in as unicode
        url = "/api/v1/list-orders/" + str(self.user.email)
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertIsInstance(resp_json, list)
        self.assertListEqual(resp_json, [self.orderid])
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_order_by_ordernum(self):
        url = "/api/v1/order/" + str(self.orderid)
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        items = {
            'orderid', 'note', 'order_source', 'order_type', 'product_opts',
            'priority', 'completion_date', 'status', 'order_date',
            'product_options'
        }
        self.assertEqual(items, set(resp_json))
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_order_status_by_ordernum(self):
        url = "/api/v1/order-status/" + str(self.orderid)
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertEqual({'orderid', 'status'}, set(resp_json))
        self.assertEqual(self.orderid, resp_json.get('orderid'))
        self.assertEqual('ordered', resp_json.get('status'))
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_item_status_by_ordernum(self):
        url = "/api/v1/item-status/%s" % self.itemorderid
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        all_names = set([s['name'].lower() for s in resp_json[self.orderid]])
        all_names -= {'plot'}
        self.assertEqual(self.input_names_all, all_names)
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_item_status_by_ordernum_itemnum(self):
        url = "/api/v1/item-status/%s/%s" % (self.itemorderid, self.itemid)
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        all_names = set([s['name'].lower() for s in resp_json[self.orderid]])
        self.assertEqual({self.itemid.lower()}, all_names)
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_current_user(self):
        url = "/api/v1/user"
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertIn('username', resp_json.keys())
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_projections(self):
        url = '/api/v1/projections'
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertIn('aea', resp_json.keys())
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_formats(self):
        url = '/api/v1/formats'
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertIn('formats', resp_json.keys())
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_resampling(self):
        url = '/api/v1/resampling-methods'
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertIn('resampling_methods', resp_json.keys())
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_order_schema(self):
        url = '/api/v1/order-schema'
        response = self.app.get(url,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertIn('properties', resp_json.keys())
        self.assertEqual(200, response.status_code)

    @patch('api.domain.user.User.get', MockUser.get)
    def test_bad_method(self):
        url = '/api/v1/available-products/'
        response = self.app.post(url,
                                 headers=self.headers,
                                 environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertEqual(405, response.status_code)
        self.assertIn('messages', resp_json)
        self.assertIn('errors', resp_json['messages'])

    @patch('api.domain.user.User.get', MockUser.get)
    def test_bad_data(self):
        url = '/api/v1/order'
        data = '{"inputs": [}'
        response = self.app.post(url,
                                 data=data,
                                 headers=self.headers,
                                 environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertEqual(400, response.status_code)
        self.assertIn('messages', resp_json)
        self.assertIn('errors', resp_json['messages'])

    @patch('api.domain.user.User.get', MockUser.get)
    def test_bad_validation_inputs(self):
        url = '/api/v1/order'
        data = '{"inputs": []}'
        response = self.app.post(url,
                                 data=data,
                                 headers=self.headers,
                                 environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertEqual(400, response.status_code)
        self.assertIn('messages', resp_json)
        self.assertIn('errors', resp_json['messages'])
        self.assertIn('Schema errors', resp_json['messages']['errors'][0])

    @patch('api.domain.user.User.get', MockUser.get)
    def test_bad_validation_sensor_inputs(self):
        url = '/api/v1/order'
        data = '{"etm7_collection": {"inputs": ["LE07_L1TP_010028_20050420_20160925_01_T1"]}}'
        response = self.app.post(url,
                                 data=data,
                                 headers=self.headers,
                                 environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertEqual(400, response.status_code)
        self.assertIn('messages', resp_json)
        self.assertIn('errors', resp_json['messages'])
        self.assertIn('2 validation errors',
                      resp_json['messages']['errors'][0])

    @patch('api.domain.user.User.get', MockUser.get)
    def test_bad_data_avail_inputs(self):
        url = '/api/v1/available-products/'
        data = '{"bad": []}'
        response = self.app.get(url,
                                data=data,
                                headers=self.headers,
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertEqual(400, response.status_code)
        self.assertIn('messages', resp_json)
        self.assertIn('errors', resp_json['messages'])
        self.assertIn('No input products supplied',
                      resp_json['messages']['errors'][0])

    @patch('api.external.ers.ERSApi._api_post',
           lambda x, y, z: {'errors': True})
    def test_messages_field_acc_denied(self):
        url = '/api/v1/available-products/'
        response = self.app.get(url, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertEqual(401, response.status_code)
        self.assertIn('messages', resp_json)
        self.assertIn('errors', resp_json['messages'])

    @patch('api.domain.user.User.get', MockUser.get)
    def test_not_found(self):
        url = '/api/v1/not-valid'
        response = self.app.get(url, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertEqual(404, response.status_code)
        self.assertIn('messages', resp_json)
        self.assertIn("errors", resp_json['messages'])

    @patch('api.domain.user.User.get', MockUser.get)
    @patch('api.interfaces.ordering.version1.API.place_order',
           MockOrder.place_order)
    def test_post_order(self):
        url = '/api/v1/order'
        data = {'etm7_collection': {'inputs': [''], 'products': ['']}}
        response = self.app.post(url,
                                 headers=self.headers,
                                 data=json.dumps(data),
                                 environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertEqual(201, response.status_code)
        self.assertEqual({'orderid', 'status'}, set(resp_json.keys()))

    @patch('api.domain.user.User.get', MockUser.get)
    @patch('api.interfaces.ordering.version1.API.cancel_order',
           MockOrder.cancel_order)
    def test_cancel_order(self):
        url = '/api/v1/order'
        data = {'orderid': self.orderid, 'status': 'cancelled'}
        response = self.app.put(url,
                                headers=self.headers,
                                data=json.dumps(data),
                                environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertEqual(data, resp_json)
        self.assertEqual(202, response.status_code)
Пример #4
0
class TransportTestCase(unittest.TestCase):

    def setUp(self):
        os.environ['espa_api_testing'] = 'True'
        # create a user
        self.mock_user = MockUser()
        self.mock_order = MockOrder()
        self.user = User.find(self.mock_user.add_testing_user())
        self.order_id = self.mock_order.generate_testing_order(self.user.id)

        self.app = http.app.test_client()
        self.app.testing = True

        self.sceneids = self.mock_order.scene_names_list(self.order_id)[0:2]

        token = ''.format(self.user.username, 'foo')
        auth_string = "Basic {}".format(base64.b64encode(token))
        self.headers = {"Authorization": auth_string}

        with db_instance() as db:
            uidsql = "select user_id, orderid from ordering_order limit 1;"
            db.select(uidsql)
            self.userid = db[0]['user_id']
            self.orderid = db[0]['orderid']

            itemsql = "select name, order_id from ordering_scene limit 1;"
            db.select(itemsql)
            self.itemid = db[0][0]
            itemorderid = db[0][1]

            ordersql = "select orderid from ordering_order where id = {};".format(itemorderid)
            db.select(ordersql)
            self.itemorderid = db[0][0]

        self.base_order = lowercase_all(testorders.build_base_order())

    def tearDown(self):
        # clean up orders
        self.mock_order.tear_down_testing_orders()
        # clean up users
        self.mock_user.cleanup()
        os.environ['espa_api_testing'] = ''

    def test_get_api_response_type(self):
        response = self.app.get('/api', headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        assert response.content_type == 'application/json'

    def test_get_api_response_content(self):
        response = self.app.get('/api', headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        self.assertEqual(set(['1', '0']), set(resp_json.keys()))

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_api_info_response_content(self):
        response = self.app.get('/api/v1', headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert "Version 1" in resp_json['description']

    @patch('api.domain.user.User.get', MockUser.get)
    @patch('api.providers.ordering.ordering_provider.OrderingProvider.available_products', mock_api.available_products)
    def test_get_available_prods(self):
        url = '/api/v1/available-products/' + ",".join(self.sceneids)
        response = self.app.get(url, headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert "etm" in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    @patch('api.providers.ordering.ordering_provider.OrderingProvider.available_products', mock_api.available_products)
    def test_post_available_prods(self):
        url = '/api/v1/available-products'
        data_dict = {'inputs': self.sceneids}
        response = self.app.post(url, data=json.dumps(data_dict), headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert "etm" in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    @patch('api.providers.ordering.ordering_provider.OrderingProvider.fetch_user_orders', mock_ordering_provider.fetch_user_orders)
    def test_get_available_orders_user(self):
        url = "/api/v1/list-orders"
        response = self.app.get(url, headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert resp_json.keys() == ['orders']

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_available_orders_email(self):
        # email param comes in as unicode
        url = "/api/v1/list-orders/" + str(self.user.email)
        response = self.app.get(url, headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert resp_json.keys() == ['orders']

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_order_by_ordernum(self):
        url = "/api/v1/order/" + str(self.orderid)
        response = self.app.get(url, headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'orderid' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_order_status_by_ordernum(self):
        url = "/api/v1/order-status/" + str(self.orderid)
        response = self.app.get(url, headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'orderid' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_item_status_by_ordernum(self):
        url = "/api/v1/item-status/%s" % self.itemorderid
        response = self.app.get(url, headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'orderid' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_item_status_by_ordernum_itemnum(self):
        url = "/api/v1/item-status/%s/%s" % (self.itemorderid, self.itemid)
        response = self.app.get(url, headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'orderid' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_current_user(self):
        url = "/api/v1/user"
        response = self.app.get(url, headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'username' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_projections(self):
        url = '/api/v1/projections'
        response = self.app.get(url, headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'aea' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_formats(self):
        url = '/api/v1/formats'
        response = self.app.get(url, headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'formats' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_resampling(self):
        url = '/api/v1/resampling-methods'
        response = self.app.get(url, headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'resampling_methods' in resp_json.keys()

    @patch('api.domain.user.User.get', MockUser.get)
    def test_get_order_schema(self):
        url = '/api/v1/order-schema'
        response = self.app.get(url, headers=self.headers, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        resp_json = json.loads(response.get_data())
        assert 'properties' in resp_json.keys()

    # Waiting for DB mock-ups to be finished
    def test_post_order(self):
        pass
class ProductionTransportTestCase(unittest.TestCase):

    def setUp(self):
        os.environ['espa_api_testing'] = 'True'
        # create a user
        self.mock_user = MockUser()
        self.mock_order = MockOrder()
        self.user = User.find(self.mock_user.add_testing_user())
        self.order_id = self.mock_order.generate_testing_order(self.user.id)

        self.app = http.app.test_client()
        self.app.testing = True

        self.sceneids = self.mock_order.scene_names_list(self.order_id)[0:2]

        with db_instance() as db:
            uidsql = "select user_id, orderid from ordering_order limit 1;"
            db.select(uidsql)
            self.userid = db[0]['user_id']
            self.orderid = db[0]['orderid']

            itemsql = "select name, order_id from ordering_scene limit 1;"
            db.select(itemsql)
            self.itemid = db[0][0]
            itemorderid = db[0][1]

            ordersql = "select orderid from ordering_order where id = {};".format(itemorderid)
            db.select(ordersql)
            self.itemorderid = db[0][0]

        self.base_order = lowercase_all(testorders.build_base_order())

    def tearDown(self):
        # clean up orders
        self.mock_order.tear_down_testing_orders()
        # clean up users
        self.mock_user.cleanup()
        os.environ['espa_api_testing'] = ''

    @patch('api.interfaces.production.version1.API.get_production_whitelist', api.get_production_whitelist)
    def test_get_production_api(self):
        response = self.app.get('/production-api', environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response.content_type == 'application/json'
        assert set(response_data.keys()) == set(['0', '1'])

    @patch('api.interfaces.production.version1.API.get_production_whitelist', api.get_production_whitelist)
    def test_get_production_api_v1(self):
        response = self.app.get('/production-api/v1', environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert set(response_data.keys()) == set(["operations", "description"])
        assert "ESPA Production" in response_data['description']

    @patch('api.providers.production.production_provider.ProductionProvider.get_products_to_process',
           production_provider.get_products_to_process_inputs)
    @patch('api.interfaces.production.version1.API.get_production_whitelist', api.get_production_whitelist)
    def test_get_production_api_products_modis(self):
        url = "/production-api/v1/products?for_user=bilbo&product_types=modis"
        response = self.app.get(url, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        correct_resp = {'encode_urls': False, 'for_user': '******',
                        'record_limit': 500, 'product_types': 'modis',
                        'priority': None}
        assert response_data == correct_resp

    @patch('api.providers.production.production_provider.ProductionProvider.get_products_to_process',
           production_provider.get_products_to_process_inputs)
    @patch('api.interfaces.production.version1.API.get_production_whitelist', api.get_production_whitelist)
    def test_get_production_api_products_landsat(self):
        url = "/production-api/v1/products?for_user=bilbo&product_types=landsat"
        response = self.app.get(url, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        correct_resp = {'encode_urls': False, 'for_user': '******',
                        'record_limit': 500, 'product_types': 'landsat',
                        'priority': None}
        assert response_data == correct_resp

    @patch('api.interfaces.production.version1.API.get_production_whitelist', api.get_production_whitelist)
    @patch('api.providers.production.production_provider.ProductionProvider.update_status',
           production_provider.update_status_inputs)
    def test_post_production_api_update_status(self):
        url = "/production-api/v1/update_status"
        data_dict = {'name': 't10000xyz401', 'orderid': '[email protected]',
                    'processing_loc': 'update_status', 'status': 'updated'}
        response = self.app.post(url, data=json.dumps(data_dict), environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data == data_dict

    @patch('api.providers.production.production_provider.ProductionProvider.set_product_error',
           production_provider.set_product_error_inputs)
    @patch('api.interfaces.production.version1.API.get_production_whitelist', api.get_production_whitelist)
    def test_post_production_api_set_product_error(self):
        url = "/production-api/v1/set_product_error"
        data_dict = {'name': 't10000xyz401', 'orderid': '[email protected]',
                     'processing_loc': 'xyz', 'error': 'oopsie'}
        response = self.app.post(url, data=json.dumps(data_dict), environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data == data_dict

    @patch('api.providers.production.production_provider.ProductionProvider.set_product_unavailable',
           production_provider.set_product_unavailable_inputs)
    @patch('api.interfaces.production.version1.API.get_production_whitelist', api.get_production_whitelist)
    def test_post_production_api_set_product_unavailable(self):
        url = "/production-api/v1/set_product_unavailable"
        data_dict = {'name': 't10000xyz401', 'orderid': '[email protected]',
                     'processing_loc': 'xyz', 'error': 'oopsie', 'note': 'them notes'}
        response = self.app.post(url, data=json.dumps(data_dict), environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data == data_dict

    @patch('api.providers.production.production_provider.ProductionProvider.mark_product_complete',
           production_provider.set_mark_product_complete_inputs)
    @patch('api.interfaces.production.version1.API.get_production_whitelist', api.get_production_whitelist)
    def test_post_production_api_mark_product_complete(self):
        url = "/production-api/v1/mark_product_complete"
        data_dict = {'name': 't10000xyz401', 'orderid': '[email protected]',
                     'processing_loc': 'xyz',
                     'completed_file_location': '/tmp',
                     'cksum_file_location': '/tmp/txt.txt',
                     'log_file_contents': 'details'}
        response = self.app.post(url, data=json.dumps(data_dict), environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data == data_dict

    @patch('api.providers.production.production_provider.ProductionProvider.handle_orders',
           production_provider.respond_true)
    @patch('api.interfaces.production.version1.API.get_production_whitelist', api.get_production_whitelist)
    def test_post_production_api_handle_orders(self):
        url = "/production-api/v1/handle-orders"
        response = self.app.get(url, data=json.dumps({}), environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data is True

    @patch('api.providers.production.production_provider.ProductionProvider.queue_products',
           production_provider.queue_products_inputs)
    @patch('api.interfaces.production.version1.API.get_production_whitelist', api.get_production_whitelist)
    def test_post_production_api_queue_products(self):
        url = "/production-api/v1/queue-products"
        data_dict = {'order_name_tuple_list': 'order_name_tuple_list',
                     'processing_location': 'processing_location',
                     'job_name': 'job_name'}
        response = self.app.post(url, data=json.dumps(data_dict), environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data == data_dict

    @patch('api.interfaces.production.version1.API.get_production_whitelist', api.get_production_whitelist)
    def test_get_production_api_configurations(self):
        url = "/production-api/v1/configuration/system_message_title"
        response = self.app.get(url, environ_base={'REMOTE_ADDR': '127.0.0.1'})
        response_data = json.loads(response.get_data())
        assert response_data.keys() == ['system_message_title']