Skip to content

Commit f771507

Browse files
committed
rebasing and some fixes
1 parent d5b87ed commit f771507

File tree

4 files changed

+49
-22
lines changed

4 files changed

+49
-22
lines changed

lib/pbench/server/api/__init__.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ def register_endpoints(api, app, config):
8686
)
8787

8888
api.add_resource(
89-
UserMetadata, f"{base_uri}/user/metadata", resource_class_args=(config, logger),
89+
UserMetadata, f"{base_uri}/metadata", resource_class_args=(config, logger, token_auth),
9090
)
9191
api.add_resource(
92-
QueryMetadata, f"{base_uri}/user/metadata/<string:id>", resource_class_args=(config, logger),
92+
QueryMetadata, f"{base_uri}/metadata/<string:id>", resource_class_args=(config, logger),
9393
)
9494

9595

@@ -129,11 +129,6 @@ def create_app(server_config):
129129
app.logger.exception("Exception while initializing sqlalchemy database")
130130
sys.exit(1)
131131

132-
@app.before_request
133-
def before_request():
134-
print(request.path)
135-
print(request.remote_addr)
136-
137132
@app.teardown_appcontext
138133
def shutdown_session(exception=None):
139134
Database.db_session.remove()

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33
import json
44
from flask_restful import Resource, abort
55
from flask import request, make_response, jsonify
6-
from pbench.server.api.resources.auth import auth
7-
from pbench.server.api.resources.models import MetadataModel
8-
from pbench.server.api.resources.database import Database
6+
from pbench.server.database.models.metadata import MetadataModel
7+
from pbench.server.database.database import Database
8+
from pbench.server.api.auth import Auth
99

1010

1111
class UserMetadata(Resource):
1212
"""
1313
Abstracted pbench API for handling user metadata by using graphql schema
1414
"""
1515

16-
def __init__(self, config, logger):
16+
def __init__(self, config, logger, auth):
1717
self.server_config = config
1818
self.logger = logger
19+
self.auth = auth
1920

20-
@auth.login_required()
21+
@Auth.token_auth.login_required(f=Auth().verify_auth())
2122
def post(self):
2223
"""
2324
Post request for creating metadata instance for a user.
@@ -56,7 +57,7 @@ def post(self):
5657
if not description:
5758
self.logger.warning("Description not provided during metadata creation")
5859
abort(400, message="Please provide a description string")
59-
current_user_id = auth.current_user().id
60+
current_user_id = self.auth.token_auth.current_user().id
6061

6162
try:
6263
# Create a new metadata session
@@ -84,7 +85,7 @@ def post(self):
8485
}
8586
return make_response(jsonify(response_object), 201)
8687

87-
@auth.login_required()
88+
@Auth.token_auth.login_required(f=Auth().verify_auth())
8889
def get(self):
8990
"""
9091
Get request for querying all the metadata sessions for a user.
@@ -105,7 +106,7 @@ def get(self):
105106
}
106107
}
107108
"""
108-
current_user_id = auth.current_user().id
109+
current_user_id = self.auth.token_auth.current_user().id
109110
try:
110111
# Fetch the metadata session
111112
sessions = (
@@ -138,7 +139,7 @@ def __init__(self, config, logger):
138139
self.server_config = config
139140
self.logger = logger
140141

141-
@auth.login_required()
142+
@Auth.token_auth.login_required(f=Auth().verify_auth())
142143
def get(self, id=None):
143144
"""
144145
Get request for querying a metadata session for a user given a metadata id.
@@ -189,7 +190,7 @@ def get(self, id=None):
189190
}
190191
return make_response(jsonify(response_object), 200)
191192

192-
@auth.login_required()
193+
@Auth.token_auth.login_required(f=Auth().verify_auth())
193194
def put(self, id=None):
194195
"""
195196
Put request for updating a metadata session for a user given a metadata id.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import datetime
2+
from dateutil import parser
3+
from pbench.server.database.database import Database
4+
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
5+
6+
7+
class MetadataModel(Database.Base):
8+
""" Metadata Model for storing user metadata details """
9+
10+
# TODO: Think about the better name
11+
__tablename__ = "metadata"
12+
13+
id = Column(Integer, primary_key=True, autoincrement=True)
14+
created = Column(DateTime, nullable=False)
15+
updated = Column(DateTime, nullable=False)
16+
config = Column(String(255), unique=False, nullable=False)
17+
description = Column(String(255), nullable=False)
18+
user_id = Column(Integer, ForeignKey('users.id'))
19+
20+
def __init__(self, created, config, description, user_id):
21+
self.created = parser.parse(created)
22+
self.updated = datetime.datetime.now()
23+
self.config = config
24+
self.description = description
25+
self.user_id = user_id
26+
27+
def __str__(self):
28+
return f"Url id: {self.id}, created on: {self.created}, description: {self.description}"
29+
30+
def as_dict(self):
31+
return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}

lib/pbench/test/unit/server/test_user_auth.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,7 @@ def test_delete_user(client, server_config):
428428

429429
class TestMetadataSession:
430430
@staticmethod
431-
def test_registration(client, server_config, pytestconfig):
432-
client.config["SESSION_FILE_DIR"] = pytestconfig.cache.get("TMP", None)
431+
def test_registration(client, server_config):
433432
""" Test for user registration """
434433
with client:
435434
response = register_user(
@@ -447,25 +446,26 @@ def test_registration(client, server_config, pytestconfig):
447446
response = login_user(client, server_config, "user", "12345")
448447
data_login = response.json
449448
assert data_login["status"] == "success"
449+
assert data_login["auth_token"]
450450

451451
response = client.post(
452-
f"{server_config.rest_uri}/user/metadata",
452+
f"{server_config.rest_uri}/metadata",
453453
json={"config": "config1", "description": "description1"},
454454
headers=dict(Authorization="Bearer " + data_login["auth_token"])
455455
)
456456
data = response.json
457457
assert data["status"] == "success"
458458

459459
response = client.post(
460-
f"{server_config.rest_uri}/user/metadata",
460+
f"{server_config.rest_uri}/metadata",
461461
json={"config": "config2", "description": "description2"},
462462
headers=dict(Authorization="Bearer " + data_login["auth_token"])
463463
)
464464
data = response.json
465465
assert data["status"] == "success"
466466

467467
response = client.get(
468-
f"{server_config.rest_uri}/user/metadata",
468+
f"{server_config.rest_uri}/metadata",
469469
headers=dict(Authorization="Bearer " + data_login["auth_token"])
470470
)
471471
data = response.json

0 commit comments

Comments
 (0)