Skip to content

Commit ed8ce0d

Browse files
committed
Change project structure and fix import issue
1 parent 913d0cd commit ed8ce0d

12 files changed

+100
-94
lines changed

client/__init__.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
from faker import Faker
21
from flask import Flask
32
from flask_restful import Api
43
from flask_sqlalchemy import SQLAlchemy
54

6-
VERSION = '0.1.0'
7-
8-
app = Flask('User')
5+
app = Flask(__name__)
96

107
app.config['SECRET_KEY'] = 'LUCAS'
118
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///thisisatest.db'
129
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
1310

14-
api = Api(app)
1511
db = SQLAlchemy(app)
16-
faker = Faker()
12+
api = Api(app)
13+
14+
import views

client/__main__.py

-4
This file was deleted.

client/endpoints.py

-66
This file was deleted.

client/models.py

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
from sqlalchemy import Column
2-
from sqlalchemy import Integer
3-
from sqlalchemy import String
4-
51
from client import db
62

73

84
class User(db.Model):
95
__tablename__ = 'user'
10-
id = Column('id', Integer, primary_key=True)
11-
name = Column('name', String)
12-
age = Column('age', Integer)
13-
birthdate = Column('birthdate', String)
14-
email = Column('email', String)
15-
username = Column('username', String)
16-
password = Column('password', String)
6+
id = db.Column('id', db.Integer, primary_key=True)
7+
name = db.Column('name', db.String)
8+
age = db.Column('age', db.Integer)
9+
birthdate = db.Column('birthdate', db.String)
10+
email = db.Column('email', db.String)
11+
username = db.Column('username', db.String)
12+
password = db.Column('password', db.String)
1713

1814
def __init__(self, id=None, name=None, age=None, username=None, password=None, email=None, birthdate=None):
1915
self.id = id
@@ -29,6 +25,3 @@ def __repr__(self):
2925

3026
def __str__(self):
3127
return u'User(id={}, username={}, name={}, email{})'.format(self.id, self.username, self.name, self.email)
32-
33-
34-
db.create_all()

client/thisisatest.db

8 KB
Binary file not shown.

client/views/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from login import LoginResource
2+
from user import UserResource, UserListResource
3+
from client import api
4+
5+
api.add_resource(LoginResource, '/login', endpoint='login')
6+
api.add_resource(UserResource, '/user/<int:id>', endpoint='user')
7+
api.add_resource(UserListResource, '/users', endpoint='users')

client/views/login.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# coding=utf-8
2+
3+
import jwt
4+
from flask import request
5+
from flask_restful import Resource
6+
from client import app
7+
8+
9+
def valid_auth(auth):
10+
if auth.username == 'username' and auth.password == 'password':
11+
return True
12+
return False
13+
14+
15+
class LoginResource(Resource):
16+
17+
def get(self):
18+
auth = request.authorization
19+
if not auth:
20+
headers = {'WWW-Authenticate': "Basic realm='teste'"}
21+
return {'message': 'Authentication must be provided.'}, 401, headers
22+
23+
if valid_auth(auth):
24+
payload = dict(username=auth.username, password=auth.password)
25+
jwt_data = jwt.encode(payload, app.config['SECRET_KEY'])
26+
return {"jwt": jwt_data}, 200
27+
28+
return {'message': 'Invalid credentials.'}, 401
29+
30+
31+

client/views/user.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import httplib as http
2+
3+
from flask import request
4+
from flask_restful import Resource
5+
from flask_restful import fields, marshal_with
6+
7+
from client import db
8+
from client.models import User
9+
10+
user_fields = {
11+
'id': fields.Integer,
12+
'username': fields.String,
13+
'email': fields.String,
14+
'uri': fields.Url('user', absolute=True),
15+
}
16+
17+
18+
class UserListResource(Resource):
19+
@marshal_with(user_fields)
20+
def get(self):
21+
users = User.query.all()
22+
return users
23+
24+
@marshal_with(user_fields)
25+
def post(self):
26+
data = request.get_json()
27+
user = User(**data)
28+
db.session.add(user)
29+
db.session.commit()
30+
return user, http.CREATED
31+
32+
33+
class UserResource(Resource):
34+
@marshal_with(user_fields)
35+
def get(self, id):
36+
user = User.query.filter_by(id=id).first()
37+
return user

__init__.py config.py

File renamed without changes.

features/steps/step_user_login.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def step_impl(context, password):
1717
@when(u'I send a "{method}" request to the "{endpoint}" endpoint')
1818
def step_impl(context, method, endpoint):
1919
auth = HTTPBasicAuth(context.username, context.password)
20-
context.response = request(method, context.base_uri + endpoint, auth=auth)
20+
login_uri = context.base_uri + endpoint
21+
print("Login URI ====>", login_uri)
22+
context.response = request(method, login_uri, auth=auth)
2123

2224

2325
@then(u'I should receive a "{status_code}" status code')

requirements.txt

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
allure-behave==2.3.1b1
2+
allure-python-commons==2.3.1b1
13
aniso8601==2.0.0
4+
attrs==17.4.0
25
behave==1.2.5
3-
certifi==2017.11.5
6+
certifi==2018.1.18
47
cffi==1.11.2
58
chardet==3.0.4
69
click==6.7
710
coverage==4.4.2
811
enum34==1.1.6
9-
Faker==0.8.8
12+
Faker==0.8.10
1013
Flask==0.12.2
1114
Flask-RESTful==0.3.6
1215
Flask-SQLAlchemy==2.3.2
@@ -17,6 +20,7 @@ Jinja2==2.10
1720
MarkupSafe==1.0
1821
parse==1.8.2
1922
parse-type==0.4.2
23+
pluggy==0.6.0
2024
pyasn1==0.4.2
2125
pycparser==2.18
2226
PyHamcrest==1.9.0
@@ -25,7 +29,7 @@ python-dateutil==2.6.1
2529
pytz==2017.3
2630
requests==2.18.4
2731
six==1.11.0
28-
SQLAlchemy==1.2.0
32+
SQLAlchemy==1.2.2
2933
text-unidecode==1.1
3034
urllib3==1.22
3135
Werkzeug==0.14.1

run.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from client import app
2+
3+
if __name__ == '__main__':
4+
app.run(debug=True, port=5000)

0 commit comments

Comments
 (0)