-
Notifications
You must be signed in to change notification settings - Fork 81
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
cleaned up test data #123
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|
@@ -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() | ||
}) | ||
|
@@ -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, | ||
|
@@ -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) | ||
|
||
|
@@ -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']) | ||
|
||
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') | ||
|
@@ -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()))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 inorganization/models.py
. Look in particular at theget_policy_instance
function and anywhere you see theOrganizationRole
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 appropriateOrganizationRole
objects. In fact, if you take a look in the latest version of mytest-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.)