def test_post_news_post(self):
     '''
     function: test_post_news_post
     description:
         submit a maximum form and test if the creation is complete 
         submit a minimum form and test if the save is complete
         submit a invalid form and test if the save is rejected
     '''
     self.login(user1)
     doctype = 'news_post'
     # submit a maximum form and test if the creation is complete 
       
     Doc, Form = get_doc_form(doctype) 
     title = random_string[0]
     content = random_string[1]
     tag = random_string[2]
     self.assertEqual(Doc.objects.filter(title=title,content=content,tag=tag).count(),0)
     response = self.client.post('/edit/%s/'% doctype, {
         'title': title,
         'content': content,
         'tag': tag,
     })
     self.assertEqual(Doc.objects.filter(title=title,content=content,tag=tag).count(),1)
 
     # submit a minimum form and test if the save is complete
     self.assertEqual(Doc.objects.filter(title=title).count(),1)
 
     doc = Doc.objects.get(title=title)
     title = random_string[1]
     content = random_string[2]
     tag = random_string[0]
     response = self.client.post('/edit/%s/%d'% (doctype,doc.id), {
         'title': title,
         'content': content,
         'tag': '',
     })
     self.assertEqual(Doc.objects.filter(title=title,content=content,tag=tag).count(),0)
     self.assertEqual(Doc.objects.filter(title=title,content=content,tag='').count(),1)
          
     # submit an invalid form and test if the save is rejected
     self.assertEqual(Doc.objects.filter(title=title).count(),1)
        
     doc = Doc.objects.get(title=title)
     title = random_string[1]
     content = random_string[2]
     tag = random_string[0]
     response = self.client.post('/edit/%s/%d'% (doctype,doc.id), {
         'title': title,
         'content': '',
         'tag': tag,
     })
     self.assertEqual(Doc.objects.filter(title=title).count(),1)
     self.assertEqual(Doc.objects.filter(title=title,content='',tag=tag).count(),0)
     self.assertEqual(Doc.objects.filter(title=title,content=content,tag='').count(),1)
    def test_get_correct_button(self):
        '''
        function: test_get_correct_button
        description: for each doctype, for each user, test whether a user gets correct buttons
        '''
        for doctype in doctypes:
            # Insert doc 
            Doc, Form = get_doc_form(doctype)
            doc = Doc()
            doc.title = random_string[0]
            doc.status = s1
            doc.save()
            

            # Get loggined user
            self.login(user1)
            user_id = self.client.session['_auth_user_id']
            user = User.objects.get(id=user_id)
            
            # user1, has user_permission for doc 1
            #   1_to_2, 
            #   1_to_1 (create)
            
            # Insert permission for doc1,user1,1_to_2, 1_to_1
            p = WorkFlowUserPermission.add_permission(doc,user,s1,s2)
            p = WorkFlowUserPermission.add_permission(doc,user,s1,s1)
            response = self.client.get('/edit/%s/%d' % (doctype,doc.id))
            self.assertTrue( 'Save' in str(response),'doctype=%s' % doctype)
            self.assertTrue('1_to_2' in str(response),'doctype=%s' % doctype )
            self.assertEqual(str(response).count('</input>'),2)
           
            # TODO add superuser case

            doc.transfer(user1,s2)
            # user2, has group_permission for doc 1
            #   2_to_3,
            #   2_to_1 (save)
            self.login(user2)
            self.assertFalse(user2.is_superuser) 
            self.assertTrue(group2 in user2.groups.all())
            p = WorkFlowGroupPermission.add_permission(doc,group2,s2,s3)
            p = WorkFlowGroupPermission.add_permission(doc,group2,s2,s2)
            p = WorkFlowGroupPermission.add_permission(doc,group2,s2,s1)
            
            response = self.client.get('/edit/%s/%d' % (doctype,doc.id))
            self.assertTrue( 'Save' in str(response), 'doctype=%s' % doctype)
            self.assertTrue( '2_to_3' in str(response), 'doctype=%s' % doctype)
            self.assertTrue( '2_to_1' in str(response), 'doctype=%s' % doctype)
    def test_post_grant(self):
        '''
        function: test_post_grant
        description:
            submit a maximum form and test if the creation is complete 
            submit a minimum form and test if the save is complete
            submit a invalid form and test if the save is rejected
        '''
        self.login(user1)
        doctype = 'grant'
        
        # submit a maximum form and test if the creation is complete 
          
        Doc, Form = get_doc_form(doctype) 
        title = random_string[0]
        summary = random_string[1] 
        content = random_string[2]
        frequency = 'every 2 year'
        expires_at = '2013-4-6'
        attachment = open(TEST_FILE,'rb')
        # TODO test attachment

        self.assertEqual(Doc.objects.filter(title=title).count(),0)
        response = self.client.post('/edit/%s/'% doctype, {
            'title': title,
            'summary': summary,
            'content': content,
            'frequency': frequency,
            'expires_at': expires_at,
            'attachment': attachment,
        })
        doc = Doc.objects.get(title=title)
        path = os.path.join(MEDIA_ROOT,str(doc.attachment))
        md5upload = os.popen('md5sum '+ path).readline().split()[0]
        md5original = os.popen('md5sum '+ TEST_FILE).readline().split()[0]
        self.assertEqual(md5upload,md5original)

        
        self.assertEqual(Doc.objects.filter(
            title=title,summary=summary,
            content=content,frequency=frequency,expires_at=expires_at
            ).count(),1)
         
        # submit a minimum form and test if the save is complete
        
        self.assertEqual(Doc.objects.filter(title=title).count(),1)
    
        doc = Doc.objects.get(title=title)
        
        response = self.client.post('/edit/%s/%d'% (doctype,doc.id), {
            'title': title,
            'summary': summary,
            'content': content,
            'frequency': '',
            'expires_at': '',
            'attachment': '',
        })
        self.assertEqual(Doc.objects.filter(title=title,content=content,summary=summary).count(),1)
        self.assertEqual(Doc.objects.filter(title=title,frequency=None).count(),1)
        self.assertEqual(Doc.objects.filter(title=title,expires_at=None).count(),1)

        # submit an invalid form and test if the save is rejected
        
        self.assertEqual(Doc.objects.filter(title=title).count(),1)
           
        doc = Doc.objects.get(title=title)
        response = self.client.post('/edit/%s/%d'% (doctype,doc.id), {
            'title': '',
            'summary': summary,
            'content': content,
            'frequency': '',
            'expires_at': '',
            'attachment': '',
        })

        self.assertEqual(Doc.objects.all().count(),1)
        doc = Doc.objects.all()[0]
        self.assertEqual(Doc.objects.filter(title=title).count(),1)

        
        # destroy uploaded attachement
        path = os.path.join(MEDIA_ROOT,str(doc.attachment))
        os.remove(path) 
    def test_post_basic(self):
        # Login, post, create and save

        self.login(user1)
        doctype = 'news_post'
        Doc, Form = get_doc_form(doctype) 
        
        # test create
        
        self.assertEqual(Doc.objects.filter(title=random_string[0]).count(),0)
        time_before_submit = timezone.now() 
        response = self.client.post('/edit/%s/'% doctype, {
            'title': random_string[0],
            'content': random_string[1],
            })
        time_after_submit = timezone.now()
         
        self.assertEqual(response.status_code,200)
        self.assertEqual(Doc.objects.filter(title=random_string[0]).count(),1)
        doc = Doc.objects.get(title=random_string[0])
        self.assertEqual(Doc.objects.get(title=random_string[0]).status,WorkFlowStatus.get_default_status()) 
        self.assertEqual(Doc.objects.get(content=random_string[1]).status,WorkFlowStatus.get_default_status()) 

        # test permissions after creation
        
        self.assertEqual(WorkFlowUserPermission.objects.filter(doc=doc,user=user1,start_status=s1,end_status=s1).count(),1) # Collector should be able to change content 
        self.assertEqual(WorkFlowUserPermission.objects.filter(doc=doc,user=user1,start_status=s1,end_status=s2).count(),1) # Collector should be able to submit as collected(reviewing)
        self.assertEqual(WorkFlowGroupPermission.objects.filter(doc=doc,group=group2,start_status=s2,end_status=s2).count(),1) # Reviewers should be able to change content
        self.assertEqual(WorkFlowGroupPermission.objects.filter(doc=doc,group=group2,start_status=s2,end_status=s3).count(),1) # Reviewers should be able to submit as reviewed 
        self.assertEqual(WorkFlowGroupPermission.objects.filter(doc=doc,group=group2,start_status=s2,end_status=s1).count(),1) # Reviewers should be able to submit as collecting
        
        # test history after creation
        
        self.assertEqual(WorkFlowHistory.objects.filter(doc=doc,user=user1,start_status=s1,end_status=s1).count(),1)
        history = WorkFlowHistory.objects.get(doc=doc,user=user1,start_status=s1,end_status=s1)
        self.assertTrue(history.timestamp>time_before_submit)
        self.assertTrue(history.timestamp<time_after_submit)

        # test save
        
        doc = Doc.objects.get(title=random_string[0])
        docid = doc.id 
        original_status = doc.status
        time_before_submit = timezone.now()
        response = self.client.post('/edit/%s/%d'% (doctype,docid), {
            'title': random_string[3],
            'content': random_string[2],
            })
        time_after_submit = timezone.now()
        self.assertEqual(Doc.objects.filter(title=random_string[3]).count(),1)
        self.assertEqual(Doc.objects.filter(content=random_string[2]).count(),1)
        doc = Doc.objects.get(title=random_string[3])
        self.assertEqual(doc.id,docid) 
        self.assertEqual(original_status,doc.status) 
        self.assertEqual(doc.content,random_string[2]) 
        
        # test history after save 
        
        self.assertEqual(WorkFlowHistory.objects.filter(doc=doc,user=user1,start_status=s1,end_status=s1).count(),2)
        history = WorkFlowHistory.objects.filter(doc=doc,user=user1,start_status=s1,end_status=s1).reverse()[0]
        self.assertTrue(history.timestamp>time_before_submit)
        self.assertTrue(history.timestamp<time_after_submit)


        # test transfer
        
        self.assertEqual(doc.id,docid) 
        start_status_id = doc.status.id
        end_status_id = 2
        time_before_submit = timezone.now()
        response = self.client.post('/edit/%s/%d'%(doctype,docid), {
            'button':Doc.button_prefix + '%d_to_%d' %(start_status_id,end_status_id),
            'title': random_string[1],})
        time_after_submit = timezone.now() 
        doc = Doc.objects.get(id=docid)
        self.assertEqual(doc.status.id,end_status_id)
        self.assertNotEqual(doc.status.id,start_status_id)
        
        # test history after transfer 
        
        self.assertEqual(WorkFlowHistory.objects.filter(doc=doc,user=user1,start_status=s1,end_status=s2).count(),1)
        self.assertEqual(WorkFlowHistory.objects.filter(doc=doc,user=user1).count(),3)
        history = WorkFlowHistory.objects.filter(doc=doc,user=user1,start_status=s1,end_status=s2).reverse()[0]
        self.assertTrue(history.timestamp>time_before_submit)
        self.assertTrue(history.timestamp<time_after_submit)