Skip to content

Commit 82fb0d2

Browse files
committed
input check, test users has units to use
1 parent 56c3d0b commit 82fb0d2

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

code/backend/database/db_repo.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from binascii import hexlify
33
from datetime import datetime
4-
4+
import numpy as np
55
from database import db_models, db
66

77

@@ -20,14 +20,24 @@ def init_api_key_object(api_key: str = None) -> db_models.ApiKey:
2020
return new_api_key
2121

2222

23-
def create_user(first_name: str, last_name: str, email: str, api_key: str = None) -> None:
23+
def create_user(first_name: str, last_name: str, email: str, api_key: str = None,
24+
generate_random_units_for_testing: bool = False) -> None:
2425
new_api_key = init_api_key_object(api_key)
26+
27+
available_units = 0
28+
used_units = 0
29+
30+
if generate_random_units_for_testing:
31+
# This is only for testing, it should be removed
32+
available_units = np.random.randint(10, 100)
33+
used_units = np.random.randint(10, 100)
34+
2535
new_user = db_models.User(first_name=first_name,
2636
last_name=last_name,
2737
email=email,
2838
date_signed_up=datetime.now(),
29-
available_units=0,
30-
used_units=0,
39+
available_units=available_units,
40+
used_units=used_units,
3141
api_key=new_api_key, )
3242
db.db_session.add(new_user)
3343
db.db_session.commit()

code/backend/rest_api.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818

1919
def not_found_error(msg):
20+
"""
21+
This is used for any type of error right now
22+
(TODO)
23+
"""
24+
2025
status_code = 404
2126
message = {
2227
'status': status_code,
@@ -49,7 +54,10 @@ def add_test_users(nb_of_users: int):
4954
nb_of_users = int(nb_of_users)
5055

5156
for i in range(nb_of_users):
52-
db_repo.create_user("Test", "User{0}".format(i), "just{0}@test.com".format(i))
57+
db_repo.create_user("Test",
58+
"User{0}".format(i),
59+
"just{0}@test.com".format(i),
60+
generate_random_units_for_testing=True)
5361

5462
app.logger.info("Test users are created")
5563

@@ -58,17 +66,29 @@ def add_test_users(nb_of_users: int):
5866

5967
@app.route("/microservice/sample_service", methods=["POST"])
6068
def use_sample_service():
69+
"""
70+
Sample endpoint for the service
71+
"""
72+
6173
req_json = request.get_json()
6274

6375
try:
6476
# For authentication
6577
api_key_str = req_json["api_key"]
6678
# For the service
67-
size = int(req_json["size"])
79+
size = req_json["size"]
6880
except KeyError:
6981
app.logger.error("Could not find the necessary keys in the request's body")
7082
return not_found_error("Required keys are not found in POST's body")
7183

84+
try:
85+
size = int(size)
86+
except Exception as e:
87+
return not_found_error("Wrong input type. It should be A Number.")
88+
89+
if size <= 0:
90+
return not_found_error("Number input should be greater than 0")
91+
7292
user = db_repo.find_user_by_api_key(api_key_str)
7393

7494
if user is None:

code/frontend/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ <h3 class="mb-0 text-white">Service</h3>
4747
<div class="input-group-prepend">
4848
<span class="input-group-text">#</span>
4949
</div>
50-
<input id="numberOfCharsInput" class="form-control" placeholder="number of characters"
50+
<input id="numberOfCharsInput" type="number" class="form-control" placeholder="number of characters"
5151
required>
5252
</div>
5353
<div class="input-group mb-3">

0 commit comments

Comments
 (0)