Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scottx611x/fix management commands #2606

Merged
merged 5 commits into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions refinery/core/management/commands/set_up_site_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.conf import settings
from django.contrib.sites.models import Site
from django.core.management.base import BaseCommand
from django.core.management.base import BaseCommand, CommandError


class Command(BaseCommand):
Expand All @@ -17,14 +17,18 @@ class Command(BaseCommand):

def handle(self, *args, **options):
try:
refinery_instance_name = args[0]
refinery_base_url = args[1]
refinery_instance_name = options['refinery_instance_name']
refinery_base_url = options['refinery_base_url']
except:
sys.stdout.write("Insufficient arguments provided:\n%s" %
self.help)
sys.exit(2)
raise CommandError(
"Insufficient arguments provided:\n {}".format(self.help)
)
else:
set_up_site_name(refinery_instance_name, refinery_base_url)

set_up_site_name(refinery_instance_name, refinery_base_url)
def add_arguments(self, parser):
parser.add_argument('refinery_instance_name')
parser.add_argument('refinery_base_url')


def set_up_site_name(refinery_instance_name, refinery_base_url):
Expand Down
53 changes: 53 additions & 0 deletions refinery/core/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

from django.apps import apps
from django.contrib.auth.models import AnonymousUser, Group, User
from django.contrib.sites.models import Site
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.management import CommandError, call_command
from django.db import connection
from django.db.migrations.executor import MigrationExecutor
from django.test import TestCase
Expand All @@ -29,6 +31,8 @@

from .api import AnalysisResource
from .management.commands.create_user import init_user
from .management.commands.import_annotations import \
Command as ImportAnnotationsCommand
from .models import (INPUT_CONNECTION, OUTPUT_CONNECTION, Analysis,
AnalysisNodeConnection, AnalysisResult, DataSet,
ExtendedGroup, InvestigationLink, Project, Tutorials,
Expand Down Expand Up @@ -2214,3 +2218,52 @@ def test_read_meta_permissions_assigned(self):
self.dataset_b))
self.assertTrue(self._check_permission(self.user_a, self.dataset_b))
self.assertTrue(self._check_permission(self.user_b, self.dataset_b))


class TestManagementCommands(TestCase):
def test_set_up_site_name(self):
site_name = "Refinery Test"
domain = "www.example.com"
call_command('set_up_site_name', site_name, domain)

self.assertIsNotNone(
Site.objects.get(domain=domain, name=site_name)
)

def test_set_up_site_name_failure(self):
with self.assertRaises(CommandError):
call_command('set_up_site_name')

def _user_in_public_group(self, user_instance):
return bool(
user_instance.groups.filter(
name=ExtendedGroup.objects.public_group().name
).count()
)

def test_add_users_to_public_group(self):
# We have a post-save hook on User for this functionality, but this
# doesn't apply when we create the super/guest user with 'loaddata'
# where save() is never actually called
call_command("loaddata", "guest.json")
user = User.objects.get(username="guest")
self.assertFalse(self._user_in_public_group(user))

call_command("add_users_to_public_group")
self.assertTrue(self._user_in_public_group(user))

def test_activate_user(self):
guest_username = "guest"
call_command("loaddata", "guest.json")
user = User.objects.get(username=guest_username)
self.assertFalse(user.is_active)

call_command("activate_user", guest_username)
self.assertTrue(User.objects.get(username=guest_username).is_active)

def test_import_annotations(self):
""" We just care about this in the context of the optparse -> argparse
upgrade for Django 1.8 and don't necessarily want to test the
neo4j interactions """
with mock.patch.object(ImportAnnotationsCommand, "handle"):
call_command("import_annotations", "-c")