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'])
示例#2
0
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)
 def test_read_invalid_json(self, mock_error):
     """ Test that it raises if the json is invalid. """
     with patch.object(pathlib.Path, 'read_text') as mock_read_text:
         mock_read_text.return_value = 'non-json'
         self.assertEqual(None, FilePersister.read_json('file\\path'))
         self.assertEqual("Invalid json format found in 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], json.decoder.JSONDecodeError)
    def test_read_json_file_not_found(self, mock_error):
        """ Test there is no result if file error happens and it is logged. """
        with patch.object(pathlib.Path, 'read_text') as mock_read_text:
            mock_read_text.side_effect = FileNotFoundError

            self.assertEqual(None, FilePersister.read_json('file\\path'))
            self.assertEqual('Error reading 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], FileNotFoundError)
示例#6
0
    def test_read_json_io_error(self, mock_error):
        """ Test there is no result if io error happens and it is logged. """
        with patch.object(pathlib.Path, 'read_text') as mock_read_text:
            mock_read_text.return_value = None
            mock_read_text.side_effect = IOError

            self.assertEqual(None, FilePersister.read_json('file\\path'))
            mock_error.assert_called_once_with('Error reading file %s.',
                                               'file\\path')
 def test_read(self):
     """ Test that the json file content is read and interpreted correctly. """
     with patch.object(pathlib.Path, 'read_text') as mock_read_text:
         mock_read_text.return_value = '{"x": "y"}'
         self.assertEqual({"x": "y"}, FilePersister.read_json('unimportant'))
示例#8
0
    def test_read_json_file_not_found(self):
        """ Test there is no result if file is not found. """
        with patch.object(pathlib.Path, 'read_text') as mock_read_text:
            mock_read_text.side_effect = FileNotFoundError

            self.assertEqual(None, FilePersister.read_json('unimportant'))