# Path: amon/apps/users/forms.py
# class InviteForm(forms.Form):
# 
#     def __init__(self, *args, **kwargs):
#         self.user = kwargs.pop('user', None)
#         super(InviteForm, self).__init__(*args, **kwargs)
# 
# 
#     email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Email'}))
# 
# 
# 
#     def clean_email(self):
#         cleaned_data = self.cleaned_data
# 
#         email = cleaned_data['email']
# 
#         # Check if invitation already exists
#         email_count = invite_model.collection.find({'email': email}).count()
# 
#         if email_count > 0:
#             raise forms.ValidationError('This user has already been invited.')
# 
# 
#         # Ignore invitations for the same email as the logged in user
#         if email == self.user.email:
#             raise forms.ValidationError("You can't invite yourself.")
# 
#         return cleaned_data['email']
# 
# 
#     def save(self):
#         cleaned_data = self.cleaned_data
#         new_invite_email = cleaned_data['email']
#         
#         data = {
#             'email': new_invite_email,
#             'invited_by': self.user.id,
#             'sent': unix_utc_now()
#         }
#         
# 
#         invitation_code_string = "{0}{1}{2}".format(self.user.id, new_invite_email , unix_utc_now())
#         encoded_invitation_code = invitation_code_string.encode()
#         data['invitation_code'] = hashlib.sha224(encoded_invitation_code).hexdigest()
# 
#         invite_model.create_invitation(data=data)
# 
#         return data
# 
# Path: amon/apps/users/models.py
# class AmonUserManager(BaseUserManager):
# class AmonUser(AbstractBaseUser, PermissionsMixin):
#     class Meta:
# class ResetPasswordCode(models.Model):
#     def create_user(self, email=None, password=None):
#     def create_superuser(self, email, password=None):
#     def get_short_name(self):
#     def get_username(self):
#     def __str__(self):
#     def generate_password_reset_token(user):
#     def __str__(self):
#     USERNAME_FIELD = 'email'


from django.test.client import Client
from django.urls import reverse
from django.test import TestCase
from nose.tools import *
from django.contrib.auth import get_user_model
from amon.apps.users.forms import InviteForm
from amon.apps.users.models import invite_model



User = get_user_model()




class TestInvite(TestCase):

    def setUp(self):
        self.c = Client()
        self.user = User.objects.create_user(password='qwerty', email='foo@test.com')
        self.c.login(username='foo@test.com', password='qwerty')

    def tearDown(self):
        self.c.logout()
        self.user.delete()
        invite_model.collection.remove()


    def test_invite(self):

        form_data = {'email': 'foo@test.com'}

# Path: amon/apps/users/forms.py
# class InviteForm(forms.Form):
# 
#     def __init__(self, *args, **kwargs):
#         self.user = kwargs.pop('user', None)
#         super(InviteForm, self).__init__(*args, **kwargs)
# 
# 
#     email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Email'}))
# 
# 
# 
#     def clean_email(self):
#         cleaned_data = self.cleaned_data
# 
#         email = cleaned_data['email']
# 
#         # Check if invitation already exists
#         email_count = invite_model.collection.find({'email': email}).count()
# 
#         if email_count > 0:
#             raise forms.ValidationError('This user has already been invited.')
# 
# 
#         # Ignore invitations for the same email as the logged in user
#         if email == self.user.email:
#             raise forms.ValidationError("You can't invite yourself.")
# 
#         return cleaned_data['email']
# 
# 
#     def save(self):
#         cleaned_data = self.cleaned_data
#         new_invite_email = cleaned_data['email']
#         
#         data = {
#             'email': new_invite_email,
#             'invited_by': self.user.id,
#             'sent': unix_utc_now()
#         }
#         
# 
#         invitation_code_string = "{0}{1}{2}".format(self.user.id, new_invite_email , unix_utc_now())
#         encoded_invitation_code = invitation_code_string.encode()
#         data['invitation_code'] = hashlib.sha224(encoded_invitation_code).hexdigest()
# 
#         invite_model.create_invitation(data=data)
# 
#         return data
# 
# Path: amon/apps/users/models.py
# class AmonUserManager(BaseUserManager):
# class AmonUser(AbstractBaseUser, PermissionsMixin):
#     class Meta:
# class ResetPasswordCode(models.Model):
#     def create_user(self, email=None, password=None):
#     def create_superuser(self, email, password=None):
#     def get_short_name(self):
#     def get_username(self):
#     def __str__(self):
#     def generate_password_reset_token(user):
#     def __str__(self):
#     USERNAME_FIELD = 'email'


# Path: amon/apps/users/tests/forms_test.py
from django.test.client import Client
from django.urls import reverse
from django.test import TestCase
from nose.tools import *
from django.contrib.auth import get_user_model
from amon.apps.users.forms import InviteForm
from amon.apps.users.models import invite_model



User = get_user_model()




class TestInvite(TestCase):

    def setUp(self):
        self.c = Client()
        self.user = User.objects.create_user(password='qwerty', email='foo@test.com')
        self.c.login(username='foo@test.com', password='qwerty')

    def tearDown(self):
        self.c.logout()
        self.user.delete()
        invite_model.collection.remove()


    def test_invite(self):

        form_data = {'email': 'foo@test.com'}

        form = InviteForm(data=form_data, user=self.user)
