def groupdelete(request, alias, domain): ''' This method deletes an existing group of the system. This will cause a cascading effect in the database, where by users of the group will be removed as well. ''' # Create model instance model = AdminModel() # Set the mode mode ="add" # Retrieve the requested alias. request_alias = model.getalias(alias,domain) # Retrieve the requested alias users. request_users = model.getaliasusers(alias,domain) # If there was no alias in the db with this name or # the function has thrown an error, we have been # given bogus input data if (request_alias and not request_alias.aliasname) or (not request_alias and model.error): # Raise domain has not be found raise Http404 else: params = {'mode': 3, 'alias': request_alias.aliasname, 'domain': request_alias.domainname, 'new_alias': None, 'notes':None } aliasdeleted = model.managealias(params) # Ensure no errors have occured if not aliasdeleted and model.error: errors = {'requestalias':request_alias, 'mode':mode, 'requestusers':request_users, 'dberror':model.error } return _groupmanageresponder(request, adict=errors) # Redirect to avoid post problems return HttpResponseRedirect (urlrel + 'admin/group/manage/')
def groupedit(request, alias, domain): ''' This method edits an existing alias of the system. it fills the form with the alias details and then awaits posted data from the user, validates said data, and if and error occurs uses the responder to display them to the user. A succesfull post will update the alias using the viewdb function and redirect to avoid post problems ''' # Create model instance model = AdminModel() # Set the mode mode ="edit" # Retrieve the requested alias. request_alias = model.getalias(alias, domain) # If there was no user in the db with this name or # the function has thrown an error, we have been # given bogus input data if ((request_alias and not request_alias.aliasname) or (not request_alias and model.error)): # Raise alias has not be found raise Http404 else: # Retrieve current alias users aliasusers = model.getaliasusers(alias, domain) # a list of users # associated to the domain of the alias domain_users = DomainUsers(domain, model.getnondisabledusers(domain)) request_users =[] if aliasusers: request_users = [user.username for user in model.getaliasusers(alias, domain)] # Check we have posted data if request.method =="POST": # copy posted data new_data = request.POST.copy() # The selected users users = request.POST.getlist('users') # Update post dict new_data.update({'users':users}) # bind form instance form = GroupEdit(groupname=request_alias.aliasname, kargs=new_data) # Ensure for is valid if form.is_valid(): # Clean the data cleaned_data = form.clean() # create function parameter list params = {'mode': 2, 'alias': request_alias.aliasname, 'domain': cleaned_data['domain'], 'new_alias': cleaned_data['alias'], 'notes': cleaned_data['notes'] } # call managealias in edit mode groupedited = model.managealias(params) # Ensure no errors have occured if not groupedited and model.error: errors = {'form':form, 'new_data':new_data, 'requestalias':request_alias, 'mode':mode, 'requestusers':request_users, 'domain_users':domain_users, 'dberror':model.error } return _groupmanageresponder(request, adict=errors) # Once the alias has been edit, # we update the selected users # Start by deleteing the previous alias users if request_users: for user in request_users: # Split the string of usernames # These need to be usernames as that # is the only unique constraint in the db userid, domain = user.split('@') # All previously selected users params = {'mode':3, # add alias users 'alias':cleaned_data['alias'], 'userid':userid, 'domain':domain } aliasuserdeleted = model.managealiasuser(params) if not aliasuserdeleted and model.error: errors = {'form':form, 'new_data':new_data, 'mode':mode, 'dberror':model.error, 'domain_users':domain_users, 'requestusers':request_users} return _groupmanageresponder(request, adict=errors) # Add new users if users: for user in users: # Split the string of usernames # These need to be usernames as that # is the only unique constraint in the db userid, domain = user.split('@') # All previously selected users params = {'mode':1, # add alias users 'alias':cleaned_data['alias'], 'userid':userid, 'domain':domain } aliasuseradded = model.managealiasuser(params) if not aliasuseradded and model.error: errors = {'form':form, 'new_data':new_data, 'mode':mode, 'dberror':model.error, 'domain_users':domain_users, 'requestusers':request_users} return _groupmanageresponder(request, adict=errors) # Redirect to avoid post problems return HttpResponseRedirect (urlrel + 'admin/group/manage/') # If not valid call responder with error else: errors = {'form':form,'new_data':new_data, 'mode':mode, 'requestalias':request_alias, 'domain_users':domain_users, 'requestusers':request_users} return _groupmanageresponder(request, adict=errors) return _groupmanageresponder(request, adict= {'requestalias':request_alias, 'requestusers':request_users, 'domain_users':domain_users, 'mode': mode}) return HttpResponseRedirect(urlrel + 'admin/group/manage/')
class MailTestCase(unittest.TestCase): def setUp(self): # Get connection singleton connection = CLWConnection(test_db_host, test_db_name, test_db_user, test_db_pass) # Create a new cursor object. cursor = connection.cursor() params = {'mode': 1, 'domainname': 'example.co.uk', 'newdomainname': None, } self.adminmodel = AdminModel() self.authmodel = AuthModel() self.mailmodel = MailModel() self.eximmodel = EximModel() domainadded = self.adminmodel.managedomain(params) # Create a user params = {'mode': 1, 'userid': 'test.a', 'password': '******', 'home': '/home/mailstore/', 'domain': 'example.co.uk', 'uid': 1001, 'gid': 1001, 'status': 1, 'typer':'Person', 'role':"", 'fullname':'Test User', 'notes':None } # add the user useradded = self.adminmodel.manageuser(params) # Check user added self.assertNotEqual(useradded, False) def test_auth(self): # Check login user = self.authmodel.login('*****@*****.**', 'password') # check user returned self.assertEqual(user.userid, 'test.a') # Check not working user = self.authmodel.login('*****@*****.**', 'password') self.assertEqual(user.userid, None) def test_usermanage(self): # Create a user params = {'mode': 1, 'userid': 'test.b', 'password': '******', 'home': '/home/mailstore/', 'domain': 'example.co.uk', 'uid': 1001, 'gid': 1001, 'status': 1, 'typer':'Person', 'role':"Architect", 'fullname':'Test User', 'notes':"here are some notes" } # add the user useradded = self.adminmodel.manageuser(params) # Check user added self.assertNotEqual(useradded, False) # add the user useradded = self.adminmodel.manageuser(params) user = self.adminmodel.getuser('test.b', 'example.co.uk') self.assertNotEqual(user, None) self.assertEqual(user.password,'password') self.assertEqual(user.userid,'test.b') self.assertEqual(user.domain,'example.co.uk') self.assertEqual(user.status,1) self.assertEqual(user.typer,'Person') self.assertEqual(user.uid,1001) self.assertEqual(user.gid,1001) self.assertEqual(user.role,'Architect') self.assertEqual(user.notes,'here are some notes') # Edit a user params = {'mode': 2, 'userid': 'test.b', 'password': '******', 'home': '/home/mailstore/', 'domain': 'example.co.uk', 'uid': 1001, 'gid': 1001, 'status': 1, 'typer':'Person', 'role':"Architect", 'fullname':'Test User edited', 'notes':"here are some notes" } # edit the user useredited = self.adminmodel.manageuser(params) # Check user edited self.assertNotEqual(useredited, False) user = self.adminmodel.getuser('test.b', 'example.co.uk') self.assertNotEqual(user, None) self.assertEqual(user.password,'changedpass') self.assertEqual(user.fullname,'Test User edited') #hide a user params = {'mode': 4, 'userid': 'test.b', 'password': None, 'home': None, 'domain':'example.co.uk', 'uid': None, 'gid': None, 'status': 9, 'typer':None, 'role':None, 'fullname':None, 'notes':None } # edit the user userhidden = self.adminmodel.manageuser(params) # Check user edited self.assertNotEqual(userhidden, False) user = self.adminmodel.getuser('test.b', 'example.co.uk') self.assertNotEqual(user, None) self.assertEqual(user.status,9) # delete a user params = {'mode': 3, 'userid': 'test.b', 'password': None, 'home': None, 'domain': 'example.co.uk', 'uid': None, 'gid': None, 'status': 9, 'typer':None, 'role':None, 'fullname':None, 'notes':None } # delete the user userdeleted = self.adminmodel.manageuser(params) self.assertNotEqual(userdeleted, False) user = self.adminmodel.getuser('test.b', 'example.co.uk') self.assertEqual(user.userid, None) def test_goupmanage(self): # Create a group params = {'mode': 1, 'alias': 'agroup', 'domain': 'example.co.uk', 'new_alias': None, 'notes':"test" } # try add the group groupadded = self.adminmodel.managealias(params) # Check group was added self.assertEqual(groupadded, True) params = {'mode':1, # add alias users 'alias':'agroup', 'userid':'test.a', 'domain':'example.co.uk' } # check added user to alias aliasuseradded = self.adminmodel.managealiasuser(params) self.assertEqual(aliasuseradded, True) # Retrieve info alias = self.adminmodel.getalias('agroup', 'example.co.uk') self.assertNotEqual(alias, None) self.assertEqual(alias.aliasname,'agroup') self.assertEqual(alias.notes,'test') users = self.adminmodel.getaliasusers('agroup','example.co.uk') self.assertNotEqual(users, None) users = list(users) self.assertEqual(len(users),1) user = users[0] self.assertEqual(user.username,'*****@*****.**') # create function parameter list params = {'mode': 2, 'alias': 'agroup', 'domain': 'example.co.uk', 'new_alias': 'bgroup', 'notes': None } # edit the group groupedited = self.adminmodel.managealias(params) # Check alias edited self.assertNotEqual(groupedited, False) alias = self.adminmodel.getalias('bgroup', 'example.co.uk') self.assertNotEqual(alias, None) self.assertEqual(alias.aliasname,'bgroup') self.assertEqual(alias.notes,None) users = self.adminmodel.getaliasusers('bgroup','example.co.uk') self.assertNotEqual(users, None) users = list(users) self.assertEqual(len(users),1) user = users[0] self.assertEqual(user.username,'*****@*****.**') # create function parameter list params = {'mode': 3, 'alias': 'bgroup', 'domain': 'example.co.uk', 'new_alias': None, 'notes':None } # delete the group groupdeleted = self.adminmodel.managealias(params) # Check user edited self.assertNotEqual(groupdeleted, False) # Ensure there was a cascding delete of alias users users = self.adminmodel.getaliasusers('bgroup','example.co.uk') self.assertEqual(users, None) def test_domainmanage(self): # Create a domain params = {'mode': 1, 'domainname': 'HOPKINS.CO.UK', 'newdomainname': None, } # try add the domain domainadded = self.adminmodel.managedomain(params) # Check domain was NOT added self.assertEqual(domainadded, False) self.assertEqual(self.adminmodel.error, "This domain already exists\n") # Create a domain params = {'mode': 1, 'domainname': 'otherdomain.com', 'newdomainname': None, } # try add the domain domainadded = self.adminmodel.managedomain(params) # Check domain was added self.assertEqual(domainadded, True) # Get the domain request_domain = self.adminmodel.getdomain('otherdomain.com') self.assertEqual(request_domain.domainname, 'otherdomain.com') params = {'mode': 2, 'domainname': 'example.co.uk', 'newdomainname': 'newexample.co.uk', } # check edited domain domainedited = self.adminmodel.managedomain(params) self.assertEqual(domainedited, True) # Get the domain request_domain = self.adminmodel.getdomain('newexample.co.uk') self.assertEqual(request_domain.domainname, 'newexample.co.uk') # Check changed reflected in users user = self.adminmodel.getuser('test.a', 'newexample.co.uk') self.assertNotEqual(user, None) self.assertEqual(user.domain, 'newexample.co.uk') self.assertEqual(user.userid, 'test.a') # Delete a domain params = {'mode': 3, 'domainname': 'otherdomain.com', 'newdomainname': None, } # try delete the domain domaindeleted = self.adminmodel.managedomain(params) # Check domain was deleted self.assertEqual(domaindeleted, True) # Get the domain request_domain = self.adminmodel.getdomain('otherdomain.com') self.assertEqual(request_domain.domainname, None) # try delete domain with reference to user params = {'mode': 3, 'domainname': 'newexample.co.uk', 'newdomainname': None, } # try delete the domain domaindeleted = self.adminmodel.managedomain(params) # Check domain was Not deleted self.assertEqual(domaindeleted, False) self.assertEqual(self.adminmodel.error, "This domain is referenced by users of the system and cannot be deleted.\n") # Get the domain request_domain = self.adminmodel.getdomain('newexample.co.uk') self.assertNotEqual(request_domain, None) def test_holidaymanage(self): #Add holiday param_dict = {'mode':1, 'username':'******', 'domain':'example.co.uk', 'hstart':'2008-01-01 09:00:00', 'hend':'2008-01-10 09:00:00', 'message':"The holiday message", 'id':-1, 'b_default':False} result = self.mailmodel.manageholiday(**param_dict) self.assertNotEqual(result, None) holidays = self.mailmodel.getuserholidays( 'test.a', 'example.co.uk') self.assertNotEqual(holidays, None) holidays = list(holidays) self.assertEqual(len(holidays), 1) holiday = holidays[0] self.assertEqual(holiday.holstart.year,2008) self.assertEqual(holiday.holstart.month,1) self.assertEqual(holiday.holstart.day,1) self.assertEqual(holiday.holstart.hour,9) self.assertEqual(holiday.holstart.minute,0) self.assertEqual(holiday.holend.year,2008) self.assertEqual(holiday.holend.month,1) self.assertEqual(holiday.holend.day,10) self.assertEqual(holiday.holend.hour,9) self.assertEqual(holiday.holend.minute,0) self.assertEqual(holiday.holmsg,'The holiday message') #Edit holiday param_dict = {'mode':2, 'username':'******', 'domain':'example.co.uk', 'hstart':'2008-01-01 10:00:00', 'hend':'2008-01-10 09:00:00', 'message':"The edit message", 'id':holiday.holid, 'b_default':False} result = self.mailmodel.manageholiday(**param_dict) self.assertNotEqual(result, None) holidays = self.mailmodel.getuserholidays( 'test.a', 'example.co.uk') self.assertNotEqual(holidays, None) holidays = list(holidays) self.assertEqual(len(holidays), 1) holiday = holidays[0] self.assertEqual(holiday.holstart.year,2008) self.assertEqual(holiday.holstart.month,1) self.assertEqual(holiday.holstart.day,1) self.assertEqual(holiday.holstart.hour,10) self.assertEqual(holiday.holstart.minute,0) self.assertEqual(holiday.holmsg,'The edit message') #Delete holiday param_dict = {'mode':3, 'username':'******', 'domain':'example.co.uk', 'hstart':None, 'hend':None, 'message':None, 'id':holiday.holid, 'b_default': None} result = self.mailmodel.manageholiday(**param_dict) self.assertNotEqual(result, None) holidays = self.mailmodel.getuserholidays( 'test.a', 'example.co.uk') self.assertEqual(holidays, None) # Check setting of holdiday to current now = datetime.now() tendays = now + timedelta (days =10) #Add holiday param_dict = {'mode':1, 'username':'******', 'domain':'example.co.uk', 'hstart':now, 'hend':tendays, 'message':"current message", 'id':-1, 'b_default':False} result = self.mailmodel.manageholiday(**param_dict) self.assertNotEqual(result, None) b_holset = self.eximmodel.setholidays() self.assertEqual(b_holset, 1) holmessage = self.mailmodel.getcurrentholiday('test.a','example.co.uk') self.assertNotEqual(holmessage, None) self.assertEqual(holmessage.message, 'current message') # Remove current holiday param_dict = {'mode':2, 'username':'******', 'domain':'example.co.uk', 'hstart':None, 'hend':None, 'message':None, 'id':None} result = self.mailmodel.removeleave(**param_dict) user = self.adminmodel.getuser('test.a', 'example.co.uk') self.assertNotEqual(user, None) self.assertEqual(user.status,1) # Test leaver # Create a user params = {'mode': 1, 'userid': 'leaver', 'password': '******', 'home': '/home/mailstore/', 'domain': 'example.co.uk', 'uid': 1001, 'gid': 1001, 'status': 1, 'typer':'Person', 'role':"", 'fullname':'Leaver', 'notes':None } # add the user useradded = self.adminmodel.manageuser(params) # Check user added self.assertNotEqual(useradded, False) # set user to leaver params = {'mode': 1, 'userid': 'leaver', 'domain': 'example.co.uk', 'leavedate': now, 'message': 'This person has left' } result = self.eximmodel.manageleaver(params) self.assertNotEqual(result, None) message = self.eximmodel.getleavermessage('leaver','example.co.uk') self.assertNotEqual(message,None) self.assertEqual(message,'This person has left') # unset user to leaver params = {'mode': 2, 'userid': 'leaver', 'domain': 'example.co.uk', 'leavedate':None, 'message':None } result = self.eximmodel.manageleaver(params) self.assertNotEqual(result, None) # Check changed reflected in users user = self.adminmodel.getuser('test.a', 'example.co.uk') self.assertNotEqual(result, None) self.assertEqual(user.status,1) def test_exim(self): ''' This test case ensures that the exim functions used are working properly''' active_user = self.eximmodel.getactiveuser('test.a','example.co.uk') self.assertNotEqual(active_user, None) self.assertEqual(active_user.userid,'test.a') holiday_user = self.eximmodel.getactiveuser('test.a','example.co.uk') self.assertNotEqual(holiday_user, None) self.assertEqual(holiday_user.userid,'test.a') now = datetime.now() tendays = now + timedelta (days =10) #Add holiday param_dict = {'mode':1, 'username':'******', 'domain':'example.co.uk', 'hstart':now, 'hend':tendays, 'message':"current message", 'id':-1, 'b_default':False} result = self.mailmodel.manageholiday(**param_dict) self.assertNotEqual(result, None) b_holset = self.eximmodel.setholidays() self.assertEqual(b_holset, 1) # unset user to leaver params = {'mode': 1, 'userid': 'test.a', 'domain': 'example.co.uk', 'leavedate':now, 'message':"leaving" } result = self.eximmodel.manageleaver(params) self.assertNotEqual(result, None) leave_user = self.eximmodel.getleaveruser('test.a','example.co.uk') self.assertNotEqual(leave_user, None) self.assertEqual(leave_user.userid,'test.a') params = {'userid': 'test.a', 'domain': 'example.co.uk'} result = self.eximmodel.manageactiveusers(params) self.assertNotEqual(result, None) active_user = self.adminmodel.getuser('test.a','example.co.uk') self.assertNotEqual(active_user, None) self.assertEqual(active_user.status,1) def tearDown(self): query = "DELETE FROM users;" self.adminmodel.executequery(query) query = "DELETE FROM domains;" self.adminmodel.executequery(query)