Skip to content

Commit

Permalink
added test fix #6 and create version 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauricio Collazos committed Aug 8, 2018
1 parent 5f0c09b commit b86533d
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 15 deletions.
26 changes: 12 additions & 14 deletions django_crud_generator/django_crud_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,41 +113,36 @@ def sanity_check(args):
sys.exit(1)


def views(args):
def generic_insert_with_folder(folder_name, file_name, args):
"""
Create the view file
:param args: command line args
:return:
In general if we need to put a file on a folder, we use this method
"""

# First we make sure views are a package instead a file
if not os.path.isdir(
os.path.join(
args['django_application_folder'],
'views'
folder_name
)
):
os.mkdir(os.path.join(args['django_application_folder'], 'views'))
codecs.open(
os.path.join(
args['django_application_folder'],
'views',
folder_name,
'__init__.py'
),
'w+'
)

view_file = create_or_open(
os.path.join(
'views',
'{}.py'.format(convert(args['model_name']).strip())
folder_name,
'{}.py'.format(file_name)
),
'',
args
)
# Load content from template
application_name = args['django_application_folder'].split("/")[-1]

render_template_with_args_in_file(
view_file,
os.path.join(
Expand All @@ -156,7 +151,6 @@ def views(args):
),
model_name=args['model_name'],
model_prefix=args['model_prefix'],
application_name=application_name,
model_name_lower=args['model_name'].lower()
)
view_file.close()
Expand Down Expand Up @@ -211,7 +205,11 @@ def execute_from_command_line():
sanity_check(args)

# Views has an specific logic, so we don't touch it
views(args)
simplified_file_name = convert(args['model_name'].strip())

generic_insert_with_folder("views", simplified_file_name,args)
# Seems like tests also has the same logic
generic_insert_with_folder("tests", "test_{}".format(simplified_file_name),args)

modules_to_inject = [
'conf',
Expand All @@ -233,7 +231,7 @@ def execute_from_command_line():
model_name=args['model_name'],
model_prefix=args['model_prefix'],
url_pattern=args['url_pattern'],
view_file=convert(args['model_name'].strip())
view_file=simplified_file_name
)

# This is just a fix to link api_urls with urls
Expand Down
107 changes: 107 additions & 0 deletions django_crud_generator/templates/tests.py.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from django.test import (
TestCase,
Client
)
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth import (
get_user_model,
models as auth_models
)

from .. import (
conf,
models
)


class ${model_name}Permission(TestCase):

@staticmethod
def perform_create():
return models.${model_name}.objects.create(
name="delete_me"
)

def args_for_reverse_lazy(self):
return {
"slug": self.object.slug
}

def setUp(self):
self.unauthorized_client = Client()
self.client = Client()
self.user = get_user_model().objects.create_user('user', password='password')
self.client.login(username='user', password='password')
self.object = self.perform_create()

def test_unauthenticated_users_cant_create(self):
response = self.unauthorized_client.get(reverse_lazy(conf.${model_prefix}_CREATE_URL_NAME))
self.assertNotEqual(response.status_code, 200)

def test_unauthenticated_users_cant_update(self):
response = self.unauthorized_client.get(
reverse_lazy(
conf.${model_prefix}_UPDATE_URL_NAME,
kwargs=self.args_for_reverse_lazy()
)
)
self.assertNotEqual(response.status_code, 200)

def test_unauthenticated_users_cant_delete(self):
response = self.unauthorized_client.get(
reverse_lazy(
conf.${model_prefix}_DELETE_URL_NAME,
kwargs=self.args_for_reverse_lazy()
)
)
self.assertNotEqual(response.status_code, 200)

def test_unauthorized_users_cant_create(self):
response = self.client.get(reverse_lazy(conf.${model_prefix}_CREATE_URL_NAME))
self.assertNotEqual(response.status_code, 200)

def test_unauthorized_users_cant_update(self):
response = self.client.get(
reverse_lazy(
conf.${model_prefix}_UPDATE_URL_NAME,
kwargs=self.args_for_reverse_lazy()
)
)
self.assertNotEqual(response.status_code, 200)

def test_unauthorized_users_cant_delete(self):
response = self.client.get(
reverse_lazy(
conf.${model_prefix}_DELETE_URL_NAME,
kwargs=self.args_for_reverse_lazy()
)
)
self.assertNotEqual(response.status_code, 200)

def test_authorized_users_can_create(self):
create_permission = auth_models.Permission.objects.get(codename="add_${model_name_lower}")
self.user.user_permissions.add(create_permission)
response = self.client.get(reverse_lazy(conf.${model_prefix}_CREATE_URL_NAME))
self.assertEqual(response.status_code, 200)

def test_authorized_users_can_update(self):
change_permission = auth_models.Permission.objects.get(codename="change_${model_name_lower}")
self.user.user_permissions.add(change_permission)
response = self.client.get(
reverse_lazy(
conf.${model_prefix}_UPDATE_URL_NAME,
kwargs=self.args_for_reverse_lazy()
)
)
self.assertEqual(response.status_code, 200)

def test_authorized_users_can_delete(self):
delete_permission = auth_models.Permission.objects.get(codename="delete_${model_name_lower}")
self.user.user_permissions.add(delete_permission)
response = self.client.get(
reverse_lazy(
conf.${model_prefix}_DELETE_URL_NAME,
kwargs=self.args_for_reverse_lazy()
)
)
self.assertEqual(response.status_code, 200)
3 changes: 3 additions & 0 deletions django_crud_generator/templates/view.py.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.core.urlresolvers import reverse_lazy
from django import http


from base import views as base_views

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def readme():

setup(
name='django_crud_generator',
version='0.1.1',
version='0.2.0',
description='A simple scaffolding for django applications',
long_description=readme(),
url='http://github.com/contraslash/django-crud-generator',
Expand Down

0 comments on commit b86533d

Please sign in to comment.