def test_rating_info(comment, user, another_user, apiclient): ct = ContentType.objects.get_for_model(comment) pk = comment.pk ratings_url = reverse( 'ratings-list', kwargs={'content_type': ct.pk, 'object_pk': pk}) apiclient.force_authenticate(user) data = {'value': 1} with active_phase(comment.content_object.module, AskPhase): response = apiclient.post(ratings_url, data, format='json') assert response.status_code == status.HTTP_201_CREATED comment_url = reverse( 'comments-detail', kwargs={ 'pk': comment.pk, 'content_type': comment.content_type.pk, 'object_pk': comment.object_pk }) response = apiclient.get(comment_url, format='json') assert response.data['ratings']['positive_ratings'] == 1 assert response.data['ratings']['current_user_rating_value'] == 1 apiclient.force_authenticate(another_user) response = apiclient.get(comment_url, format='json') assert response.data['ratings']['positive_ratings'] == 1 assert response.data['ratings']['current_user_rating_value'] is None data = {'value': -1} with active_phase(comment.content_object.module, AskPhase): response = apiclient.post(ratings_url, data, format='json') response.status_code == status.HTTP_201_CREATED response = apiclient.get(comment_url, format='json') assert response.data['ratings']['positive_ratings'] == 1 assert response.data['ratings']['current_user_rating_value'] == -1
def test_meta_info_of_rating(rating_factory, question, question_ct, apiclient, user, another_user): apiclient.force_authenticate(user) url = reverse('ratings-list', kwargs={ 'content_type': question_ct.pk, 'object_pk': question.pk }) data = {'value': 1} with active_phase(question.module, RatePhase): response = apiclient.post(url, data, format='json') rating_id = response.data['id'] assert response.status_code == status.HTTP_201_CREATED assert response.data['value'] == 1 metadata = response.data['meta_info'] assert metadata['positive_ratings_on_same_object'] == 1 assert metadata['user_rating_on_same_object_value'] == 1 assert metadata['user_rating_on_same_object_id'] == rating_id apiclient.force_authenticate(another_user) with active_phase(question.module, RatePhase): response = apiclient.post(url, data, format='json') rating_id = response.data['id'] assert response.status_code == status.HTTP_201_CREATED assert response.data['value'] == 1 metadata = response.data['meta_info'] assert metadata['positive_ratings_on_same_object'] == 2 assert metadata['user_rating_on_same_object_value'] == 1 assert metadata['user_rating_on_same_object_id'] == rating_id
def test_cta_paginator_mixin(rf, user, phase): class TestView(mixins.CtaPaginatorMixin, generic.ListView): model = models.Idea view = TestView.as_view() module = phase.module request = rf.get('/ideas/') request.user = AnonymousUser() response = view(request) instance = response.context_data['view'] assert not instance.is_cta_enabled assert instance.cta_object is None request = rf.get('/ideas/') request.user = user response = view(request) instance = response.context_data['view'] assert not instance.is_cta_enabled assert instance.cta_object is None with active_phase(module, phases.IdeaPhase): request = rf.get('/ideas/') request.user = user response = view(request) instance = response.context_data['view'] assert instance.is_cta_enabled assert instance.cta_object == module
def test_participant_can_vote_in_private_project(user, apiclient, poll_factory, question_factory, choice_factory): poll = poll_factory() project = poll.module.project project.is_public = False project.participants.add(user) project.save() question = question_factory(poll=poll) choice1 = choice_factory(question=question) choice_factory(question=question) assert Vote.objects.count() == 0 apiclient.force_authenticate(user=user) url = reverse('votes-list', kwargs={'question_pk': question.pk}) data = {'choices': [choice1.pk]} with active_phase(poll.module, VotingPhase): response = apiclient.post(url, data, format='json') assert response.status_code == status.HTTP_201_CREATED assert Vote.objects.count() == 1
def test_anonymous_user_can_not_rating(apiclient, question, question_ct): url = reverse('ratings-list', kwargs={ 'content_type': question_ct.pk, 'object_pk': question.pk }) data = {} with active_phase(question.module, RatePhase): response = apiclient.post(url, data, format='json') assert response.status_code == status.HTTP_403_FORBIDDEN
def test_phase_allows_add_active(user_factory, question_factory, project_factory, organisation): user = user_factory() admin = user_factory(is_superuser=True) project = project_factory(access=Access.PUBLIC) question = question_factory(module__project=project) with active_phase(question.module, AskPhase): assert not predicates.phase_allows_add(Question)(user, False) assert predicates.phase_allows_add(Question)(user, question.module) assert predicates.phase_allows_add(Question)(admin, question.module)
def test_phase_allows_comment_other(user_factory, question_factory, project_factory, organisation): user = user_factory() admin = user_factory(is_superuser=True) project = project_factory(access=Access.PUBLIC) question = question_factory(module__project=project) with active_phase(question.module, RatePhase): assert not predicates.phase_allows_comment(user, False) assert not predicates.phase_allows_comment(user, question) assert not predicates.phase_allows_comment(admin, question)
def test_post_invalid_data(user, apiclient, question, question_ct): apiclient.force_authenticate(user=user) url = reverse('ratings-list', kwargs={ 'content_type': question_ct.pk, 'object_pk': question.pk }) data = {} with active_phase(question.module, RatePhase): response = apiclient.post(url, data, format='json') assert response.status_code == status.HTTP_400_BAD_REQUEST
def test_anonymous_user_can_not_delete_rating(rating, apiclient): apiclient.force_authenticate(user=None) url = reverse('ratings-detail', kwargs={ 'pk': rating.pk, 'content_type': rating.content_type.pk, 'object_pk': rating.object_pk }) with active_phase(rating.content_object.module, RatePhase): response = apiclient.delete(url) assert response.status_code == status.HTTP_403_FORBIDDEN
def test_authenticated_user_can_post_valid_data(user, question_ct, question, apiclient): apiclient.force_authenticate(user=user) url = reverse( 'comments_async-list', kwargs={'content_type': question_ct.pk, 'object_pk': question.pk}) data = {'comment': 'comment comment'} with active_phase(question.module, AskPhase): response = apiclient.post(url, data, format='json') assert response.status_code == status.HTTP_201_CREATED
def test_anonymous_user_can_not_edit_comment(comment, apiclient): apiclient.force_authenticate(user=None) url = reverse('comments-detail', kwargs={ 'pk': comment.pk, 'content_type': comment.content_type.pk, 'object_pk': comment.object_pk }) data = {'comment': 'comment comment comment'} with active_phase(comment.content_object.module, AskPhase): response = apiclient.patch(url, data, format='json') assert response.status_code == status.HTTP_403_FORBIDDEN
def test_authenticated_user_can_reply_to_comment(another_user, comment, apiclient): comment_contenttype = ContentType.objects.get_for_model(comment).pk original_url = reverse('comments-detail', kwargs={ 'pk': comment.pk, 'content_type': comment.content_type.pk, 'object_pk': comment.object_pk }) response = apiclient.get(original_url) assert len(response.data['child_comments']) == 0 apiclient.force_authenticate(user=another_user) reply_url = reverse('comments-list', kwargs={ 'content_type': comment_contenttype, 'object_pk': comment.pk }) data = { 'comment': 'comment-reply1', } with active_phase(comment.content_object.module, AskPhase): response = apiclient.post(reply_url, data, format='json') assert response.status_code == status.HTTP_201_CREATED data = { 'comment': 'comment-reply2', } with active_phase(comment.content_object.module, AskPhase): response = apiclient.post(reply_url, data, format='json') assert response.status_code == status.HTTP_201_CREATED response = apiclient.get(original_url) assert len(response.data['child_comments']) == 2 assert 'child_comments' not in response.data['child_comments'][0] assert response.data['child_comments'][0]['comment'] == 'comment-reply1' assert response.data['child_comments'][1]['comment'] == 'comment-reply2'
def test_authenticated_user_can_edit_own_comment(comment, apiclient): apiclient.force_authenticate(user=comment.creator) url = reverse('comments-detail', kwargs={ 'pk': comment.pk, 'content_type': comment.content_type.pk, 'object_pk': comment.object_pk }) data = {'comment': 'comment comment comment'} with active_phase(comment.content_object.module, AskPhase): response = apiclient.patch(url, data, format='json') assert response.status_code == status.HTTP_200_OK assert response.data['comment'] == 'comment comment comment'
def test_creator_of_rating_can_set_zero(rating, question_ct, apiclient): question = rating.content_object url = reverse('ratings-detail', kwargs={ 'pk': rating.pk, 'content_type': rating.content_type.pk, 'object_pk': rating.object_pk, }) apiclient.force_authenticate(user=rating.creator) with active_phase(question.module, RatePhase): response = apiclient.delete(url) assert response.status_code == status.HTTP_200_OK assert response.data['value'] == 0
def test_creater_of_comment_can_set_removed_flag(comment, user, apiclient): url = reverse('comments-detail', kwargs={ 'pk': comment.pk, 'content_type': comment.content_type.pk, 'object_pk': comment.object_pk }) apiclient.force_authenticate(user=user) with active_phase(comment.content_object.module, AskPhase): response = apiclient.delete(url) assert response.status_code == status.HTTP_200_OK assert response.data['is_deleted'] is True assert response.data['comment'] == 'deleted by creator'
def test_idea_rate_rules(admin, user, idea_factory, module): idea = idea_factory(module=module) user = UserFactory() assert not rules.has_perm('civic_europe_ideas.rate_idea', user, idea) idea.creator = user assert not rules.has_perm('civic_europe_ideas.rate_idea', user, idea) idea.co_workers.add(user) assert not rules.has_perm('civic_europe_ideas.rate_idea', user, idea) user2 = UserFactory() idea2 = idea_factory(creator=user2, module=module) with active_phase(module, phases.CommunityAwardRatingPhase): assert not rules.has_perm('civic_europe_ideas.rate_idea', user, idea) assert not rules.has_perm('civic_europe_ideas.rate_idea', user, idea2)
def test_extra_object_tools(client, admin, module): client.force_login(admin) changelist_url = reverse('admin:civic_europe_ideas_idea_changelist') response = client.get(changelist_url) object_tools = response.context_data['cl']\ .model_admin\ .extra_list_object_tools assert len(object_tools) == 0 with active_phase(module, InterimShortlistPublicationPhase): response = client.get(changelist_url) object_tools = response.context_data['cl']\ .model_admin\ .extra_list_object_tools assert len(object_tools) == 2
def test_authenticated_user_can_edit_own_rating(rating, question_ct, apiclient): question = rating.content_object apiclient.force_authenticate(user=rating.creator) data = {'value': 0} url = reverse('ratings-detail', kwargs={ 'pk': rating.pk, 'content_type': question_ct.pk, 'object_pk': question.pk }) with active_phase(question.module, RatePhase): response = apiclient.patch(url, data, format='json') assert response.status_code == status.HTTP_200_OK assert response.data['value'] == 0
def test_idea_list_view_idea_phase(client, idea_factory, module): url = reverse('idea-list') # the default filters for the ideaSketchPhase set # 'status': 'idea' and the year (project) to the active one with active_phase(module, phases.IdeaPhase): idea_factory(module=module) idea_factory(module=module) response = client.get(url) assert len(response.context_data['object_list']) == 2 assert response.status_code == 200 response = client.get(url + '?field_of_action=ES&ordering=newest&' 'project=&status=') assert len(response.context_data['object_list']) == 2 assert response.status_code == 200
def test_fields(user, apiclient, question_ct, question): '''This will need to get adapeted whenever things change''' apiclient.force_authenticate(user=user) url = reverse( 'comments_async-list', kwargs={'content_type': question_ct.pk, 'object_pk': question.pk}) data = {'comment': 'comment comment'} with active_phase(question.module, AskPhase): apiclient.post(url, data, format='json') url = reverse( 'comments_async-list', kwargs={'content_type': question_ct.pk, 'object_pk': question.pk}) response = apiclient.get(url) assert len(response.data) == 4 assert 'count' in response.data assert 'next' in response.data assert 'previous' in response.data assert 'results' in response.data assert response.data['count'] == 1 commentDict = response.data['results'][0] assert len(commentDict) == 17 assert 'child_comments' in commentDict assert 'comment' in commentDict assert 'comment_categories' in commentDict assert 'content_type' in commentDict assert 'created' in commentDict assert 'id' in commentDict assert 'is_deleted' in commentDict assert 'is_moderator' in commentDict assert 'is_moderator_marked' in commentDict assert 'last_discussed' in commentDict assert 'modified' in commentDict assert 'object_pk' in commentDict assert 'ratings' in commentDict assert 'user_name' in commentDict assert 'user_pk' in commentDict assert 'user_profile_url' in commentDict assert 'user_image' in commentDict
def test_notify_shortlist( client, admin, module, idea_factory, idea_follow_factory ): """ Send notifications to shortlist """ idea_factory(module=module) i1 = idea_factory(module=module, is_on_shortlist=True) i2 = idea_factory( module=module, is_on_shortlist=True, community_award_winner=True ) follow = idea_follow_factory(followable=i1.idea) client.force_login(admin) # notify shortlist followers notifyshortlist_url = reverse( 'admin:civic_europe_ideas_idea_notify_shortlist' ) with active_phase(module, InterimShortlistPublicationPhase): response = client.get(notifyshortlist_url) assert response.status_code == 200 assert set(response.context_data['ideas']) == set([i1.idea, i2.idea]) with intercept_emails() as emails: data = {'idea_ids': [i1.id, i2.id]} response = client.post(notifyshortlist_url, data) assert len(emails) == 2 email0 = emails[0] followers = set( i2.co_workers.all() ) assert set(email0.get_receivers()) == followers email1 = emails[1] followers = set( i1.co_workers.all() ) followers.add(follow.creator) assert set(email1.get_receivers()) == followers
def test_notify_winners( client, admin, module, idea_factory, idea_follow_factory ): """ Send notifications to all winners. List the currently selected winners. Send notifications to to winners listed even if there has been added an other winner in between. """ i1 = idea_factory(module=module, is_on_shortlist=True, is_winner=True) i2 = idea_factory(module=module, is_on_shortlist=True) idea_factory( module=module, is_on_shortlist=True, community_award_winner=True ) idea_factory(module=module) follow = idea_follow_factory(followable=i1) client.force_login(admin) # notify winner followers notifywinners_url = reverse( 'admin:civic_europe_ideas_idea_notify_winners' ) with active_phase(module, InterimShortlistPublicationPhase): response = client.get(notifywinners_url) assert response.status_code == 200 assert list(response.context_data['ideas']) == [i1] with intercept_emails() as emails: data = {'idea_ids': [i1.id]} response = client.post(notifywinners_url, data) i2.is_winner = True i2.save() assert len(emails) == 1 followers = set( i1.co_workers.all() ) followers.add(follow.creator) assert set(emails[0].get_receivers()) == followers
def test_comment_id_in_list(user, apiclient, question_ct, question): url = reverse( 'comments_async-list', kwargs={'content_type': question_ct.pk, 'object_pk': question.pk}) url += '?commentID=no_number' response = apiclient.get(url) assert response.status_code == status.HTTP_400_BAD_REQUEST url = reverse( 'comments_async-list', kwargs={'content_type': question_ct.pk, 'object_pk': question.pk}) url += '?commentID=-1' response = apiclient.get(url) assert response.status_code == status.HTTP_200_OK assert not response.data['comment_found'] commentID = 0 apiclient.force_authenticate(user=user) url = reverse( 'comments_async-list', kwargs={'content_type': question_ct.pk, 'object_pk': question.pk}) data = {'comment': 'comment comment'} with active_phase(question.module, AskPhase): response = apiclient.post(url, data, format='json') commentID = response.data['id'] assert commentID > 0 url = reverse( 'comments_async-list', kwargs={'content_type': question_ct.pk, 'object_pk': question.pk}) url += '?commentID={}'.format(commentID) response = apiclient.get(url) assert response.status_code == status.HTTP_200_OK assert response.data['comment_found']
def test_idea_add_rule_idea_phase(user, module): with active_phase(module, phases.IdeaPhase): assert rules.has_perm('civic_europe_ideas.add_idea', user, module)
def test_ideasketch_create_wizard(client, user, module, image): client.login(username=user.email, password='******') url = reverse('idea-create', kwargs={'slug': module.slug}) with active_phase(module, IdeaPhase): # Form 1 (Applicant) response = client.get(url) assert response.status_code == 200 data = { 'idea_create_wizard-current_step': '0', '0-first_name': 'Qwertz', '0-last_name': 'Uiopü', '0-lead_organisation_status': 'OT', } response = client.post(url, data) assert response.context['form'].errors == {'lead_organisation_details': ["You selected 'other' as " "organization status. " "Please provide more " "information about your " "current status."]} assert response.status_code == 200 data = { 'idea_create_wizard-current_step': '0', '0-lead_organisation_details': 'We are great', '0-first_name': 'Qwertz', '0-last_name': 'Uiopü', '0-lead_organisation_status': 'OT' } # Form 2 (Partners) response = client.post(url, data) assert response.context['form'].errors == {} assert response.status_code == 200 data = { 'idea_create_wizard-current_step': '1', } # Form 3 (Idea details) response = client.post(url, data) assert response.status_code == 200 img_file = open(finders.find('images/cta_tile_logo_colour.png'), "rb") data = { 'idea_create_wizard-current_step': '2', '2-title': 'My very good idea', '2-pitch': 'My very good idea is such a good idea!', '2-image': img_file, '2-country_of_implementation': 'CZ', '2-field_of_action': 'YP' } response = client.post(url, data) assert response.context['form'].errors == {} assert response.status_code == 200 img_file.close() img_file2 = open(finders.find('images/cta_tile_logo.png'), "rb") data = { 'idea_create_wizard-current_step': '2', '2-title': 'My very good idea', '2-subtitle': 'My very good idea - subtitle', '2-pitch': 'My very good idea is such a good idea!', '2-image': img_file2, '2-country_of_implementation': 'CZ', '2-field_of_action': 'YP' } # Form 4 (Local Dimension) response = client.post(url, data) assert response.context['form'].errors == {} assert response.status_code == 200 img_file2.close() data = { 'idea_create_wizard-current_step': '3', '3-location': 'Balance a ball on your nose', '3-location_details': 'UA', '3-cohesion': 'Because there`re no balls around', '3-challenge': 'I balanced a ball on my nose', '3-target_group': 'Children', '3-local_embedding': 'Children', '3-perspective_and_dialog': 'The ball can`t talk', } # Form 5 (Road to Impact) response = client.post(url, data) assert response.context['form'].errors == {} assert response.status_code == 200 data = { 'idea_create_wizard-current_step': '4', '4-plan': 'not_sure', '4-reach_out': 'Mr. Not So Sure', '4-results': '*****@*****.**', '4-impact': 'I will balance a ball on my nose', '4-sustainability': 'We will be very sure afterwards', '4-contribution': 'contribution', '4-knowledge': 'knowledge', '4-motivation': 'motivation' } # Form 6 (Finance Section) response = client.post(url, data) assert response.context['form'].errors == {} assert response.status_code == 200 data = { 'idea_create_wizard-current_step': '5', '5-total_budget': 20000, '5-budget_requested': 30000, '5-major_expenses': 'Food', '5-duration': 12 } response = client.post(url, data) assert response.context['form'].errors == \ {'__all__': ["The requested budget can't " "be higher than the total budget"]} data = { 'idea_create_wizard-current_step': '5', '5-total_budget': 20000, '5-budget_requested': 10000, '5-major_expenses': 'Food', '5-duration': 12 } # Form 7 (Network and Community Section) response = client.post(url, data) assert response.context['form'].errors == {} assert response.status_code == 200 data = { 'idea_create_wizard-current_step': '6', '6-network': 'network', '6-accept_conditions': True, '6-confirm_publicity': True } # Form 8 (Finish) response = client.post(url, data) assert response.context['form'].errors == {} assert response.status_code == 200 assert Idea.objects.all().count() == 0 data = { 'idea_create_wizard-current_step': '7', } response = client.post(url, data) my_idea = Idea.objects.get(title='My very good idea') assert response.status_code == 302 assert Idea.objects.all().count() == 1 assert my_idea.first_name == 'Qwertz' assert my_idea.target_group == 'Children' assert mail.outbox[0].subject == ( 'Welcome and thanks for sharing your idea!' ) assert mail.outbox[0].recipients() == [user.email]
def test_idea_add_rule_idea_community_rating_phase(user, module): with active_phase(module, phases.CommunityAwardRatingPhase): assert not rules.has_perm('civic_europe_ideas.add_idea', user, module)