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

cleaned up test data #123

Merged
merged 1 commit into from
Apr 1, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 109 additions & 106 deletions cadasta/core/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
from datetime import datetime, timezone

from accounts.models import User
from organization.models import Organization
from tutelary.models import Policy, Role
from accounts.tests.factories import UserFactory
from organization.tests.factories import OrganizationFactory # ProjectsFactory

from organization.models import Organization, Project
from tutelary.models import Policy, Role, PolicyInstance, RolePolicyAssign

class RoleFactory(factory.django.DjangoModelFactory):
class Meta:
model = Role
from accounts.tests.factories import UserFactory
from organization.tests.factories import OrganizationFactory, ProjectFactory


class PolicyFactory(factory.django.DjangoModelFactory):
Expand All @@ -30,27 +26,31 @@ def _adjust_kwargs(cls, **kwargs):
return kwargs


class RoleFactory(factory.django.DjangoModelFactory):
class Meta:
model = Role


class FixturesData():
def add_test_users(self):
users = []
# the first two named users will have superuser access
named_users = [
{'username': 'iross', 'email': '[email protected]',
'first_name': 'Ian', 'last_name': 'Ross'},
{'username': 'oroick', 'email': '[email protected]',
'first_name': 'Oliver', 'last_name': 'Roick'}
]
named_users.append({
'username': 'testuserhb',
'email': '[email protected]',
'first_name': 'Oliver', 'last_name': 'Roick'}]
# add user's with names in languages that need to be tested.
languages = ['el_GR', 'ja_JP', 'hi_IN', 'hr_HR', 'lt_LT']
lang_users = [{
'first_name': 'עזרא',
'last_name': 'ברש'
})
users = []
languages = ['el_GR', 'ja_JP', 'hi_IN', 'hr_HR', 'lt_LT']
}]
for lang in languages:
fake = Factory.create(lang)
named_users.append({
'username': 'testuser%s' % lang,
'email': '%suser@example.com' % lang,
lang_users.append({
'username': 'testuser{}'.format(lang),
'email': '{}user@example.com'.format(lang),
'first_name': fake.first_name(),
'last_name': fake.last_name()
})
Expand All @@ -63,7 +63,7 @@ def add_test_users(self):
last_login=datetime.now(tz=timezone.utc),
is_active=True,
))
else:
elif n < len(named_users) + len(lang_users):
users.append(UserFactory.create(
password='password',
email_verified=True,
Expand All @@ -74,7 +74,6 @@ def add_test_users(self):
self.stdout.write(self.style.SUCCESS('Successfully added test users.'))
return users

# these are being applied but not listed in user objects.
def add_test_users_and_roles(self):
users = FixturesData.add_test_users(self)

Expand All @@ -95,108 +94,102 @@ def add_test_users_and_roles(self):
roles[org + '-' + abbrev] = RoleFactory.create(
name=org + '-' + abbrev,
policies=[pols['default'], pols[pol]],
variables={'organization': org}
)
variables={'organization': org})

users[0].assign_policies(roles['superuser'])
users[1].assign_policies(roles['superuser'])
users[2].assign_policies(roles['cadasta-oa'])
users[3].assign_policies(roles['habitat-for-humanity-oa'])
users[4].assign_policies(roles['habitat-for-humanity-oa'], roles['cadasta-oa'])
users[4].assign_policies(roles['habitat-for-humanity-oa'],
roles['cadasta-oa'])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, you should try using the new OrganizationRole model to set up the roles for organization admins. You can see how it works in organization/models.py. Look in particular at the get_policy_instance function and anywhere you see the OrganizationRole class being used. It might take a bit of experimentation, but if you set things up right, it ought to be possible to make users organization administrators just by creating the appropriate OrganizationRole objects. In fact, if you take a look in the latest version of my test-data/test-data-setup.py script, right at the bottom, you'll see the simplest possible way to do it. (I'm going to merge this anyway, since it otherwise is probably fine, and we'll find problems with it quickly enough once we start using it more.)

self.stdout.write(self.style.SUCCESS(
"\n{} and {} have superuser policies."
.format(users[0], users[1])))

self.stdout.write(self.style.SUCCESS(
"\n{} and {} have cadasta-oa policies."
.format(users[2], users[4])))

self.stdout.write(self.style.SUCCESS(
'\n%s and %s have superuser policies.\n%s and %s have cadasta-oa policies.\n%s and %s have habitat-for-humanity-oa policies.' % (users[0], users[1], users[2], users[4], users[3], users[4])))
"\n{} and {} have habitat-for-humanity-oa policies."
.format(users[3], users[4])))

def add_test_organizations(self):
orgs = []
named_orgs = [{
'name': 'Test: Habitat for Humanity',
'slug': 'habitat-for-humanity',
'description': """Habitat for Humanity is a nonprofit, ecumenical Christian ministry
that builds with people in need regardless of race or religion. Since
1976, Habitat has helped 6.8 million people find strength, stability
and independence through safe, decent and affordable shelter.""",
'urls': ['http://www.habitat.org'],
'contacts': [{'email': '[email protected]'}],
'add_users': None
}, {
'name': 'Test: Cadasta',
'slug': 'cadasta',
'description': """Cadasta Foundation is dedicated to the support, continued
development and growth of the Cadasta Platform – an innovative, open
source suite of tools for the collection and management of ownership,
occupancy, and spatial data that meets the unique challenges of this
process in much of the world.""",
'urls': ['http://www.cadasta.org'],
'contacts': [{'email': '[email protected]'}],
'add_users': User.objects.filter(username__startswith='testuser')
}]
for org in named_orgs:
users = User.objects.filter(username__startswith='testuser')

for n in range(3):
orgs.append(OrganizationFactory.create(
name=org['name'],
slug=org['slug'],
description=org['description'],
urls=org['urls'],
contacts=org['contacts'],
add_users=org['add_users']
add_users=users
))

self.stdout.write(self.style.SUCCESS('\nSuccessfully added organizations "%s"' % Organization.objects.all()))

# Uncomment once projects have been merged.

# def add_test_projects(self):
# projs = []
# orgs = Organization.objects.all()
# projs.append(ProjectFactory.create(
# name='Kibera',
# slug='kibera',
# description="""This is a test project. This is a test project. This is a test
# project. This is a test project. This is a test project. This is a
# test project. This is a test project. This is a test project. This
# is a test project.""",
# organization=orgs[0],
# country='KE'
# ))
# projs.append(ProjectFactory.create(
# name='H4H Test Project',
# slug='h4h-test-project',
# description="""This is a test project. This is a test project. This is a test
# project. This is a test project. This is a test project. This is a
# test project. This is a test project. This is a test project. This
# is a test project.""",
# organization=orgs[0],
# country='PH'
# ))
# projs.append(ProjectFactory.create(
# name='Cadasta Indonesia Test Project',
# slug='cadasta-indonesia-test-project',
# description="""This is another test project. This is another test project. This
# is another test project. This is another test project. This is
# another test project. This is a test project. This is another test
# project. This is another test project. This is another test
# project.""",
# organization=orgs[1],
# country='ID'
# ))
# projs.append(ProjectFactory.create(
# name='Cadasta Myanmar Test Project',
# slug='cadasta-myanmar-test-project',
# description="""This is another test project. This is another test project. This
# is another test project. This is another test project. This is
# another test project. This is a test project. This is another test
# project. This is another test project. This is another test
# project.""",
# organization=orgs[1],
# country='MM'
# ))
self.stdout.write(self.style.SUCCESS(
'\nSuccessfully added organizations {}'
.format(Organization.objects.all())))

def add_test_projects(self):
projs = []
if Organization.objects.all().exists():
orgs = Organization.objects.all()
else:
orgs = OrganizationFactory.create_batch(2)
projs.append(ProjectFactory.create(
name='Kibera',
project_slug='kibera',
description="""This is a test project. This is a test project.
This is a test project. This is a test project. This is a test
project. This is a test project. This is a test project. This
is a test project. This is a test project.""",
organization=orgs[0],
country='KE'
))
projs.append(ProjectFactory.create(
name='H4H Test Project',
project_slug='h4h-test-project',
description="""This is a test project. This is a test project.
This is a test project. This is a test project. This is a test
project. This is a test project. This is a test project. This
is a test project. This is a test project.""",
organization=orgs[0],
country='PH'
))
projs.append(ProjectFactory.create(
name='Cadasta Indonesia Test Project',
project_slug='cadasta-indonesia-test-project',
description="""This is another test project. This is another test
project. This is another test project. This is another test
project. This is another test project. This is a test project.
This is another test project. This is another test project.
This is another test project.""",
organization=orgs[1],
country='ID'
))
projs.append(ProjectFactory.create(
name='Cadasta Myanmar Test Project',
project_slug='cadasta-myanmar-test-project',
description=""""This is another test project. This is another test
project. This is another test project. This is another test
project. This is another test project. This is a test project.
This is another test project. This is another test project.
This is another test project.""",
organization=orgs[1],
country='MM'
))

def delete_test_organizations(self):
orgs = Organization.objects.filter(name__startswith='Test:')
orgs = Organization.objects.filter(name__startswith='Organization #')
for org in orgs:
org.delete()

self.stdout.write(self.style.SUCCESS('Successfully deleted all test organizations. Remaining organizations: "%s"' % Organization.objects.all()))
PolicyInstance.objects.all().delete()
RolePolicyAssign.objects.all().delete()
Policy.objects.all().delete()

self.stdout.write(self.style.SUCCESS(
"""Successfully deleted all test organizations.
Remaining organizations: {}"""
.format(Organization.objects.all())))

def delete_test_users(self):
users = User.objects.filter(username__startswith='testuser')
Expand All @@ -208,4 +201,14 @@ def delete_test_users(self):
if User.objects.filter(username=user).exists():
User.objects.get(username=user).delete()

self.stdout.write(self.style.SUCCESS('Successfully deleted test users. Remaining users: "%s"' % User.objects.all()))
self.stdout.write(self.style.SUCCESS(
'Successfully deleted test users. Remaining users: {}'
.format(User.objects.all())))

def delete_test_projects(self):
projs = Project.objects.filter(name__contains='Test Project')
projs.delete()

self.stdout.write(self.style.SUCCESS(
'Successfully deleted test projects. Remaining users: {}'
.format(User.objects.all())))
55 changes: 46 additions & 9 deletions cadasta/core/management/commands/loadfixtures.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,77 @@
from django.core.management.base import BaseCommand

from accounts.models import User
from organization.models import Organization
from core.factories import FixturesData


class Command(BaseCommand):
help = 'Load in all test data for organizations, users, roles, and projects.'
help = """Loads in test data for
organizations, users, roles,
and projects. Using loadfixtures
without any additional arguments
will load in all test data."""

def add_arguments(self, parser):
parser.add_argument('--users',
action='store_true',
dest='users',
default=False,
help='Load in just the test users, and nothing else.'
help="""Load in just
the test users, and
nothing else."""
)

parser.add_argument('--organizations',
action='store_true',
dest='organizations',
default=False,
help='Load in just the test organizations, and nothing else.'
help="""Load in just
the test
organizations,
and nothing else."""
)

parser.add_argument('--projects',
action='store_true',
dest='projects',
default=False,
help="""Load in just
the test projects,
and nothing else."""
)

parser.add_argument('--delete-users',
action='store_true',
dest='delete-users',
default=False,
help='Delete test users, but leave everything else.'
help="""Delete test
users, but leave
everything else."""
)

parser.add_argument('--delete-organizations',
action='store_true',
dest='delete-organizations',
default=False,
help='Delete test organizations, but leave the users.'
help="""Delete test
organizations, but
leave the users."""
)

parser.add_argument('--delete-projects',
action='store_true',
dest='delete-projects',
default=False,
help="""Delete test
projects, but leave
everything else."""
)

parser.add_argument('--delete',
action='store_true',
dest='delete',
default=False,
help='Delete all of the test data.'
help="""Delete all of
the test data."""
)

def handle(self, *args, **options):
Expand All @@ -52,18 +82,25 @@ def handle(self, *args, **options):
elif options['organizations']:
data.add_test_organizations(self)

elif options['projects']:
data.add_test_projects(self)

elif options['delete-organizations']:
data.delete_test_organizations(self)

elif options['delete-users']:
data.delete_test_users(self)

elif options['delete-projects']:
data.delete_test_projects(self)

elif options['delete']:
data.delete_test_users(self)
data.delete_test_organizations(self)
data.delete_test_projects(self)

else:
data.add_test_users_and_roles(self)
data.add_test_organizations(self)
# data.add_test_projects(self)
data.add_test_projects(self)
self.stdout.write(self.style.SUCCESS("All test data loaded."))