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
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
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
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'
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}'
def test_list_with_query_parameter(self): self.recipe = RecipeFactory.create( name='asd', prep_time=3, difficulty=1, vegetarian=True, ) response = self.client.get( '/recipes/?name=asd', headers=[self.headers]) response_recipes = json.loads(response.data.decode()) self.assertEqual( self.recipe['name'], response_recipes['results'][0]['name'] ) self.assertEqual( self.recipe['prep_time'], response_recipes['results'][0]['prep_time'] ) self.assertEqual( self.recipe['vegetarian'], response_recipes['results'][0]['vegetarian'] ) self.assertEqual( self.recipe['difficulty'], response_recipes['results'][0]['difficulty'] ) self.assertEqual(response.status_code, HTTP_STATUS_CODE['200_OK'])
def setUp(self): self.recipe = RecipeFactory.create( name='asdasdas', prep_time=3, difficulty=1, vegetarian=True, )
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
class BasicPushTest(TestCase): def test_push(self): self.u = UserFactory() self.d = DeploymentFactory() self.setting = SettingFactory(deployment=self.d) self.recipe = RecipeFactory() self.shell_recipe = ShellRecipeFactory() self.stage = StageFactory( deployment=self.d, recipe=self.recipe) self.stage2 = StageFactory( deployment=self.d, recipe=self.shell_recipe, name="test stage 2", ) push = self.d.new_push(self.u, "test push") # haven't run anything so it should be inprogress self.assertEquals(push.status, "inprogress") for s in self.d.stage_set.all(): push.run_stage(s.id) # should have completed successfully self.assertEquals(push.status, "ok") # and let's make sure a setting variable has round-tripped ps = push.pushstage_set.get(stage=self.stage2) self.assertEquals(ps.stdout(), "TEST_BAR\n") self.assertEquals(ps.stderr(), "") self.assertEquals(self.d.most_recent_push(), push) self.assertEquals(self.d.status(), push.status) self.assertEquals(self.d.last_message(), push.comment) self.assertEquals(self.d.last_push_date(), push.start_time) self.assertEquals(push.get_absolute_url(), "/push/%d/" % push.id) push.run_stage(self.stage.id, rollback_id=self.stage.id) for pstage in push.reverse_pushstages(): self.assertEquals(pstage.setting('foo'), '') def test_recipe_absolute_url(self): self.recipe = RecipeFactory() self.assertEquals( self.recipe.get_absolute_url(), "/cookbook/%d/" % self.recipe.id) def test_stage_absolute_url(self): self.stage = StageFactory() self.assertEquals( self.stage.get_absolute_url(), "/stage/%d/" % self.stage.id) def test_stage_all_recipes(self): self.shell_recipe = ShellRecipeFactory() self.stage = StageFactory() self.stage2 = StageFactory(recipe=self.shell_recipe) self.assertEquals( [r.id for r in self.stage.all_recipes()], [self.shell_recipe.id])
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()
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
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
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
def test_rating_recipe(self): recipe = RecipeFactory.create( name='asdasdas', prep_time=3, difficulty=1, vegetarian=True, ) recipe.save() recipe.rate(2) rates = Rate.objects(recipe_id=recipe.id) self.assertTrue(rates)
def test_should_display_top_rated_recipes(self, api_client): for i in range(10): new_user = UserFactory() api_client.force_authenticate(new_user) recipe_data = RecipeFactory.build() data = { 'title': {recipe_data.title}, 'description': {recipe_data.description}, 'flavor_type': {recipe_data.flavor_type}, } api_client.post(create_recipe_url, data) new_recipe = Recipe.objects.all().get(title=recipe_data.title) new_recipe.stars = 5 new_recipe.save() api_client.logout() for i in range(10): new_user = UserFactory() api_client.force_authenticate(new_user) recipe_data = RecipeFactory.build() data = { 'title': {recipe_data.title}, 'description': {recipe_data.description}, 'flavor_type': {recipe_data.flavor_type}, } api_client.post(create_recipe_url, data) new_recipe = Recipe.objects.all().get(title=recipe_data.title) new_recipe.stars = 4 new_recipe.save() api_client.logout() api_client.force_authenticate(new_user) response = api_client.get(top_rated_recipes_url) top_rated_recipes = Recipe.objects.all().filter(stars=5) bottom_rated_recipes = Recipe.objects.all().filter(stars=4) for recipe in top_rated_recipes: assert f'{recipe}' in f'{response.content}' for recipe in bottom_rated_recipes: assert f'{recipe}' not in f'{response.content}'
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
def test_recipe_create_post_request(self, api_client): new_user = UserFactory() api_client.force_authenticate(new_user) recipe_data = RecipeFactory.build() data = { 'title': {recipe_data.title}, 'description': {recipe_data.description}, 'flavor_type': {recipe_data.flavor_type}, } response = api_client.post(create_recipe_url, data) assert response.status_code == 201
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
def test_recipe_create_post_request_not_allowed(self, api_client): new_user = UserFactory() recipe_data = RecipeFactory.build() data = { 'author': {new_user.id}, 'title': {recipe_data.title}, 'description': {recipe_data.description}, 'flavor_type': {recipe_data.flavor_type}, } response = api_client.post(create_recipe_url, data) assert response.status_code == 401
def test_get_account_stars_score(api_client): user = UserFactory() api_client.force_authenticate(user) first_recipe = RecipeFactory.build() create_recipe_url = reverse('recipes:create') data = { 'title': {first_recipe.title}, 'description': {first_recipe.description}, 'flavor_type': {first_recipe.flavor_type}, } api_client.post(create_recipe_url, data) 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) first_recipe = Recipe.objects.all().get(title__exact=first_recipe.title) data = {'recipe': first_recipe.id, 'stars': 5} api_client.post(rating_create_url, data) second_recipe = Recipe.objects.all().get(title__exact=second_recipe.title) data = {'recipe': second_recipe.id, 'stars': 0} api_client.post(rating_create_url, data) second_user = UserFactory() api_client.force_authenticate(second_user) data = {'recipe': first_recipe.id, 'stars': 1} api_client.post(rating_create_url, data) data = {'recipe': second_recipe.id, 'stars': 3} api_client.post(rating_create_url, data) account_avg_stars = Rating.get_account_stars_score(user=user) assert account_avg_stars == 2.25
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
def test_recipe_author_is_current_logged_in_user(self, api_client): ''' testing the method perform_create ''' first_user = UserFactory() api_client.force_authenticate(first_user) recipe_data = RecipeFactory.build() data = { 'title': {recipe_data.title}, 'description': {recipe_data.description}, 'flavor_type': {recipe_data.flavor_type}, } api_client.post(create_recipe_url, data) new_recipe = Recipe.objects.get(title=recipe_data.title) assert new_recipe.author == first_user
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
def test_should_display_all_recipes_of_users_that_in_account_following_list( self, api_client): first_user = RecipeFactory().author second_user = UserFactory() api_client.force_authenticate(second_user) for i in range(10): recipe_data = RecipeFactory.build() data = { 'title': {recipe_data.title}, 'description': {recipe_data.description}, 'flavor_type': {recipe_data.flavor_type}, } api_client.post(create_recipe_url, data) api_client.logout() api_client.force_authenticate(first_user) first_user.following.add(second_user) response = api_client.get(followed_users_recipes_url) recipes = Recipe.objects.all().filter(author=second_user) for recipe in recipes: assert f'{recipe}' in f'{response.content}'
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
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
def test_pagination(self): self.recipes = RecipeFactory.create_batch( size=50, **{ 'name': 'asdasdas', 'prep_time': 3, 'difficulty': 1, 'vegetarian': True, } ) response = self.client.get('/recipes/?page=1', headers=[self.headers]) response_recipes = json.loads(response.data.decode()) self.assertEqual(30, len(response_recipes['results'])) response = self.client.get('/recipes/?page=2', headers=[self.headers]) response_recipes = json.loads(response.data.decode()) self.assertEqual(21, len(response_recipes['results'])) self.assertEqual(response.status_code, HTTP_STATUS_CODE['200_OK'])
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
def test_push(self): self.u = UserFactory() self.d = DeploymentFactory() self.setting = SettingFactory(deployment=self.d) self.recipe = RecipeFactory() self.shell_recipe = ShellRecipeFactory() self.stage = StageFactory( deployment=self.d, recipe=self.recipe) self.stage2 = StageFactory( deployment=self.d, recipe=self.shell_recipe, name="test stage 2", ) push = self.d.new_push(self.u, "test push") # haven't run anything so it should be inprogress self.assertEquals(push.status, "inprogress") tempdir = tempfile.mkdtemp() with self.settings(SCRIPT_DIR=tempdir): for s in self.d.stage_set.all(): push.run_stage(s.id) shutil.rmtree(tempdir) # should have completed successfully self.assertEquals(push.status, "ok") # and let's make sure a setting variable has round-tripped ps = push.pushstage_set.get(stage=self.stage2) self.assertEquals(ps.stdout(), "TEST_BAR\n") self.assertEquals(ps.stderr(), "") self.assertEquals(self.d.most_recent_push(), push) self.assertEquals(self.d.status(), push.status) self.assertEquals(self.d.last_message(), push.comment) self.assertEquals(self.d.last_push_date(), push.start_time) self.assertEquals(push.get_absolute_url(), "/push/%d/" % push.id) push.run_stage(self.stage.id, rollback_id=self.stage.id) for pstage in push.reverse_pushstages(): self.assertEquals(pstage.setting('foo'), '')
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
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))
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
def test__str__(): new_recipe = RecipeFactory() assert new_recipe.__str__() == new_recipe.title
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
def test_recipe_delete(self, api_client): new_recipe = RecipeFactory() response = api_client.delete(new_recipe.get_absolute_url()) assert response.status_code == 401
def test_recipe_absolute_url(self): self.recipe = RecipeFactory() self.assertEquals( self.recipe.get_absolute_url(), "/cookbook/%d/" % self.recipe.id)
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