def test_autoconfig_file_download(self): api_creator = ResourceCreator(self.api) with GetStorage(self.api) as storage: try: # this really isnt an api call but whatever res = self.api.get('/autoconfig/download') self.assertTrue(res.status_code == 406) # Setup default env without anything defined create_valid_configs(self, api_creator, auth=False) storage.upload_all() res = self.api.get('/autoconfig/download') self.assertTrue(res.status_code == 200) self.assertTrue(pd_host == res.content.decode('utf-8')) create_valid_configs(self, api_creator) res = self.api.get('/autoconfig/download') self.assertTrue(res.status_code == 200) auth = f"{email_base}:{pwd_base}" expected = f"{pd_host}\n{auth}" self.assertTrue(expected == res.content.decode('utf-8')) except Exception: raise finally: rgc_delete = self.api.delete('/api/autoconf/rgc') pd_delete = self.api.delete('/api/autoconf/pd') api_creator.remove_resources() self.assertTrue(rgc_delete.status_code == 200) self.assertTrue(pd_delete.status_code == 200)
def test_no_configured_endpoints(self): api_creator = ResourceCreator(self.api) session_id = None try: res = self.mitm.post('/autoconfig/register') self.assertTrue(res.status_code == 201) session_id = res.content.decode('utf-8') (dev_info, _) = api_creator.create_valid_resource('device') accept_info = { 'status': 1, 'device_id': dev_info['uri'] } with GetStorage(self.api) as storage: storage.upload_all() res = self.api.post('/api/autoconf/{}'.format(session_id), json=accept_info) self.assertTrue(res.status_code == 406) expected_issues = { 'X-Critical': [ AutoConfIssues.pd_not_configured.value, AutoConfIssues.rgc_not_configured.value, ], 'X-Warnings': [ AutoConfIssues.no_ggl_login.value, AutoConfIssues.auth_not_configured.value, ] } self.assertListEqual(expected_issues['X-Critical'], json.loads(res.headers['X-Critical'])) self.assertListEqual(expected_issues['X-Warnings'], json.loads(res.headers['X-Warnings'])) finally: api_creator.remove_resources() if session_id is not None: self.mitm.delete('/autoconfig/{}/complete'.format(session_id))
def decorated(self, *args, **kwargs): api_creator = ResourceCreator(self.api) gacct = None session_id = None with GetStorage(self.api) as storage: try: storage.upload_all() create_valid_configs(self, api_creator) res = self.mitm.post('/autoconfig/register') self.assertTrue(res.status_code == 201) session_id = res.content.decode('utf-8') # Create Google Account gacc = { "login_type": "google", "username": "******", "password": "******" } res = self.api.post('/api/pogoauth', json=gacc) gacct = res.headers['X-URI'] self.assertTrue(res.status_code == 201) dev_payload = copy.copy( global_variables.DEFAULT_OBJECTS['device']['payload']) dev_payload['account_id'] = gacct (dev_info, _) = api_creator.create_valid_resource('device', payload=dev_payload) accept_info = {'status': 1, 'device_id': dev_info['uri']} res = self.api.post('/api/autoconf/{}'.format(session_id), json=accept_info) self.assertTrue(res.status_code == 200) res = self.mitm.get('/autoconfig/{}/status'.format(session_id)) self.assertTrue(res.status_code == 200) (ss_info, _) = api_creator.create_valid_resource('devicesetting') func(self, session_id, dev_info, ss_info, api_creator, *args, **kwargs) except Exception: raise finally: self.api.delete('/api/autoconf/rgc') self.api.delete('/api/autoconf/pd') api_creator.remove_resources() if session_id is not None: self.mitm.delete( '/autoconfig/{}/complete'.format(session_id)) if gacct is not None: self.api.delete(gacct)
def test_lower_case(self): api_creator = ResourceCreator(self.api) with GetStorage(self.api): try: pd_email = 'UPPERcase' pd_update = {'user_id': pd_email} create_valid_configs(self, api_creator) res = self.api.patch('/api/autoconf/pd', json=pd_update) self.assertTrue(res.status_code == 200) res = self.api.get('/api/autoconf/pd') self.assertTrue(res.status_code == 200) self.assertTrue(res.json()['user_id'] == pd_email.lower()) except Exception: raise finally: self.api.delete('/api/autoconf/rgc') self.api.delete('/api/autoconf/pd') api_creator.remove_resources()
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)
result = runner.run(suite) exit_code = 0 if result.wasSuccessful() else 1 raise KeyboardInterrupt else: exit_code = 1 else: while True: time.sleep(10) except KeyboardInterrupt or Exception: logger.info("Shutdown signal received") finally: try: db_wrapper = None logger.success("Stop called") if device_creator: device_creator.remove_resources() terminate_mad.set() # now cleanup all threads... # TODO: check against args or init variables to None... if mitm_receiver_process is not None: logger.info("Trying to stop receiver") mitm_receiver_process.shutdown() logger.debug( "MITM child threads successfully shutdown. Terminating parent thread" ) mitm_receiver_process.terminate() logger.debug("Trying to join MITMReceiver") mitm_receiver_process.join() logger.debug("MITMReceiver joined") if mitm_data_processor_manager is not None: mitm_data_processor_manager.shutdown()