title: Django testing: mock user login
body: <p>I would like to test a Django project:</p>
  
  <p>template <code>news.html</code>:</p>
  
  <pre><code>{% if user.is_authentication %}
    &lt;a href=#&gt;Logout ({{user.username}})&lt;/a&gt;
  {% else %}
    &lt;a href=#&gt;Login&lt;/a&gt;
  {% endif %}
  </code></pre>
  
  <p>How do I create the situation that the user is already logged in (the <code>accounts</code> app, responsible for user login, has not implemented yet) such that the template will display the <code>Logout</code> link?</p>
  
  <p>(a) Functional test:</p>
  
  <pre><code>def test_logged_in_user_can_see_logout_link(self):
      # A logged-in user arrives at the main page
      ## How to mock a logged-in user here? 
      self.browser.get(self.live_server_url)
      self.assertTrue(self.browser.find_element_by_link_text('Logout'))
  </code></pre>
  
  <p>(b) Unit test:</p>
  
  <pre><code>def test_logged_in_user_can_see_logout_link**(self):
      # How to login the user here?
      request = self.factory.get('/news/')
      response = news(request)
      self.assertContains(response, 'Logout')
  </code></pre>