# Path: sendinel/backend/models.py
# class AuthenticationCall(models.Model):
#     """
#     Queues all calls that were made to authenticate a user via his 
#     mobile phone number. These calls get deleted once the user has
#     been authenticated successfully.
#     """
#     
#     number = models.CharField(max_length = 20)
#     time = models.DateTimeField(auto_now_add = True)
# 
# Path: sendinel/backend/authhelper.py
# def format_and_validate_phonenumber(number):
# def check_and_delete_authentication_call(number):
# def delete_timed_out_authentication_calls():
# def redirect_to_authentication_or(url):
# 
# Path: sendinel/notifications/models.py
# class NotificationType(models.Model):
#     """
#     Represent an notification type like follow-up consultation.
#     """
#     
#     name = models.CharField(max_length = 255)
#     verbose_name = models.CharField(max_length = 255)
#     template = models.CharField(max_length = 255)
#     notify_immediately = models.BooleanField()
# 
#     def __unicode__(self):
#         return self.verbose_name
#     
#     @classmethod
#     def get_notification_type(cls, type_name):
#         """
#         Return the given NotificationType
#         """
#         
#         notification_type = NotificationType.objects.get(name = type_name)
#         
#         return notification_type
# 
# Path: sendinel/notifications/views.py
# def create_notification(request, notification_type_name = None):
# def save_notification(request):
# def send_notification_via_bluetooth(request):
# 
# Path: sendinel/web/views.py
# def index(request):
# def choose_language(request):
# def jsi18n(request):
# def authenticate_phonenumber(request):
# def check_call_received(request):
# def list_bluetooth_devices(request):
# def get_bluetooth_devices(request):


from datetime import datetime, timedelta
from django.core.urlresolvers import reverse
from django.test import TestCase
from sendinel.backend.models import AuthenticationCall
from sendinel.backend import authhelper
from sendinel.notifications.models import NotificationType
from sendinel.notifications import views as notification_views
from sendinel.web import views as web_views

        # TODO check delete_timed_out_authentication_calls gets called
        
        self.assertTemplateUsed(response,'web/authenticate_phonenumber_call.html')
    
    def test_check_call_received(self):        
        # settings up the environment
        notification_type = NotificationType.objects.get(pk=1)
        original_value = authhelper.AUTHENTICATION_ENABLED
        authhelper.AUTHENTICATION_ENABLED = True
        
        self.client.get(reverse('notifications_create', \
                kwargs={"notification_type_name": notification_type.name })) 
        data = {'date': '2012-08-12',
                'phone_number': '0123456789012',
                'way_of_communication': 1}
        self.client.post(reverse('notifications_create', \
                kwargs = {"notification_type_name": notification_type.name }), data)
                
        # make sure there are no AuthenticationCall objects in the db
        AuthenticationCall.objects.all().delete()
                
        self.client.post("/web/authenticate_phonenumber/", 
            {'number':'01234 / 56789012'})
    
        response = self.client.post("/web/check_call_received/")
    
        self.failUnlessEqual(response.status_code, 200)
        self.assertContains(response, "waiting")
    

# Path: sendinel/backend/models.py
# class AuthenticationCall(models.Model):
#     """
#     Queues all calls that were made to authenticate a user via his 
#     mobile phone number. These calls get deleted once the user has
#     been authenticated successfully.
#     """
#     
#     number = models.CharField(max_length = 20)
#     time = models.DateTimeField(auto_now_add = True)
# 
# Path: sendinel/backend/authhelper.py
# def format_and_validate_phonenumber(number):
# def check_and_delete_authentication_call(number):
# def delete_timed_out_authentication_calls():
# def redirect_to_authentication_or(url):
# 
# Path: sendinel/notifications/models.py
# class NotificationType(models.Model):
#     """
#     Represent an notification type like follow-up consultation.
#     """
#     
#     name = models.CharField(max_length = 255)
#     verbose_name = models.CharField(max_length = 255)
#     template = models.CharField(max_length = 255)
#     notify_immediately = models.BooleanField()
# 
#     def __unicode__(self):
#         return self.verbose_name
#     
#     @classmethod
#     def get_notification_type(cls, type_name):
#         """
#         Return the given NotificationType
#         """
#         
#         notification_type = NotificationType.objects.get(name = type_name)
#         
#         return notification_type
# 
# Path: sendinel/notifications/views.py
# def create_notification(request, notification_type_name = None):
# def save_notification(request):
# def send_notification_via_bluetooth(request):
# 
# Path: sendinel/web/views.py
# def index(request):
# def choose_language(request):
# def jsi18n(request):
# def authenticate_phonenumber(request):
# def check_call_received(request):
# def list_bluetooth_devices(request):
# def get_bluetooth_devices(request):


# Path: sendinel/web/tests/auth_test.py
from datetime import datetime, timedelta
from django.core.urlresolvers import reverse
from django.test import TestCase
from sendinel.backend.models import AuthenticationCall
from sendinel.backend import authhelper
from sendinel.notifications.models import NotificationType
from sendinel.notifications import views as notification_views
from sendinel.web import views as web_views


        # TODO check delete_timed_out_authentication_calls gets called
        
        self.assertTemplateUsed(response,'web/authenticate_phonenumber_call.html')
    
    def test_check_call_received(self):        
        # settings up the environment
        notification_type = NotificationType.objects.get(pk=1)
        original_value = authhelper.AUTHENTICATION_ENABLED
        authhelper.AUTHENTICATION_ENABLED = True
        
        self.client.get(reverse('notifications_create', \
                kwargs={"notification_type_name": notification_type.name })) 
        data = {'date': '2012-08-12',
                'phone_number': '0123456789012',
                'way_of_communication': 1}
        self.client.post(reverse('notifications_create', \
                kwargs = {"notification_type_name": notification_type.name }), data)
                
        # make sure there are no AuthenticationCall objects in the db
        AuthenticationCall.objects.all().delete()
                
        self.client.post("/web/authenticate_phonenumber/", 
            {'number':'01234 / 56789012'})
    
        response = self.client.post("/web/check_call_received/")
    
        self.failUnlessEqual(response.status_code, 200)
        self.assertContains(response, "waiting")
    

        AuthenticationCall(number = "0123456789012").save()