Skip to content

Commit 1db51d6

Browse files
feat: TT-418 crud customer v2 (#361)
* feat: TT-418 create added * feat: TT-418 create tests added * feat: TT-418 create get_all, get_by_id and delete methods * feat: TT-418 solve conflicts after add tests * feat: TT-418 crud and tests added * fix: TT-418 renamed tests and problems solved * fix: TT-418 code smell resolved * fix: TT-418 serverless corrected and github files retored Co-authored-by: Gabriel Cobeña <[email protected]>
1 parent f9e1403 commit 1db51d6

24 files changed

+731
-25
lines changed

.github/workflows/time-tracker-v1-on-pull-request-workflow.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ jobs:
2626
python -m pip install --upgrade pip
2727
pip install -r requirements/time_tracker_api/dev.txt
2828
pip install -r requirements/time_tracker_events/dev.txt
29-
3029
- name: Login to azure
3130
uses: Azure/login@v1
3231
with:
@@ -54,7 +53,6 @@ jobs:
5453
AZURE_STORAGE_ACCOUNT_KEY: ${{ steps.timeTrackerAzureVault.outputs.AZURE-STORAGE-ACCOUNT-KEY }}
5554
run: |
5655
pytest tests
57-
5856
- name: Test the build of the app
5957
run: |
60-
docker build .
58+
docker build .

.github/workflows/time-tracker-v1-on-push-workflow.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ jobs:
2626
python -m pip install --upgrade pip
2727
pip install -r requirements/time_tracker_api/dev.txt
2828
pip install -r requirements/time_tracker_events/dev.txt
29-
3029
- name: Login to azure
3130
uses: Azure/login@v1
3231
with:
@@ -54,7 +53,6 @@ jobs:
5453
AZURE_STORAGE_ACCOUNT_KEY: ${{ steps.timeTrackerAzureVault.outputs.AZURE-STORAGE-ACCOUNT-KEY }}
5554
run: |
5655
pytest tests
57-
5856
- name: Login to docker registry
5957
uses: azure/docker-login@v1
6058
with:
@@ -64,4 +62,4 @@ jobs:
6462
- name: Build and push image
6563
run: |
6664
docker build . -t ${{ secrets.REGISTRY_LOGIN_SERVER }}/timetrackerapi:${{ github.sha }}
67-
docker push ${{ secrets.REGISTRY_LOGIN_SERVER }}/timetrackerapi:${{ github.sha }}
65+
docker push ${{ secrets.REGISTRY_LOGIN_SERVER }}/timetrackerapi:${{ github.sha }}

V2/serverless.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ package:
3636
- '!.gitignore'
3737
- '!.git/**'
3838

39+
#region start Functions
40+
3941
functions:
42+
43+
#region Start Functions Activities
44+
4045
get_activities:
4146
handler: time_tracker/activities/interface.get_activities
4247
events:
@@ -77,6 +82,10 @@ functions:
7782
route: activities/
7883
authLevel: anonymous
7984

85+
#endregion End Functions Activities
86+
87+
#region Start Functions Time-Entries
88+
8089
create_time_entry:
8190
handler: time_tracker/time_entries/interface.create_time_entry
8291
events:
@@ -127,6 +136,10 @@ functions:
127136
route: time-entries/latest/
128137
authLevel: anonymous
129138

139+
#endregion End Functions Time-Entries
140+
141+
#region Start Functions Customers
142+
130143
create_customer:
131144
handler: time_tracker/customers/interface.create_customer
132145
events:
@@ -137,6 +150,40 @@ functions:
137150
route: customers/
138151
authLevel: anonymous
139152

153+
get_customers:
154+
handler: time_tracker/customers/interface.get_customers
155+
events:
156+
- http: true
157+
x-azure-settings:
158+
methods:
159+
- GET
160+
route: customers/{id:?}
161+
authLevel: anonymous
162+
163+
update_customer:
164+
handler: time_tracker/customers/interface.update_customer
165+
events:
166+
- http: true
167+
x-azure-settings:
168+
methods:
169+
- PUT
170+
route: customers/{id}
171+
authLevel: anonymous
172+
173+
delete_customer:
174+
handler: time_tracker/customers/interface.delete_customer
175+
events:
176+
- http: true
177+
x-azure-settings:
178+
methods:
179+
- DELETE
180+
route: customers/{id}
181+
authLevel: anonymous
182+
183+
#endregion End Functions Customers
184+
185+
#region Start Functions Projects
186+
140187
get_projects:
141188
handler: time_tracker/projects/interface.get_projects
142189
events:
@@ -177,3 +224,7 @@ functions:
177224
route: projects/
178225

179226
authLevel: anonymous
227+
228+
#endregion End Functions Projects
229+
230+
#endregion End Functions

V2/tests/api/azure/customer_azure_endpoints_test.py

Lines changed: 169 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from http import HTTPStatus
12
import json
23
from faker import Faker
34

@@ -8,7 +9,7 @@
89
CUSTOMER_URL = "/api/customers/"
910

1011

11-
def test__customer_azure_endpoint__creates_a_customer__when_customer_has_all_necesary_attributes(
12+
def test__create_customer_azure_endpoint__creates_a_customer__when_customer_has_all_necesary_attributes(
1213
customer_factory
1314
):
1415
customer_body = customer_factory().__dict__
@@ -24,11 +25,11 @@ def test__customer_azure_endpoint__creates_a_customer__when_customer_has_all_nec
2425
customer_json_data = json.loads(response.get_body())
2526
customer_body['id'] = customer_json_data['id']
2627

27-
assert response.status_code == 201
28+
assert response.status_code == HTTPStatus.CREATED
2829
assert customer_json_data == customer_body
2930

3031

31-
def test__customer_azure_endpoint__returns_a_status_400__when_dont_recieve_all_necessary_attributes():
32+
def test__create_customer_azure_endpoint__returns_a_status_400__when_dont_recieve_all_necessary_attributes():
3233
customer_to_insert = {
3334
"id": None,
3435
"name": Faker().user_name(),
@@ -45,5 +46,169 @@ def test__customer_azure_endpoint__returns_a_status_400__when_dont_recieve_all_n
4546

4647
response = azure_customers._create_customer.create_customer(req)
4748

48-
assert response.status_code == 400
49+
assert response.status_code == HTTPStatus.BAD_REQUEST
4950
assert response.get_body() == b'Invalid format or structure of the attributes of the customer'
51+
52+
53+
def test__delete_customer_azure_endpoint__returns_a_customer_with_true_deleted__when_its_id_is_found(
54+
test_db, customer_factory, insert_customer
55+
):
56+
customer_preinsert = customer_factory()
57+
inserted_customer = insert_customer(customer_preinsert, test_db).__dict__
58+
59+
req = func.HttpRequest(
60+
method='DELETE',
61+
body=None,
62+
url=CUSTOMER_URL,
63+
route_params={"id": inserted_customer["id"]},
64+
)
65+
66+
response = azure_customers._delete_customer.delete_customer(req)
67+
customer_json_data = json.loads(response.get_body().decode("utf-8"))
68+
69+
assert response.status_code == HTTPStatus.OK
70+
assert customer_json_data['deleted'] is True
71+
72+
73+
def test__delete_customer_azure_endpoint__returns_not_found__when_its_id_is_not_found():
74+
req = func.HttpRequest(
75+
method='DELETE',
76+
body=None,
77+
url=CUSTOMER_URL,
78+
route_params={"id": Faker().pyint()},
79+
)
80+
81+
response = azure_customers._delete_customer.delete_customer(req)
82+
83+
assert response.status_code == HTTPStatus.NOT_FOUND
84+
assert response.get_body() == b'Not found'
85+
86+
87+
def test__update_customer_azure_endpoint__returns_an_updated_customer__when_customer_has_all_necesary_attributes(
88+
test_db, customer_factory, insert_customer
89+
):
90+
existent_customer = customer_factory()
91+
inserted_customer = insert_customer(existent_customer, test_db).__dict__
92+
93+
inserted_customer["description"] = Faker().sentence()
94+
95+
body = json.dumps(inserted_customer).encode("utf-8")
96+
req = func.HttpRequest(
97+
method='PUT',
98+
body=body,
99+
url=CUSTOMER_URL,
100+
route_params={"id": inserted_customer["id"]},
101+
)
102+
103+
response = azure_customers._update_customer.update_customer(req)
104+
customer_json_data = json.loads(response.get_body())
105+
106+
assert response.status_code == HTTPStatus.OK
107+
assert customer_json_data == inserted_customer
108+
109+
110+
def test__update_customer_azure_endpoint__returns_update_a_customer__when_customer_has_all_necesary_attributes(
111+
customer_factory
112+
):
113+
existent_customer = customer_factory().__dict__
114+
115+
body = json.dumps(existent_customer).encode("utf-8")
116+
req = func.HttpRequest(
117+
method='PUT',
118+
body=body,
119+
url=CUSTOMER_URL,
120+
route_params={"id": Faker().pyint()},
121+
)
122+
123+
response = azure_customers._update_customer.update_customer(req)
124+
125+
assert response.status_code == HTTPStatus.CONFLICT
126+
assert response.get_body() == b'This customer does not exist or is duplicated'
127+
128+
129+
def test__update_customer_azure_endpoint__returns_invalid_format__when_customer_doesnt_have_all_necesary_attributes(
130+
customer_factory, insert_customer, test_db
131+
):
132+
existent_customer = customer_factory()
133+
inserted_customer = insert_customer(existent_customer, test_db).__dict__
134+
135+
inserted_customer.pop("name")
136+
137+
body = json.dumps(inserted_customer).encode("utf-8")
138+
req = func.HttpRequest(
139+
method='PUT',
140+
body=body,
141+
url=CUSTOMER_URL,
142+
route_params={"id": inserted_customer["id"]},
143+
)
144+
145+
response = azure_customers._update_customer.update_customer(req)
146+
147+
assert response.status_code == HTTPStatus.BAD_REQUEST
148+
assert response.get_body() == b'Invalid format or structure of the attributes of the customer'
149+
150+
151+
def test__delete_customers_azure_endpoint__returns_a_status_code_400__when_customer_recive_invalid_id(
152+
):
153+
req = func.HttpRequest(
154+
method="DELETE",
155+
body=None,
156+
url=CUSTOMER_URL,
157+
route_params={"id": "invalid id"},
158+
)
159+
160+
response = azure_customers._delete_customer.delete_customer(req)
161+
162+
assert response.status_code == HTTPStatus.BAD_REQUEST
163+
assert response.get_body() == b'Invalid Format ID'
164+
165+
166+
def test__customers_azure_endpoint__returns_all_customers(
167+
test_db, customer_factory, insert_customer
168+
):
169+
customer_to_insert = customer_factory()
170+
171+
inserted_customer = insert_customer(customer_to_insert, test_db).__dict__
172+
173+
req = func.HttpRequest(method='GET', body=None, url=CUSTOMER_URL)
174+
response = azure_customers._get_customers.get_customers(req)
175+
customers_json_data = response.get_body().decode("utf-8")
176+
customer_list = json.loads(customers_json_data)
177+
178+
assert response.status_code == HTTPStatus.OK
179+
assert customers_json_data <= json.dumps(inserted_customer)
180+
assert customer_list.pop() == inserted_customer
181+
182+
183+
def test__customer_azure_endpoint__returns_a_customer__when_customer_matches_its_id(
184+
test_db, customer_factory, insert_customer
185+
):
186+
existent_customer = customer_factory()
187+
inserted_customer = insert_customer(existent_customer, test_db).__dict__
188+
189+
req = func.HttpRequest(
190+
method='GET',
191+
body=None,
192+
url=CUSTOMER_URL,
193+
route_params={"id": inserted_customer["id"]},
194+
)
195+
196+
response = azure_customers._get_customers.get_customers(req)
197+
customer_json_data = response.get_body().decode("utf-8")
198+
199+
assert response.status_code == HTTPStatus.OK
200+
assert customer_json_data == json.dumps(inserted_customer)
201+
202+
203+
def test__customer_azure_endpoint__returns_invalid_id__when_customer_not_matches_its_id():
204+
req = func.HttpRequest(
205+
method='GET',
206+
body=None,
207+
url=CUSTOMER_URL,
208+
route_params={"id": "Invalid ID"},
209+
)
210+
211+
response = azure_customers._get_customers.get_customers(req)
212+
213+
assert response.status_code == HTTPStatus.BAD_REQUEST
214+
assert response.get_body() == b'The id has an invalid format'

V2/tests/fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from faker import Faker
33

44
import time_tracker.activities._domain as activities_domain
5-
import time_tracker.activities._infrastructure as activities_infrastructure
65
import time_tracker.time_entries._domain as time_entries_domain
76
import time_tracker.customers._domain as customers_domain
7+
import time_tracker.activities._infrastructure as activities_infrastructure
88
import time_tracker.customers._infrastructure as customers_infrastructure
99
import time_tracker.projects._domain as projects_domain
1010
from time_tracker._infrastructure import DB

0 commit comments

Comments
 (0)