Beispiel #1
0
class AuthenticationTests(unittest.TestCase):
    @patch('os.getenv')
    def setUp(self, mock_getenv):
        mock_getenv.side_effect = [
            "test_client_id",
            "test_username",
            "test_password",
            BASE_URL
        ]
        self.client = DemandAPIClient()
        self.assertEqual(self.client.client_id, "test_client_id")

    @responses.activate
    def test_authenticate(self):
        responses.add(
            responses.POST,
            "{}{}".format(self.client.auth_base_url, "/token/password"),
            json={
                "accessToken": "access_token",
                "refreshToken": "refresh_token"
            }
        )
        self.client.authenticate()
        self.assertEqual(self.client._access_token, "access_token")
        self.assertEqual(self.client._refresh_token, "refresh_token")
        self.assertIsNone(self.client._check_authentication())
Beispiel #2
0
 def setUp(self):
     self.validator = DemandAPIValidator()
     self.api = DemandAPIClient(client_id='test',
                                username='******',
                                password='******',
                                base_host=BASE_HOST)
     self.api._access_token = 'Bearer testtoken'
Beispiel #3
0
class TestEventEndpoints(unittest.TestCase):
    def setUp(self):
        self.api = DemandAPIClient(client_id='test',
                                   username='******',
                                   password='******',
                                   base_host=BASE_HOST)
        self.api._access_token = 'Bearer testtoken'

    @responses.activate
    def test_get_event(self):
        with open('./tests/test_files/get_event.json', 'r') as event_file:
            event_json = json.load(event_file)
        responses.add(responses.GET,
                      '{}/sample/v1/events/1337'.format(BASE_HOST),
                      json=event_json,
                      status=200)
        self.api.get_event(1337)
        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].response.json(), event_json)

    @responses.activate
    def test_get_events(self):
        with open('./tests/test_files/get_events.json', 'r') as event_file:
            event_json = json.load(event_file)
        responses.add(responses.GET,
                      '{}/sample/v1/events'.format(BASE_HOST),
                      json=event_json,
                      status=200)
        self.api.get_events()
        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].response.json(), event_json)
Beispiel #4
0
class TestUsersEndpoints(unittest.TestCase):
    def setUp(self):
        self.api = DemandAPIClient(client_id='test',
                                   username='******',
                                   password='******',
                                   base_host=BASE_HOST)
        self.api._access_token = 'Bearer testtoken'

    @responses.activate
    def test_get_user_info(self):
        # Tests getting currently logged in user info.
        with open('./tests/test_files/get_user_info.json', 'r') as user_info:
            user_info_json = json.load(user_info)
        # Success response
        responses.add(responses.GET,
                      '{}/sample/v1/user'.format(BASE_HOST),
                      json=user_info_json,
                      status=200)
        self.api.get_user_info()
        self.assertEqual(len(responses.calls), 1)

    @responses.activate
    def test_get_company_users(self):
        # Tests getting all company users.
        with open('./tests/test_files/get_company_users.json',
                  'r') as company_users:
            company_users_json = json.load(company_users)
        # Success response
        responses.add(responses.GET,
                      '{}/sample/v1/users'.format(BASE_HOST),
                      json=company_users_json,
                      status=200)
        self.api.get_company_users()
        self.assertEqual(len(responses.calls), 1)
Beispiel #5
0
 def setUp(self, mock_getenv):
     mock_getenv.side_effect = [
         "test_client_id",
         "test_username",
         "test_password",
         BASE_URL
     ]
     self.client = DemandAPIClient()
     self.assertEqual(self.client.client_id, "test_client_id")
Beispiel #6
0
    def test_authentication_params(self):
        DemandAPIClient(client_id="test", username="******", password="******", base_host=BASE_URL)

        with self.assertRaises(DemandAPIError):
            DemandAPIClient(username="******", password="******", base_host=BASE_URL)

        with self.assertRaises(DemandAPIError):
            DemandAPIClient(client_id="test", password="******", base_host=BASE_URL)

        with self.assertRaises(DemandAPIError):
            DemandAPIClient(client_id="test", username="******", base_host=BASE_URL)
Beispiel #7
0
    def test_authentication_params_with_env(self, mock_getenv):
        mock_getenv.side_effect = [
            "test_client_id",
            "test_username",
            "test_password",
            BASE_URL
        ]

        # None of these should raise an error if the appropriate missing data is available in the environment.
        DemandAPIClient(client_id="test", username="******", password="******", base_host=BASE_URL)
        DemandAPIClient(username="******", password="******", base_host=BASE_URL)
        DemandAPIClient(client_id="test", password="******", base_host=BASE_URL)
        DemandAPIClient(client_id="test", username="******", base_host=BASE_URL)
Beispiel #8
0
class TestTeamsEndpoints(unittest.TestCase):
    def setUp(self):
        self.api = DemandAPIClient(client_id='test', username='******', password='******', base_host=BASE_HOST)
        self.api._access_token = 'Bearer testtoken'

    @responses.activate
    def test_get_teams(self):
        # Tests getting all teams.
        with open('./tests/test_files/get_teams.json', 'r') as teams:
            teams_json = json.load(teams)
        # Success response
        responses.add(responses.GET, '{}/sample/v1/teams'.format(BASE_HOST), json=teams_json, status=200)
        self.api.get_company_teams()
        self.assertEqual(len(responses.calls), 1)
Beispiel #9
0
 def test_missing_password(self, mock_getenv):
     mock_getenv.side_effect = [
         "test_client_id",
         "test_username",
         None,
         BASE_URL
     ]
     with self.assertRaises(DemandAPIError):
         DemandAPIClient()
class TestFeasibilityEndpoints(unittest.TestCase):
    def setUp(self):
        self.api = DemandAPIClient(client_id='test',
                                   username='******',
                                   password='******',
                                   base_host=BASE_HOST)
        self.api._access_token = 'Bearer testtoken'

    @responses.activate
    def test_get_feasibility(self):
        with open('./tests/test_files/get_feasibility.json',
                  'r') as project_file:
            project_json = json.load(project_file)
        responses.add(responses.GET,
                      '{}/sample/v1/projects/1/feasibility'.format(BASE_HOST),
                      json=project_json,
                      status=200)
        self.api.get_feasibility(1)
        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].response.json(), project_json)
class TestInvoiceEndpoints(unittest.TestCase):
    def setUp(self):
        self.api = DemandAPIClient(client_id='test',
                                   username='******',
                                   password='******',
                                   base_host=BASE_HOST)
        self.api._access_token = 'Bearer testtoken'

    @responses.activate
    def test_get_invoice(self):
        with open('./tests/test_files/get_invoice.pdf', 'rb') as invoice_file:
            responses.add(
                responses.GET,
                '{}/sample/v1/projects/1337/invoices'.format(BASE_HOST),
                body=invoice_file.read(),
                content_type='application/pdf',
                stream=True,
                status=200)
        self.api.get_invoice(1337)
        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].response.headers['content-type'],
                         'application/pdf')

    @responses.activate
    def test_get_invoices_summary(self):
        with open('./tests/test_files/get_invoices_summary.pdf',
                  'rb') as summary_file:
            responses.add(
                responses.GET,
                '{}/sample/v1/projects/invoices/summary'.format(BASE_HOST),
                body=summary_file.read(),
                content_type='application/pdf',
                stream=True,
                status=200)
        self.api.get_invoices_summary(
            startDate='2019-06-12',
            endDate='2019-06-19',
            extProjectId='010528ef-8984-48c1-a06d-4dae730da027')
        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].response.headers['content-type'],
                         'application/pdf')
Beispiel #12
0
class TestStudyMetadataEndpoint(unittest.TestCase):
    def setUp(self):
        self.api = DemandAPIClient(client_id='test',
                                   username='******',
                                   password='******',
                                   base_host=BASE_HOST)
        self.api._access_token = 'Bearer testtoken'

    @responses.activate
    def test_get_study_metadata(self):
        with open('./tests/test_files/get_study_metadata.json',
                  'r') as survey_metadata_file:
            survey_metadata_json = json.load(survey_metadata_file)
        responses.add(responses.GET,
                      '{}/sample/v1/studyMetadata'.format(BASE_HOST),
                      json=survey_metadata_json,
                      status=200)
        self.api.get_study_metadata()
        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].response.json(),
                         survey_metadata_json)
Beispiel #13
0
class TestAttributeEndpoints(unittest.TestCase):
    def setUp(self):
        self.api = DemandAPIClient(client_id='test',
                                   username='******',
                                   password='******',
                                   base_host=BASE_HOST)
        self.api._access_token = 'Bearer testtoken'

    @responses.activate
    def test_get_attributes(self):
        with open('./tests/test_files/get_attributes.json',
                  'r') as attributes_file:
            attributes_json = json.load(attributes_file)
        responses.add(responses.GET,
                      '{}/sample/v1/attributes/no/no'.format(BASE_HOST),
                      json=attributes_json,
                      status=200)
        self.api.get_attributes('no', 'no')
        self.assertEqual(len(responses.calls), 1)
        print('flaws')
        print(responses.calls[0].response.json())
        self.assertEqual(responses.calls[0].response.json(), attributes_json)
class TestProjectPermissionsEndpoints(unittest.TestCase):
    def setUp(self):
        self.api = DemandAPIClient(client_id='test',
                                   username='******',
                                   password='******',
                                   base_host=BASE_HOST)
        self.api._access_token = 'Bearer testtoken'

    @responses.activate
    def test_get_project_permissions(self):
        with open('./tests/test_files/get_project_permissions.json',
                  'r') as get_permissions_file:
            permissions_json = json.load(get_permissions_file)
        responses.add(responses.GET,
                      '{}/sample/v1/projects/1/permissions'.format(BASE_HOST),
                      json=permissions_json,
                      status=200)
        self.api.get_project_permissions(1)
        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].response.json(), permissions_json)

    @responses.activate
    def test_upsert_project_permissions(self):
        # Tests updating a project.
        with open('./tests/test_files/upsert_project_permissions.json',
                  'r') as upsert_project_file:
            upsert_project_data = json.load(upsert_project_file)

        # Success response
        responses.add(responses.POST,
                      '{}/sample/v1/projects/1/permissions'.format(BASE_HOST),
                      json={'status': {
                          'message': 'success'
                      }},
                      status=200)
        # Error message included
        responses.add(responses.POST,
                      '{}/sample/v1/projects/1/permissions'.format(BASE_HOST),
                      json={'status': {
                          'message': 'error'
                      }},
                      status=200)

        # Test successful response.
        self.api.upsert_project_permissions(1, upsert_project_data)
        self.assertEqual(len(responses.calls), 1)

        # Test response with error included.
        with self.assertRaises(DemandAPIError):
            self.api.upsert_project_permissions(1, upsert_project_data)
        self.assertEqual(len(responses.calls), 2)
Beispiel #15
0
class TestLineItemEndpoints(unittest.TestCase):
    def setUp(self):
        self.api = DemandAPIClient(client_id='test', username='******', password='******', base_host=BASE_HOST)
        self.api._access_token = 'Bearer testtoken'

    @responses.activate
    def test_get_line_item(self):
        # Tests getting a line item.
        with open('./tests/test_files/get_line_item.json', 'r') as line_item_file:
            line_item_json = json.load(line_item_file)
        responses.add(
            responses.GET,
            '{}/sample/v1/projects/1/lineItems/100'.format(BASE_HOST),
            json=line_item_json,
            status=200)
        self.api.get_line_item(1, 100)
        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].response.json(), line_item_json)

    @responses.activate
    def test_get_line_items(self):
        # Tests getting all line items for a project.
        with open('./tests/test_files/get_line_items.json', 'r') as line_item_file:
            line_item_json = json.load(line_item_file)
        responses.add(responses.GET, '{}/sample/v1/projects/1/lineItems'.format(BASE_HOST), json=line_item_json, status=200)
        self.api.get_line_items(1)
        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].response.json(), line_item_json)

    @responses.activate
    def test_get_line_item_detailed_report(self):
        # Tests getting a line item detailed report.
        with open('./tests/test_files/get_line_item_detailed_report.json', 'r') as line_item_detailed_report_file:
            line_item_detailed_report_json = json.load(line_item_detailed_report_file)
        responses.add(
            responses.GET,
            '{}/sample/v1/projects/1/lineItems/100/detailedReport'.format(BASE_HOST),
            json=line_item_detailed_report_json,
            status=200)
        self.api.get_line_item_detailed_report(1, 100)
        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].response.json(), line_item_detailed_report_json)

    @responses.activate
    def test_add_line_item(self):
        # Tests creating a line item.
        with open('./tests/test_files/create_line_item.json', 'r') as new_lineitem_file:
            new_lineitem_data = json.load(new_lineitem_file)
        # Success response
        responses.add(
            responses.POST,
            '{}/sample/v1/projects/24/lineItems'.format(BASE_HOST),
            json={'status': {'message': 'success'}},
            status=200)
        # Response with error status
        responses.add(
            responses.POST,
            '{}/sample/v1/projects/24/lineItems'.format(BASE_HOST),
            json={'status': {'message': 'error'}},
            status=200)
        # Test success response
        self.api.add_line_item(24, new_lineitem_data)
        self.assertEqual(len(responses.calls), 1)

        with self.assertRaises(DemandAPIError):
            self.api.add_line_item(24, new_lineitem_data)
        self.assertEqual(len(responses.calls), 2)

    @responses.activate
    def test_close_line_item(self):
        # Tests closing a line item.
        responses.add(
            responses.POST,
            '{}/sample/v1/projects/69/lineItems/1337/close'.format(BASE_HOST),
            json={'status': {'message': 'success'}},
            status=200
        )

        # Response with error status
        responses.add(
            responses.POST,
            '{}/sample/v1/projects/69/lineItems/1337/close'.format(BASE_HOST),
            json={'status': {'message': 'error'}},
            status=200
        )

        # Test successful response
        self.api.close_line_item(69, 1337)
        self.assertEqual(len(responses.calls), 1)

        # Test error response
        with self.assertRaises(DemandAPIError):
            self.api.close_line_item(69, 1337)
        self.assertEqual(len(responses.calls), 2)

    @responses.activate
    def test_launch_line_item(self):
        # Tests launching a line item.
        responses.add(
            responses.POST,
            '{}/sample/v1/projects/24/lineItems/180/launch'.format(BASE_HOST),
            json={'status': {'message': 'success'}},
            status=200
        )

        # Response with error status
        responses.add(
            responses.POST,
            '{}/sample/v1/projects/24/lineItems/180/launch'.format(BASE_HOST),
            json={'status': {'message': 'error'}},
            status=200
        )

        # Test successful response
        self.api.launch_line_item(24, 180)
        self.assertEqual(len(responses.calls), 1)

        # Test error response
        with self.assertRaises(DemandAPIError):
            self.api.launch_line_item(24, 180)
        self.assertEqual(len(responses.calls), 2)

    @responses.activate
    def test_pause_line_item(self):
        # Tests pausing a line item.
        responses.add(
            responses.POST,
            '{}/sample/v1/projects/24/lineItems/180/pause'.format(BASE_HOST),
            json={'status': {'message': 'success'}},
            status=200
        )
        # Response with error
        responses.add(
            responses.POST,
            '{}/sample/v1/projects/24/lineItems/180/pause'.format(BASE_HOST),
            json={'status': {'message': 'error'}},
            status=200
        )

        # Test successful response
        self.api.pause_line_item(24, 180)
        self.assertEqual(len(responses.calls), 1)

        # Test error response
        with self.assertRaises(DemandAPIError):
            self.api.pause_line_item(24, 180)
        self.assertEqual(len(responses.calls), 2)

    @responses.activate
    def test_set_quotacell_status(self):
        # Tests launching a quotacell.
        responses.add(
            responses.POST,
            '{}/sample/v1/projects/24/lineItems/180/quotaCells/1/launch'.format(BASE_HOST),
            json={'status': {'message': 'success'}},
            status=200
        )
        # Response with error launching a quotacell.
        responses.add(
            responses.POST,
            '{}/sample/v1/projects/24/lineItems/180/quotaCells/1/launch'.format(BASE_HOST),
            json={'status': {'message': 'error'}},
            status=200
        )
        # Tests pausing a quotacell.
        responses.add(
            responses.POST,
            '{}/sample/v1/projects/24/lineItems/180/quotaCells/1/pause'.format(BASE_HOST),
            json={'status': {'message': 'success'}},
            status=200
        )
        # Response with error for pausing a quotacell
        responses.add(
            responses.POST,
            '{}/sample/v1/projects/24/lineItems/180/quotaCells/1/pause'.format(BASE_HOST),
            json={'status': {'message': 'error'}},
            status=200
        )

        # Test successful response for launch quotacell.
        self.api.set_quotacell_status(24, 180, 1, "launch")
        self.assertEqual(len(responses.calls), 1)

        # Test error response for launch quotacell.
        with self.assertRaises(DemandAPIError):
            self.api.set_quotacell_status(24, 180, 1, "launch")
        self.assertEqual(len(responses.calls), 2)

        # Test successful response for pause quotacell.
        self.api.set_quotacell_status(24, 180, 1, "pause")
        self.assertEqual(len(responses.calls), 3)

        # Test error response for pause quotacell.
        with self.assertRaises(DemandAPIError):
            self.api.set_quotacell_status(24, 180, 1, "pause")
        self.assertEqual(len(responses.calls), 4)

    @responses.activate
    def test_update_line_item(self):
        # Tests updating a line item.
        with open('./tests/test_files/update_line_item.json', 'r') as new_lineitem_file:
            update_lineitem_data = json.load(new_lineitem_file)

        # Success response
        responses.add(
            responses.POST,
            '{}/sample/v1/projects/1/lineItems/1'.format(BASE_HOST),
            json={'status': {'message': 'success'}},
            status=200
        )
        # Response with error status
        responses.add(
            responses.POST,
            '{}/sample/v1/projects/1/lineItems/1'.format(BASE_HOST),
            json={'status': {'message': 'error'}},
            status=200
        )

        # Test success response
        self.api.update_line_item(1, 1, update_lineitem_data)
        self.assertEqual(len(responses.calls), 1)

        # Test error response
        with self.assertRaises(DemandAPIError):
            self.api.update_line_item(1, 1, update_lineitem_data)
            self.assertEqual(len(responses.calls), 2)
class TestProjectEndpoints(unittest.TestCase):
    def setUp(self):
        self.api = DemandAPIClient(client_id='test',
                                   username='******',
                                   password='******',
                                   base_host=BASE_HOST)
        self.api._access_token = 'Bearer testtoken'

    @responses.activate
    def test_get_project(self):
        with open('./tests/test_files/get_project.json', 'r') as project_file:
            project_json = json.load(project_file)
        responses.add(responses.GET,
                      '{}/sample/v1/projects/1'.format(BASE_HOST),
                      json=project_json,
                      status=200)
        self.api.get_project(1)
        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].response.json(), project_json)

    @responses.activate
    def test_get_projects(self):
        with open('./tests/test_files/get_projects.json', 'r') as project_file:
            project_json = json.load(project_file)
        responses.add(responses.GET,
                      '{}/sample/v1/projects'.format(BASE_HOST),
                      json=project_json,
                      status=200)
        self.api.get_projects()
        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].response.json(), project_json)

    @responses.activate
    def test_get_project_detailed_report(self):
        with open('./tests/test_files/get_project_detailed_report.json',
                  'r') as project_detailed_report_file:
            project_detailed_report_json = json.load(
                project_detailed_report_file)
        responses.add(
            responses.GET,
            '{}/sample/v1/projects/1/detailedReport'.format(BASE_HOST),
            json=project_detailed_report_json,
            status=200)
        self.api.get_project_detailed_report(1)
        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].response.json(),
                         project_detailed_report_json)

    @responses.activate
    def test_create_project(self):
        # Tests creating a project. This also tests validating the project data as part of `api.create_project`.
        with open('./tests/test_files/create_project.json',
                  'r') as new_project_file:
            new_project_data = json.load(new_project_file)
        responses.add(responses.POST,
                      '{}/sample/v1/projects'.format(BASE_HOST),
                      json={'status': {
                          'message': 'success'
                      }},
                      status=200)
        self.api.create_project(new_project_data)
        self.assertEqual(len(responses.calls), 1)

    @responses.activate
    def test_buy_project(self):
        # Tests buying a project.
        with open('./tests/test_files/buy_project.json',
                  'r') as buy_project_file:
            buy_project_data = json.load(buy_project_file)
        # Success response
        responses.add(responses.POST,
                      '{}/sample/v1/projects/24/buy'.format(BASE_HOST),
                      json={'status': {
                          'message': 'success'
                      }},
                      status=200)
        # Response with error status
        responses.add(responses.POST,
                      '{}/sample/v1/projects/24/buy'.format(BASE_HOST),
                      json={'status': {
                          'message': 'error'
                      }},
                      status=200)
        # Test success response
        self.api.buy_project(24, buy_project_data)
        self.assertEqual(len(responses.calls), 1)
        # Test error response
        with self.assertRaises(DemandAPIError):
            self.api.buy_project(24, buy_project_data)
        self.assertEqual(len(responses.calls), 2)

    @responses.activate
    def test_close_project(self):
        # Tests closing a project.
        responses.add(responses.POST,
                      '{}/sample/v1/projects/24/close'.format(BASE_HOST),
                      json={'status': {
                          'message': 'success'
                      }},
                      status=200)
        responses.add(responses.POST,
                      '{}/sample/v1/projects/24/close'.format(BASE_HOST),
                      json={'status': {
                          'message': 'error'
                      }},
                      status=200)
        self.api.close_project(24)
        self.assertEqual(len(responses.calls), 1)
        with self.assertRaises(DemandAPIError):
            self.api.close_project(24)
        self.assertEqual(len(responses.calls), 2)

    @responses.activate
    def test_update_project(self):
        # Tests updating a project.
        with open('./tests/test_files/update_project.json',
                  'r') as update_project_file:
            update_project_data = json.load(update_project_file)

        # Success response
        responses.add(responses.POST,
                      '{}/sample/v1/projects/24'.format(BASE_HOST),
                      json={'status': {
                          'message': 'success'
                      }},
                      status=200)
        # Error message included
        responses.add(responses.POST,
                      '{}/sample/v1/projects/24'.format(BASE_HOST),
                      json={'status': {
                          'message': 'error'
                      }},
                      status=200)

        # Test successful response.
        self.api.update_project(24, update_project_data)
        self.assertEqual(len(responses.calls), 1)

        # Test response with error included.
        with self.assertRaises(DemandAPIError):
            self.api.update_project(24, update_project_data)
        self.assertEqual(len(responses.calls), 2)

    @responses.activate
    def test_reconcile_project(self):
        # Tests reconciling a project.
        message = 'testing reconciliation'
        with open('./tests/test_files/Data+Quality+Request+Template.xlsx',
                  'rb') as file:
            # Success response
            responses.add(
                responses.POST,
                '{}/sample/v1/projects/24/reconcile'.format(BASE_HOST),
                json={'status': {
                    'message': 'success'
                }},
                status=200)
            # Error message included
            responses.add(
                responses.POST,
                '{}/sample/v1/projects/24/reconcile'.format(BASE_HOST),
                json={'status': {
                    'message': 'error'
                }},
                status=400)

            # Test successful response.
            self.api.reconcile_project(24, file, message)
            self.assertEqual(len(responses.calls), 1)

            # Test response with error included.
            with self.assertRaises(DemandAPIError):
                self.api.reconcile_project(24, file, message)
            self.assertEqual(len(responses.calls), 2)
Beispiel #17
0
class TestValidator(unittest.TestCase):
    def setUp(self):
        self.validator = DemandAPIValidator()
        self.api = DemandAPIClient(client_id='test',
                                   username='******',
                                   password='******',
                                   base_host=BASE_HOST)
        self.api._access_token = 'Bearer testtoken'

    def test_schemas(self):
        for schema_type in self.validator.schemas.keys():
            '''
            #TODO allow path schema validation when we receive valid ones
            '''
            if schema_type == 'path':
                continue
            for schema in self.validator.schemas[schema_type].values():
                Draft7Validator.check_schema(schema)

    def test_query_params(self):
        self.validator.validate_request('get_projects',
                                        query_params={'limit': 100})

        with self.assertRaises(ValidationError):
            self.validator.validate_request(
                'get_projects', query_params={'limit': 'five hundred'})

    @responses.activate
    def test_query_params_with_api_client(self):
        responses.add(responses.GET,
                      '{}/sample/v1/projects'.format(BASE_HOST),
                      json={},
                      status=200)

        # Valid keyword arguments
        self.api.get_projects(limit=100,
                              offset=0,
                              created_at="today",
                              sort=["ordering"])

        with self.assertRaises(ValidationError):
            # Limit greater than allowed
            self.api.get_projects(limit=500)

        with self.assertRaises(ValidationError):
            # Limit not a number
            self.api.get_projects(limit="five")

    @responses.activate
    def test_path_validation(self):
        responses.add(
            responses.GET,
            '{}/sample/v1/projects/my%20project/lineItems/my%20line%20item'.
            format(BASE_HOST),
            json={},
            status=200)

        # All path components are strings and don't have any validation besides that.
        # This is technically valid even if it looks weird.
        self.api.get_line_item("my project", "my line item")

    @responses.activate
    def test_body_validation(self):
        # Tests implementation of validation through the API.
        with open('./tests/test_files/update_project.json',
                  'r') as update_project_file:
            update_project_data = json.load(update_project_file)
        responses.add(responses.POST,
                      '{}/sample/v1/projects/1'.format(BASE_HOST),
                      json={'status': {
                          'message': 'success'
                      }},
                      status=200)

        self.api.update_project(1, update_project_data)
class TestTemplateEndpoints(unittest.TestCase):
    def setUp(self):
        self.api = DemandAPIClient(client_id='test', username='******', password='******', base_host=BASE_HOST)
        self.api._access_token = 'Bearer testtoken'

    @responses.activate
    def test_get_templates(self):
        # Tests getting all templates.
        with open('./tests/test_files/get_templates.json', 'r') as options:
            options_json = json.load(options)
        # Success response
        responses.add(
            responses.GET,
            '{}/sample/v1/templates/quotaplan/{}/{}'.format(BASE_HOST, 'US', 'en'),
            json=options_json,
            status=200)
        self.api.get_templates('US', 'en')
        self.assertEqual(len(responses.calls), 1)

    @responses.activate
    def test_create_template(self):
        # Tests creating a template.
        with open('./tests/test_files/create_template.json', 'r') as new_template_file:
            new_template_data = json.load(new_template_file)
        # Success response
        responses.add(
            responses.POST,
            '{}/sample/v1/templates/quotaplan'.format(BASE_HOST),
            json={'status': {'message': 'success'}},
            status=200)
        # Response with error status
        responses.add(
            responses.POST,
            '{}/sample/v1/templates/quotaplan'.format(BASE_HOST),
            json={'status': {'message': 'error'}},
            status=200)
        # Test success response
        self.api.create_template(new_template_data)
        self.assertEqual(len(responses.calls), 1)

    @responses.activate
    def test_update_template(self):
        # Tests creating a template.
        with open('./tests/test_files/update_template.json', 'r') as new_template_file:
            new_template_data = json.load(new_template_file)
        # Success response
        responses.add(
            responses.POST,
            '{}/sample/v1/templates/quotaplan/{}'.format(BASE_HOST, 1),
            json={'status': {'message': 'success'}},
            status=200)
        # Response with error status
        responses.add(
            responses.POST,
            '{}/sample/v1/templates/quotaplan/{}'.format(BASE_HOST, 1),
            json={'status': {'message': 'error'}},
            status=200)
        # Test success response
        self.api.update_template(1, new_template_data)
        self.assertEqual(len(responses.calls), 1)

    @responses.activate
    def test_delete_template(self):
        # Tests deleteing templates
        responses.add(
            responses.DELETE,
            '{}/sample/v1/templates/quotaplan/1'.format(BASE_HOST),
            json={'status': {'message': 'success'}},
            status=200)
        # Response with error status
        responses.add(
            responses.DELETE,
            '{}/sample/v1/templates/quotaplan/1'.format(BASE_HOST),
            json={'status': {'message': 'error'}},
            status=200)
        # Test successful response
        self.api.delete_template(1)
        self.assertEqual(len(responses.calls), 1)