def get_connection_mitm(api: LocalAPI = None): origin = None auth = None headers = {} if api: params = {'hide_resource': 1} res = api.get('api/auth', params=params) if res.status_code == 200: try: uri = next(iter(res.json())) login_check = api.get(uri) auth = (login_check.json()['username'], login_check.json()['password']) except StopIteration: pass res = api.get('api/device', params=params) if res.status_code == 200: try: origin = next(iter(res.json().values())) except StopIteration: pass if origin is not None: headers['Origin'] = origin return LocalAPI(api_type='mitm', auth=auth, headers=headers)
def setUp(self): self.api = LocalAPI() self.creator = ResourceCreator(self.api)
class APITestBase(TestCase, object): generated_uris = [] index = 0 def setUp(self): self.api = LocalAPI() self.creator = ResourceCreator(self.api) def tearDown(self): self.creator.remove_resources() self.api.close() def create_resource(self, uri, payload, **kwargs): response = self.creator.create_resource(uri, payload, **kwargs) self.assertEqual(response.status_code, 201) return response def delete_resource(self, uri): return self.creator.delete_resource(uri) def create_valid(self, payload, uri=None, **kwargs): uri = uri if uri else self.uri return self.create_resource(uri, payload, **kwargs) def remove_resources(self): self.creator.remove_resources() # =========================== # ===== Basic Resources ===== # =========================== def create_valid_resource(self, resource, **kwargs): elem, response = self.creator.create_valid_resource(resource, **kwargs) self.assertEqual(response.status_code, 201) elem['uri'] = response.headers['X-Uri'] return elem def get_valid_resource(self, resource, **kwargs): return self.creator.get_valid_resource(resource, **kwargs) # =========================== # ========== Tests ========== # =========================== def landing_page(self, test_resource=True): response = self.api.get(self.uri) self.assertEqual(response.status_code, 200) json_data = response.json() self.assertTrue('resource' in json_data) self.assertTrue('results' in json_data) if test_resource: self.assertTrue(len(json_data['resource']['fields']) > 0) if 'settings' in json_data['resource']: self.assertTrue(len(json_data['resource']['settings']) > 0) params = {'hide_resource': 1} response = self.api.get(self.uri, params=params) self.assertEqual(response.status_code, 200) self.assertFalse('resource' in response.json()) self.assertTrue(type(response.json()) is dict) params['fetch_all'] = 1 response = self.api.get(self.uri, params=params) self.assertEqual(response.status_code, 200) for key, value in response.json().items(): self.assertTrue(type(value) is dict) def invalid_uri(self): path = '%s/%s' % (self.uri, '-1') response = self.api.get(path) self.assertEqual(response.status_code, 404) def invalid_post(self, payload, errors, error_code=422, **kwargs): response = self.api.post(self.uri, json=payload, **kwargs) self.assertEqual(response.status_code, error_code) self.assertDictEqual(response.json(), errors) def valid_post(self, payload, result, **kwargs): response = self.create_valid(payload, **kwargs) self.assertEqual(response.status_code, 201) self.assertDictEqual(result, response.json()) uri = response.headers['X-Uri'] response = self.delete_resource(uri) self.assertEqual(response.status_code, 202) def invalid_put(self, payload, errors, error_code=422, **kwargs): response = self.create_valid(self.base_payload) uri = response.headers['X-Uri'] response = self.api.put(uri, json=payload, **kwargs) self.assertEqual(response.status_code, error_code) self.assertDictEqual(response.json(), errors) response = self.delete_resource(uri) self.assertEqual(response.status_code, 202) def valid_put(self, payload, result, **kwargs): response = self.create_valid(self.base_payload) uri = response.headers['X-Uri'] response = self.api.put(uri, json=payload, **kwargs) self.assertEqual(response.status_code, 204) response = self.api.get(uri) self.assertDictEqual(result, response.json()) response = self.delete_resource(uri) def invalid_patch(self, payload, errors, error_code=422, **kwargs): response = self.create_valid(self.base_payload) uri = response.headers['X-Uri'] response = self.api.patch(uri, json=payload, **kwargs) self.assertEqual(response.status_code, error_code) self.assertDictEqual(errors, response.json()) response = self.delete_resource(uri) def valid_patch(self, payload, result, original=None, **kwargs): base_payload = original if original else self.base_payload response = self.create_valid(base_payload) uri = response.headers['X-Uri'] response = self.api.patch(uri, json=payload, **kwargs) self.assertEqual(response.status_code, 204) response = self.api.get(uri) self.assertDictEqual(result, response.json()) response = self.delete_resource(uri)
def get_connection_api(): return LocalAPI(api_type='api')
mad_plugins.apply_all_plugins_on_value() if args.with_madmin or args.config_mode: logger.info("Starting Madmin on port {}", str(args.madmin_port)) t_madmin = Thread(name="madmin", target=madmin.madmin_start) t_madmin.daemon = True t_madmin.start() logger.info("MAD is now running.....") exit_code = 0 device_creator = None try: if args.unit_tests: from mapadroid.tests.local_api import LocalAPI api_ready = False api = LocalAPI() logger.info('Checking API status') if not data_manager.get_root_resource('device').keys(): from mapadroid.tests.test_utils import ResourceCreator logger.info('Creating a device') device_creator = ResourceCreator(api, prefix='MADCore') res = device_creator.create_valid_resource('device')[0] mapping_manager.update() max_attempts = 30 attempt = 0 while not api_ready and attempt < max_attempts: try: api.get('/api') api_ready = True logger.info('API is ready for unit testing') except Exception: