diff --git a/migrations/678-fixup-monolith-api.sql b/migrations/678-fixup-monolith-api.sql new file mode 100644 index 0000000000..fe70989c3c --- /dev/null +++ b/migrations/678-fixup-monolith-api.sql @@ -0,0 +1,2 @@ +UPDATE groups SET name='Monolith API', rules='Monolith:API,RevenueStats:View' +WHERE name='Monolith:API' LIMIT 1; diff --git a/mkt/monolith/management/commands/create_monolith_user.py b/mkt/monolith/management/commands/create_monolith_user.py index 41e440feec..88bc334021 100644 --- a/mkt/monolith/management/commands/create_monolith_user.py +++ b/mkt/monolith/management/commands/create_monolith_user.py @@ -10,14 +10,13 @@ from mkt.site.management.commands.add_test_users import create_user class Command(BaseCommand): help = """Create an user with access to the monolith API""" option_list = BaseCommand.option_list + ( - make_option('--overwrite', - action='store_true', - dest='overwrite', - default=False, - help='Overwrite the user if it already exist'),) + make_option( + '--overwrite', action='store_true', + dest='overwrite', default=False, + help='Overwrite the user access token if it already exists'),) def handle(self, *args, **kw): create_user('monolith@mozilla.com', - delete_user=kw['overwrite'], + overwrite=kw['overwrite'], password=settings.MONOLITH_PASSWORD, - permissions=('Monolith:API', )) + group_name='Monolith API') diff --git a/mkt/site/management/commands/add_test_users.py b/mkt/site/management/commands/add_test_users.py index 460eb737c9..65c7b57794 100644 --- a/mkt/site/management/commands/add_test_users.py +++ b/mkt/site/management/commands/add_test_users.py @@ -3,35 +3,28 @@ from datetime import datetime from optparse import make_option from django.conf import settings -from django.contrib.auth.models import User from django.core.management.base import BaseCommand from django.db import transaction import amo -from access.models import GroupUser, Group +from access.models import Group, GroupUser from apps.users.models import UserProfile from mkt.api.models import Access @transaction.commit_on_success -def create_user(email, password, group_name=None, delete_user=False, - permissions=None): +def create_user(email, password, group_name=None, overwrite=False): """Create an user if he doesn't exist already, assign him to a group and create a token for him. On token creation, we generate the token key and the token secret. Each of - them are generated in a predictible way: sha512(password + email + 'key') or - sha512(password + email + 'secret'). + them are generated in a predictible way: sha512(password + email + 'key') + or sha512(password + email + 'secret'). """ - if delete_user: - users = User.objects.filter(email=email) - if users.exists(): - users[0].delete() - # Create the user. profile, created = UserProfile.objects.get_or_create( - username=email, email=email, source=amo.LOGIN_SOURCE_UNKNOWN, - display_name=email) + username=email, email=email, source=amo.LOGIN_SOURCE_UNKNOWN, + display_name=email) if created: profile.create_django_user() @@ -48,35 +41,29 @@ def create_user(email, password, group_name=None, delete_user=False, # We also want to grant these users access, so let's create tokens for # them. + if overwrite: + Access.objects.filter(user=profile.user).delete() + if not Access.objects.filter(user=profile.user).exists(): key = hashlib.sha512(password + email + 'key').hexdigest() secret = hashlib.sha512(password + email + 'secret').hexdigest() - consumer = Access(key=key, secret=secret, user=profile.user) - consumer.save() - - if permissions is not None: - for permission in permissions: - group, _ = Group.objects.get_or_create(name=permission, - rules=permission) - GroupUser.objects.get_or_create(group=group, user=profile) + Access.objects.create(key=key, secret=secret, user=profile.user) class Command(BaseCommand): - help = """Create three users with different profiles (App Review, Admin, - Developer) + help = """Create users with different profiles (App Review, Admin, + Developer, End User) """ option_list = BaseCommand.option_list + ( - make_option('--clear', - action='store_true', - dest='clear', - default=False, - help='Clear the users before recreating them'),) + make_option( + '--clear', action='store_true', dest='clear', default=False, + help='Clear the user access tokens before recreating them'),) def handle(self, *args, **kw): options = {'password': settings.API_PASSWORD} if kw['clear']: - options['delete_user'] = True + options['overwrite'] = True create_user('appreviewer@mozilla.com', group_name='App Reviewers', **options)