def setUp(self): super(ReviewerManagerTests, self).setUp() self.manager = ReviewerManager() self.manager.model = Reviewer # create a test user user_manager = UserManager() user_manager.model = User self.user = user_manager.create_user(username="******", first_name="test", last_name="test", email="*****@*****.**", password="******") # create another test user to be used a module leader # (module leader model not implemented atm) self.module_leader = user_manager.create_user(username="******", first_name="Module", last_name="Leader", email="*****@*****.**", password="******") # create a test module module_model = Module self.module = module_model.objects.create( module_code="CM1101", module_name="Test Module", module_credits="10", module_level="1", semester="Autumn Semester", delivery_language="English", module_leader=self.module_leader)
def setUp(self): super(LoggedInTestCase, self).setUp() # set up sessions in unit tests session_engine = import_module(settings.SESSION_ENGINE) store = session_engine.SessionStore() store.save() # set the client self.client = Client() self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key manager = UserManager() manager.model = User # create a sample admin user self.admin = manager.create_superuser( username='******', first_name='admin', last_name='admin', email='*****@*****.**', password='******' ) # create sample basic user self.user = manager.create_user( username='******', first_name='user', last_name='user', email='*****@*****.**', password='******' )
def setUp(self): manager = UserManager() manager.model = User self.password = "******" self.user = manager.create(username="******", first_name="Tester", last_name="Test", email="*****@*****.**", password=self.password) self.email = UserPasswordEmail
def setUp(self): super(TestModule, self).setUp() user_manager = UserManager() user_manager.model = User self.model = Module self.user = user_manager.create_user(username="******", first_name="test", last_name="test", email="*****@*****.**", password="******")
def setUp(self): super(TestUserPasswordForm, self).setUp() # test user to check that the password is changed self.manager = UserManager() self.manager.model = User self.user = self.manager.create_user(username="******", first_name="test", last_name="tester", email="*****@*****.**", password="******") self.form = UserPasswordForm self.std_error = ['This field is required.']
def setUp(self): super(ProgrammeTutorTests, self).setUp() self.model = ProgrammeTutor user_manager = UserManager() user_manager.model = User self.user = user_manager.create_user(username="******", first_name="test", last_name="test", email="*****@*****.**", password="******")
def perform_create(self, serializer): instance = serializer.save() if instance.business: info = dict() # create business info['id'] = instance.id info['email'] = instance.email info['username'] = instance.username info['business_name'] = instance.business_name info['user_api_key'] = instance.business_api_key info['nmi_login'] = '' info['expiration_date'] = instance.expiration_date info['phone_number'] = instance.phone_number info['first_name'] = instance.first_name info['last_name'] = instance.last_name info['street_branch_address'] = instance.street_branch_address info['apt_branch_address'] = instance.apt_branch_address info['city_branch_address'] = instance.city_branch_address info['state_branch_address'] = instance.state_branch_address info['country_branch_address'] = instance.country_branch_address info['zip_branch_address'] = instance.zip_branch_address info['street_hq_address'] = instance.street_hq_address info['apt_hq_address'] = instance.apt_hq_address info['city_hq_address'] = instance.city_hq_address info['state_hq_address'] = instance.state_hq_address info['country_hq_address'] = instance.country_hq_address info['zip_hq_address'] = instance.zip_hq_address u = UserManager() u.createBusiness(info) if instance.customer: #create customer info = dict() info['id'] = instance.id info['business'] = instance.business_FK # business_FK info['username'] = instance.username info['first_name'] = instance.first_name info['last_name'] = instance.last_name info['phone_number'] = instance.phone_number info['email'] = instance.email info['user_api_key'] = instance.business_api_key info['active'] = instance.active info['street_home_address'] = instance.street_home_address info['apt_home_address'] = instance.apt_home_address info['city_home_address'] = instance.city_home_address info['state_home_address'] = instance.state_home_address info['country_home_address'] = instance.country_home_address info['zip_home_address'] = instance.zip_home_address u = UserManager() u.createCustomer(info) instance.set_password(instance.password) instance.save()
def setUp(self): super(ModuleTestCase, self).setUp() manager = UserManager() manager.model = User # Create a sample module leader self.module_leader = manager.create_user( username='******', first_name='Module', last_name='Leader', email='*****@*****.**', password='******' ) # Create a test module self.module = Module.objects.create( module_code="CMXXXX", module_name="Test Module", module_credits="10", module_level="L1", semester="Autumn Semester", delivery_language="English", module_leader=self.module_leader ) # Create a second test module self.module_two = Module.objects.create( module_code="CMYYYY", module_name="Test Module 2", module_credits="10", module_level="L1", semester="Autumn Semester", delivery_language="English", module_leader=self.module_leader )
class ReviewerManager(object): """ Manager to assist in the creation of Reviewer models Module(s) must be stored in a list """ def __init__(self): self.user_manager = UserManager() self.user_manager.model = User def create_new_reviewer(self, modules, username, first_name, last_name, email, password=None): if not isinstance(modules, (list,)): modules = [modules] # create the basic user user = self.user_manager.create_user( username=username, first_name=first_name, last_name=last_name, email=email, password=password ) # create the reviewer model model = self.__create_model(modules, user) # set the permissions user.is_module_reviewer = True user.save() return model def create_reviewer(self, modules, user=None): """ Method to create a reviewer from an existing user. Will configure the user permissions to be a reviewer. Accepts a list of module objects and a user object """ if modules is None: raise ValueError("At least one module must be selected") if user is None: raise ValueError("Must select a user") if not isinstance(modules, (list,)): modules = [modules] # create the reviewer model = self.__create_model(modules, user) if model is None: return None # update user permissions now the model has been created. user.is_module_reviewer = True user.save() return model def __create_model(self, modules, user): try: reviewer = self.model.objects.create(user=user) reviewer.save() for module in modules: reviewer.modules.add(module) return reviewer except IntegrityError: return None
def __init__(self): self.user_manager = UserManager() self.user_manager.model = User
class ProgrammeTutorManager(object): """ Manager to assit in the creation of Programme Tutors models """ def __init__(self): self.user_manager = UserManager() self.user_manager.model = User def create_new_tutor(self, programme_name, tutor_year, username, first_name, last_name, email, password=None): """ Create a new user with all of the expected parameters as the user manager for the programme tutor. """ # create the user to become a programme tutor user = self.user_manager.create_user(username=username, first_name=first_name, last_name=last_name, email=email, password=password) # create the year tutor model model = self.__create_model(programme_name, tutor_year, user) # set the permissions now the model is created user.is_year_tutor = True user.save() return model def create_tutor(self, programme_name, tutor_year, user): """ Method to create a programme tutor on an existing user. Will configure the user permissions to be a year tutor. """ if user is None: raise ValueError("User must not be None") # create the year tutor model = self.__create_model(programme_name, tutor_year, user) if model is None: return None # update user permissions now the model has been created. user.is_year_tutor = True user.save() return model def __create_model(self, programme_name, tutor_year, user): """ Private method to actually create the model. Could raise the Django IntegrityError if one to one relationship is violated. """ # if the programme name and the tutor year are blank if len(programme_name) <= 0 or len(tutor_year) <= 0: raise ValueError( "Programme name and tutor year cannot be empty strings") try: tutor = self.model.objects.create(programme_name=programme_name, tutor_year=tutor_year, programme_tutor_user=user) return tutor except IntegrityError: return None
class TestUserPasswordForm(TestCase): """ Unit for UserPasswordForm """ def setUp(self): super(TestUserPasswordForm, self).setUp() # test user to check that the password is changed self.manager = UserManager() self.manager.model = User self.user = self.manager.create_user(username="******", first_name="test", last_name="tester", email="*****@*****.**", password="******") self.form = UserPasswordForm self.std_error = ['This field is required.'] def test_valid_password_form(self): password1 = "new_password" password2 = "new_password" data = {'password1': password1, 'password2': password2} form = self.form(data) # check that the form is valid self.assertTrue(form.is_valid()) self.assertEquals(form.cleaned_data['password1'], password1) self.assertEquals(form.cleaned_data['password2'], password2) self.assertEquals(form.errors, {}) # check that the passwords match self.assertEquals(form.clean_password(), password1) # check that the password is updated for a user self.assertTrue(form.update_password(self.user.id)) def test_valid_password_form_mismatch_data(self): """ Test case for checking how the form handles password mismatch. """ password1 = "new_password1" password2 = "new_password2" data = {'password1': password1, 'password2': password2} form = self.form(data) # check that the form is valid self.assertTrue(form.is_valid()) self.assertEquals(form.cleaned_data['password1'], password1) self.assertEquals(form.cleaned_data['password2'], password2) self.assertEquals(form.errors, {}) # check exception is raise when cleaning password with self.assertRaises(ValidationError): form.clean_password() # check password cannot be processed self.assertFalse(form.update_password(self.user.id)) def test_invalid_password_form_empty_data(self): """ Test case to see how form handles empty data """ data = {} form = self.form(data) self.assertFalse(form.is_valid()) self.assertEquals(form.errors['password1'], self.std_error) self.assertEquals(form.errors['password2'], self.std_error) # check that empty data raises an exception with self.assertRaises(ValidationError): form.clean_password() # cannot update the password so returns false self.assertFalse(form.update_password(self.user.id)) def test_update_password_method_with_invlaid_params(self): """ Test case for possible cases that could occur for with an invalid user_id parameter. """ # provide valid input to test the method password1 = "new_password1" password2 = "new_password2" data = {'password1': password1, 'password2': password2} form = self.form(data) # test the method with negative numbers self.assertFalse(form.update_password(-13)) self.assertFalse(form.update_password(-1358584)) # test with zero self.assertFalse(form.update_password(0)) # test with an invalid user id self.assertFalse(form.update_password(1000)) self.assertFalse(form.update_password(100045456))
def setUp(self): self.manager = UserManager() self.manager.model = User
class UserManagerTestCase(TestCase): """ Test case for UserManager object that is found with the user model. """ def setUp(self): self.manager = UserManager() self.manager.model = User def test_creating_standard_user(self): """ Test creation of basic user """ user = self.manager.create_user( username='******', first_name='test', last_name='user', email='*****@*****.**', password='******' ) self.assertEqual(user.username, "test1") # make sure the manager gave all false permissions self.assertFalse(user.is_admin) self.assertFalse(user.is_staff()) self.assertFalse(user.is_module_leader) self.assertFalse(user.is_office_admin) self.assertFalse(user.is_year_tutor) def test_creating_standard_user_no_username(self): """ Test to determine exception is raised when no username is provided. """ with self.assertRaises(ValueError): user = self.manager.create_user( username='', first_name='test', last_name='user', email='*****@*****.**', password='******' ) def test_creating_standard_user_no_first_name(self): """ Test to determine exception is raised when no first name is provided. """ with self.assertRaises(ValueError): user = self.manager.create_user( username='******', first_name='', last_name='user', email='*****@*****.**', password='******' ) def test_creating_standard_user_no_last_name(self): """ Test to determine exception is raised when no last name is provided. """ with self.assertRaises(ValueError): user = self.manager.create_user( username='******', first_name='test', last_name='', email='*****@*****.**', password='******' ) def test_creating_standard_user_no_email(self): """ Test to determine exception is raised when no email is provided. """ with self.assertRaises(ValueError): user = self.manager.create_user( username='******', first_name='test', last_name='user', email='', password='******' ) def test_creating_user_same_username(self): """ Test to determine what happens when a user creates an account with an existing username. """ # create the test user self.manager.create_user( username='******', first_name='test', last_name='user', email='*****@*****.**', password='******' ) # check that the exception is raised with self.assertRaises(ValueError): user = self.manager.create_user( username='******', first_name='test', last_name='user', email='test', password='******' ) def test_creating_super_user(self): """ Test for creating a user user """ user = self.manager.create_superuser( username='******', first_name='admin', last_name='user', email='*****@*****.**', password='******' ) self.assertEqual(user.username, "admin") # make sure it is a superuser self.assertTrue(user.is_admin) self.assertTrue(user.is_staff())