def setUp(self): # Getting all permissions codenames all_perms = Permission.objects.all() perms_codenames = list() for perm in all_perms: perms_codenames.append(perm.codename) self.test_cases = 10 # Create group Administrator (Allowed: Create, Update, Delete) self.user = UserFactory.build() self.user.username = "******" self.user.set_password("password") self.user_permissions = perms_codenames # Give all permissions to user self.user.save() self.login_url = reverse("api:login") self.extdatas_url = reverse("api:sources-externaldata_list") # Create token for user authentication self.auth_token = Token.objects.create(user=self.user) self.auth_url = "Token " + self.auth_token.key self.extdatas = list() for i in range(self.test_cases): self.new_extdata = ExternalDataFactory.build() self.new_extdata.save() self.extdatas.append(self.new_extdata) i = i + 1
def step_user_exists(self, username): r'user "([^"]+)" exists' user = UserFactory.build(username=username) user.is_staff = True user.set_password(username) user.save()
def test_should_return_not_valid_if_too_many_tasks(self): # Arrange author = UserFactory.build() owner = UserFactory.build() tasks_manager = Mock(Task.objects) tasks_manager.get_for_owner_by_author.return_value.count.return_value = TASKS_LIMIT + 1 form = RestrictedAddTaskForm( self._data, author=author, owner=owner, tasks_manager=tasks_manager) # Act result = form.is_valid() # Assert self.assertFalse(result) msg = 'You have added to many tasks for %s' % owner.username self.assertTrue(msg in form.errors['__all__'])
def test_should_ignore_priority_if_passed(self): # Arrange author = UserFactory.build() owner = UserFactory.build() tasks_manager = Mock(Task.objects) tasks_manager.get_for_owner_by_author.return_value.count.return_value = 0 form = RestrictedAddTaskForm( self._data, author=author, owner=owner, tasks_manager=tasks_manager) # Act result = form.is_valid() # Assert self.assertTrue(result) self.assertEqual(form.cleaned_data['title'], self._title) self.assertTrue('priority' not in form.cleaned_data)
def test_should_return_not_valid_if_too_many_tasks(self): # Arrange author = UserFactory.build() owner = UserFactory.build() tasks_manager = Mock(Task.objects) tasks_manager.get_for_owner_by_author.return_value.count.return_value = TASKS_LIMIT + 1 form = RestrictedAddTaskForm(self._data, author=author, owner=owner, tasks_manager=tasks_manager) # Act result = form.is_valid() # Assert self.assertFalse(result) msg = 'You have added to many tasks for %s' % owner.username self.assertTrue(msg in form.errors['__all__'])
def test_should_ignore_priority_if_passed(self): # Arrange author = UserFactory.build() owner = UserFactory.build() tasks_manager = Mock(Task.objects) tasks_manager.get_for_owner_by_author.return_value.count.return_value = 0 form = RestrictedAddTaskForm(self._data, author=author, owner=owner, tasks_manager=tasks_manager) # Act result = form.is_valid() # Assert self.assertTrue(result) self.assertEqual(form.cleaned_data['title'], self._title) self.assertTrue('priority' not in form.cleaned_data)
def test_should_return_task_absolute_url(self): # Arrange owner = UserFactory.build(pk=1) task = TaskFactory.build(owner=owner, author=owner) # Act url = task.get_absolute_url() # Assert self.assertEqual(url, '/%s/' % owner.username)
def test_should_validate_input(self): # Arrange owner = UserFactory.build() form = AddTaskForm(self._data, author=owner, owner=owner) # Act result = form.is_valid() # Assert self.assertTrue(result) self.assertEqual(form.cleaned_data['title'], self._title) self.assertEqual(form.cleaned_data['priority'], self._priority)
def test_should_create_task_with_default_priority(self): # Arrange author = UserFactory.create() owner = UserFactory.create() tasks_manager = Mock(Task.objects) form = RestrictedAddTaskForm( self._data, author=author, owner=owner, tasks_manager=tasks_manager) author_tasks = tasks_manager.get_for_owner_by_author.return_value author_tasks.count.return_value = 2 # Act form.is_valid() obj = form.save() # Assert self.assertIsNotNone(obj.pk) self.assertEqual(obj.title, self._title) self.assertEqual(obj.priority, 4) self.assertEqual(obj.author, author) self.assertEqual(obj.owner, owner)
def test_should_show_only_author_tasks_on_foreign_profile(self): # Arrange template_name = 'tasks/show_current_tasks.html' owner = UserFactory.build() wife = UserFactory.build() bread = "buy bread" milk = "buy milk" tasks = [ TaskFactory.build(title=bread, owner=owner, author=owner), TaskFactory.build(title=milk, owner=owner, author=wife), ] context = { 'tasks': tasks, 'user': wife, } # Act result = render_to_string(template_name, context) # Assert self.assertFalse("buy bread" in result) self.assertTrue("buy milk" in result)
def test_should_create_task_with_default_priority(self): # Arrange author = UserFactory.create() owner = UserFactory.create() tasks_manager = Mock(Task.objects) form = RestrictedAddTaskForm(self._data, author=author, owner=owner, tasks_manager=tasks_manager) author_tasks = tasks_manager.get_for_owner_by_author.return_value author_tasks.count.return_value = 2 # Act form.is_valid() obj = form.save() # Assert self.assertIsNotNone(obj.pk) self.assertEqual(obj.title, self._title) self.assertEqual(obj.priority, 4) self.assertEqual(obj.author, author) self.assertEqual(obj.owner, owner)
def test_should_return_tasks_for_owner_by_author(self): # Arrange author = UserFactory.create() TaskFactory.create_batch(self._num, owner=self._owner, author=author) # Act result = Task.objects.get_for_owner_by_author(self._owner, author) # Assert self.assertEqual(len(result), self._num) for task in result: self.assertEqual(task.owner, self._owner) self.assertEqual(task.author, author) self.assertNotEqual(author, self._owner)
def test_should_create_task(self): # Arrange owner = UserFactory.create() form = AddTaskForm(self._data, author=owner, owner=owner) # Act form.is_valid() obj = form.save() # Assert self.assertIsNotNone(obj.pk) self.assertEqual(obj.title, self._title) self.assertEqual(obj.priority, self._priority) self.assertEqual(obj.author, owner) self.assertEqual(obj.owner, owner)
def test_should_return_tasks_for_owner_by_author(self): # Arrange author = UserFactory.create() TaskFactory.create_batch( self._num, owner=self._owner, author=author) # Act result = Task.objects.get_for_owner_by_author(self._owner, author) # Assert self.assertEqual(len(result), self._num) for task in result: self.assertEqual(task.owner, self._owner) self.assertEqual(task.author, author) self.assertNotEqual(author, self._owner)
def setUp(self): self._owner = UserFactory.create() self._num = 10