def test_receive_obs_error(self): """Test error handling in the receive routine.""" repository = Repository() station_id = 1 # Check what happens if the path is misconfigured (or the server is not able to write file) self.app.root = app.config["storage"][ 'image_root'] = '/nonexistent/path' secret = repository.read_station_secret(station_id) data = { 'aos': datetime.datetime(2020, 3, 28, 12, 00), 'tca': datetime.datetime(2020, 3, 28, 12, 15), 'los': datetime.datetime(2020, 3, 28, 12, 30), 'sat': 'NOAA 15', # 'notes': optional, "file0": open("tests/x.png", 'rb'), "file1": open("tests/x.png", 'rb') } header_value = get_authorization_header_value(str(station_id), secret, data) headers = {'Authorization': header_value} response = self.app.post('/receive', data=data, headers=headers) self.assertEqual(response.status_code, 503) # Check if there's appropriate entry in the log file. self.check_log([ "Failed to write /nonexistent/path/", "tests_x.png (image_root=/nonexistent/path)" ])
def _get_secret(station_id) -> bytes: ''' Fetch station secret from database ToDo: Returned value should be cached to avoid DDoS and DB call before authorization ''' repository = Repository() return repository.read_station_secret(station_id)
def test_receive_obs(self): repository = Repository() station_id = 1 secret = repository.read_station_secret(station_id) data = { 'aos': datetime.datetime(2020, 3, 28, 12, 00), 'tca': datetime.datetime(2020, 3, 28, 12, 15), 'los': datetime.datetime(2020, 3, 28, 12, 30), 'sat': 'NOAA 15', 'config': '{"text":"note text"}', "file0": open("tests/x.png", 'rb'), "file1": open("tests/x.png", 'rb'), "tle": [ # Include trailling character "1 25544U 98067A 08264.51782528 -.00002182 00000-0 -11606-4 0 2927 ", "2 25544 51.6416 247.4627 0006703 130.5360 325.0288 15.72125391563537" ], "rating": 0.75 } header_value = get_authorization_header_value(str(station_id), secret, data) headers = {'Authorization': header_value} response = self.app.post('/receive', data=data, headers=headers) self.assertEqual(response.status_code, 204) file_count = lambda dir_: len([ f for f in os.listdir(dir_) if os.path.isfile(os.path.join(dir_, f)) ]) self.assertEqual(file_count(IMAGE_ROOT), 2) self.assertEqual(file_count(os.path.join(IMAGE_ROOT, "thumbs")), 1) chart_dir = os.path.join(IMAGE_ROOT, "charts") self.assertEqual(file_count(chart_dir), 2) chart_files = sorted(os.listdir(chart_dir)) self.assertEqual(chart_files, ["by_time-1.png", "polar-1.png"]) # Todo: Need to check if the DB entries have been added. # Check if there are appropriate entries in the log file. self.check_log([ "0-tests_x.png written to tests/images", "1-tests_x.png written to tests/images" ])