def test_write(self): """ Test that a json is correctly written to a file. """ with patch.object(pathlib.Path, 'write_text') as mock_write_text: FilePersister.write_json({"x": 10}, 'file\\path') mock_write_text.assert_called_once_with('{\n "x": 10\n}', encoding='utf-8') saved_json = json.loads(''.join([args[0] for (args, kwargs) in mock_write_text.call_args_list])) self.assertEqual(10, saved_json['x'])
def authorized(): """Handler for the application's Redirect Uri.""" if bottle.request.query.state != MSGRAPH.auth_state: raise Exception('state returned to redirect URL does not match!') tokens = MSGRAPH.fetch_token(AUTHORITY_URL.format(org_id='common') + TOKEN_ENDPOINT, client_secret=CLIENT_SECRET, authorization_response=bottle.request.url, verify=False) file = FilePersister() file.write_json({'refresh_token': tokens['refresh_token']}, 'refresh_token.json') return {"token": file.read_json('refresh_token.json'), "token_file": os.getcwd() + '/refresh_token.json'}
def test_write_io_error(self, mock_error): """ Test that an incorrect json raises when it is tried to write it to a file. """ with patch.object(pathlib.Path, 'write_text') as mock_write_text: mock_error.return_value = None mock_write_text.side_effect = IOError FilePersister.write_json('unimportant', 'file\\path') mock_write_text.assert_called_once_with('"unimportant"', encoding='utf-8') self.assertEqual('Error writing file %s. Reason: %s.', mock_error.call_args_list[0][0][0]) self.assertEqual('file\\path', mock_error.call_args_list[0][0][1]) self.assertIsInstance(mock_error.call_args_list[0][0][2], IOError)