Example #1
0
 def test_client_can_make_request(self, mock_request):
     mock_request.return_value = self.success_mocked_response
     client = ToshlClient('abcd1234')
     client._make_request('/me', 'GET')
     mock_request.assert_called_once_with(
         headers={'Authorization': 'Bearer abcd1234'},
         method='GET', params=None, url='https://api.toshl.com/me')
Example #2
0
    def test_update_account_successful(self, mock_request):
        json_payload = {
            'name': 'Test Account',
            'currency': {
                'code': 'GBP'
            },
            'extra': {
                'test': 'foo'
            }
        }

        mock_response = mock.Mock()
        mock_response.status_code = 200
        mock_response.json.return_value = json_payload
        mock_response.headers = {'Location': '/accounts/1'}
        mock_request.return_value = mock_response

        client = ToshlClient('abcd1234')
        account_client = Account(client)

        response = account_client.update('1', json_payload)

        mock_request.assert_called_once_with(
            headers={
                'Authorization': 'Bearer abcd1234',
                'Content-Type': 'application/json'
            },
            method='PUT', params=None, url='https://api.toshl.com/accounts/1',
            json=json_payload)
        assert response == json_payload
Example #3
0
    def test_merge_account_successful(self, mock_request):
        accounts_list = ['1', '2']
        dest_account = '2'

        json_payload = {
            'accounts': accounts_list,
            'account': dest_account
        }

        mock_response = mock.Mock()
        mock_response.json.return_value = ''
        mock_response.status_code = 204
        mock_request.return_value = mock_response

        client = ToshlClient('abcd1234')
        account = Account(client)
        response = account.merge(accounts_list, dest_account)

        mock_request.assert_called_once_with(
            headers={
                'Authorization': 'Bearer abcd1234',
                'Content-Type': 'application/json'
            },
            method='POST',
            params=None, url='https://api.toshl.com/accounts/merge',
            json=json_payload)
        assert response.json() == ''
Example #4
0
    def test_search_accounts_successful_multiple_accounts(self, mock_request):
        mock_response = mock.Mock()
        expected_dict = [
            {
                "id": "42",
                "name": "Account Test",
                "balance": 3000
            },
            {
                "id": "123",
                "name": "Test Found",
                "balance": 22000
            }
        ]

        mock_response.json.return_value = expected_dict
        mock_response.status_code = 200
        mock_request.return_value = mock_response

        client = ToshlClient('abcd1234')
        account = Account(client)
        account_found = account.search('Test Found')

        mock_request.assert_called_once_with(
            headers={'Authorization': 'Bearer abcd1234'},
            method='GET', params=None, url='https://api.toshl.com/accounts')
        assert account_found is not None
        assert account_found == '123'
Example #5
0
    def test_create_entry_successful(self, mock_request):
        mock_response = mock.Mock()
        mock_response.status_code = 201
        mock_response.headers = {'Location': '/entries/1'}
        mock_request.return_value = mock_response

        client = ToshlClient('abcd1234')
        entry = Entry(client)

        json_payload = {
            'amount': -123.68,
            'currency': {
                'code': 'GBP'
            },
            'date': '2016-04-07',
            'account': 'abcd1234',
            'category': 'category-001'
        }

        response = entry.create(json_payload)

        mock_request.assert_called_once_with(
            headers={
                'Authorization': 'Bearer abcd1234',
                'Content-Type': 'application/json'
            },
            method='POST',
            params=None,
            url='https://api.toshl.com/entries',
            json=json_payload)
        assert response == '1'
Example #6
0
    def test_client_request_raises_exception_on_400(self, mock_request):
        mock_response = mock.Mock()
        expected_dict = {
            'error_id': 'exception_id',
            'description': 'Exception description'
        }
        mock_response.json.return_value = expected_dict
        mock_response.status_code = 400
        mock_request.return_value = mock_response

        client = ToshlClient('abcd1234')

        with self.assertRaises(ToshlException) as ex:
            client._make_request('/me', 'GET')

        assert ex.exception.status_code == 400
        assert ex.exception.error_id == 'exception_id'
        assert ex.exception.error_description == 'Exception description'
        assert ex.exception.extra_info is None
Example #7
0
    def test_account_not_found_raises_exception(self, mock_request):
        mock_response = mock.Mock()
        expected_dict = {
            'error_id': 'error.object.not_found',
            'description': 'Object with id 111 not found.'
        }
        mock_response.json.return_value = expected_dict
        mock_response.status_code = 404
        mock_request.return_value = mock_response

        client = ToshlClient('abcd1234')

        with self.assertRaises(ToshlException) as ex:
            client._make_request('/accounts/111', 'GET')

        assert ex.exception.status_code == 404
        assert ex.exception.error_id == 'error.object.not_found'
        assert ex.exception.error_description == 'Object with id 111 not found.'
        assert ex.exception.extra_info is None
Example #8
0
    def test_client_request_raises_exception_with_extra_info(
            self, mock_request):
        mock_response = mock.Mock()
        expected_dict = {
            'error_id': 'exception_id',
            'description': 'Exception description',
            "fields": [
                {
                    "field": "amount",
                    "error": "Amount cannot be zero."
                },
                {
                    "field": "category",
                    "error": "Please select at least one category."
                }
            ]
        }
        mock_response.json.return_value = expected_dict
        mock_response.status_code = 400
        mock_request.return_value = mock_response

        client = ToshlClient('abcd1234')

        with self.assertRaises(ToshlException) as ex:
            client._make_request('/me', 'GET')

        assert ex.exception.status_code == 400
        assert ex.exception.error_id == 'exception_id'
        assert ex.exception.error_description == 'Exception description'
        assert ex.exception.extra_info == [
            {
                "field": "amount",
                "error": "Amount cannot be zero."
            },
            {
                "field": "category",
                "error": "Please select at least one category."
            }
        ]
Example #9
0
    def test_delete_account_successful(self, mock_request):
        mock_response = mock.Mock()
        mock_response.json.return_value = ''
        mock_response.status_code = 204
        mock_request.return_value = mock_response

        client = ToshlClient('abcd1234')
        account = Account(client)
        response = account.delete('1')

        mock_request.assert_called_once_with(
            headers={'Authorization': 'Bearer abcd1234'},
            method='DELETE',
            params=None, url='https://api.toshl.com/accounts/1')
        assert response.json() == ''
Example #10
0
    def test_list_categories_successful(self, mock_request):
        mock_response = mock.Mock()
        expected_dict = [{
            "id": "42",
            "name": "Entertainment",
            "modified": "2012-09-04T13:55:15Z",
            "type": "expense",
            "deleted": False,
            "counts": {
                "entries": 21,
                "tags": 5
            }
        }, {
            "id": "58",
            "name": "Sport",
            "modified": "2012-09-04T13:55:15Z",
            "type": "expense",
            "deleted": False,
            "counts": {
                "entries": 21,
                "tags": 5
            }
        }]

        mock_response.json.return_value = expected_dict
        mock_response.status_code = 200
        mock_request.return_value = mock_response

        client = ToshlClient('abcd1234')
        category = Category(client)
        response = category.list()

        mock_request.assert_called_once_with(
            headers={'Authorization': 'Bearer abcd1234'},
            method='GET',
            params=None,
            url='https://api.toshl.com/categories')
        assert response == expected_dict
Example #11
0
    def test_list_accounts_successful(self, mock_request):
        mock_response = mock.Mock()
        expected_dict = {
            "id": "42",
            "name": "Account Test",
            "balance": 3000,
            "initial_balance": 3000,
            "currency": {
                "code": "USD",
                "rate": 1,
                "fixed": False
            },
            "median": {
                "expenses": 55,
                "incomes": 1300
            },
            "status": "active",
            "order": 0,
            "modified": "2012-09-04T13:55:15Z",
            "goal": {
                "amount": 63570,
                "start": "2013-07-01",
                "end": "2015-07-01"
            }
        }
        mock_response.json.return_value = expected_dict
        mock_response.status_code = 200
        mock_request.return_value = mock_response

        client = ToshlClient('abcd1234')
        account = Account(client)
        response = account.list()

        mock_request.assert_called_once_with(
            headers={'Authorization': 'Bearer abcd1234'},
            method='GET', params=None, url='https://api.toshl.com/accounts')
        assert response == [expected_dict]
Example #12
0
 def test_client_init(self):
     client = ToshlClient('abcd1234')
     assert client._token == 'abcd1234'
Example #13
0
 def test_category_init(self):
     client = ToshlClient('abcd1234')
     category = Category(client)
     assert category.client == client
Example #14
0
 def test_account_init(self):
     client = ToshlClient('abcd1234')
     account = Account(client)
     assert account.client == client
Example #15
0
 def test_client_has_base_url(self):
     client = ToshlClient('abcd1234')
     base_api_url = getattr(client, 'BASE_API_URL', None)
     assert base_api_url is not None
Example #16
0
 def test_client_set_auth_token_correctly(self, mock_request):
     mock_request.return_value = self.success_mocked_response
     client = ToshlClient('AAABBBCCCDDD')
     client._make_request('/me', 'GET', params={'a': 'foo1', 'b': 'foo2'})
     assert mock_request.call_args[1]['headers']['Authorization'] == \
         'Bearer AAABBBCCCDDD'
Example #17
0
import logging
import os
from pathlib import Path

from dotenv import load_dotenv

from toshl.client import ToshlClient

logging.basicConfig(level=logging.INFO,
                    format='[%(pathname)s %(levelname)s]: %(message)s')

load_dotenv()

MANUALLY_TAGGED_FROM_DATE = os.getenv('MANUALLY_TAGGED_FROM_DATE')
MANUALLY_TAGGED_TO_DATE = os.getenv('MANUALLY_TAGGED_TO_DATE')
DATA_DIR = Path(os.getenv('APP_DATA'))

TOKEN = os.getenv('TOSHL_TOKEN')
client = ToshlClient(TOKEN)

EXPENSE_CLASSIFIER_URL = os.getenv('EXPENSE_CLASSIFIER_URL') + '/invocations'
Example #18
0
 def test_entry_init(self):
     client = ToshlClient('abcd1234')
     entry = Entry(client)
     assert entry.client == client