def test_multiple_account_ownership(self): """Ensure that a User can be a member of and own multiple accounts""" from accounts.models import AccountUser from accounts.utils import create_account account1 = create_account(name="one", owner=self.user) account2 = create_account(name="two", owner=self.user) account3 = create_account(name="three", owner=self.seconduser) additional_account = AccountUser.objects.create(account=account3, user=self.user)
def test_account_owner_is_member(self): """Ensure that the account owner must be a member of the group""" from accounts.models import AccountUser, AccountOwner from accounts.exceptions import AccountMismatch from accounts.utils import create_account account = create_account(name="mytest", owner=self.user) second_account = create_account(name="blah", owner=self.seconduser) second_account_user = AccountUser.objects.create( account=second_account, user=self.thirduser) first_owner = AccountOwner.objects.get(account=account) first_owner.account_user = second_account_user self.assertRaises(AccountMismatch, first_owner.save)
def test_account_user_delete(self): """Ensure special exceptions don't prohibit normal delete""" from accounts.models import AccountUser from accounts.utils import create_account account = create_account(name="mytest", owner=self.user) secondary_user = AccountUser.objects.create(account=account, user=self.seconduser) secondary_user.delete()
def test_switch_account_owner(self): """Ensure that the account owner can be cleanly switched""" from accounts.models import AccountUser from accounts.utils import create_account, change_owner account = create_account(name="mytest", owner=self.user) secondary_user = AccountUser.objects.create(account=account, user=self.seconduser) new_owner = change_owner(account, secondary_user) self.assertEqual(new_owner.account_user, account.owner.account_user)
def test_duplicate_domain(self): """Full domains must be unique or form is not valid""" from accounts.forms import AccountForm from accounts.utils import create_account account = create_account("First", self.user, domain="sub.example.com") form = AccountForm(initial={ "name": "My test", "domain": "sub.example.com", }) self.assertFalse(form.is_valid())
def test_duplicate_subdomain(self): """Subdomains must be unique or form is not valid""" from accounts.forms import AccountForm from accounts.utils import create_account account = create_account("First", self.user, subdomain="first") form = AccountForm(initial={ "name": "My test", "subdomain": "first", }) self.assertFalse(form.is_valid())
def test_no_orphans(self): """Ensure that the account owner cannot be deleted""" from accounts.models import AccountUser from accounts.exceptions import OwnershipRequired from accounts.utils import create_account account = create_account(name="mytest", owner=self.user) secondary_user = AccountUser.objects.create(account=account, user=self.seconduser) owner_user = AccountUser.objects.get(account=account, user=self.user) self.assertRaises(OwnershipRequired, owner_user.delete)
def test_create_account(self): """create_account method should create an account and an owner user""" from accounts.models import Account from accounts.utils import create_account new_account = create_account( name="My test account", owner=self.user) self.assertTrue(isinstance(new_account, Account)) self.assertRaises(TypeError, 'create_user()', create_account, name="No owner")
def test_account_removal(self): """Ensure that deleting account removes account users""" from accounts.models import Account, AccountUser, AccountOwner from accounts.utils import create_account # Create an account account = create_account(name="mytest", owner=self.user) secondary_user = AccountUser.objects.create(account=account, user=self.seconduser) self.assertEqual(2, len(AccountUser.objects.filter(account=account))) account.delete() self.assertEqual(0, len(AccountUser.objects.filter(account=account)))
def create(self, valiated_data): person_model = create_person(valiated_data.pop('person')) account_model = create_account(valiated_data.pop('account')) try: teacher = Teacher.objects.create(account=account_model, person=person_model, **valiated_data) except Exception: raise serializers.ValidationError( 'Something wrong with your teacher information') return teacher
def get_or_create_user(self, user_info): try: user = User.objects.get(username=user_info['user_name']) check_profile( user_info['user_name'], CONFIG['DEFAULT_IP4'], CONFIG['DEFAULT_GROUP'], user_info['full_name'], ) return user except ObjectDoesNotExist: return create_account( user_info['user_name'], CONFIG['DEFAULT_IP4'], CONFIG['DEFAULT_GROUP'], user_info['full_name'], )
def create(self, validated_data): person_model = create_person(validated_data.pop('person')) account_model = create_account(validated_data.pop('account')) try: student = Student.objects.create(account=account_model, person=person_model, **validated_data) except Exception: raise serializers.ValidationError( 'Something wrong with your student information') try: create_health(validated_data.pop('health')) except KeyError: pass student.save() return student
def test_one_account_owner(self): """Ensure only one account owner exists""" from django.db import IntegrityError from accounts.models import Account, AccountUser, AccountOwner from accounts.utils import create_account new_account = create_account( name="My test account", owner=self.user) # Account object creation is implied by owernship creation self.assertEqual(new_account.owner.account_user.user, self.user) new_account_user = AccountUser.objects.create( account=new_account, user=self.seconduser, ) self.assertRaises(IntegrityError, AccountOwner.objects.create, **{ 'account': new_account, 'account_user': new_account_user })
def post(self, request): account = create_account(request.data, is_admin=True) return Response(status=status.HTTP_200_OK)