Skip to content

Commit

Permalink
Add joker methods
Browse files Browse the repository at this point in the history
  • Loading branch information
leoKagohara-Stark committed May 20, 2024
1 parent 4318460 commit 520dbb5
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 1 deletion.
1 change: 1 addition & 0 deletions starkbank/Request/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .__request import (get, post, patch, put, delete)
51 changes: 51 additions & 0 deletions starkbank/Request/__request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from ..utils import rest


def get(path, query=None, user=None):
"""# Retrieve any StarkBank resource
Receive a json of resources previously created in the Stark Bank API
## Parameters (required):
- path [string]: StarkBank resource's route. ex: "/invoice/"
- query [dict, default None]: Query parameters. ex: {"limit": 1, "status": paid}
## Parameters (optional):
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call
## Return:
- generator of Invoice objects with updated attributes
"""
return rest.get_raw(
path=path,
query=query,
user=user
)


def post(path, body=None, user=None):
return rest.post_raw(
path=path,
payload=body,
user=user
)


def patch(path, body=None, user=None):
return rest.patch_raw(
path=path,
payload=body,
user=user
)


def put(path, body=None, user=None):
return rest.put_raw(
path=path,
payload=body,
user=user
)


def delete(path, body=None, user=None):
return rest.delete_raw(
path=path,
payload=body,
user=user
)
3 changes: 3 additions & 0 deletions starkbank/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,6 @@

from . import splitprofile
from .splitprofile.__splitprofile import SplitProfile

from . import Request

5 changes: 4 additions & 1 deletion starkbank/utils/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@
get_raw = set_relay(rest.get_raw)
post_raw = set_relay(rest.post_raw)
patch_id = set_relay(rest.patch_id)
put_multi = set_relay(rest.put_multi)
put_raw = set_relay(rest.put_raw)
patch_raw = set_relay(rest.patch_raw)
delete_raw = set_relay(rest.delete_raw)

199 changes: 199 additions & 0 deletions tests/sdk/test_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import starkbank
from datetime import datetime, timedelta
import time
from unittest import TestCase, main
from tests.utils.date import randomPastDate
from tests.utils.user import exampleProject

import json

starkbank.user = exampleProject


class TestJokerGet(TestCase):

def test_get(self):
example_id = starkbank.Request.get(
path=f'/invoice/',
query={"limit": 1, "status": "paid"},
)["invoices"][0]["id"]

request = starkbank.Request.get(
path=f'/invoice/{example_id}',
user=exampleProject
)
self.assertEqual(request["invoice"]["id"], example_id)

def test_get_pdf(self):
example_id = starkbank.Request.get(
path=f'/invoice/',
query={"limit": 10, "status": "paid"}
)["invoices"][0]["id"]
pdf = starkbank.Request.get(
path=f'/invoice/{example_id}/pdf',
)

self.assertGreater(len(pdf), 1000)

def test_get_qrcode(self):
example_id = starkbank.Request.get(
path=f'/invoice/',
query={"limit": 10, "status": "paid"}
)["invoices"][0]["id"]

qrcode = starkbank.Request.get(
path=f'/invoice/{example_id}/qrcode',
query={"size": 15},
)
self.assertGreater(len(qrcode), 1000)

def test_get_devolution_receipt(self):
example_id = starkbank.Request.get(
path=f'/deposit/log/',
query={"limit": 1, "types": "reversed"}
)
example_id = example_id["logs"][0]["id"]
devolution_pdf = starkbank.Request.get(
path=f'/deposit/log/{example_id}/pdf/',
)
self.assertGreater(len(devolution_pdf), 1000)

def test_get_page(self):
after = randomPastDate(days=10)
before = datetime.today()
request = starkbank.Request.get(
path=f'/invoice/',
query={
"limit": 10,
"after": after.strftime("%Y-%m-%d"),
"before": before.strftime("%Y-%m-%d"),
"status": "paid"
}
)
for item in request["invoices"]:
self.assertTrue(after.date() <= datetime.strptime(item["created"], "%Y-%m-%dT%H:%M:%S.%f%z").date() <= (before + timedelta(hours=3)).date())
self.assertEqual(10, len(request["invoices"]))

def test_get_pagination(self):
after = randomPastDate(days=10)
before = datetime.today()
total_items = 0
cursor = None
i = 0
while i <= 2:
request = starkbank.Request.get(
path=f'/invoice/',
query={
"limit": 10,
"after": after.strftime("%Y-%m-%d"),
"before": before.strftime("%Y-%m-%d"),
"status": "paid",
"cursor": cursor
}
)
cursor = request["cursor"]
total_items += len(request["invoices"])
for item in request["invoices"]:
self.assertTrue(after.date() <= datetime.strptime(item["created"], "%Y-%m-%dT%H:%M:%S.%f%z").date() <= (before + timedelta(hours=3)).date())
if cursor is None:
break
i += 1
self.assertLessEqual(len(request["invoices"]), 10)
self.assertLessEqual(total_items, 30)


class TestJokerPost(TestCase):

def test_post(self):
data={
"invoices": [{
"amount": 100,
"name": "Iron Bank S.A.",
"taxId": "20.018.183/0001-80"
}]
}
request = starkbank.Request.post(
path=f'/invoice/',
body=data,
)
print(request)


class TestJokerPatch(TestCase):

def test_patch(self):
initial_state = starkbank.Request.get(
path=f'/invoice/',
query={"limit": 1, "status": "paid"}
)
example_id = initial_state["invoices"][0]["id"]
amount = initial_state["invoices"][0]["amount"]

request = starkbank.Request.patch(
path=f'/invoice/{example_id}/',
body={"amount": amount-amount},
)

final_state = starkbank.Request.get(
path=f'/invoice/{example_id}',
)
self.assertEqual(final_state["invoice"]["amount"],0)


class TestJokerPut(TestCase):

def test_put(self):
data = {
"profiles": [
{
"interval": "day",
"delay": 0
}
]
}
request = starkbank.Request.put(
path=f'/split-profile/',
body=data,
)

result = starkbank.Request.get(path=f'/split-profile/')

self.assertEqual(result["profiles"][0]["delay"], 0)
self.assertEqual(result["profiles"][0]["interval"], "day")


class TestJokerDelete(TestCase):

def test_delete(self):
future_date = datetime.now().date() + timedelta(days=10)

data = {
"transfers": [
{
"amount": 10000,
"name": "Steve Rogers",
"taxId": "330.731.970-10",
"bankCode": "001",
"branchCode": "1234",
"accountNumber": "123456-0",
"accountType": "checking",
"scheduled": future_date.strftime("%Y-%m-%d"),
"externalId": str(int(time.time() * 1000)),
}
]
}

create = starkbank.Request.post(
path=f'/transfer/',
body=data,
)

request = starkbank.Request.delete(
path=f'/transfer/{create["transfers"][0]["id"]}',
)

self.assertEqual(request['message'], 'Transfer(s) successfully deleted')


if __name__ == '__main__':
main()

0 comments on commit 520dbb5

Please sign in to comment.