def tearDown(self):
     Tag.objects.all().delete()
     Place.objects.all().delete()
     Thing.objects.all().delete()
     TagFactory.reset_sequence()
     PlaceFactory.reset_sequence()
     ThingFactory.reset_sequence()
 def test_thing_taggable_some_tags(self):
     thing = ThingFactory()
     thing.tags.add(TagFactory())
     thing.tags.add(TagFactory())
     thing.tags.add(TagFactory())
     tags = thing.get_tag_names()
     self.assertEqual(3, tags.count())
     tags_list = list(tags)
     self.assertEqual(['tag1', 'tag2', 'tag3'], tags_list)
 def test_update_thing(self):
     client = APIClient()
     user = UserFactory()
     client.force_authenticate(
         user=user,
         token=AccessToken.objects.create(
             user=user,
             application=self.application,
             scope='read write',
             expires=datetime.datetime.now() + datetime.timedelta(days=1)
         )
     )
     thing = ThingFactory(user=user)
     self.assertNotEqual("new thing", thing.description)
     thing_url = reverse("api:thing-detail", kwargs={'pk': thing.pk})
     response = client.put(thing_url, {"description": "new thing"}, format='json')
     self.assertEqual(200, response.status_code)
     self.assertEqual('{"id":1,"description":"new thing","place":null,"tags":[]}', response.content)
     thing.refresh_from_db()
     self.assertEqual("new thing", thing.description)
 def test_tag_things(self):
     client = APIClient()
     user = UserFactory()
     tag = TagFactory(user=user)
     thing = ThingFactory(user=user)
     thing.tags.add(tag)
     thing.save()
     thing = ThingFactory(user=user)
     thing.tags.add(tag)
     thing.save()
     ThingFactory(user=user)
     client.force_authenticate(
         user=user,
         token=AccessToken.objects.create(
             user=user,
             application=self.application,
             scope='read write',
             expires=datetime.datetime.now() + datetime.timedelta(days=1)
         )
     )
     tag_thing_url = reverse("api:thing-get-things-for-tags")
     query = "{0}?tag_id={1}".format(tag_thing_url, tag.pk)
     response = client.get(query)
     self.assertEqual(200, response.status_code)
     self.assertEqual('[{"id":1,"description":"Some Thing 1","place":null,"tags":[1]},{"id":2,"description":"Some Thing 2","place":null,"tags":[1]}]', response.content)
 def test_update_thing_with_place_different_user(self):
     client = APIClient()
     user = UserFactory()
     user2 = UserFactory()
     client.force_authenticate(
         user=user,
         token=AccessToken.objects.create(
             user=user,
             application=self.application,
             scope='read write',
             expires=datetime.datetime.now() + datetime.timedelta(days=1)
         )
     )
     thing = ThingFactory(user=user)
     PlaceFactory(user=user2)
     self.assertIsNone(thing.place)
     thing_url = reverse("api:thing-detail", kwargs={'pk': thing.pk})
     response = client.put(thing_url, {"description": "new thing", "tags": [], "place": 1}, format='json')
     self.assertEqual(400, response.status_code)
     self.assertEqual('{"place":["Place does not belong to user"]}', response.content)
     thing.refresh_from_db()
     self.assertIsNone(thing.place)
    def test_retreive_with_place(self):
        client = APIClient()
        user = UserFactory()
        client.force_authenticate(
            user=user,
            token=AccessToken.objects.create(
                user=user,
                application=self.application,
                scope='read write',
                expires=datetime.datetime.now() + datetime.timedelta(days=1)
            )
        )

        thing = ThingFactory(user=user)
        thing.place = PlaceFactory(user=user)
        thing.save()

        thing_url = reverse("api:thing-detail", kwargs={"pk": thing.pk})
        response = client.get(thing_url)

        self.assertEqual(200, response.status_code)
        self.assertEqual('{"id":1,"description":"Some Thing 1","place":1,"tags":[]}', response.content)
 def test_thing_taggable_no_tags(self):
     thing = ThingFactory()
     tags = thing.get_tag_names()
     self.assertEqual(0, tags.count())