Skip to content

Commit f5b37a5

Browse files
committed
Refactor and add unit tests
1 parent 2c882f1 commit f5b37a5

File tree

5 files changed

+528
-90
lines changed

5 files changed

+528
-90
lines changed

lib/pbench/server/api/__init__.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,29 @@ def register_endpoints(api, app):
3535
to make the APIs active."""
3636

3737
api.add_resource(
38-
Upload, f"{app.config['REST_URI']}/upload/ctrl/<string:controller>", resource_class_args=(app, api)
38+
Upload,
39+
f"{app.config['REST_URI']}/upload/ctrl/<string:controller>",
40+
resource_class_args=(app, api),
41+
)
42+
api.add_resource(
43+
HostInfo, f"{app.config['REST_URI']}/host_info", resource_class_args=(app, api)
44+
)
45+
api.add_resource(
46+
Elasticsearch,
47+
f"{app.config['REST_URI']}/elasticsearch",
48+
resource_class_args=(app, api),
49+
)
50+
api.add_resource(
51+
GraphQL, f"{app.config['REST_URI']}/graphql", resource_class_args=(app, api)
3952
)
40-
api.add_resource(HostInfo, f"{app.config['REST_URI']}/host_info", resource_class_args=(app, api))
41-
api.add_resource(Elasticsearch, f"{app.config['REST_URI']}/elasticsearch", resource_class_args=(app, api))
42-
api.add_resource(GraphQL, f"{app.config['REST_URI']}/graphql", resource_class_args=(app, api))
4353
api.add_resource(
4454
RegisterUser, f"{app.config['REST_URI']}/user", resource_class_args=(app, api)
4555
)
4656
api.add_resource(
4757
LoginAPI, f"{app.config['REST_URI']}/session", resource_class_args=(app, api)
4858
)
4959
api.add_resource(
50-
LogoutAPI,
51-
f"{app.config['REST_URI']}/session",
52-
resource_class_args=(app, api),
60+
LogoutAPI, f"{app.config['REST_URI']}/session", resource_class_args=(app, api),
5361
)
5462
api.add_resource(
5563
GetUser,

lib/pbench/server/api/resources/database.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import os
32
import sys
43
from sqlalchemy import create_engine
@@ -17,8 +16,7 @@ def get_engine_uri():
1716
psql = config.conf["Postgres"]
1817
except NoSectionError as e:
1918
logger.error(
20-
"Failed to find an [Postgres] section"
21-
" in {} configuration file.",
19+
"Failed to find an [Postgres] section" " in {} configuration file.",
2220
" ".join(config.files),
2321
)
2422
sys.exit(1)
@@ -42,15 +40,14 @@ def get_engine_uri():
4240
sys.exit(1)
4341

4442

45-
db_session = scoped_session(sessionmaker(autocommit=False,
46-
autoflush=False))
43+
db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False))
4744

4845
Base = declarative_base()
4946
Base.query = db_session.query_property()
5047

5148

5249
def init_engine():
53-
return create_engine(get_engine_uri(), convert_unicode=True)
50+
return create_engine(get_engine_uri())
5451

5552

5653
def init_db():
@@ -59,6 +56,7 @@ def init_db():
5956
# you will have to import them first before calling init_db()
6057

6158
import pbench.server.api.resources.models
59+
6260
engine = init_engine()
6361
Base.metadata.create_all(bind=engine)
6462
db_session.configure(bind=engine)

lib/pbench/server/api/resources/models.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class UserModel(Base):
1616
lastName = Column(String(255), unique=False, nullable=False)
1717
password = Column(String(255), nullable=False)
1818
registered_on = Column(DateTime, nullable=False)
19-
email = Column(String(255), unique=False, nullable=False)
19+
email = Column(String(255), unique=True, nullable=False)
2020

2121
def __init__(self, app, **kwargs):
2222
super().__init__(**kwargs)
@@ -43,10 +43,12 @@ def encode_auth_token(self, app, user_id):
4343
"exp": datetime.datetime.utcnow() + datetime.timedelta(minutes=5),
4444
"sub": user_id,
4545
}
46-
return jwt.encode(payload, app.config.get("JWT_SECRET_KEY"), algorithm="HS256")
46+
return jwt.encode(
47+
payload, app.config.get("JWT_SECRET_KEY"), algorithm="HS256"
48+
)
4749
except Exception as e:
4850
app.logger.info(
49-
f"Some exception occurred while encoding the auth token for user_id {user_id}"
51+
f"Exception occurred while encoding the auth token for user_id {user_id}, exception: {e}"
5052
)
5153
return None
5254

@@ -59,7 +61,9 @@ def decode_auth_token(auth_token, app):
5961
:return: integer|string
6062
"""
6163
try:
62-
payload = jwt.decode(auth_token, app.config.get("SECRET_KEY"))
64+
payload = jwt.decode(
65+
auth_token, app.config.get("JWT_SECRET_KEY"), algorithms="HS256"
66+
)
6367
if auth_token in app.blacklist:
6468
return "Token blacklisted. Please log in again."
6569
return payload["sub"]
@@ -73,6 +77,7 @@ def decode_auth_token(auth_token, app):
7377

7478
class URLModel(Base):
7579
""" URL Model for storing URL related details """
80+
7681
# TODO: Think about the better name
7782
__tablename__ = "urls"
7883

0 commit comments

Comments
 (0)