示例#1
0
    def test_get_404(self):
        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET, 'https://api.fortnox.se/3/instance/4',
                     json={}, status=404, content_type='application/json')

            with self.assertRaises(ObjectNotFound):
                Request.get('https://api.fortnox.se/3/instance/4')
示例#2
0
    def test_get(self):
        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET, 'https://api.fortnox.se/3/instance',
                     json={"Instances": [{"Id": 1}, {"Id": 2}]}, status=200,
                     content_type='application/json')

            response = Request.get('https://api.fortnox.se/3/instance')
            response_json = response.json()
            self.assertTrue("Instances" in response_json.keys())
            for item in response_json['Instances']:
                self.assertTrue("Id" in item.keys())

            self.assertEqual(1, len(rsps.calls))
            self.assertEqual("https://api.fortnox.se/3/instance", rsps.calls[0].request.url)
            self.assertEqual("application/json", rsps.calls[0].request.headers['Accept'])
            self.assertEqual("application/json", rsps.calls[0].request.headers['Content-Type'])
            self.assertEqual("access-token", rsps.calls[0].request.headers['Access-Token'])
            self.assertEqual("client-secret", rsps.calls[0].request.headers['Client-Secret'])

            rsps.add(responses.GET, 'https://api.fortnox.se/3/instance/1',
                     json={"Instance": {"Id": 1}}, status=200,
                     content_type='application/json')

            response = Request.get('https://api.fortnox.se/3/instance/1')
            response_json = response.json()
            self.assertTrue("Instance" in response_json.keys())
            self.assertTrue("Id" in response_json['Instance'].keys())
            self.assertEqual(1, response_json['Instance']['Id'])

            self.assertEqual(2, len(rsps.calls))
            self.assertEqual("https://api.fortnox.se/3/instance/1", rsps.calls[1].request.url)
            self.assertEqual("application/json", rsps.calls[1].request.headers['Accept'])
            self.assertEqual("application/json", rsps.calls[1].request.headers['Content-Type'])
            self.assertEqual("access-token", rsps.calls[1].request.headers['Access-Token'])
            self.assertEqual("client-secret", rsps.calls[1].request.headers['Client-Secret'])
示例#3
0
    def test_get_404(self):
        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     'https://api.fortnox.se/3/instance/4',
                     json={},
                     status=404,
                     content_type='application/json')

            with self.assertRaises(ObjectNotFound):
                Request.get('https://api.fortnox.se/3/instance/4')
示例#4
0
    def test_get(self):
        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     'https://api.fortnox.se/3/instance',
                     json={"Instances": [{
                         "Id": 1
                     }, {
                         "Id": 2
                     }]},
                     status=200,
                     content_type='application/json')

            response = Request.get('https://api.fortnox.se/3/instance')
            response_json = response.json()
            self.assertTrue("Instances" in response_json.keys())
            for item in response_json['Instances']:
                self.assertTrue("Id" in item.keys())

            self.assertEqual(1, len(rsps.calls))
            self.assertEqual("https://api.fortnox.se/3/instance",
                             rsps.calls[0].request.url)
            self.assertEqual("application/json",
                             rsps.calls[0].request.headers['Accept'])
            self.assertEqual("application/json",
                             rsps.calls[0].request.headers['Content-Type'])
            self.assertEqual("access-token",
                             rsps.calls[0].request.headers['Access-Token'])
            self.assertEqual("client-secret",
                             rsps.calls[0].request.headers['Client-Secret'])

            rsps.add(responses.GET,
                     'https://api.fortnox.se/3/instance/1',
                     json={"Instance": {
                         "Id": 1
                     }},
                     status=200,
                     content_type='application/json')

            response = Request.get('https://api.fortnox.se/3/instance/1')
            response_json = response.json()
            self.assertTrue("Instance" in response_json.keys())
            self.assertTrue("Id" in response_json['Instance'].keys())
            self.assertEqual(1, response_json['Instance']['Id'])

            self.assertEqual(2, len(rsps.calls))
            self.assertEqual("https://api.fortnox.se/3/instance/1",
                             rsps.calls[1].request.url)
            self.assertEqual("application/json",
                             rsps.calls[1].request.headers['Accept'])
            self.assertEqual("application/json",
                             rsps.calls[1].request.headers['Content-Type'])
            self.assertEqual("access-token",
                             rsps.calls[1].request.headers['Access-Token'])
            self.assertEqual("client-secret",
                             rsps.calls[1].request.headers['Client-Secret'])
示例#5
0
    def test_post_with_relative_url(self):
        with responses.RequestsMock() as rsps:
            rsps.add(responses.POST,
                     'https://api.fortnox.se/3/instance',
                     json={"Instance": {
                         "Id": 1
                     }},
                     status=200,
                     content_type='application/json')

            response = Request.post('/instance', {"Id": 1})
            response_json = response.json()
            self.assertTrue("Instance" in response_json.keys())
            self.assertTrue("Id" in response_json['Instance'].keys())
            self.assertEqual(1, response_json['Instance']['Id'])

            self.assertEqual(1, len(rsps.calls))
            self.assertEqual("https://api.fortnox.se/3/instance",
                             rsps.calls[0].request.url)
            self.assertEqual("application/json",
                             rsps.calls[0].request.headers['Accept'])
            self.assertEqual("application/json",
                             rsps.calls[0].request.headers['Content-Type'])
            self.assertEqual("access-token",
                             rsps.calls[0].request.headers['Access-Token'])
            self.assertEqual("client-secret",
                             rsps.calls[0].request.headers['Client-Secret'])
示例#6
0
    def list(cls, params=None):
        return_list = []
        search_params = {}
        done = False

        if params:
            for key in params.keys():
                if key in cls.valid_search_params:
                    search_params[key] = params[key]

        while not done:
            response = Request.get(cls.item_url, search_params)
            content = response.json()
            logger.debug(content)
            for item in content['FinancialYears']:
                return_list.append(FinancialYear(item))

            if content['MetaInformation']['@TotalPages'] == content[
                    'MetaInformation']['@CurrentPage']:
                done = True
            else:
                if 'page' in params.keys():
                    done = True
                elif 'limit' in params.keys() and int(
                        params['limit']) == len(return_list):
                    done = True
                elif content['MetaInformation']['@TotalPages'] < content[
                        'MetaInformation']['@CurrentPage'] + 1:
                    search_params['page'] = content['MetaInformation'][
                        '@CurrentPage'] + 1
                else:
                    done = True

        return return_list
示例#7
0
    def create(self):
        response = Request.post(self.item_url, self.to_dict())
        content = response.json()
        logger.debug(content)

        self._update(VoucherSeries(content['VoucherSeries']))

        return self
示例#8
0
    def create(self):
        response = Request.post(self.item_url, self.to_dict())
        content = response.json()
        logger.debug(content)

        self._update(FinancialYear(content['FinancialYear']))

        return self
示例#9
0
    def create(self):
        response = Request.post(self.item_url, self.to_dict())
        content = response.json()
        logger.debug(content)

        self._update(VoucherSeries(content['VoucherSeries']))

        return self
示例#10
0
    def list(cls):
        return_list = []

        response = Request.get(cls.item_url)
        content = response.json()
        logger.debug(content)
        for item in content['VoucherSeriesCollection']:
            return_list.append(VoucherSeries(item))

        return return_list
示例#11
0
    def list(cls):
        return_list = []

        response = Request.get(cls.item_url)
        content = response.json()
        logger.debug(content)
        for item in content['VoucherSeriesCollection']:
            return_list.append(VoucherSeries(item))

        return return_list
示例#12
0
    def get(cls, code):
        try:
            response = Request.get("%s/%s" % (cls.item_url, code))
            content = response.json()
            logger.debug(content)

            return VoucherSeries(content['VoucherSeries'])

        except ObjectNotFound as e:
            e.message = "Unable to find Voucher series with code: %s" % code
            raise e
示例#13
0
    def get(cls, url):
        try:
            response = Request.get(url)
            content = response.json()
            logger.debug(content)

            return Voucher(content['Voucher'])

        except ObjectNotFound as e:
            e.message = "Unable to find Voucher with url: %s" % url
            raise e
示例#14
0
    def get(cls, id):
        try:
            response = Request.get("%s/%s" % (cls.item_url, id))
            content = response.json()
            logger.debug(content)

            return FinancialYear(content['FinancialYear'])

        except ObjectNotFound as e:
            e.message = "Unable to find Financial year with id: %s" % id
            raise e
示例#15
0
    def get(cls, code):
        try:
            response = Request.get("%s/%s" % (cls.item_url, code))
            content = response.json()
            logger.debug(content)

            return VoucherSeries(content['VoucherSeries'])

        except ObjectNotFound as e:
            e.message = "Unable to find Voucher series with code: %s" % code
            raise e
示例#16
0
    def save(self):
        try:
            response = Request.put("%s/%s" % (self.item_url, self.code), self.to_dict())
            content = response.json()
            logger.debug(content)

            self._update(VoucherSeries(content['VoucherSeries']))

            return self

        except ObjectNotFound as e:
            e.message = "Unable to find Voucher series with code: %s" % self.code
            raise e
示例#17
0
    def save(self):
        try:
            response = Request.put("%s/%s" % (self.item_url, self.code),
                                   self.to_dict())
            content = response.json()
            logger.debug(content)

            self._update(VoucherSeries(content['VoucherSeries']))

            return self

        except ObjectNotFound as e:
            e.message = "Unable to find Voucher series with code: %s" % self.code
            raise e
示例#18
0
    def test_post_with_relative_url(self):
        with responses.RequestsMock() as rsps:
            rsps.add(responses.POST, 'https://api.fortnox.se/3/instance',
                     json={"Instance": {"Id": 1}}, status=200,
                     content_type='application/json')

            response = Request.post('/instance', {"Id": 1})
            response_json = response.json()
            self.assertTrue("Instance" in response_json.keys())
            self.assertTrue("Id" in response_json['Instance'].keys())
            self.assertEqual(1, response_json['Instance']['Id'])

            self.assertEqual(1, len(rsps.calls))
            self.assertEqual("https://api.fortnox.se/3/instance", rsps.calls[0].request.url)
            self.assertEqual("application/json", rsps.calls[0].request.headers['Accept'])
            self.assertEqual("application/json", rsps.calls[0].request.headers['Content-Type'])
            self.assertEqual("access-token", rsps.calls[0].request.headers['Access-Token'])
            self.assertEqual("client-secret", rsps.calls[0].request.headers['Client-Secret'])
示例#19
0
    def get(cls, voucher_series_code, voucher_number, financial_year=None, financial_year_date=None):
        try:
            params = {}

            if financial_year:
                params['financialyear'] = financial_year

            if financial_year_date:
                params['financialyeardate'] = financial_year_date

            response = Request.get("%s/%s/%s" % (cls.item_url, voucher_series_code, voucher_number), params=params)

            content = response.json()
            logger.debug(content)

            return Voucher(content['Voucher'])

        except ObjectNotFound as e:
            e.message = "Unable to find Voucher with voucher series code: %s, voucher number: %s" % \
                        (voucher_series_code, voucher_number)
            raise e
示例#20
0
    def test_put(self):
        with responses.RequestsMock() as rsps:
            rsps.add(responses.PUT, 'https://api.fortnox.se/3/instance/1',
                     json={"Instance": {"Id": 1, "Name": "Test name"}}, status=200,
                     content_type='application/json')

            response = Request.put('https://api.fortnox.se/3/instance/1', {"Id": 1, "Name": "Test name"})
            response_json = response.json()
            self.assertTrue("Instance" in response_json.keys())
            self.assertTrue("Id" in response_json['Instance'].keys())
            self.assertEqual(1, response_json['Instance']['Id'])
            self.assertTrue("Name" in response_json['Instance'].keys())
            self.assertEqual("Test name", response_json['Instance']['Name'])

            self.assertEqual(1, len(rsps.calls))
            self.assertEqual("https://api.fortnox.se/3/instance/1", rsps.calls[0].request.url)
            self.assertEqual(json.dumps({"Id": 1, "Name": "Test name"}), rsps.calls[0].request.body)
            self.assertEqual("application/json", rsps.calls[0].request.headers['Accept'])
            self.assertEqual("application/json", rsps.calls[0].request.headers['Content-Type'])
            self.assertEqual("access-token", rsps.calls[0].request.headers['Access-Token'])
            self.assertEqual("client-secret", rsps.calls[0].request.headers['Client-Secret'])
示例#21
0
    def list(cls, financial_year=None, financial_year_date=None, params={}):
        return_list = []
        search_params = {}
        done = False

        if params:
            for key in params.keys():
                if key in cls.valid_search_params:
                    search_params[key] = params[key]

        if financial_year:
            search_params['financialyear'] = financial_year

        if financial_year_date:
            search_params['financialyeardate'] = financial_year_date

        while not done:
            response = Request.get(cls.item_url, search_params)
            content = response.json()
            logger.debug(content)
            for item in content['Vouchers']:
                return_list.append(Voucher(item))

            if content['MetaInformation']['@TotalPages'] == content['MetaInformation']['@CurrentPage'] or \
                content['MetaInformation']['@TotalPages'] == 0:
                done = True
            else:
                if 'page' in params.keys():
                    done = True
                elif 'limit' in params.keys() and int(params['limit']) == len(return_list):
                    done = True
                elif content['MetaInformation']['@TotalPages'] < content['MetaInformation']['@CurrentPage'] + 1:
                    search_params['page'] = content['MetaInformation']['@CurrentPage'] + 1
                else:
                    done = True

        return return_list
示例#22
0
    def test_put(self):
        with responses.RequestsMock() as rsps:
            rsps.add(responses.PUT,
                     'https://api.fortnox.se/3/instance/1',
                     json={"Instance": {
                         "Id": 1,
                         "Name": "Test name"
                     }},
                     status=200,
                     content_type='application/json')

            response = Request.put('https://api.fortnox.se/3/instance/1', {
                "Id": 1,
                "Name": "Test name"
            })
            response_json = response.json()
            self.assertTrue("Instance" in response_json.keys())
            self.assertTrue("Id" in response_json['Instance'].keys())
            self.assertEqual(1, response_json['Instance']['Id'])
            self.assertTrue("Name" in response_json['Instance'].keys())
            self.assertEqual("Test name", response_json['Instance']['Name'])

            self.assertEqual(1, len(rsps.calls))
            self.assertEqual("https://api.fortnox.se/3/instance/1",
                             rsps.calls[0].request.url)
            self.assertEqual(json.dumps({
                "Id": 1,
                "Name": "Test name"
            }), rsps.calls[0].request.body)
            self.assertEqual("application/json",
                             rsps.calls[0].request.headers['Accept'])
            self.assertEqual("application/json",
                             rsps.calls[0].request.headers['Content-Type'])
            self.assertEqual("access-token",
                             rsps.calls[0].request.headers['Access-Token'])
            self.assertEqual("client-secret",
                             rsps.calls[0].request.headers['Client-Secret'])