Esempio n. 1
0
        def test_should_only_display_recipes_of_users_that_in_account_following_list(
                self, api_client):
            first_user = RecipeFactory().author
            api_client.force_authenticate(first_user)

            second_recipe = RecipeFactory()
            third_recipe = RecipeFactory()

            first_user.following.add(second_recipe.author)

            response = api_client.get(followed_users_recipes_url)

            assert f'{second_recipe}' in f'{response.content}'
            assert f'{third_recipe}' not in f'{response.content}'
Esempio n. 2
0
        def test_not_author_cant_delete_recipe(self, api_client):
            new_recipe = RecipeFactory()
            random_user = UserFactory()
            api_client.force_authenticate(random_user)
            response = api_client.delete(new_recipe.get_absolute_url())

            assert response.status_code == 403
Esempio n. 3
0
        def test_author_can_update_own_recipe(self, api_client):
            new_recipe = RecipeFactory()
            api_client.force_authenticate(new_recipe.author)
            data = {'title': 'updated title'}
            response = api_client.patch(new_recipe.get_absolute_url(), data)

            assert response.status_code == 200
Esempio n. 4
0
        def test_ratings_from_different_users(self, api_client):
            new_user = UserFactory()
            api_client.force_authenticate(new_user)
            new_recipe = RecipeFactory()

            data = {
                'recipe': new_recipe.id,
                'stars': '5'
            }
            api_client.post(rating_rate_url, data)
            api_client.logout()

            new_user2 = UserFactory()
            api_client.force_authenticate(new_user2)
            data = {
                'recipe': new_recipe.id,
                'stars': '4'
            }
            api_client.post(rating_rate_url, data)

            first_rating = Rating.objects.all().get(author__exact=new_user.id)
            second_rating = Rating.objects.all().get(author__exact=new_user2.id)


            assert Rating.objects.all().count() == 2
            assert first_rating.stars == 5
            assert second_rating.stars== 4
Esempio n. 5
0
        def test_like_post_request(self, api_client):
            new_user = UserFactory()
            api_client.force_authenticate(new_user)
            new_recipe = RecipeFactory()
            data = {'recipe': new_recipe.id}
            response = api_client.post(like_url, data)

            assert response.status_code == 200
Esempio n. 6
0
        def test_not_author_cant_update_recipe(self, api_client):
            new_recipe = RecipeFactory()
            random_user = UserFactory()
            api_client.force_authenticate(random_user)
            data = {'title': 'updated title'}
            response = api_client.patch(new_recipe.get_absolute_url(), data)

            assert response.status_code == 403
Esempio n. 7
0
        def test_user_second_like_to_recipe_will_unlike(self, api_client):
            new_user = UserFactory()
            api_client.force_authenticate(new_user)
            new_recipe = RecipeFactory()
            data = {'recipe': new_recipe.id}
            api_client.post(like_url, data)
            api_client.post(like_url, data)

            assert new_recipe.likes.all().count() == 0
Esempio n. 8
0
def search_comment_response(api_client):
    new_recipe = RecipeFactory()
    search_comment_url = '/api/comments/search/'

    data = {
        'recipe': {new_recipe.id},
    }

    response = api_client.post(search_comment_url, data)

    return response
Esempio n. 9
0
        def test_rate_post_request(self, api_client):
            new_user = UserFactory()
            api_client.force_authenticate(new_user)
            new_recipe = RecipeFactory()
            new_recipe = Recipe.objects.all().get(id__exact=new_recipe.id)
            data = {
                'recipe': new_recipe.id,
                'stars': 5
            }
            response = api_client.post(rating_rate_url, data)

            assert response.status_code == 200
Esempio n. 10
0
        def test_rate_post_request_should_fail(self, api_client):
            new_recipe = RecipeFactory()

            
            new_recipe = Recipe.objects.all().get(id__exact=new_recipe.id)
            data = {
                'recipe': new_recipe.id,
                'stars': 5
            }
            response = api_client.post(rating_rate_url, data)

            assert response.status_code == 401
Esempio n. 11
0
        def test_rating_recipe_calculate_account_stars(self, api_client):
            first_recipe = RecipeFactory()
            recipe_author = first_recipe.author
            api_client.force_authenticate(recipe_author)

            second_recipe = RecipeFactory.build()
            data = {
                'title': {second_recipe.title},
                'description': {second_recipe.description},
                'flavor_type': {second_recipe.flavor_type}, 
            }
            api_client.post(create_recipe_url, data)
            second_recipe = Recipe.objects.all().get(title=second_recipe.title)

            data = {
                'recipe': first_recipe.id,
                'stars': 5
            }

            api_client.post(rating_rate_url, data)
            
            data = {
                'recipe': second_recipe.id,
                'stars': 0
            }
            api_client.post(rating_rate_url, data)
            api_client.logout()


            second_user = UserFactory()
            api_client.force_authenticate(second_user)
            data = {
                'recipe': first_recipe.id,
                'stars': 5
            }
            api_client.post(rating_rate_url, data)

            data = {
                'recipe': second_recipe.id,
                'stars': 5
            }
            api_client.post(rating_rate_url, data)
            api_client.logout()


            first_recipe = Recipe.objects.all().get(title=first_recipe.title)
            second_recipe = Recipe.objects.all().get(title=second_recipe.title)
            user = UserAccount.objects.all().get(id=recipe_author.id)

            assert first_recipe.stars == '5.0'
            assert second_recipe.stars == '2.5'
            assert Rating.get_account_stars_score(user=user) == 3.75
            assert user.stars == '3.75'
Esempio n. 12
0
        def test_comment_create_post_request_not_allowed(self, api_client):
            new_user = UserFactory()
            comment_data = CommentFactory.build()
            new_recipe = RecipeFactory()

            data = {
                'title': {comment_data.title},
                'recipe': {new_recipe.id},
                'author': {new_user.id}
            }
            response = api_client.post(create_comment_url, data)

            assert response.status_code == 401
Esempio n. 13
0
        def test_likes_from_different_users(self, api_client):
            new_user = UserFactory()
            api_client.force_authenticate(new_user)
            new_recipe = RecipeFactory()
            data = {'recipe': new_recipe.id}
            api_client.post(like_url, data)
            api_client.logout()

            new_user2 = UserFactory()
            api_client.force_authenticate(new_user2)
            data = {'recipe': new_recipe.id}
            api_client.post(like_url, data)

            assert new_recipe.likes.all().count() == 2
Esempio n. 14
0
def test_get_recipe_avg_rating_score(api_client):
    new_recipe = RecipeFactory()
    api_client.force_authenticate(new_recipe.author)
    new_recipe = Recipe.objects.all().get(id__exact=new_recipe.id)
    data = {'recipe': new_recipe.id, 'stars': 5}
    api_client.post(rating_create_url, data)
    api_client.logout()

    new_user = UserFactory()
    api_client.force_authenticate(new_user)
    data = {'recipe': new_recipe.id, 'stars': 1}
    api_client.post(rating_create_url, data)

    recipe_avg_stars = Rating.get_recipe_stars_score(recipe=new_recipe)

    assert recipe_avg_stars == 3.0
Esempio n. 15
0
        def test_comment_author_is_current_logged_in_user(self, api_client):
            ''' testing the method perform_create '''
            new_user = UserFactory()
            api_client.force_authenticate(new_user)
            new_recipe = RecipeFactory()
            comment_data = CommentFactory.build()

            data = {
                'title': {comment_data.title},
                'recipe': {new_recipe.id},
                'author': {new_user.id}
            }
            api_client.post(create_comment_url, data)
            new_comment = Comment.objects.get(title=comment_data.title)

            assert new_comment.author == new_user
Esempio n. 16
0
def test_get_all_likes(api_client):
    new_recipe = RecipeFactory()
    new_user = new_recipe.author
    api_client.force_authenticate(new_user)
    like_url = reverse('likes:like')
    data = {'recipe': new_recipe.id}
    api_client.post(like_url, data)
    api_client.logout()

    new_user = UserFactory()
    api_client.force_authenticate(new_user)
    like_url = reverse('likes:like')
    data = {'recipe': new_recipe.id}
    api_client.post(like_url, data)

    assert new_recipe.get_all_likes() == 2
    assert new_recipe.get_all_likes() == new_recipe.likes.all().count()
Esempio n. 17
0
        def test_recipe_author_can_delete_other_users_recipe_comments_on_his_recipe(
                self, api_client):
            new_recipe = RecipeFactory()
            new_user = UserFactory()
            api_client.force_authenticate(new_user)
            comment_data = CommentFactory.build()

            data = {
                'title': {comment_data.title},
                'recipe': {new_recipe.id},
                'author': {new_user.id}
            }
            response = api_client.post(create_comment_url, data)

            api_client.logout()
            api_client.force_authenticate(new_recipe.author)
            new_comment = Comment.objects.get(title=comment_data.title)
            response = api_client.delete(new_comment.get_delete_url())

            assert response.status_code == 204
Esempio n. 18
0
        def test_user_second_rate_will_update_the_first_one(self, api_client):
            new_user = UserFactory()
            api_client.force_authenticate(new_user)
            new_recipe = RecipeFactory()
            new_recipe = Recipe.objects.all().get(id__exact=new_recipe.id)
            data = {
                'recipe': new_recipe.id,
                'stars': 5
            }
            
            api_client.post(rating_rate_url, data)
            data = {
                'recipe': new_recipe.id,
                'stars': 4
            }
            response = api_client.post(rating_rate_url, data)

            rating = Rating.objects.all().get(author__exact=new_user.id)

            assert response.status_code == 200
            assert rating.stars == 4
            assert Rating.objects.all().count() == 1  
Esempio n. 19
0
        def test_rating_recipe_calculate_recipe_stars(self, api_client):
            recipe = RecipeFactory()
            first_user = UserFactory()
            api_client.force_authenticate(first_user)

            data = {
                'recipe': recipe.id,
                'stars': 4
            }
            api_client.post(rating_rate_url, data)
            api_client.logout()

            second_user = UserFactory()
            api_client.force_authenticate(second_user)
            data = {
                'recipe': recipe.id,
                'stars': 2
            }
            api_client.post(rating_rate_url, data)
            recipe = Recipe.objects.all().get(id=recipe.id)

            assert recipe.stars == '3.0'
            assert recipe.stars == str(Rating.get_recipe_stars_score(recipe=recipe))
Esempio n. 20
0
def test_get_absolute_url():
    new_recipe = RecipeFactory()

    assert new_recipe.get_absolute_url() == reverse(
        'recipes:detail', kwargs={"pk": new_recipe.id})
Esempio n. 21
0
        def test_guest_user_cant_update_recipe(self, api_client):
            first_recipe = RecipeFactory()
            data = {'title': 'updated title'}
            response = api_client.patch(first_recipe.get_absolute_url(), data)

            assert response.status_code == 401
Esempio n. 22
0
        def test_recipe_delete(self, api_client):
            new_recipe = RecipeFactory()
            response = api_client.delete(new_recipe.get_absolute_url())

            assert response.status_code == 401
Esempio n. 23
0
        def test_like_post_request_not_allowed(self, api_client):
            new_recipe = RecipeFactory()
            data = {'recipe': new_recipe.id}
            response = api_client.post(like_url, data)

            assert response.status_code == 401
Esempio n. 24
0
        def test_author_can_delete_own_recipe(self, api_client):
            new_recipe = RecipeFactory()
            api_client.force_authenticate(new_recipe.author)
            response = api_client.delete(new_recipe.get_absolute_url())

            assert response.status_code == 204
Esempio n. 25
0
        def test_recipe_detail_page_render(self, api_client):
            new_recipe = RecipeFactory()
            response = api_client.get(new_recipe.get_absolute_url())

            assert response.status_code == 200
Esempio n. 26
0
def test__str__():
    new_recipe = RecipeFactory()

    assert new_recipe.__str__() == new_recipe.title
Esempio n. 27
0
 def test_detail_reverse(self):
     new_recipe = RecipeFactory()
     url = reverse('recipes:detail', kwargs={'pk': new_recipe.id})
     assert url == f'/api/recipes/{new_recipe.id}/'
Esempio n. 28
0
 def test_detail_resolve(self):
     new_recipe = RecipeFactory()
     url = f'/api/recipes/{new_recipe.id}/'
     assert resolve(url).view_name == 'recipes:detail'