def test_addtopic(self):

    url = '/addtopic/'
    redirect_url = '/login/?next=/addtopic/'

    #Check the redirect when a user is not logged in.
    resp = self.client.get(url)
    self.checkRedirects(resp, redirect_url)

    del resp

    #Check the redirect when a user is logged in without 
    #   being a program manager.
    C = Client()
    #Login a user.
    C.login(username='******',password='******')
    resp = C.get(url)
    self.checkRedirects(resp,redirect_url,target_status_code=302)
    #Logout the user.
    C.logout()

    del resp

    #Login a user.
    self.create_user('test.pmanager',groups=('Program Manager',))
    C.login(username='******',password='******')
    resp = C.get(url)
    self.checkTemplateUsed(resp,'topic_management/addtopic.html')

    #Check the return of an invalid form.
    postData = {
      'name':'',
      'description':'',
      'category':'',
    }
    resp = C.post(url,postData)
    self.checkTemplateUsed(resp,'topic_management/addtopic.html')

    self.add_topic_form(C)

    #Logout the user.
    C.logout()
  def test_viewtopics(self):   
    
    url = '/viewtopics/'
    redirect_url = '/login/?next=/viewtopics/'
    template = 'topic_management/viewtopics.html'

    #Check the redirect when a user is not logged in.
    resp = self.client.get(url)
    self.checkRedirects(resp, redirect_url)

    del resp

    #Check that a logged in user can see the topics
    #   that have been inserted into the databse.
    C = Client()
    #Login a user.
    C.login(username='******',password='******')
    resp = C.get(url)
    self.checkTemplateUsed(resp, template)
    self.checkRespContains(resp,'OTHER')
    #Logout the user.
    C.logout()

    del resp

    #Check that the search get variable works.
    C = Client()
    C.login(username='******',password='******')
    resp = C.get(url,{'search':'UNIQUEVAL'})
    self.checkRespNotContains(resp,'OTHER')
    self.checkRespContains(resp,'UNIQUEVAL')
    #Logout the user.
    C.logout()

    del resp

    #Check the mytopics variable is working.
    C = Client()
    C.login(username='******',password='******')
    resp = C.get(url,{'mytopics':'1'})
    self.checkRespContains(resp,'OTHER')
    self.checkTemplateUsed(resp, template)
    #Logout the user.
    C.logout()

    del resp

    #Delete all of the topic objects.
    Topic.objects.all().delete()

    #Check that an empty viewtopics is loading properly.
    C = Client()
    #Login a user.
    C.login(username='******',password='******')
    resp = C.get(url,{'page':'30'})
    self.checkTemplateUsed(resp, template)
    self.checkRespContains(resp,'No results found')
    #Logout the user.
    C.logout()

    del resp
  def test_viewtopic(self):
    
    url = '/viewtopic/'
    redirect_url = '/login/?next=/viewtopic/'
    template = 'topic_management/viewtopic.html'

    #Check the redirect when a user is not logged in.
    resp = self.client.get(url)
    self.checkRedirects(resp, redirect_url)

    del resp

    #Check the redirect to viewing topics when there 
    #   we go to a topic that doesn't exist.
    C = Client()
    C.login(username='******',password='******')
    resp = C.get(url)
    self.checkRedirects(resp,'/viewtopics/')
    #self.checkTemplateUsed(resp, template)

    del resp

    #Logout the user.
    C.logout()

   #self.create_topics(extuser=ExtendedUser.objects.get(user__username='******'))

    #Get a topic to test with
    topic = Topic.objects.get(name='Topic1')
    topic_publicid = topic.publicid
    document = topic.documents.get(name__icontains='Document1')
    document_publicid = document.publicid
    document_text = 'A Document Name XCVF'

    #Check the mytopics variable is working.
    C = Client()

    comment_content = 'COMMENT TEXT'

    #Create postData for the url.
    postData = {
      'topicid':topic_publicid,
      'content':comment_content
    }
    #Get the url for the topic.
    topic_url = "%s?publicid=%s" % (url,topic_publicid)
    
    #We should not be able to edit the topic being
    #   logged in as a user that doesn't own it.
    #We should be able to see a comment that we add
    #   with a POST.
    #It should also contain at least on document.
    C.login(username='******',password='******')
    resp = C.post(topic_url,postData)
    #self.checkRedirects(resp,'/viewtopics/')
    self.checkTemplateUsed(resp, template)
    #The string below will only show up if we own the topic
    #   and we are a supervisor.
    self.checkRespNotContains(resp, 'Presentation Length: ')
    #The string below will only exist if the post to the 
    #   database was successful for the topic.
    self.checkRespContains(resp, comment_content)
    #The string below will only exist if the documents 
    #   loaded right.
    self.checkRespContains(resp, document_text)

    del resp

    document_comment_content = 'COMMENT ON DOCUMENT TEXT'

    #Create postData for the url.
    postData = {
      'documentid':document_publicid,
      'content':document_comment_content
    }
    #A comment on the document should post to the page
    #   the page with the content listed above.
    resp = C.post(topic_url,postData)
    #self.checkRedirects(resp,'/viewtopics/')
    self.checkTemplateUsed(resp, template)
    #The string below will only exist if the post to the 
    #   database was successful for the topic.
    self.checkRespContains(resp, document_comment_content)

    del resp

    #Get the comment we just posted and test a comment reply.
    comment = document.comments.get(content=document_comment_content)
    comment_publicid = comment.publicid

    comment_reply_content = 'COMMENT REPLY TEXT'

    #Create postData for the url.
    postData = {
      'commentid':comment_publicid,
      'content':comment_reply_content,
    }
    #A reply to the comment should post to the page below.
    resp = C.post(topic_url,postData)
    #self.checkRedirects(resp,'/viewtopics/')
    self.checkTemplateUsed(resp, template)
    #The string below will only exist if the post to the 
    #   database was successful for the topic.
    self.checkRespContains(resp, comment_reply_content)

    del resp

    C.logout()

    C.login(username='******',password='******')

    topic_description = 'update DESCRIPTION TEST'

    #Create postData for the url.
    postData = {
      'update_topic_description':topic_publicid,
      'description':topic_description,

    }
    #A reply to the comment should post to the page below.
    resp = C.post(topic_url,postData)
    #self.checkRedirects(resp,'/viewtopics/')
    self.checkTemplateUsed(resp, template)
    #The string below will only exist if the post to the 
    #   database was successful for the topic.
    self.checkRespContains(resp, topic_description)

    del resp

    #A reply to the comment should post to the page below.
    resp = C.get(url,{'publicid':'nopublicid'})
    #Check the redirect
    self.checkRedirects(resp,urlstring='/viewtopics/')

    del resp

    presentation_length = '30'
    release_update_value = "%s minute presentation" % presentation_length

    #Create postData for the url.
    postData = {
      'topic_ready_for_review_id':topic_publicid,
      'topic_presentationlength':presentation_length,

    }
    #A test to release the topic to being able to be
    #   dragged into the meeting schedule.
    resp = C.post(topic_url,postData)
    #self.checkRedirects(resp,'/viewtopics/')
    self.checkTemplateUsed(resp, template)
    #The string below will only exist if the post to the 
    #   database was successful for the topic.
    self.checkRespContains(resp, release_update_value)

    del resp

    extuser = self.create_user('user.name')

    #Create a meeting to test the update of a topic release.
    meeting = Meeting(
                      name='Meeting Name',
                      description='Meeting Description',
                      duedate='2013-05-05',
                      startdate='2013-05-05',
                      starttime='13:00:00',
                      user=extuser.user,
                      maxscheduleitems=8,
                      duration=0
                      )
    meeting.save()
    topic.meeting = meeting
    meeting.topics.add(topic)
    topic.save()

    #Create postData for the url.
    postData = {
      'topic_ready_for_review_id':topic_publicid,

    }
    #A test to unrelease the topic.
    resp = C.post(topic_url,postData)
    #self.checkRedirects(resp,'/viewtopics/')
    self.checkTemplateUsed(resp, template)
    #The string below will only exist if the post to the 
    #   database was successful for the topic.
    self.checkRespNotContains(resp, release_update_value)

    del resp

    #Create postData for the url.
    postData = {
      'deleted_documentid':document_publicid,
    }
    #A test to unrelease the topic.
    resp = C.post(topic_url,postData)
    #self.checkRedirects(resp,'/viewtopics/')
    self.checkTemplateUsed(resp, template)
    #The string below will only exist if the post to the 
    #   database was successful for the topic.
    self.checkRespNotContains(resp, 'Document1 %s' % document_text)

    del resp

    #Upload a new document.
    newtopic = self.add_topic_form(C)
    newtopic_publicid = newtopic.publicid
    newdocument = newtopic.documents.all()[0]
    newdocument_publicid = newdocument.publicid

    newtopic_url = "/viewtopic/?publicid=%s" % newtopic_publicid

    #Test the update of a document
    file = "%s/www/css/style.css" % settings.BASE_DIR
    with open(file) as fp:

      #Create postData for the url.
      postData = {
        'updated_documentid':newdocument_publicid,
        'file':fp,
      }
      resp = C.post(newtopic_url,postData)
      #self.checkRedirects(resp,'/viewtopics/')
      self.checkTemplateUsed(resp, template)
      #The string below will only exist if the post to the 
      #   database was successful for the topic.
      self.checkRespContains(resp, 'style.css')

      del resp


    ###
    # Test the download.
    ###

    downloaddocument = newtopic.documents.all()[0]

    document_filename = downloaddocument.fileName

    download_url = '/download/%s/%s' % (newtopic_publicid,document_filename)

    resp = C.get(download_url)

    self.assertIn(document_filename, resp['Content-Disposition'])

    del resp

    #Test the adding of a document.
    file = "%s/www/js/jquery.min.js" % settings.BASE_DIR
    with open(file) as fp:

      #Create postData for the url.
      postData = {
        'add_document':'1',
        'file':fp,
      }
      resp = C.post(newtopic_url,postData)
      #self.checkRedirects(resp,'/viewtopics/')
      self.checkTemplateUsed(resp, template)
      #The string below will only exist if the post to the 
      #   database was successful for the topic.
      self.checkRespContains(resp, 'jquery.min.js')

      del resp

    #Logout the user.
    C.logout()

    #Create a supervisor
    self.create_user(username='******',groups=('Supervisor','Program Manager',))

    #Login the supervisor
    C.login(username='******',password='******')

    #Create postData for the url.
    postData = {
      'released_topicid':newtopic_publicid,
    }
    #A test to aprove the topic.
    resp = C.post(newtopic_url,postData)
    #self.checkRedirects(resp,'/viewtopics/')
    self.checkTemplateUsed(resp, template)
    self.assertTrue(os.path.exists("%s/%s" % (settings.APPROVED_TOPIC_DIR,newtopic_publicid)))

    del resp


    #Upload a new document.
    newtopic2 = self.add_topic_form(C,description='Testing')
    newtopic2_publicid = newtopic2.publicid

    newtopic2_url = "/viewtopic/?publicid=%s" % newtopic2_publicid

    #Create postData for the url. 
    postData = {
      'deleted_topicid':newtopic2_publicid,
    }

    #A test to aprove the topic.
    resp = C.post(newtopic2_url,postData)

    #self.checkRedirects(resp,'/viewtopics/')
    self.assertIn('viewtopics/?deleted',resp['Location'])
    self.assertFalse(os.path.exists("%s/%s" % (settings.UPLOADED_TOPIC_DIR,newtopic2_publicid)))
    self.assertTrue(os.path.exists("%s/%s" % (settings.DELETED_TOPIC_DIR,newtopic2_publicid)))

    del resp

    #Logout the user.
    C.logout()
  def test_viewmeetings(self):

    url = '/viewmeetings/'
    template = 'meeting_management/oldviewmeetings.html'
    #Define a variable for the password.
    password='******'

    #Load a client
    C = Client()
    
    #Load /viewmeetings/, the response should redrect
    #   to /login/ because the user has not logged in.
    resp = C.get(url)
    #Test if there is a location to redirect to.
    self.assertIn('Location',resp)
    #Check that the location is to a login screen.
    self.assertIn('/login/',resp['Location'])



    #Create a guest user
    username = '******'
    self.create_user(username,password=password)

    #Login to the client
    C.login(username=username,password=password)

    #Load /viewmeetings/, the response should not contain an
    #   user interface because we are not a Supervisor.
    resp = C.get(url)
    #Test if the add document form is present in the page,
    #   if it is not the check passes.
    self.assertNotContains(resp,'function add_meeting_modal(date_clicked)')

    #Log the user out.
    C.logout()


    #Create a 'Program Manager' user
    username = '******'
    self.create_user(username,password=password,groups=('Program Manager',))

    #Login the user.
    C.login(username=username,password=password)

    #Load /viewmeetings/, the response should not contain an
    #   user interface because we are not a Supervisor.
    resp = C.get(url)
    #Test if the add document form is present in the page,
    #   if it is not the check passes.
    self.assertNotContains(resp,'function add_meeting_modal(date_clicked)')

    #Log the user out.
    C.logout()



    #Create a 'Supervisor' user
    username = '******'
    self.create_user(username,password=password,groups=('Program Manager','Supervisor'))

    #Login the user.
    C.login(username=username,password=password)

    #Load /viewmeetings/, the response should contain a
    #   user interface because we are a supervisor
    resp = C.get(url)

    #Test if the add document form is present in the page,
    #   if it is the check will pass.
    self.assertContains(resp,'function add_meeting_modal(date_clicked)')


    meeting_name = 'XIBNFELUQKO'

    #Generate post data for the first meeting form.
    postData = {
      'addmeetingform':'1',
      'name':meeting_name,
      'description':'Test Meeting Description',
      'duedate':'2013-05-05',
      'startdate':'2013-05-06',
      'starttime':'2:00am',
    }
    #Load /viewmeetings/, test that the posting of the 
    #   meeting form works.
    resp = C.post("%s?loadnext" % url,postData)


    #Believe it or not this is a unique identifier for the 
    #  editing a schedule form. Because the meeting object
    #   has not been created it doesn't have a value after
    #   the # (hash).
    unique_identifier = '''<center>
<form action="/viewmeetings/#"'''

    #Check that the second meeting form loads when it is
    #   expected too.
    self.assertContains(resp,unique_identifier)
    
    #load 
    session = C.session
    session['addmeetingform'] = postData
    session.save()

    #Load topics into the database.
    topics = [
           self.add_topic_form(C,description=' Topic 1'),
           self.add_topic_form(C,description=' Topic 2'),
           self.add_topic_form(C,description=' Topic 3'),   
    ]
    #Create a string to send through to the 
    schedule_items = ""
    for t in topics:
      #Add the public id to the list
      schedule_items += "%s," % t.publicid

    #Add a schedule_items variable.
    postData = {'schedule_items':schedule_items}
    #Get the redirect url.
    next_url = C.post("%s?loadnext" % url, postData)['Location']
    #Load the response of the redirect url.
    resp = C.post(next_url)
    #Check that the meeting is posted out onto the page.
    self.assertContains(resp,meeting_name)

    #Load a meeting to edit.
    meeting = Meeting.objects.get(name__contains=meeting_name)
    
    #Define a meeting description string.
    meeting_description = 'KRPOUSILUEKI9081'
    meeting_publicid = meeting.publicid

    #Generate post data for the first meeting form.
    postData = {
      'update_meeting_information':meeting_publicid,
      'name':meeting_name,
      'description':meeting_description,
      'duedate':'2013-05-05',
      'startdate':'2013-05-06',
      'starttime':'2:00am',
    }

    #Get the redirect url.
    next_url = C.post(url, postData)['Location']
    #Load the redirected url.
    resp = C.post(next_url)
    #Check that the meeting information has been updated.
    self.assertContains(resp,meeting_description)

    #Create a new topic description.
    topic_description = ' XESRFNIF'
    #Append the topic to the end of the schedule list.
    newtopic = self.add_topic_form(C,description=topic_description)
    schedule_items += '%s,'%newtopic.publicid
    #Create some postData.
    postData = {
      'update_meeting_schedule_publicid':meeting_publicid,
      'schedule_items':schedule_items,
    }

    #Get the next url.
    next_url = C.post(url,postData)['Location']
    #Load the redirected url.
    resp = C.post(next_url)
    #Check that the topic has been added to the meeting list.
    self.assertContains(resp, topic_description)

    #Send post data to delete the meeting we have been working on.
    postData = {
      'delete_meeting':meeting_publicid,
    }

    #Get the next url.
    next_url = C.post(url,postData)['Location']
    #Load the redirected url.
    resp = C.post(next_url)
    #Check that the meeting has been deleted, if the meeting 
    #   has been deleted the meeting_description will not be
    #   in the page anymore.
    self.assertNotContains(resp, meeting_description)


    #Log the user out.
    C.logout()