Skip to content

Commit 29cefaf

Browse files
committed
update_contractors flag on company create
1 parent 93d099b commit 29cefaf

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

tcsocket/app/views/company.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ async def company_create(request):
1616
1717
Authentication and json parsing are done by middleware.
1818
"""
19+
data = await request.json()
20+
update_contractors = data.get('update_contractors', True)
1921
company: CompanyCreateModal = request['model']
2022
existing_company = bool(company.private_key)
2123
data = company.dict()
@@ -41,7 +43,7 @@ async def company_create(request):
4143
new_company.public_key,
4244
new_company.private_key,
4345
)
44-
if existing_company:
46+
if update_contractors and existing_company:
4547
await request.app['redis'].enqueue_job('update_contractors', company=dict(new_company))
4648
return json_response(
4749
request,

tests/test_set_company.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,61 @@ async def test_create_with_keys(cli, db_conn, worker):
7979
}
8080

8181

82+
async def test_create_with_keys_update_contractors_false(cli, db_conn, worker):
83+
data = {
84+
'name': 'foobar',
85+
'public_key': 'x' * 20,
86+
'private_key': 'y' * 40,
87+
'_request_time': int(time()),
88+
'update_contractors': False,
89+
}
90+
payload = json.dumps(data)
91+
b_payload = payload.encode()
92+
m = hmac.new(b'this is the master key', b_payload, hashlib.sha256)
93+
94+
headers = {
95+
'Webhook-Signature': m.hexdigest(),
96+
'Content-Type': 'application/json',
97+
}
98+
r = await cli.post('/companies/create', data=payload, headers=headers)
99+
assert r.status == 201
100+
curr = await db_conn.execute(sa_companies.select())
101+
result = await curr.first()
102+
assert result.name == 'foobar'
103+
await worker.run_check()
104+
assert {
105+
(cs.id, cs.first_name, cs.last_name) async for cs in await db_conn.execute(sa_contractors.select())
106+
} == set()
107+
108+
109+
async def test_create_with_keys_update_contractors_true(cli, db_conn, worker):
110+
data = {
111+
'name': 'foobar',
112+
'public_key': 'x' * 20,
113+
'private_key': 'y' * 40,
114+
'_request_time': int(time()),
115+
'update_contractors': True,
116+
}
117+
payload = json.dumps(data)
118+
b_payload = payload.encode()
119+
m = hmac.new(b'this is the master key', b_payload, hashlib.sha256)
120+
121+
headers = {
122+
'Webhook-Signature': m.hexdigest(),
123+
'Content-Type': 'application/json',
124+
}
125+
r = await cli.post('/companies/create', data=payload, headers=headers)
126+
assert r.status == 201
127+
curr = await db_conn.execute(sa_companies.select())
128+
result = await curr.first()
129+
assert result.name == 'foobar'
130+
await worker.run_check()
131+
assert {(cs.id, cs.first_name, cs.last_name) async for cs in await db_conn.execute(sa_contractors.select())} == {
132+
(22, 'James', 'Higgins'),
133+
(23, None, 'Person 2'),
134+
}
135+
136+
82137
async def test_create_not_auth(cli):
83138
data = json.dumps({'name': 'foobar', '_request_time': int(time())})
84139
headers = {'Content-Type': 'application/json'}

0 commit comments

Comments
 (0)