Ejemplo n.º 1
0
    def _edit_category(self, category_tc, is_new = True):
        dbFacade = self.dbFacade();
        new_category_name = request.params.get('name');
        parent_name = request.params.get('parent');

        if dbFacade.balances.categoryDao.find_by_name(new_category_name, h.authenticated_user().uid):
            return { "failure" : Messages.categoryAlreadyExists(name=new_category_name) }
        parent_tc = dbFacade.balances.categoryDao.find_by_name(parent_name, h.authenticated_user().uid)

        if not parent_tc or parent_tc.name == '' or not new_category_name:
            return { "failure" : Messages.invalidArguments() }
        if not hasattr(parent_tc, 'subcats') or not parent_tc.subcats:            
            return { "failure" : Messages.wrongParentCategory(name=parent_name) }

        category_tc.name = new_category_name
        category_tc.parent = parent_tc.key
        category_tc.user_uid = h.authenticated_user().uid
        dbFacade.balances.categoryDao.save(category_tc)

        if not is_new:
            self._change_changes_category(request.params.get('old_name'), new_category_name)

        dbFacade.db.commit()
        return { "id" : category_tc.uid }     
    def test_category_manage(self):

        existing_balance_url = url_for(controller='rest/balance_changes', action='list')
        response = self.app.get(existing_balance_url)
        self.assertEquals(200, response.status)

        signin_form = response.forms.get('login_form')
        self.failUnless(signin_form)
        signin_form.set('username', 'disorder@3sk8tm')
        signin_form.set('password', 'd0n0tch@nge')
        response = signin_form.submit()
        self.assertEquals(200, response.status)

        category_name = 'Test1'        
        
        self.failIf(self.__in_categories(category_name)) 
        
        """ Add new category - Test1 """
        existing_balance_url = url_for(controller='rest/balance_categories', action='add', name=category_name, parent='Dom')
        response = self.app.post(existing_balance_url)
        self.assertEquals(200, response.status)
        self.assertEquals('application/x-javascript', response.header('Content-Type'))
        result = json.loads(response.body)
        self.failIf(result is None)        
        status = result.get('id')
        self.failIf(status is None)


        """ Add category again - Test1 should be failure"""
        existing_balance_url = url_for(controller='rest/balance_categories', action='add', name=category_name, parent='Dom')
        response = self.app.post(existing_balance_url)
        self.assertEquals(200, response.status)
        self.assertEquals('application/x-javascript', response.header('Content-Type'))
        result = json.loads(response.body)
        self.failIf(result is None)
        status = result.get('failure')
        self.assertEquals(status, Messages.categoryAlreadyExists(name=category_name))
        self.assertEquals(self.__in_categories(category_name), True)

        """ Edit category name """
        category_name_edit = 'Test2'
        existing_balance_url = url_for(controller='rest/balance_categories', 
                                       action='edit', name=category_name_edit, 
                                       old_name = category_name,
                                       parent='Dom')
        response = self.app.post(existing_balance_url)
        self.assertEquals(200, response.status)
        self.assertEquals('application/x-javascript', response.header('Content-Type'))
        result = json.loads(response.body)
        self.failIf(result is None)
        
        status = result.get('id')
        self.failIf(status is None)

        """ Edit category name again - should be error"""
        category_name_edit = 'Test2'
        existing_balance_url = url_for(controller='rest/balance_categories', 
                                       action='edit', name=category_name_edit, 
                                       old_name = category_name, 
                                       parent='Dom')
        response = self.app.post(existing_balance_url)
        self.assertEquals(200, response.status)
        self.assertEquals('application/x-javascript', response.header('Content-Type'))
        result = json.loads(response.body)
        self.failIf(result is None)
        
        status = result.get('failure')
        self.assertEquals(status, Messages.elementNotFound(key=category_name))

        """ Delete category Test 2 """        
        existing_balance_url = url_for(controller='rest/balance_categories', 
                                       action='delete', name=category_name_edit, 
                                       )
        response = self.app.post(existing_balance_url)
        self.assertEquals(200, response.status)
        self.assertEquals('application/x-javascript', response.header('Content-Type'))
        result = json.loads(response.body)
        self.failIf(result is None)
        
        status = result.get('failure')
        self.assertEquals(status, None)
        self.failIf(self.__in_categories(category_name_edit))

        """ Delete category Test 1 - Fail """        
        existing_balance_url = url_for(controller='rest/balance_categories', 
                                       action='delete', name=category_name, 
                                       )
        response = self.app.post(existing_balance_url)
        self.assertEquals(200, response.status)
        self.assertEquals('application/x-javascript', response.header('Content-Type'))
        result = json.loads(response.body)
        self.failIf(result is None)
        
        status = result.get('failure')
        self.assertEquals(status, Messages.elementNotFound(key=category_name))
        self.failIf(self.__in_categories(category_name))