# 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
            "/admin/simulator/thing/",
            "/admin/simulator/thing/add/",
           
            
        ]
        for page in admin_pages:
            logger.info('testing GET page %s',page)
            response = self.client.get(page)
            self.assertEqual(response.status_code, 200)
            
      
    
        
    
    def test_All(self):
        
        self.Test_admin_pages()
    
    def tearDown(self):
        #remove device from ldap
        logger.info('tests: tearDown device tests')
        removeFromLDAPGroup(test_device_id,'device')
       
        dn= str("cn=")+str(test_device_id)+str(",")+str(settings.AUTH_LDAP_USERS_OU_DN)
        con= getLDAPConn()
        result=con.delete(dn)
        logger.info('delete device in test_cleanup %s', result)
        
        #remove test user from ldap if necessary, also remove from groups.
        removeFromLDAPGroup(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

            "/admin/simulator/thing/",
            "/admin/simulator/thing/add/",
           
            
        ]
        for page in admin_pages:
            logger.info('testing GET page %s',page)
            response = self.client.get(page)
            self.assertEqual(response.status_code, 200)
            
      
    
        
    
    def test_All(self):
        
        self.Test_admin_pages()
    
    def tearDown(self):
        #remove device from ldap
        logger.info('tests: tearDown device tests')
        removeFromLDAPGroup(test_device_id,'device')
       
        dn= str("cn=")+str(test_device_id)+str(",")+str(settings.AUTH_LDAP_USERS_OU_DN)
        con= getLDAPConn()
        result=con.delete(dn)
        logger.info('delete device in test_cleanup %s', result)
        
        #remove test user from ldap if necessary, also remove from groups.
        removeFromLDAPGroup(testuser,'active')

        removeFromLDAPGroup(testuser,'editor')