def test_ad_hoc_check_vote_query(self): """As of 11/4/2017, the tutorial contains an query done outside Relay, to check whether a vote already exists. (On the client side? Really? Ask forgiveness rather than permisson, and save a round trip.) The query is done using a private API function (relay.environment._network.fetch) that, sure enough, went away in recent versions of Relay. Test that that query works (for older Relay versions, and in the event the tutorial is fixed for newer versions.) """ create_Link_orderBy_test_data() # creates more than one link user = create_test_user() user_gid = Node.to_global_id('User', user.pk) user2 = create_test_user(name='Another User', password='******', email='*****@*****.**') # create multiple votes for link in LinkModel.objects.all(): last_vote = VoteModel.objects.create(link_id=link.pk, user_id=user.pk) VoteModel.objects.create(link_id=link.pk, user_id=user2.pk) last_link = link link_gid = Node.to_global_id('Link', last_link.pk) vote_gid = Node.to_global_id('Vote', last_vote.pk) # make sure the query only returns one vote query = ''' query CheckVoteQuery($userId: ID!, $linkId: ID!) { viewer { allVotes(filter: { user: { id: $userId }, link: { id: $linkId } }) { edges { node { id } } } } } ''' variables = { 'userId': user_gid, 'linkId': link_gid, } expected = { 'viewer': { 'allVotes': { 'edges': [ { 'node': { 'id': vote_gid, } } ] } } } schema = graphene.Schema(query=Query) result = schema.execute(query, variable_values=variables) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def test_node_for_vote(self): link = LinkModel.objects.create(description='Test', url='http://a.com') user = create_test_user() vote = VoteModel.objects.create(link_id=link.pk, user_id=user.pk) vote_gid = Node.to_global_id('Vote', vote.pk) query = ''' query { node(id: "%s") { id ...on Vote { link { url } } } } ''' % vote_gid expected = { 'node': { 'id': vote_gid, 'link': { 'url': 'http://a.com', } } } schema = graphene.Schema(query=Query) result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def test_no_announcement_on_ideas(graphql_request, idea_with_en_fr): from graphene.relay import Node idea_id = idea_with_en_fr.id node_id = Node.to_global_id('Idea', idea_id) res = schema.execute(u""" query Idea($id: ID!, $lang: String!){ idea: node(id: $id) { ... on Idea { announcement { title(lang: $lang) body(lang: $lang) } } } }""", context_value=graphql_request, variable_values={ "id": node_id, "lang": "en" }) assert json.loads(json.dumps(res.data)) == { u'idea': { u'announcement': None } }
def test_node_for_user(self): user = create_test_user() user_gid = Node.to_global_id('User', user.pk) query = ''' query { node(id: "%s") { id ...on User { name } } } ''' % user_gid expected = { 'node': { 'id': user_gid, 'name': user.name, } } schema = graphene.Schema(query=Query) result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, expected, msg='\n' + repr(expected) + '\n' + repr(result.data))
def test_votes_count_on_link_test(self): """test count field on votes field on Link type""" # first link will have one vote, last link will have two create_Link_orderBy_test_data() # creates more than one link user = create_test_user() first_link_id = None for link in LinkModel.objects.all(): vote = VoteModel.objects.create(link_id=link.pk, user_id=user.pk) # save these for below first_link_id = first_link_id or link.pk last_link_id = link.pk user2 = create_test_user(name='Another User', password='******', email='*****@*****.**') VoteModel.objects.create(link_id=last_link_id, user_id=user2.pk) # check vote counts first_link_gid = Node.to_global_id('Link', first_link_id) last_link_gid = Node.to_global_id('Link', last_link_id) query = ''' query VotesOnLinkTest($linkId: ID!) { node(id: $linkId) { ... on Link { votes { count } } } } ''' variables = { 'linkId': first_link_gid, } expected = { 'node': { 'votes': { 'count': 1, } } } schema = graphene.Schema(query=Query) result = schema.execute(query, variable_values=variables) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data)) variables['linkId'] = last_link_gid expected['node']['votes']['count'] = 2 result = schema.execute(query, variable_values=variables) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def get_post_url(self, post): if current_phase_use_v1_interface(self.discussion.timeline_events): return self.get_discussion_url() + self.get_relative_post_url(post) else: route = None phase = post.get_created_phase() # The created post must be created within an associated phase assert phase if post.__class__.__name__ == 'SynthesisPost': synthesis_id = Node.to_global_id('Post', post.id) route = self.get_frontend_url('synthesis', slug=self.discussion.slug, synthesisId=synthesis_id) return urljoin(self.discussion.get_base_url(), route) first_idea = None ideas = [ link.idea for link in post.indirect_idea_content_links_without_cache() if link.__class__.__name__ == 'IdeaRelatedPostLink' ] if ideas: first_idea = ideas[0] else: # orphan post, redirect to home return self.get_discussion_url() if first_idea is not None and first_idea.__class__.__name__ ==\ 'Question': thematic = post.get_closest_thematic() route = self.get_frontend_url( 'post', **{ 'phase': phase.identifier, 'themeId': thematic.graphene_id(), 'element': '' }) if not route: route = self.get_frontend_url( 'post', **{ 'phase': phase.identifier, 'themeId': Node.to_global_id('Idea', first_idea.id), 'element': Node.to_global_id('Post', post.id) }) return urljoin(self.discussion.get_base_url(), route)
def setUp(self): create_Link_orderBy_test_data() self.link_gid = Node.to_global_id('Link', LinkModel.objects.latest('created_at').pk) self.user = create_test_user() self.user_gid = Node.to_global_id('User', self.user.pk) self.query = ''' mutation CreateVoteMutation($input: CreateVoteInput!) { createVote(input: $input) { vote { link { id votes { count } } } clientMutationId } } ''' self.schema = graphene.Schema(query=Query, mutation=Mutation)
def test_url_to_synthesis_post_with_timeline(discussion, synthesis_post_1, timeline_phase2_interface_v2): from assembl.lib.frontend_urls import (FrontendUrls, get_current_phase_identifier, current_phase_use_v1_interface) from graphene.relay import Node frontend_urls = FrontendUrls(discussion) assert get_current_phase_identifier(discussion.timeline_events) ==\ u'thread' assert current_phase_use_v1_interface(discussion.timeline_events) is False post_id = Node.to_global_id('Post', synthesis_post_1.id) assert '/syntheses/{id}'.format(id=post_id)\ in frontend_urls.get_post_url(synthesis_post_1)
def test_create_vote_bad_link(self): """ensure invalid linkId causes failuer""" last_link_pk = LinkModel.objects.order_by('id').last().pk invalid_link_gid = Node.to_global_id('Link', last_link_pk + 1) result = self.schema.execute( self.query, variable_values=self.variables(invalid_link_gid, self.user_gid), context_value=self.context_with_token() ) self.assertIsNotNone(result.errors, msg='createVote should have failed: invalid linkId') self.assertIn('link not found', repr(result.errors)) expected = { 'createVote': None } self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def test_create_vote_user_mismatch(self): """ensure logged user must match supplied userId""" user2 = create_test_user(name='Another User', password='******', email='*****@*****.**') user2_gid = Node.to_global_id('User', user2.pk) result = self.schema.execute( self.query, variable_values=self.variables(self.link_gid, user2_gid), context_value=self.context_with_token() ) self.assertIsNotNone(result.errors, msg='createVote should have failed: userId and logged user mismatch') self.assertIn('user id does not match logged-in user', repr(result.errors)) expected = { 'createVote': None } self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def setUp(self): self.user = create_test_user() self.user_gid = Node.to_global_id('User', self.user.pk) self.query = ''' mutation CreateLinkMutation($input: CreateLinkInput!) { createLink(input: $input) { link { url description postedBy { id } } clientMutationId } } ''' self.schema = graphene.Schema(query=Query, mutation=Mutation)
def test_node_for_link(self): link = LinkModel.objects.create(description='Test', url='http://a.com') link_gid = Node.to_global_id('Link', link.pk) query = ''' query { node(id: "%s") { id ...on Link { url } } } ''' % link_gid expected = { 'node': { 'id': link_gid, 'url': 'http://a.com', } } schema = graphene.Schema(query=Query) result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def resolve_token_category_id(self, args, context, info): return Node.to_global_id('TokenCategorySpecification', self.token_category_id)
def resolve_vote_session_id(self, args, context, info): return Node.to_global_id('VoteSession', self.widget_id)
def resolve_parent_id(self, args, context, info): if self.parent_id is None: return None return Node.to_global_id('Post', self.parent_id)
def resolve_voter_id(self, args, context, info): return Node.to_global_id('AgentProfile', self.voter_id)
def resolve_ancestors(self, args, context, info): return [ Node.to_global_id('Idea', id) for id in self.get_all_ancestors(id_only=True) ]
def resolve_parent_id(self, args, context, info): parents = self.get_parents() if not parents: return None return Node.to_global_id('Idea', parents[0].id)
def resolve_vote_spec_id(self, args, context, info): return Node.to_global_id(self.vote_spec.__class__.__name__, self.vote_spec_id)
def resolve_proposal_id(self, args, context, info): return Node.to_global_id('Idea', self.idea_id)
def graphene_id_for(cls, id): # who made the Post global Ids not follow the base class? from graphene.relay import Node return Node.to_global_id('Post', id)
def resolve_parent_id(self, args, context, info): if not self.source_links: return None return Node.to_global_id('Idea', self.source_links[0].source_id)