from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase


def add_user(username, email):
    """
    Create and return a new user with the given username and email.
    """
    user = User.objects.create_user(
        username=username, email=email, password='test')
    user.save()
    return user


class TraitTests(APITestCase):
    def setUp(self):
        add_user('test', 'test@test.com')


    def test_create_trait_with_authentication(self):
        """
        Ensure that we can create a new trait after logging in.
        """
        self.client.login(username='test', password='test')

        # Send POST request to create trait.
        url = reverse('trait-list')
        data = {'name': 'test'}
        response = self.client.post(url, data, format='json')

        # Make sure trait was created with expected data.
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['name'], data['name'])
        new_trait_url = reverse('trait-detail', kwargs={'pk': 1})
        self.assertTrue(new_trait_url in response.data['url'])

    def test_create_trait_without_authentication(self):
        """
        Ensure that we cannot create a new trait without loggin in.
        """
        # Send POST request to create trait.
        url = reverse('trait-list')
        data = {'name': 'test'}
        response = self.client.post(url, data, format='json')

        # Make sure authentication error was returned.
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertTrue('name' not in response.data)
@@ -1201,16 +1201,17 @@
 url'%5D)%0A%0A
+%0A
     def 
@@ -1333,16 +1333,17 @@
 t loggin
+g
  in.%0A   
@@ -1708,8 +1708,1321 @@
 se.data)
+%0A%0A%0Aclass WineryTests(APITestCase):%0A    def setUp(self):%0A        add_user('test', 'test@test.com')%0A%0A%0A    def test_create_winery_with_authentication(self):%0A        %22%22%22%0A        Ensure that we can create a new winery after logging in.%0A        %22%22%22%0A        self.client.login(username='test', password='test')%0A%0A        # Send POST request to create winey.%0A        url = reverse('winery-list')%0A        data = %7B'name': 'test'%7D%0A        response = self.client.post(url, data, format='json')%0A%0A        # Make sure winey was created with expected data.%0A        self.assertEqual(response.status_code, status.HTTP_201_CREATED)%0A        self.assertEqual(response.data%5B'name'%5D, data%5B'name'%5D)%0A        new_wine_url = reverse('winery-detail', kwargs=%7B'pk': 1%7D)%0A        self.assertTrue(new_wine_url in response.data%5B'url'%5D)%0A%0A%0A    def test_create_winery_without_authentication(self):%0A        %22%22%22%0A        Ensure that we cannot create a new winery without logging in.%0A        %22%22%22%0A        # Send POST request to create winery.%0A        url = reverse('winery-list')%0A        data = %7B'name': 'test'%7D%0A        response = self.client.post(url, data, format='json')%0A%0A        # Make sure authentication error was returned.%0A        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)%0A        self.assertTrue('name' not in response.data)

Add tests for creating a winery.