# Path: app/cache.py
# class RedisClusterCache(object):
#     """
#     Class used for accessing the redis cluster used for caching.
#     """
#     def __init__(self):
#         self.client = self._create_client()
# 
#     def _create_client(self):
#         """
#         Function to connect to the redis cluster and init the client.
#         """
#         server_list = settings.REDIS_SERVER_LIST.replace(' ', '').split(',')
# 
#         nodes = []
#         for server in server_list:
#             if ':' not in server:
#                 continue
#             host, port = server.split(':')
#             nodes.append({'host': host, 'port': port})
# 
#         return StrictRedisCluster(startup_nodes=nodes, decode_responses=True)
# 
#     def get(self, key):
#         return self.client.get(key)
# 
#     def exists(self, key):
#         return self.client.exists(key)
# 
#     def set(self, key, value, timeout=DEFAULT_TIMEOUT):
#         self.client.set(key, value, timeout)
# 
# Path: app/models.py
# class App(models.Model):
#     """
#     Model that contains information about the supported apps by the middleware.
#     """
#     platform = models.CharField(choices=PLATFORM_CHOICES, max_length=10)
#     app_id = models.CharField(max_length=255)
# 
#     push_key = models.CharField(max_length=255)
#     logentries_token = models.CharField(max_length=255, blank=False, null=False, default='')
#     partner_logentries_token = models.CharField(max_length=255, blank=True, null=True, default='')
# 
#     def __str__(self):
#         return '{0} for {1}'.format(self.app_id, self.platform)
# 
#     class Meta:
#         unique_together = ('app_id', 'platform')
# 
# class Device(models.Model):
#     """
#     Model for all device who register at the middleware.
#     """
#     # FIXME: We need this to be backwards compatible for one release.
#     id = models.CharField(max_length=255, unique=True)
# 
#     name = models.CharField(max_length=255, blank=True, null=True)
#     sip_user_id = models.CharField(max_length=255, unique=True, primary_key=True)
#     os_version = models.CharField(max_length=255, blank=True, null=True)
#     client_version = models.CharField(max_length=255, blank=True, null=True)
#     token = models.CharField(max_length=250)
#     sandbox = models.BooleanField(default=False)
#     last_seen = models.DateTimeField(blank=True, null=True)
#     app = models.ForeignKey(App)
#     remote_logging_id = models.CharField(max_length=255, blank=True, null=True)
# 
#     def __str__(self):
#         return '{0} - {1}'.format(self.sip_user_id, self.name)
# 
#     # FIXME: We need this to be backwards compatible for one release.
#     def save(self, *args, **kwargs):
#         """
#         Make sure the id fields stays the same as sip_user_id so we are
#         backwards compatible.
#         """
#         if self.sip_user_id:
#             self.id = self.sip_user_id
#             obj = super(Device, self).save(*args, **kwargs)
#             return obj
# 
# class ResponseLog(models.Model):
#     """
#     Model for logging info about the device response.
#     """
#     platform = models.CharField(choices=PLATFORM_CHOICES, max_length=10)
#     roundtrip_time = models.FloatField()
#     available = models.BooleanField()
#     date = models.DateTimeField(auto_now_add=True)
# 
# Path: main/prometheus/consts.py
# APP_VERSION_KEY = 'app_version'
# 
# CALL_SETUP_SUCCESSFUL_KEY = 'call_setup_successful'
# 
# CODEC_KEY = 'codec'
# 
# CONNECTION_TYPE_KEY = 'connection_type'
# 
# DIRECTION_KEY = 'direction'
# 
# FAILED_REASON_KEY = 'failed_reason'
# 
# HANGUP_REASON_KEY = 'hangup_reason'
# 
# MOS_KEY = 'mos'
# 
# NETWORK_KEY = 'network'
# 
# NETWORK_OPERATOR_KEY = 'network_operator'
# 
# OS_KEY = 'os'
# 
# OS_VERSION_KEY = 'os_version'
# 
# Path: api/tests/utils.py
# def mocked_send_apns_message(device, app, message_type, data=None):
#     cache.set('attempts', data.get('attempt', 1), 300)
#     print('WORKED APNS')
#     print(data.get('attempt', 1))
# 
# def mocked_send_fcm_message(device, app, message_type, data=None):
#     cache.set('attempts', data.get('attempt', 1), 300)
#     print('WORKED FCM')
#     print(data.get('attempt', 1))
# 
# class ThreadWithReturn(Thread):
#     def __init__(self, *args, **kwargs):
#         super(ThreadWithReturn, self).__init__(*args, **kwargs)
# 
#         self._return = None
# 
#     def run(self):
#         if self._target:
#             self._return = self._target(*self._args, **self._kwargs)
# 
#     def join(self, *args, **kwargs):
#         super(ThreadWithReturn, self).join(*args, **kwargs)
# 
#         return self._return


from ast import literal_eval
from datetime import datetime, timedelta
from unittest import mock
from django.conf import settings
from django.core.cache import cache
from django.test import TestCase, TransactionTestCase
from freezegun import freeze_time
from rest_framework.test import APIClient
from testfixtures import LogCapture
from app.cache import RedisClusterCache
from app.models import App, Device, ResponseLog
from main.prometheus.consts import (
    APP_VERSION_KEY,
    CALL_SETUP_SUCCESSFUL_KEY,
    CODEC_KEY,
    CONNECTION_TYPE_KEY,
    DIRECTION_KEY,
    FAILED_REASON_KEY,
    HANGUP_REASON_KEY,
    MOS_KEY,
    NETWORK_KEY,
    NETWORK_OPERATOR_KEY,
    OS_KEY,
    OS_VERSION_KEY)
from .utils import mocked_send_apns_message, mocked_send_fcm_message, ThreadWithReturn
import time




class RegisterDeviceTest(TestCase):

    def setUp(self):
        super(RegisterDeviceTest, self).setUp()

# Path: app/cache.py
# class RedisClusterCache(object):
#     """
#     Class used for accessing the redis cluster used for caching.
#     """
#     def __init__(self):
#         self.client = self._create_client()
# 
#     def _create_client(self):
#         """
#         Function to connect to the redis cluster and init the client.
#         """
#         server_list = settings.REDIS_SERVER_LIST.replace(' ', '').split(',')
# 
#         nodes = []
#         for server in server_list:
#             if ':' not in server:
#                 continue
#             host, port = server.split(':')
#             nodes.append({'host': host, 'port': port})
# 
#         return StrictRedisCluster(startup_nodes=nodes, decode_responses=True)
# 
#     def get(self, key):
#         return self.client.get(key)
# 
#     def exists(self, key):
#         return self.client.exists(key)
# 
#     def set(self, key, value, timeout=DEFAULT_TIMEOUT):
#         self.client.set(key, value, timeout)
# 
# Path: app/models.py
# class App(models.Model):
#     """
#     Model that contains information about the supported apps by the middleware.
#     """
#     platform = models.CharField(choices=PLATFORM_CHOICES, max_length=10)
#     app_id = models.CharField(max_length=255)
# 
#     push_key = models.CharField(max_length=255)
#     logentries_token = models.CharField(max_length=255, blank=False, null=False, default='')
#     partner_logentries_token = models.CharField(max_length=255, blank=True, null=True, default='')
# 
#     def __str__(self):
#         return '{0} for {1}'.format(self.app_id, self.platform)
# 
#     class Meta:
#         unique_together = ('app_id', 'platform')
# 
# class Device(models.Model):
#     """
#     Model for all device who register at the middleware.
#     """
#     # FIXME: We need this to be backwards compatible for one release.
#     id = models.CharField(max_length=255, unique=True)
# 
#     name = models.CharField(max_length=255, blank=True, null=True)
#     sip_user_id = models.CharField(max_length=255, unique=True, primary_key=True)
#     os_version = models.CharField(max_length=255, blank=True, null=True)
#     client_version = models.CharField(max_length=255, blank=True, null=True)
#     token = models.CharField(max_length=250)
#     sandbox = models.BooleanField(default=False)
#     last_seen = models.DateTimeField(blank=True, null=True)
#     app = models.ForeignKey(App)
#     remote_logging_id = models.CharField(max_length=255, blank=True, null=True)
# 
#     def __str__(self):
#         return '{0} - {1}'.format(self.sip_user_id, self.name)
# 
#     # FIXME: We need this to be backwards compatible for one release.
#     def save(self, *args, **kwargs):
#         """
#         Make sure the id fields stays the same as sip_user_id so we are
#         backwards compatible.
#         """
#         if self.sip_user_id:
#             self.id = self.sip_user_id
#             obj = super(Device, self).save(*args, **kwargs)
#             return obj
# 
# class ResponseLog(models.Model):
#     """
#     Model for logging info about the device response.
#     """
#     platform = models.CharField(choices=PLATFORM_CHOICES, max_length=10)
#     roundtrip_time = models.FloatField()
#     available = models.BooleanField()
#     date = models.DateTimeField(auto_now_add=True)
# 
# Path: main/prometheus/consts.py
# APP_VERSION_KEY = 'app_version'
# 
# CALL_SETUP_SUCCESSFUL_KEY = 'call_setup_successful'
# 
# CODEC_KEY = 'codec'
# 
# CONNECTION_TYPE_KEY = 'connection_type'
# 
# DIRECTION_KEY = 'direction'
# 
# FAILED_REASON_KEY = 'failed_reason'
# 
# HANGUP_REASON_KEY = 'hangup_reason'
# 
# MOS_KEY = 'mos'
# 
# NETWORK_KEY = 'network'
# 
# NETWORK_OPERATOR_KEY = 'network_operator'
# 
# OS_KEY = 'os'
# 
# OS_VERSION_KEY = 'os_version'
# 
# Path: api/tests/utils.py
# def mocked_send_apns_message(device, app, message_type, data=None):
#     cache.set('attempts', data.get('attempt', 1), 300)
#     print('WORKED APNS')
#     print(data.get('attempt', 1))
# 
# def mocked_send_fcm_message(device, app, message_type, data=None):
#     cache.set('attempts', data.get('attempt', 1), 300)
#     print('WORKED FCM')
#     print(data.get('attempt', 1))
# 
# class ThreadWithReturn(Thread):
#     def __init__(self, *args, **kwargs):
#         super(ThreadWithReturn, self).__init__(*args, **kwargs)
# 
#         self._return = None
# 
#     def run(self):
#         if self._target:
#             self._return = self._target(*self._args, **self._kwargs)
# 
#     def join(self, *args, **kwargs):
#         super(ThreadWithReturn, self).join(*args, **kwargs)
# 
#         return self._return


# Path: api/tests/test_views.py
from ast import literal_eval
from datetime import datetime, timedelta
from unittest import mock
from django.conf import settings
from django.core.cache import cache
from django.test import TestCase, TransactionTestCase
from freezegun import freeze_time
from rest_framework.test import APIClient
from testfixtures import LogCapture
from app.cache import RedisClusterCache
from app.models import App, Device, ResponseLog
from main.prometheus.consts import (
    APP_VERSION_KEY,
    CALL_SETUP_SUCCESSFUL_KEY,
    CODEC_KEY,
    CONNECTION_TYPE_KEY,
    DIRECTION_KEY,
    FAILED_REASON_KEY,
    HANGUP_REASON_KEY,
    MOS_KEY,
    NETWORK_KEY,
    NETWORK_OPERATOR_KEY,
    OS_KEY,
    OS_VERSION_KEY)
from .utils import mocked_send_apns_message, mocked_send_fcm_message, ThreadWithReturn
import time





class RegisterDeviceTest(TestCase):

    def setUp(self):
        super(RegisterDeviceTest, self).setUp()

        self.client = APIClient()