# Path: tests/drf/cases.py
# class MongoengineCase(SimpleTestCase):
#     @classmethod
#     def setUpClass(cls):
#         super(MongoengineCase, cls).setUpClass()
#         mongoengine.connection.disconnect()
#         mongoengine.connect('test_djangular')
#         cls.setUpClassData()
# 
#     @classmethod
#     def setUpClassData(cls):
#         pass
# 
#     @classmethod
#     def tearDownClass(cls):
#         connection = mongoengine.connection.get_connection()
#         connection.drop_database('test_djangular')
#         mongoengine.connection.disconnect()
# 
# Path: tests/drf/mocks.py

# 
# Path: authentication/models.py
# class User(AbstractUser):
#     tagline = StringField(max_length=140, required=False)
#     created_at = DateTimeField(default=datetime.utcnow)
#     updated_at = DateTimeField()
# 
# 
#     USERNAME_FIELD = 'email'
#     REQUIRED_FIELDS = ['username']
# 
# Path: posts/models.py
# class Post(Document):
#     author = ReferenceField(User, required=True, reverse_delete_rule=CASCADE)
#     content = StringField(max_length=240, required=True)
# 
#     created_at = DateTimeField(default=datetime.utcnow)
#     updated_at = DateTimeField()
# 
#     def __unicode__(self):
#         return u'{0}'.format(self.content)


import json
from bson import ObjectId
from django.test.client import Client
from tests.drf.cases import MongoengineCase
from tests.drf.mocks import mock_user1, mock_user2
from authentication.models import User
from posts.models import Post
        self.assertEquals(response.status_code, 200)
        response = self.client.post('/api/v1/posts/', data=json.dumps({"content": "new post from test", "author": "5688edf51166aa49ea2eb074"}), content_type="application/json")
        post = json.loads(response.content)
        self.assertEqual(post["author"]["id"], str(self.user["id"]))

    def test_update_post(self):
        response = self.client.post('/api/v1/auth/login/',
                                    data=json.dumps({"email": mock_user1["email"], "password": mock_user1["password"]}),
                                    content_type="application/json")
        self.assertEquals(response.status_code, 200)
        response = self.client.put('/api/v1/posts/%s/' % self.post["id"], data=json.dumps({"content": "updated post"}),
                                   content_type="application/json")
        self.assertEquals(response.status_code, 200)
        response = self.client.put('/api/v1/posts/%s/' % self.post["id"], data=json.dumps({"wrong_data": "sure"}),
                                   content_type="application/json")
        self.assertEquals(response.status_code, 400)
        self.assertEquals(unicode(self.post), u"post1")

    def test_delete_post(self):
        response = self.client.post('/api/v1/auth/login/',
                                    data=json.dumps({"email": mock_user1["email"], "password": mock_user1["password"]}),
                                    content_type="application/json")
        self.assertEquals(response.status_code, 200)
        response = self.client.post('/api/v1/posts/', data=json.dumps({"content": "new post for delete"}), content_type="application/json")
        self.assertEquals(response.status_code, 201)
        post = json.loads(response.content)
        response = self.client.delete('/api/v1/posts/%s/' % post["id"])
        self.assertEquals(response.status_code, 204)

    def test_errors(self):

# Path: tests/drf/cases.py
# class MongoengineCase(SimpleTestCase):
#     @classmethod
#     def setUpClass(cls):
#         super(MongoengineCase, cls).setUpClass()
#         mongoengine.connection.disconnect()
#         mongoengine.connect('test_djangular')
#         cls.setUpClassData()
# 
#     @classmethod
#     def setUpClassData(cls):
#         pass
# 
#     @classmethod
#     def tearDownClass(cls):
#         connection = mongoengine.connection.get_connection()
#         connection.drop_database('test_djangular')
#         mongoengine.connection.disconnect()
# 
# Path: tests/drf/mocks.py

# 
# Path: authentication/models.py
# class User(AbstractUser):
#     tagline = StringField(max_length=140, required=False)
#     created_at = DateTimeField(default=datetime.utcnow)
#     updated_at = DateTimeField()
# 
# 
#     USERNAME_FIELD = 'email'
#     REQUIRED_FIELDS = ['username']
# 
# Path: posts/models.py
# class Post(Document):
#     author = ReferenceField(User, required=True, reverse_delete_rule=CASCADE)
#     content = StringField(max_length=240, required=True)
# 
#     created_at = DateTimeField(default=datetime.utcnow)
#     updated_at = DateTimeField()
# 
#     def __unicode__(self):
#         return u'{0}'.format(self.content)


# Path: posts/test.py
import json
from bson import ObjectId
from django.test.client import Client
from tests.drf.cases import MongoengineCase
from tests.drf.mocks import mock_user1, mock_user2
from authentication.models import User
from posts.models import Post

        self.assertEquals(response.status_code, 200)
        response = self.client.post('/api/v1/posts/', data=json.dumps({"content": "new post from test", "author": "5688edf51166aa49ea2eb074"}), content_type="application/json")
        post = json.loads(response.content)
        self.assertEqual(post["author"]["id"], str(self.user["id"]))

    def test_update_post(self):
        response = self.client.post('/api/v1/auth/login/',
                                    data=json.dumps({"email": mock_user1["email"], "password": mock_user1["password"]}),
                                    content_type="application/json")
        self.assertEquals(response.status_code, 200)
        response = self.client.put('/api/v1/posts/%s/' % self.post["id"], data=json.dumps({"content": "updated post"}),
                                   content_type="application/json")
        self.assertEquals(response.status_code, 200)
        response = self.client.put('/api/v1/posts/%s/' % self.post["id"], data=json.dumps({"wrong_data": "sure"}),
                                   content_type="application/json")
        self.assertEquals(response.status_code, 400)
        self.assertEquals(unicode(self.post), u"post1")

    def test_delete_post(self):
        response = self.client.post('/api/v1/auth/login/',
                                    data=json.dumps({"email": mock_user1["email"], "password": mock_user1["password"]}),
                                    content_type="application/json")
        self.assertEquals(response.status_code, 200)
        response = self.client.post('/api/v1/posts/', data=json.dumps({"content": "new post for delete"}), content_type="application/json")
        self.assertEquals(response.status_code, 201)
        post = json.loads(response.content)
        response = self.client.delete('/api/v1/posts/%s/' % post["id"])
        self.assertEquals(response.status_code, 204)

    def test_errors(self):

        u = User(email=mock_user2["email"], username=mock_user2["username"])