def test_filter_favorited_by(self): item1 = item_factory() item2 = item_factory() user = user_factory() FavoritedItem.objects.create(item=item1, user=user) res = self.app.get(self.url, params={"favorited_by": user.pk}) self.assertEqual({item1.id}, {obj["id"] for obj in res.json["results"]})
def test_user_can_remove_item_from_collection(self): item = item_factory() item_to_delete = item_factory() CollectedItem.objects.create(item=item, collection=self.collection) CollectedItem.objects.create(item=item_to_delete, collection=self.collection) assert {item, item_to_delete} == set(self.collection.items.all()) url = reverse('collecteditem-delete-detail', args=(self.collection.pk,)) self.app.delete(url, json.dumps({'item': item_to_delete.pk}), content_type='application/json', user=self.collection.owner) self.assertEqual({item}, set(self.collection.items.all()))
def test_404_when_remove_an_item_which_is_not_in_the_collection(self): item = item_factory() assert CollectedItem.objects.count() == 0 url = reverse('collecteditem-delete-detail', args=(self.collection.pk,)) res = self.app.delete(url, json.dumps({'item': item.pk}), content_type='application/json', user=self.collection.owner, expect_errors=True) self.assertEqual(res.status_code, 404)
def test_user_can_add_styletag_to_item(self): item = item_factory() styletag = styletag_factory() url = reverse('itemstyletag-create-list') res = self.app.post(url, json.dumps({'item': item.pk, 'styletag': styletag.name}), content_type='application/json', user=self.user) self.assertEqual({styletag}, set(item.styletags.all()))
def test_user_cannot_add_items_to_others_collections(self): item = item_factory() user = user_factory() url = reverse('collecteditem-create-list') print("hitting", url) res = self.app.post(url, json.dumps({'item': item.pk, 'collection': self.collection.pk}), content_type='application/json', user=user, expect_errors=True) self.assertEqual(res.status_code, 403)
def test_favorites_returned_when_logged_in(self): favorited_item = FavoritedItem.objects.create(item=item_factory(), user=self.user) with mock_facebook_login(self.user) as (access_token, uid): # important as front-end assumes can use json response from login, instead of fetching from server url = reverse('api.login', args=('facebook',)) res = self.app.post(url, json.dumps({'access_token': access_token}), content_type='application/json') res_retrieve = self.app.get(reverse('userprofile-detail', args=(res.json['profile']['id'],))) self.assertEqual(res.json['favorited_items'], [favorited_item.item.pk])
def test_user_cannot_remove_item_from_others_collection(self): item = item_factory() user = user_factory() CollectedItem.objects.create(item=item, collection=self.collection) assert {item} == set(self.collection.items.all()) url = reverse('collecteditem-delete-detail', args=(self.collection.pk,)) res = self.app.delete(url, json.dumps({'item': item.pk}), content_type='application/json', user=user, expect_errors=True) self.assertEqual(res.status_code, 403)
def test_favorited_by_me(self): item = item_factory() user_who_favorited = user_factory() other_user = user_factory() FavoritedItem.objects.create(item=item, user=user_who_favorited) res = self.app.get(self.url, user=user_who_favorited) self.assertEqual(res.json["results"][0]["favorited_by_me"], True) res = self.app.get(self.url, user=other_user) self.assertEqual(res.json["results"][0]["favorited_by_me"], False)
def test_remove_item_when_exists_in_many_collections(self): item = item_factory() CollectedItem.objects.create(item=item, collection=self.collection) other_collection = collection_factory(initial_items=0) CollectedItem.objects.create(item=item, collection=other_collection) assert {item} == set(self.collection.items.all()) url = reverse('collecteditem-delete-detail', args=(self.collection.pk,)) res = self.app.delete(url, json.dumps({'item': item.pk}), content_type='application/json', user=self.collection.owner, expect_errors=True) self.assertEqual(set(), set(self.collection.items.all())) self.assertEqual({item}, set(other_collection.items.all()))
def collection_factory(initial_items=3, **kwargs): if 'owner' not in kwargs: if UserProfile.objects.count() == 0: kwargs['owner'] = user_factory() else: kwargs['owner'] = UserProfile.objects.order_by('?')[0] if 'title' not in kwargs: kwargs['title'] = nextname('Collection') if 'description' not in kwargs: kwargs['description'] = lorem collection = G(Collection, **kwargs) if 'items' not in kwargs: if Item.objects.count() < initial_items: for _ in range(initial_items - Item.objects.count()): item_args = subargs(kwargs, 'item') item_factory(**item_args) if initial_items: for item in Item.objects.order_by('?')[:initial_items]: CollectedItem.objects.create(item=item, collection=collection) return collection
def test_anonymous_cannot_add_item(self): item = item_factory() url = reverse('collecteditem-create-list') res = self.app.post(url, json.dumps({'item': item.pk, 'collection': self.collection.pk}), content_type='application/json', expect_errors=True) self.assertEqual(res.status_code, 403)
def test_user_cannot_add_non_existing_styletag_to_item(self): item = item_factory() url = reverse('itemstyletag-create-list') res = self.app.post(url, json.dumps({'item': item.pk, 'styletag': 'doesnotexist'}), content_type='application/json', user=self.user, expect_errors=True) self.assertEqual(res.status_code, 400)
def test_styletags(self): item = item_factory() styletag = styletag_factory() ItemStyleTag.objects.create(item=item, styletag=styletag) res = self.app.get(self.url) self.assertEqual(res.json["results"][0]["styletags"], [styletag.name])
def test_user_can_add_item_to_their_collection(self): item = item_factory() url = reverse('collecteditem-create-list') res = self.app.post(url, json.dumps({'item': item.pk, 'collection': self.collection.pk}), content_type='application/json', user=self.collection.owner) self.assertEqual({item}, set(self.collection.items.all()))
def test_404_when_no_item_parameter(self): CollectedItem.objects.create(item=item_factory(), collection=self.collection) url = reverse('collecteditem-delete-detail', args=(self.collection.pk,)) res = self.app.delete(url, user=self.collection.owner, expect_errors=True) self.assertEqual(res.status_code, 404)
def setUp(self): self.user = user_factory() self.collection = collection_factory(initial_items=0, owner=self.user) self.item1 = item_factory() self.item2 = item_factory() self.url = reverse('collection-update-detail', args=[self.collection.pk])
def test_filter_max_price(self): item1 = item_factory(price=1) item2 = item_factory(price=2) res = self.app.get(self.url, params={"max_price": 1}) self.assertEqual({item1.id}, {obj["id"] for obj in res.json["results"]})
def test_filter_color(self): item1 = item_factory(color=1) item2 = item_factory(color=2) res = self.app.get(self.url, params={"colors": [1]}) self.assertEqual({item1.id}, {obj["id"] for obj in res.json["results"]})
def test_list_items(self): item1 = item_factory() item2 = item_factory() res = self.app.get(self.url) self.assertIsSubsetOf({item1.id, item2.id}, {obj["id"] for obj in res.json["results"]})