Skip to content

Commit

Permalink
add createsuperuser command, add accounts module test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
doomdagger committed Mar 26, 2016
1 parent a1b0339 commit f3c570d
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 10 deletions.
20 changes: 10 additions & 10 deletions etc/fixtures/initial_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
{
"name" : "admin",
"email" : "admin@example.com",
"gravatar_email": "rochacbruno+quokka@gmail.com",
"gravatar_email": "jet.in.brain@gmail.com",
"password" : "admin",
"roles" : [ "admin" ],
"bio": "Quokka Example Admin",
"tagline": "Quokka is the best CMS!",
"bio": "Lingobarter Example Admin",
"tagline": "Lingobarter is the best CMS!",
"links": [
{
"title" : "facebook",
Expand Down Expand Up @@ -35,11 +35,11 @@
{
"name" : "author",
"email" : "author@example.com",
"gravatar_email": "rochacbruno+quokka@gmail.com",
"gravatar_email": "jet.in.brain@gmail.com",
"password" : "author",
"roles" : [ "author" ],
"bio": "Quokka Example Author",
"tagline": "It is nice to write in Quokka CMS!",
"bio": "Lingobarter Example Author",
"tagline": "It is nice to write in Lingobarter CMS!",
"links": [
{
"title" : "facebook",
Expand Down Expand Up @@ -72,12 +72,12 @@
"values": [
{
"name": "site_name",
"rawvalue": "Quokka website",
"rawvalue": "Lingobarter website",
"formatter": "text"
},
{
"name": "site_keywords",
"rawvalue": "cms,quokka,flask,mongo,python",
"rawvalue": "cms,lingobarter,flask,mongo,python",
"formatter": "text"
},
{
Expand Down Expand Up @@ -107,12 +107,12 @@
},
{
"name": "menu_items",
"rawvalue":"[[\"Quokka CMS\", \"http://www.quokkaproject.org\"], [\"Slack Chat\", \"https://quokkaslack.herokuapp.com/\"]]",
"rawvalue":"[[\"Lingobarter\", \"http://www.lingobarter.org\"], [\"Slack Chat\", \"https://quokkaslack.herokuapp.com/\"]]",
"formatter": "json"
},
{
"name": "social_items",
"rawvalue":"[[\"github\", \"http://github.com/quokkaproject\"], [\"twitter\", \"https://twitter.com/quokkaproject/\"]]",
"rawvalue":"[[\"github\", \"http://github.com/lingobarter\"], [\"twitter\", \"https://twitter.com/lingobarter/\"]]",
"formatter": "json"
}
]
Expand Down
Empty file.
24 changes: 24 additions & 0 deletions lingobarter/modules/accounts/commands/createsuperuser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
import click
from mongoengine import DoesNotExist
from ..models import User, Role


@click.command()
@click.option('--name', help='Full name', prompt=True)
@click.option('--email', help='A valid email address', prompt=True)
@click.option('--password', prompt=True, hide_input=True,
confirmation_prompt=True)
def cli(name, email, password):
"""Create a user with administrator permissions"""
if all([name, email, password]):
try:
admin = Role.objects.get(name='admin')
except DoesNotExist:
admin = Role(name='admin')
admin.save()
user = User.createuser(name, email, password, roles=[admin])
else:
user = "Cant create the supersuser"

click.echo(user)
Empty file.
83 changes: 83 additions & 0 deletions lingobarter/modules/accounts/tests/test_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# coding: utf-8
import sys

from flask.ext.security.utils import encrypt_password

from lingobarter.core.tests import BaseTestCase
from ..models import User, Role

if sys.version_info.major == 3:
unicode = lambda x: u'{}'.format(x) # flake8: noqa


def eval_b(x):
"""Evaluate if to encode the value is necessary.
"""
if sys.version_info.major == 2:
return x
else:
import codecs
return codecs.utf_8_encode(x)[0]


class TestAccountsModels(BaseTestCase):
def setUp(self):
self.user_dict = {
'name': u'Guybrush Treepwood',
'email': u'guybrush@monkeyisland.com',
'password': encrypt_password(eval_b('lechucksucks')),
}
self.role = Role.objects.create(
name='kill pirates',
description='hell yeah!'
)
self.user = User.objects.create(**self.user_dict)

def tearDown(self):
User.objects.all().delete()
Role.objects.all().delete()

def test_user_fields(self):
self.assertIsInstance(self.user, User)
self.assertEqual(self.user.username, u'guybrush-treepwood')
self.assertEqual(self.user.name, u'Guybrush Treepwood')
self.assertEqual(self.user.email, u'guybrush@monkeyisland.com')
self.assertEqual(self.user.password, self.user_dict['password'])
self.assertEqual(self.user.display_name, self.user.name)
self.assertEqual(unicode(self.user),
'Guybrush Treepwood <guybrush@monkeyisland.com>')

def test_createuser_classmethod(self):
user = User.createuser(
u'Lechuck',
'lechuck@monkeyisland.com',
'guybrushsucks',
)
self.assertEqual(user.username, u'lechuck')
self.assertEqual(user.name, u'Lechuck')
self.assertEqual(user.email, u'lechuck@monkeyisland.com')
self.assertEqual(user.display_name, user.name)

def test_generate_username_classmethod(self):
email = u'elaine.treepwood-ca@monkeyisland.com'
generated = User.generate_username(email)
self.assertEqual(generated, u'elaine-treepwood-ca-monkeyisland-com')

def test_role_field(self):
self.assertEqual(unicode(self.role.name), u'kill pirates')
self.assertEqual(unicode(self.role.description), u'hell yeah!')
self.assertEqual(unicode(self.role), u'kill pirates (hell yeah!)')

def test_createrole_classmethod(self):
role = Role.createrole(u'test role', description=u'test description')
self.assertEqual(role.name, u'test role')
self.assertEqual(role.description, u'test description')

def test_user_clean(self):
self.user.clean()
self.assertEqual(self.user.username, 'guybrush-treepwood')

def test_add_role_to_user(self):
self.user.roles.append(self.role)
self.assertEqual(self.user.roles.count(), 1)
self.assertEqual(self.user.roles[0], self.role)

0 comments on commit f3c570d

Please sign in to comment.