# Path: devices/models.py
# class Device(models.Model):
#     account = models.ForeignKey(User, on_delete=models.CASCADE,editable=False)
#     device_id=models.CharField(unique=True,max_length=50,validators=[alphanumeric],help_text="device should publish to mqtt on account_name/deviceID/appID/channelID")
#     device_desc = models.CharField(max_length=50,blank=True)
#     section = models.ForeignKey(Section, on_delete=models.CASCADE)
#     group=models.CharField(max_length=50,blank=True)
#     subgroup=models.CharField(max_length=50,blank=True)
#     latitude=models.FloatField(blank=True,null=True)
#     longitude=models.FloatField(blank=True,null=True)
#     model_name = models.CharField(max_length=50,blank=True)
#     install_date = models.DateTimeField('install_date',default=timezone.now)
# 
# 
#     def __str__(self):
#         return self.device_id
# 
# class Section(models.Model):
#     account = models.ForeignKey(User, on_delete=models.CASCADE,editable=False)
#     section_desc = models.CharField(max_length=50)
#     
#     def __str__(self):
#         return self.section_desc
# 
# Path: stack_configs/ldap_functions.py
# def createLDAPuser(new_user,password):
#     #returns true or false uses LDAP3  
#     con= getLDAPConn()
#     dn= str("cn=")+str(new_user.username)+str(",")+str(settings.AUTH_LDAP_USERS_OU_DN)
#     logger.info('createldap user dn %s',dn)
#     result=con.add(dn, 'inetOrgPerson', {
#         'givenName': new_user.first_name, 
#         'sn': new_user.last_name,
#         'mail': new_user.email,
#         'cn': new_user.username,
#         'displayName': str(new_user.id),
#         'uid': new_user.username,
#         'userPassword':password,
#         })
#     con.unbind()
#     logger.info('createldap user result %s',result)
#     return result
# 
# def addToLDAPGroup(user_name,group_name):
#     
#     #returns true or false uses LDAP3  
#     con= getLDAPConn()
#     group_dn= str("cn=")+str(group_name)+str(",")+str(settings.AUTH_LDAP_GROUPS_OU_DN)
#     logger.info('add user %s ToLdapgroup %s',user_name,group_name)
#     
#     result=con.modify(group_dn,
#                       {'memberUid': (MODIFY_ADD, [user_name])
#     })
#     
#     logger.info ("add group result is %s", result )
#     con.unbind()
#     return result
# 
# def removeFromLDAPGroup(user_name,group_name):
#     #this is used in testing
#     #returns true or false uses LDAP3  
#     con= getLDAPConn()
#     group_dn= str("cn=")+str(group_name)+str(",")+str(settings.AUTH_LDAP_GROUPS_OU_DN)
#     result=con.modify(group_dn,
#                       {'memberUid': (MODIFY_DELETE, [user_name])
#     })
#     
#     logger.info ("remove %s from group %s result is %s", user_name, group_name, result )
#     con.unbind()
#     return result
# 
# def getLDAPConn():
#     #ldap3
#     dn=settings.AUTH_LDAP_BIND_DN
#     password= settings.AUTH_LDAP_BIND_PASSWORD
#     conn=getLDAPConnWithUser(dn, password)
#     return conn


from django.test import TestCase
from django.test import TestCase,Client
from devices.models import Device,Section
from django.contrib.auth.models import User,Group,Permission
from django.conf import settings
from stack_configs.ldap_functions import createLDAPuser,addToLDAPGroup,removeFromLDAPGroup,getLDAPConn
import logging
logger = logging.getLogger(__name__)
# Create your tests here.

  
testuser="autosimulatortestuser"
test_device_id="autotestdevice"
# Create your tests here.
class SimulatorTests(TestCase):

 
    
    @classmethod
    def setUpTestData(cls):
        # Set up data for the whole TestCase
        
        logger.info('tests: settingup simulator tests')
        cls.group = Group.objects.get_or_create(name='editor')
        
        
        cls.user=User.objects.create(username=testuser,first_name="test",last_name="test",is_superuser=True)
        #create above user in LDAP Note user is not included in any groups unless
        #we specify to do so.
        result=createLDAPuser(cls.user,'supersecret')
        addToLDAPGroup(testuser,'active')

# Path: devices/models.py
# class Device(models.Model):
#     account = models.ForeignKey(User, on_delete=models.CASCADE,editable=False)
#     device_id=models.CharField(unique=True,max_length=50,validators=[alphanumeric],help_text="device should publish to mqtt on account_name/deviceID/appID/channelID")
#     device_desc = models.CharField(max_length=50,blank=True)
#     section = models.ForeignKey(Section, on_delete=models.CASCADE)
#     group=models.CharField(max_length=50,blank=True)
#     subgroup=models.CharField(max_length=50,blank=True)
#     latitude=models.FloatField(blank=True,null=True)
#     longitude=models.FloatField(blank=True,null=True)
#     model_name = models.CharField(max_length=50,blank=True)
#     install_date = models.DateTimeField('install_date',default=timezone.now)
# 
# 
#     def __str__(self):
#         return self.device_id
# 
# class Section(models.Model):
#     account = models.ForeignKey(User, on_delete=models.CASCADE,editable=False)
#     section_desc = models.CharField(max_length=50)
#     
#     def __str__(self):
#         return self.section_desc
# 
# Path: stack_configs/ldap_functions.py
# def createLDAPuser(new_user,password):
#     #returns true or false uses LDAP3  
#     con= getLDAPConn()
#     dn= str("cn=")+str(new_user.username)+str(",")+str(settings.AUTH_LDAP_USERS_OU_DN)
#     logger.info('createldap user dn %s',dn)
#     result=con.add(dn, 'inetOrgPerson', {
#         'givenName': new_user.first_name, 
#         'sn': new_user.last_name,
#         'mail': new_user.email,
#         'cn': new_user.username,
#         'displayName': str(new_user.id),
#         'uid': new_user.username,
#         'userPassword':password,
#         })
#     con.unbind()
#     logger.info('createldap user result %s',result)
#     return result
# 
# def addToLDAPGroup(user_name,group_name):
#     
#     #returns true or false uses LDAP3  
#     con= getLDAPConn()
#     group_dn= str("cn=")+str(group_name)+str(",")+str(settings.AUTH_LDAP_GROUPS_OU_DN)
#     logger.info('add user %s ToLdapgroup %s',user_name,group_name)
#     
#     result=con.modify(group_dn,
#                       {'memberUid': (MODIFY_ADD, [user_name])
#     })
#     
#     logger.info ("add group result is %s", result )
#     con.unbind()
#     return result
# 
# def removeFromLDAPGroup(user_name,group_name):
#     #this is used in testing
#     #returns true or false uses LDAP3  
#     con= getLDAPConn()
#     group_dn= str("cn=")+str(group_name)+str(",")+str(settings.AUTH_LDAP_GROUPS_OU_DN)
#     result=con.modify(group_dn,
#                       {'memberUid': (MODIFY_DELETE, [user_name])
#     })
#     
#     logger.info ("remove %s from group %s result is %s", user_name, group_name, result )
#     con.unbind()
#     return result
# 
# def getLDAPConn():
#     #ldap3
#     dn=settings.AUTH_LDAP_BIND_DN
#     password= settings.AUTH_LDAP_BIND_PASSWORD
#     conn=getLDAPConnWithUser(dn, password)
#     return conn


# Path: simulator/tests.py
from django.test import TestCase
from django.test import TestCase,Client
from devices.models import Device,Section
from django.contrib.auth.models import User,Group,Permission
from django.conf import settings
from stack_configs.ldap_functions import createLDAPuser,addToLDAPGroup,removeFromLDAPGroup,getLDAPConn
import logging

logger = logging.getLogger(__name__)
# Create your tests here.

  
testuser="autosimulatortestuser"
test_device_id="autotestdevice"
# Create your tests here.
class SimulatorTests(TestCase):

 
    
    @classmethod
    def setUpTestData(cls):
        # Set up data for the whole TestCase
        
        logger.info('tests: settingup simulator tests')
        cls.group = Group.objects.get_or_create(name='editor')
        
        
        cls.user=User.objects.create(username=testuser,first_name="test",last_name="test",is_superuser=True)
        #create above user in LDAP Note user is not included in any groups unless
        #we specify to do so.
        result=createLDAPuser(cls.user,'supersecret')
        addToLDAPGroup(testuser,'active')

        addToLDAPGroup(testuser,'editor')