375 lines
11 KiB
Python
375 lines
11 KiB
Python
import json
|
|
from decimal import Decimal
|
|
|
|
from django.contrib.auth.models import Group, Permission
|
|
from django.core.urlresolvers import reverse
|
|
from django.test import Client, TestCase
|
|
from django.utils import timezone
|
|
|
|
from ..models import Account, Checkout, Operation, OperationGroup
|
|
|
|
from .testcases import ViewTestCaseMixin
|
|
from .utils import create_team, create_user
|
|
|
|
|
|
class LoginGenericTeamViewTests(ViewTestCaseMixin, TestCase):
|
|
url_name = 'kfet.login.genericteam'
|
|
url_expected = '/k-fet/login/genericteam'
|
|
|
|
auth_user = 'team'
|
|
auth_forbidden = [None, 'user']
|
|
|
|
def test_ok(self):
|
|
r = self.client.get(self.url)
|
|
self.assertEqual(r.status_code, 200)
|
|
logged_in_username = r.wsgi_request.user.username
|
|
self.assertEqual(logged_in_username, 'kfet_genericteam')
|
|
|
|
|
|
class AccountListViewTests(ViewTestCaseMixin, TestCase):
|
|
url_name = 'kfet.account'
|
|
url_expected = '/k-fet/accounts/'
|
|
|
|
auth_user = 'team'
|
|
auth_forbidden = [None, 'user']
|
|
|
|
def test_ok(self):
|
|
r = self.client.get(self.url)
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
class AccountValidFreeTests(ViewTestCaseMixin, TestCase):
|
|
url_name = 'kfet.account.is_validandfree.ajax'
|
|
url_expected = '/k-fet/accounts/is_validandfree'
|
|
|
|
auth_user = 'team'
|
|
auth_forbidden = [None, 'user']
|
|
|
|
def test_ok_isvalid_isfree(self):
|
|
"""Upper case trigramme not taken is valid and free."""
|
|
r = self.client.get(self.url, {'trigramme': 'AAA'})
|
|
self.assertDictEqual(json.loads(r.content.decode('utf-8')), {
|
|
'is_valid': True,
|
|
'is_free': True,
|
|
})
|
|
|
|
def test_ok_isvalid_notfree(self):
|
|
"""Already taken trigramme is not free, but valid."""
|
|
r = self.client.get(self.url, {'trigramme': '000'})
|
|
self.assertDictEqual(json.loads(r.content.decode('utf-8')), {
|
|
'is_valid': True,
|
|
'is_free': False,
|
|
})
|
|
|
|
def test_ok_notvalid_isfree(self):
|
|
"""Lower case if forbidden but free."""
|
|
r = self.client.get(self.url, {'trigramme': 'aaa'})
|
|
self.assertDictEqual(json.loads(r.content.decode('utf-8')), {
|
|
'is_valid': False,
|
|
'is_free': True,
|
|
})
|
|
|
|
|
|
class AccountCreateViewTests(ViewTestCaseMixin, TestCase):
|
|
url_name = 'kfet.account.create'
|
|
url_expected = '/k-fet/accounts/new'
|
|
|
|
auth_user = 'team'
|
|
auth_forbidden = [None, 'user']
|
|
|
|
@property
|
|
def users_extra(self):
|
|
return {
|
|
'team__add_account': create_team(
|
|
'team__add_account', '101',
|
|
perms=['kfet.add_account'],
|
|
),
|
|
}
|
|
|
|
def test_get_ok(self):
|
|
r = self.client.get(self.url)
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
def test_post_ok(self):
|
|
post_data = {
|
|
'trigramme': 'AAA',
|
|
'username': 'plopplopplop',
|
|
'first_name': 'first',
|
|
'last_name': 'last',
|
|
'email': 'email@domain.net',
|
|
}
|
|
|
|
client = Client()
|
|
client.login(
|
|
username='team__add_account',
|
|
password='team__add_account',
|
|
)
|
|
r = client.post(self.url, post_data)
|
|
|
|
self.assertRedirects(r, self.url)
|
|
a = Account.objects.get(trigramme='AAA')
|
|
self.assertEqual(a.username, 'plopplopplop')
|
|
|
|
def test_post_forbidden(self):
|
|
post_data = {
|
|
'trigramme': 'AAA',
|
|
'username': 'plopplopplop',
|
|
'first_name': 'first',
|
|
'last_name': 'last',
|
|
'email': 'email@domain.net',
|
|
}
|
|
|
|
# A team member (without kfet.add_account) is authenticated with
|
|
# self.client.
|
|
r = self.client.post(self.url, post_data)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
with self.assertRaises(Account.DoesNotExist):
|
|
Account.objects.get(trigramme='AAA')
|
|
|
|
|
|
class AccountCreateAjaxViewTests(ViewTestCaseMixin, TestCase):
|
|
urls_conf = [
|
|
{
|
|
'name': 'kfet.account.create.fromuser',
|
|
'kwargs': {'username': 'user'},
|
|
'expected': '/k-fet/accounts/new/user/user',
|
|
},
|
|
{
|
|
'name': 'kfet.account.create.fromclipper',
|
|
'kwargs': {
|
|
'login_clipper': 'myclipper',
|
|
'fullname': 'first last1 last2',
|
|
},
|
|
'expected': (
|
|
'/k-fet/accounts/new/clipper/myclipper/first%20last1%20last2'
|
|
),
|
|
},
|
|
{
|
|
'name': 'kfet.account.create.empty',
|
|
'expected': '/k-fet/accounts/new/empty',
|
|
},
|
|
]
|
|
|
|
auth_user = 'team'
|
|
auth_forbidden = [None, 'user']
|
|
|
|
def test_fromuser(self):
|
|
r = self.client.get(self.t_urls[0])
|
|
|
|
user = self.users['user']
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
self.assertEqual(r.context['user_form'].instance, user)
|
|
self.assertEqual(r.context['cof_form'].instance, user.profile)
|
|
self.assertIn('account_form', r.context)
|
|
|
|
def test_fromclipper(self):
|
|
r = self.client.get(self.t_urls[1])
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
self.assertIn('user_form', r.context)
|
|
self.assertIn('cof_form', r.context)
|
|
self.assertIn('account_form', r.context)
|
|
|
|
def test_empty(self):
|
|
r = self.client.get(self.t_urls[0])
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
self.assertIn('user_form', r.context)
|
|
self.assertIn('cof_form', r.context)
|
|
self.assertIn('account_form', r.context)
|
|
|
|
|
|
class AccountCreateAutocompleteViewTests(ViewTestCaseMixin, TestCase):
|
|
url_name = 'kfet.account.create.autocomplete'
|
|
url_expected = '/k-fet/autocomplete/account_new'
|
|
|
|
auth_user = 'team'
|
|
auth_forbidden = [None, 'user']
|
|
|
|
def test_ok(self):
|
|
r = self.client.get(self.url, {'q': 'first'})
|
|
self.assertEqual(r.status_code, 200)
|
|
self.assertListEqual(list(r.context['users_notcof']), [])
|
|
self.assertListEqual(list(r.context['users_cof']), [])
|
|
self.assertListEqual(
|
|
list(r.context['kfet']),
|
|
[(self.accounts['user'], self.users['user'])],
|
|
)
|
|
|
|
|
|
class AccountSearchViewTests(ViewTestCaseMixin, TestCase):
|
|
url_name = 'kfet.account.search.autocomplete'
|
|
url_expected = '/k-fet/autocomplete/account_search'
|
|
|
|
auth_user = 'team'
|
|
auth_forbidden = [None, 'user']
|
|
|
|
def test_ok(self):
|
|
r = self.client.get(self.url, {'q': 'first'})
|
|
self.assertEqual(r.status_code, 200)
|
|
self.assertListEqual(
|
|
list(r.context['accounts']),
|
|
[('000', 'first last')],
|
|
)
|
|
|
|
|
|
class AccountReadViewTests(ViewTestCaseMixin, TestCase):
|
|
url_name = 'kfet.account.read'
|
|
url_kwargs = {'trigramme': '001'}
|
|
url_expected = '/k-fet/accounts/001'
|
|
|
|
auth_user = 'team'
|
|
auth_forbidden = [None, 'user']
|
|
|
|
@property
|
|
def users_extra(self):
|
|
return {
|
|
'user1': create_user('user1', '001'),
|
|
}
|
|
|
|
def setUp(self):
|
|
super().setUp()
|
|
|
|
user1_acc = self.accounts['user1']
|
|
team_acc = self.accounts['team']
|
|
|
|
# Dummy operations and operation groups
|
|
checkout = Checkout.objects.create(
|
|
created_by=team_acc, name="checkout",
|
|
valid_from=timezone.now(),
|
|
valid_to=timezone.now() + timezone.timedelta(days=365)
|
|
)
|
|
opeg_data = [
|
|
(timezone.now(), Decimal('10')),
|
|
(timezone.now() - timezone.timedelta(days=3), Decimal('3')),
|
|
]
|
|
OperationGroup.objects.bulk_create([
|
|
OperationGroup(
|
|
on_acc=user1_acc, checkout=checkout, at=at, is_cof=False,
|
|
amount=amount
|
|
)
|
|
for (at, amount) in opeg_data
|
|
])
|
|
self.operation_groups = OperationGroup.objects.order_by("-amount")
|
|
Operation.objects.create(
|
|
group=self.operation_groups[0],
|
|
type=Operation.PURCHASE,
|
|
amount=Decimal('10')
|
|
)
|
|
Operation.objects.create(
|
|
group=self.operation_groups[1],
|
|
type=Operation.PURCHASE,
|
|
amount=Decimal('3')
|
|
)
|
|
|
|
def test_ok(self):
|
|
"""We can query the "Account - Read" page."""
|
|
r = self.client.get(self.url)
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
def test_ok_self(self):
|
|
client = Client()
|
|
client.login(username='user1', password='user1')
|
|
r = client.get(self.url)
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
class AccountUpdateViewTests(ViewTestCaseMixin, TestCase):
|
|
url_name = 'kfet.account.update'
|
|
url_kwargs = {'trigramme': '001'}
|
|
url_expected = '/k-fet/accounts/001/edit'
|
|
|
|
auth_user = 'team'
|
|
auth_forbidden = [None, 'user']
|
|
|
|
@property
|
|
def users_extra(self):
|
|
return {
|
|
'user1': create_user('user1', '001'),
|
|
}
|
|
|
|
def test_ok(self):
|
|
r = self.client.get(self.url)
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
def test_ok_self(self):
|
|
client = Client()
|
|
client.login(username='user1', password='user1')
|
|
r = client.get(self.url)
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
class BaseAccountGroupViewTests(ViewTestCaseMixin):
|
|
auth_user = 'team__manage_perms'
|
|
auth_forbidden = [None, 'user', 'team']
|
|
|
|
@property
|
|
def users_extra(self):
|
|
return {
|
|
'team__manage_perms': create_team(
|
|
'team__manage_perms', '101',
|
|
perms=['kfet.manage_perms'],
|
|
),
|
|
}
|
|
|
|
|
|
class AccountGroupListViewTests(BaseAccountGroupViewTests, TestCase):
|
|
url_name = 'kfet.account.group'
|
|
url_expected = '/k-fet/accounts/groups'
|
|
|
|
def test_ok(self):
|
|
r = self.client.get(self.url)
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
self.assertQuerysetEqual(
|
|
r.context['groups'],
|
|
Group.objects.filter(name__icontains='K-Fêt'),
|
|
ordered=False,
|
|
)
|
|
|
|
|
|
class AccountGroupCreateViewTests(BaseAccountGroupViewTests, TestCase):
|
|
url_name = 'kfet.account.group.create'
|
|
url_expected = '/k-fet/accounts/groups/new'
|
|
|
|
def test_ok(self):
|
|
r = self.client.get(self.url)
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
class AccountGroupUpdateViewTests(BaseAccountGroupViewTests, TestCase):
|
|
url_name = 'kfet.account.group.update'
|
|
url_kwargs = {'pk': 42}
|
|
url_expected = '/k-fet/accounts/groups/42/edit'
|
|
|
|
def setUp(self):
|
|
super().setUp()
|
|
self.group1 = Group.objects.create(pk=42, name='K-Fêt - Group')
|
|
self.group1.permissions = [
|
|
Permission.objects.get(
|
|
content_type__app_label='kfet',
|
|
codename='is_team',
|
|
)
|
|
]
|
|
|
|
def test_get_ok(self):
|
|
r = self.client.get(self.url)
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
def test_post_ok(self):
|
|
post_data = {
|
|
'name': 'Group42',
|
|
'permissions': (
|
|
self.group1.permissions
|
|
.values_list('pk', flat=True)
|
|
),
|
|
}
|
|
|
|
r = self.client.post(self.url, post_data)
|
|
|
|
self.assertRedirects(r, reverse('kfet.account.group'))
|
|
|
|
self.group1.refresh_from_db()
|
|
self.assertEqual(self.group1.name, 'K-Fêt Group42')
|