Skip to content

Commit

Permalink
Changed so that the dummy test service accepts any VRN, and treats the
Browse files Browse the repository at this point in the history
state for that VRN as the state from vat-data.json.  Makes testing easier.
  • Loading branch information
cybermaggedon committed Mar 18, 2022
1 parent a6db65c commit 54d5154
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
8 changes: 4 additions & 4 deletions gnucash_uk_vat/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ def to_dict(self):
obj["due"] = self.due.isoformat()
return obj
def in_range(self, start, end):
if self.status == "O":
return self.due >= start and self.due <= end
# if self.status == "O":
# return self.due >= start and self.due <= end
return self.received >= start and self.received <= end

class Liability:
Expand Down Expand Up @@ -309,5 +309,5 @@ def to_dict(self):
def from_json(s):
data = json.loads(s)
return VATData.from_dict(data)
def add_return(self, vrn, rtn):
self.data[vrn].add_return(rtn)
# def add_return(self, vrn, rtn):
# self.data[vrn].add_return(rtn)
44 changes: 26 additions & 18 deletions scripts/vat-test-service
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,46 @@ from datetime import date, datetime, timedelta
import sys
import argparse
from urllib.parse import urlencode, quote_plus
import secrets
import copy

from gnucash_uk_vat.model import *

class Api:

def __init__(self, data, listen="0.0.0.0:8080", headers=False):
def __init__(self, template, listen="0.0.0.0:8080", headers=False):
self.listen = listen
self.data = data
self.template = template
self.data = {}
self.headers = headers
self.access_token = secrets.token_hex(16)

def dump_headers(self, request):

for k in request.headers:
print("%s: %s" % (k, request.headers[k]))
print()

def get_data(self, vrn):
if vrn not in self.data:
self.data[vrn] = copy.deepcopy(self.template)
return self.data[vrn]

async def get_return(self, request):

if self.headers: self.dump_headers(request)

key = request.match_info["periodKey"]
vrn = request.match_info["vrn"]

if vrn in self.data.data:
user = self.data.data[vrn]
for v in user.returns:
if v.periodKey == key:
return web.Response(
body=json.dumps(v.to_dict(), indent=4),
content_type="application/json"
)
user = self.get_data(vrn)

for v in user.returns:
if v.periodKey == key:
return web.Response(
body=json.dumps(v.to_dict(), indent=4),
content_type="application/json"
)

raise web.HTTPBadRequest()

Expand Down Expand Up @@ -77,7 +86,7 @@ class Api:
pass

try:
obls = self.data.data[vrn].obligations
obls = self.get_data(vrn).obligations
except:
raise web.HTTPBadRequest()

Expand Down Expand Up @@ -117,7 +126,7 @@ class Api:

vrn = request.match_info["vrn"]

liabilities = self.data.data[vrn].liabilities
liabilities = self.get_data(vrn).liabilities

resp = {
"liabilities": [
Expand All @@ -143,7 +152,7 @@ class Api:

vrn = request.match_info["vrn"]

payments = self.data.data[vrn].payments
payments = self.get_data(vrn).payments

resp = {
"payments": [
Expand All @@ -163,15 +172,14 @@ class Api:

body = await request.json()

print(request.headers)
rtn = Return.from_dict(body)

if not rtn.finalised:
raise web.HTTPBadRequest()

vrn = request.match_info["vrn"]

self.data.add_return(vrn, rtn)
self.get_data(vrn).add_return(rtn)

resp = {
"processingDate": datetime.utcnow().isoformat(),
Expand All @@ -188,7 +196,7 @@ class Api:
async def get_token(self, request):

token = {
"access_token": "12345",
"access_token": self.access_token,
"refresh_token": "67890",
"token_type": "bearer",
"expires_in": 1440
Expand Down Expand Up @@ -317,8 +325,8 @@ parser.add_argument('--dump-headers', '-H', action='store_true',
args = parser.parse_args(sys.argv[1:])

data = open(args.data).read()
v = VATData.from_json(data)
a = Api(v, args.listen, args.dump_headers)
template = VATData.from_json(data).data["TEMPLATE"]
a = Api(template, args.listen, args.dump_headers)

loop = asyncio.get_event_loop()
loop.run_until_complete(a.run())
Expand Down
10 changes: 8 additions & 2 deletions vat-data.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"123456789": {
"TEMPLATE": {
"payments": [
{
"amount": 600,
Expand Down Expand Up @@ -63,7 +63,6 @@
"periodKey": "#001",
"start": "2020-05-29",
"end": "2020-08-26",
"received": "2020-09-26",
"due": "2020-09-26"
},
{
Expand All @@ -72,6 +71,13 @@
"start": "2020-08-27",
"end": "2020-11-25",
"due": "2020-12-25"
},
{
"status": "O",
"periodKey": "#003",
"start": "2021-02-08",
"end": "2021-04-30",
"due": "2021-06-07"
}
]
}
Expand Down

0 comments on commit 54d5154

Please sign in to comment.