def reverse(name, *args, **kwargs): """Shorter Reverse function for the very lazy tester""" full_url = kwargs.pop("full", False) uri = rf_reverse(name, args=args, kwargs=kwargs) if "request" not in kwargs and full_url: return f"http://testserver{uri}" return uri
def test_create(self): url = rf_reverse('v1:math:fractions-list') data = { "name": "Fill-in-the-Blank Multiplication Fraction", "qtype": 0, "stem": {"statement": "{{numerator1}}/{{denominator1}} * {{numerator2}}/{{denominator2}}"}, "keys": { "numerator1": [8, 4], "denominator1": [5, 5], "numerator2": [9, 2], "denominator2": [6, 10], "answer": ["72/30", "4/50"] }, "editors": [ 1 ] } self.client.force_login(user=self.u1) response = self.client.post(url, data=data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) response = self.client.get(url, format='json') print(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK)
def to_response(self, request): """method will return dict which can be passed to response """ author = "{} {}".format(self.author.first_name, self.author.last_name) data = { "id": self.id, "author": author, "title": self.title, "description": self.description, "target_audience": dict(ProposalTargetAudience.CHOICES)[ self.target_audience ], "status": dict(ProposalStatus.CHOICES)[self.status], "review_status": dict(ProposalReviewStatus.CHOICES)[self.review_status], "proposal_type": self.proposal_type.name, "proposal_section": self.proposal_section.name, "votes_count": self.get_votes_count(), "speaker_info": self.speaker_info, "speaker_links": self.speaker_links, "content_urls": self.content_urls, "conference": rf_reverse( "conference-detail", kwargs={"pk": self.conference_id}, request=request ), } return data
def test_can_mark_as_seen(self): notifs = [ Notification.objects.create( id=i, content=str(i), link="https://lafranceinsoumise.fr", person=self.person, ) for i in range(3) ] client = APIClient() client.force_authenticate(self.person.role) request = Mock() request.user = self.person.role res = client.post(rf_reverse("notifications_seen"), data={"notifications": [0, 2]}) self.assertEqual(res.status_code, 200) for n in notifs: n.refresh_from_db() self.assertEqual( [n.status for n in notifs], [ Notification.STATUS_SEEN, Notification.STATUS_UNSEEN, Notification.STATUS_SEEN, ], )
def test_add_power(self): url = rf_reverse('v1:editor:power-sensei-list') self.client.force_login(self.u1) response = self.client.post(url, data=dict(), secure=True, format='json') print(response.status_text) self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_create_no_user_pk(self): url = rf_reverse('v1:editor:editors-list') self.client.login(username='******', password='******') response = self.client.post(url, data=dict(pen_name='Big L', ), secure=True, format='json') print(response.status_text) self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def reverse(*args, **kwargs): """Allows use of the hyperlinked fields, without passing a request. This is very useful for situations where you don't have a request to pass, for instance, when using the shell or during testing. """ request = kwargs.pop('request') if request: return rf_reverse(*args, request=request, **kwargs) else: return dj_reverse(*args, **kwargs)
def reverse(*args, **kwargs): """Allows use of the hyperlinked fields, without passing a request. This is very useful for situations where you don't have a request to pass, for instance, when using the shell or during testing. """ request = kwargs.pop('request') if request: return rf_reverse(*args, request=request, **kwargs) else: return dj_reverse(*args, **kwargs)
def to_response(self, request): """method will return dict which can be passed to response """ author = u"{} {}".format(self.author.first_name, self.author.last_name) data = {'id': self.id, 'author': author, 'title': self.title, 'description': self.description, 'target_audience': dict(ProposalTargetAudience.CHOICES)[self.target_audience], 'status': dict(ProposalStatus.CHOICES)[self.status], 'review_status': dict(ProposalReviewStatus.CHOICES)[self.review_status], 'proposal_type': self.proposal_type.name, 'proposal_section': self.proposal_section.name, 'votes_count': self.get_votes_count(), 'speaker_info': self.speaker_info, 'speaker_links': self.speaker_links, 'content_urls': self.content_urls, 'conference': rf_reverse("conference-detail", kwargs={'pk': self.conference_id}, request=request)} return data
def to_response(self, request): """method will return dict which can be passed to response """ author = u"{} {}".format(self.author.first_name, self.author.last_name) data = { 'id': self.id, 'author': author, 'title': self.title, 'description': self.description, 'target_audience': dict(ProposalTargetAudience.CHOICES)[self.target_audience], 'status': dict(ProposalStatus.CHOICES)[self.status], 'review_status': dict(ProposalReviewStatus.CHOICES)[self.review_status], 'proposal_type': self.proposal_type.name, 'proposal_section': self.proposal_section.name, 'votes_count': self.get_votes_count(), 'speaker_info': self.speaker_info, 'speaker_links': self.speaker_links, 'content_urls': self.content_urls, 'conference': rf_reverse("conference-detail", kwargs={'pk': self.conference_id}, request=request) } return data
def test_list(self): url = rf_reverse('v1:classroom:exam-problems-list') response = self.client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data[0]['problems'], [1, 2, 3])
def test_list(self): url = rf_reverse('v1:math:fractions-list') response = self.client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_list(self): url = rf_reverse('v1:editor:editors-list') self.client.login(username='******', password='******') response = self.client.get(url, format='json') print(response.status_text) self.assertEqual(response.status_code, status.HTTP_200_OK)
def api_root(request, format=None): return Response({ 'hosts': rf_reverse('host_list', request=request, format=format), })