Skip to content

Commit

Permalink
Merge 83833b5 into d974d7b
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiago Jobson committed Sep 1, 2020
2 parents d974d7b + 83833b5 commit 5765cf7
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,6 +2,7 @@ __pycache__
.idea
.coverage*
anenv
venv/
coverage.xml
docs/_build/
junit.xml
Expand Down
41 changes: 41 additions & 0 deletions pygameweb/fixtures.py
@@ -0,0 +1,41 @@
""" Populates the database with sample data
"""
import yaml
from flask_sqlalchemy_session import current_session

from pygameweb.user.models import User, Group
from pygameweb.app import create_app


def _user_fixtures(fixture='pygameweb/user/fixtures/fixtures.yml'):
""" Adds user fixtures.
"""
with open(fixture, 'r') as f:
data = yaml.load(f, Loader=yaml.SafeLoader)
current_session.bulk_insert_mappings(Group, data['groups'])
current_session.bulk_insert_mappings(User, data['users'])
current_session.commit()

# Apply roles to users
for user_data in data['users']:
user = current_session.query(User).get(user_data['id'])
for role in user_data['roles']:
group = current_session.query(Group) \
.filter_by(name=role).first()
user.roles.append(group)
current_session.add(user)
current_session.commit()


# Note: `make_app` is used in tests.fixtures for testing
def populate_db(make_app=create_app):
""" Adds fixtures to database. Useful for testing locally.
"""
app = make_app('pygameweb.config.Config')
# Avoid accidentally running this in production
if app.config['ENV'] == 'production':
raise RuntimeError('Please enable development mode. '
'Are you running in production?')
with app.app_context():
_user_fixtures()
app.logger.info('Fixtures added to database.')
152 changes: 152 additions & 0 deletions pygameweb/user/fixtures/fixtures.yml
@@ -0,0 +1,152 @@
# Define roles
groups:
- id: 1
name: newbie
title: Newbie
orders: 1
description: null

- id: 2
name: commenter
title: Commenter
orders: 11
description: null

- id: 3
name: moderator
title: Moderator
orders: 3
description: null

- id: 4
name: members
title: Members
orders: 4
description: null

- id: 5
name: admin
title: Admin
orders: 5
description: null


# Define an admin, newbie, member, moderator and a blcoked user
# password is 'password' for all users
users:
- id: 1
name: admin
email: admin@example.com
password: $2b$12$0hsnVl3JrOLMKZ1UgTWMqe7fKoj5ybZytIdNdN7FRk9SeRNclqbzK
title: Superuser
disabled: 0
super: 1
roles:
- admin
- members
- moderator
active: 1
confirmed_at: !!timestamp 2020-01-01
last_login_at: !!timestamp 2020-01-01
current_login_at: null
last_login_ip: null
current_login_ip: null
login_count: null
registered_at: !!timestamp 2020-01-01 00:00:00
registered_ip: 127.0.0.1
twitter_user: null
github_user: null
bitbucket_user: null
blog_url: null

- id: 2
name: newbie
email: newbie@example.com
password: $2b$12$0hsnVl3JrOLMKZ1UgTWMqe7fKoj5ybZytIdNdN7FRk9SeRNclqbzK
title: Newbie User
disabled: 0
super: 0
active: 1
roles:
- newbie
confirmed_at: !!timestamp 2020-01-01
last_login_at: !!timestamp 2020-01-01
current_login_at: null
last_login_ip: null
current_login_ip: null
login_count: null
registered_at: !!timestamp 2020-01-01 00:00:00
registered_ip: 127.0.0.1
twitter_user: null
github_user: null
bitbucket_user: null
blog_url: null

- id: 3
name: user
email: user@example.com
password: $2b$12$0hsnVl3JrOLMKZ1UgTWMqe7fKoj5ybZytIdNdN7FRk9SeRNclqbzK
title: Regular User
disabled: 0
super: 0
active: 1
roles:
- members
confirmed_at: !!timestamp 2020-01-01
last_login_at: !!timestamp 2020-01-01
current_login_at: null
last_login_ip: null
current_login_ip: null
login_count: null
registered_at: !!timestamp 2020-01-01 00:00:00
registered_ip: 127.0.0.1
twitter_user: null
github_user: null
bitbucket_user: null
blog_url: null

- id: 4
name: moderator
email: moderator@example.com
password: $2b$12$0hsnVl3JrOLMKZ1UgTWMqe7fKoj5ybZytIdNdN7FRk9SeRNclqbzK
title: Moderator User
disabled: 0
super: 0
active: 1
roles:
- moderator
confirmed_at: !!timestamp 2020-01-01
last_login_at: !!timestamp 2020-01-01
current_login_at: null
last_login_ip: null
current_login_ip: null
login_count: null
registered_at: !!timestamp 2020-01-01 00:00:00
registered_ip: 127.0.0.1
twitter_user: null
github_user: null
bitbucket_user: null
blog_url: null

- id: 5
name: blocked_user
email: blocked_user@example.com
password: $2b$12$0hsnVl3JrOLMKZ1UgTWMqe7fKoj5ybZytIdNdN7FRk9SeRNclqbzK
title: Blocked User
disabled: 1
super: 0
active: 1
roles:
- members
confirmed_at: !!timestamp 2020-01-01
last_login_at: !!timestamp 2020-01-01
current_login_at: null
last_login_ip: null
current_login_ip: null
login_count: null
registered_at: !!timestamp 2020-01-01 00:00:00
registered_ip: 127.0.0.1
twitter_user: null
github_user: null
bitbucket_user: null
blog_url: null
41 changes: 22 additions & 19 deletions requirements.dev.txt
@@ -1,20 +1,23 @@
Flask-SQLAlchemy
mypy
mock
ipdb
pep8
pytest
pytest-cache
pytest-cov
pytest-flask
pytest-mock
pytest-mccabe
pytest-pep8
pytest-pylint
pytest-timeout
pytest-watch
pytest-xdist
sqlalchemy_pytest_fixtures
twine
wheel
attrs==17.4.0
Flask-SQLAlchemy==2.3.2
mypy==0.560
mock==2.0.0
ipdb==0.11
pep8==1.7.1
pylint==1.8.2
pytest==3.4.0
pytest-cache==1.0
pytest-cov==2.5.1
pytest-flask==0.15.0
pytest-forked==0.2
pytest-mccabe==0.1
pytest-mock==1.7.0
pytest-pep8==1.0.6
pytest-pylint==0.8.0
pytest-timeout==1.2.1
pytest-watch==4.1.0
pytest-xdist==1.22.1
sqlalchemy_pytest_fixtures==0.3.0
twine==1.13.0
wheel==0.35.1
-r requirements.txt
58 changes: 31 additions & 27 deletions requirements.txt
@@ -1,28 +1,32 @@
alembic
bcrypt
click
dnspython
feedparser
Flask
flask_admin
flask-bootstrap
Flask-Caching
flask-debugtoolbar
Flask-Gravatar
flask_limiter
flask-nav
alembic==0.9.8
bcrypt==3.1.4
click==6.7
dnspython==1.15.0
feedparser==5.2.1
Flask==0.12.2
Flask-Admin==1.5.0
Flask-Bootstrap==3.3.7.1
Flask-Caching==1.3.3
Flask-DebugToolbar==0.10.1
Flask-Gravatar==0.5.0
Flask_Limiter==1.0.1
Flask-Login==0.4.1
flask-nav==0.6
Flask-Security-Fork==2.0.1
flask_sqlalchemy_session
Flask-WTF
ghdiff
numpy
pandas
psycopg2
python-dotenv
python-slugify
pyquery
pygments
pq
scikit-learn
scipy
sphinx
Flask-SQLAlchemy-Session==1.1
Flask-WTF==0.14.2
ghdiff==0.4
numpy==1.14.0
pandas==0.22.0
psycopg2==2.7.4
python-dotenv==0.9.1
python-slugify==1.2.5
pyquery==1.4.0
Pygments==2.4.2
pq==1.5
psutil==5.4.3
pyyaml==5.3.1
scikit-learn==0.19.1
Sphinx==1.8.4
Werkzeug==0.14.1
WTForms==2.1
2 changes: 2 additions & 0 deletions setup.py
Expand Up @@ -82,6 +82,8 @@ def get_requirements():
'pygameweb.builds.update_version_from_git:release_version_correct',
'pygameweb_github_releases='
'pygameweb.project.gh_releases:sync_github_releases',
'pygameweb_fixtures=' +
'pygameweb.fixtures:populate_db',
],
},
)
31 changes: 31 additions & 0 deletions tests/functional/pygameweb/fixtures/test_user_fixtures.py
@@ -0,0 +1,31 @@
import pytest

from pygameweb.fixtures import populate_db
from pygameweb.user.models import User, Group, users_groups


def test_populate_db(app, session):
""" Ensures the populate_db adds sample data.
"""
assert session.query(Group).count() == 0
assert session.query(User).count() == 0

app.config['ENV'] = 'development'
populate_db(make_app=lambda x: app)
assert session.query(Group).count() == 5
assert session.query(User).count() == 5

# Makes sure the admin is really admin
assert 1 == session.query(users_groups) \
.filter_by(users_id=1, groups_id=5).count()


def test_populate_db_production(app, session):
""" Ensures the populate_db won't work in production.
"""
app.config['ENV'] = 'production'
with pytest.raises(RuntimeError):
populate_db(make_app=lambda x: app)

assert session.query(Group).count() == 0
assert session.query(User).count() == 0

0 comments on commit 5765cf7

Please sign in to comment.