def test_get_average_donation_amount(self): donation = Donation(amount=500).save() donation.mission.connect(self.mission) donation2 = Donation(amount=2500).save() donation2.mission.connect(self.mission) res = self.mission.get_average_donation_amount() self.assertEqual(res, "15.00")
def test_get_total_donation_amount(self): donation1 = Donation(amount=100).save() donation2 = Donation(amount=200).save() donation1.mission.connect(self.mission) donation2.mission.connect(self.mission) res = self.mission.get_total_donation_amount() self.assertEqual(res, "2.25")
def setUp(self): cache.clear() self.unit_under_test_name = 'goal' self.email = "*****@*****.**" self.pleb = create_user_util_test(self.email) self.user = User.objects.get(email=self.email) self.email2 = "*****@*****.**" self.pleb2 = create_user_util_test(self.email2) self.user2 = User.objects.get(email=self.email2) self.url = "http://testserver" self.donation = Donation(completed=False, amount=1000, owner_username=self.user.username).save()
def get_donations(cls, owner_username): from sb_donations.neo_models import Donation query = 'MATCH (c:Quest {owner_username:"******"})-[:EMBARKS_ON]->' \ '(mission:Mission)<-' \ '[:CONTRIBUTED_TO]-(d:Donation) RETURN d' % owner_username res, _ = db.cypher_query(query) return [Donation.inflate(donation[0]) for donation in res]
def test_get_donations(self): donation = Donation(amount=100).save() mission = Mission().save() self.quest.missions.connect(mission) donation.mission.connect(mission) donation.quest.connect(self.quest) res = Quest.get_donations(self.quest.owner_username) self.assertEqual(donation.object_uuid, res[0].object_uuid)
def test_get_donors(self): donation = Donation(amount=500, completed=True, owner_username=self.owner.username).save() donation.owned_by.connect(self.owner) donation.mission.connect(self.mission) cache.clear() res = Mission.get_donors(self.mission.object_uuid) self.assertEqual(res[0], self.owner.username)
def setUp(self): cache.clear() self.unit_under_test_name = 'donations' self.email = "*****@*****.**" create_user_util_test(self.email) self.pleb = Pleb.nodes.get(email=self.email) self.user = User.objects.get(email=self.email) self.url = "http://testserver" self.donation = Donation(completed=False, amount=1000, owner_username=self.user.username).save() self.pleb.is_verified = True self.pleb.save() cache.set(self.pleb.username, self.pleb)
def donations(self, request): query = 'MATCH (a:Pleb {username:"******"})-[:DONATIONS_GIVEN]->' \ '(d:Donation)-[:CONTRIBUTED_TO]->(:Mission) ' \ 'RETURN d ORDER BY d.created DESC' % \ request.user.username res, _ = db.cypher_query(query) queryset = [Donation.inflate(row[0]) for row in res] html = self.request.query_params.get('html', 'false') page = self.paginate_queryset(queryset) serializer = self.get_serializer(page, many=True, context={'request': request}) if html == 'true': html_array = [] for item in serializer.data: context = RequestContext(request, item) html_array.append( render_to_string("settings/donation_block.html", context)) return self.get_paginated_response(html_array) return self.get_paginated_response(serializer.data)
def get_donations(cls, object_uuid): from sb_donations.neo_models import Donation query = 'MATCH (c:Mission {object_uuid:"%s"})<-' \ '[:CONTRIBUTED_TO]-(d:Donation) RETURN d' % object_uuid res, _ = db.cypher_query(query) return [Donation.inflate(donation[0]) for donation in res]
def test_get_sagebrew_donations(self): donation = Donation(amount=100).save() self.pleb.donations.connect(donation) donation.owned_by.connect(self.pleb) self.assertTrue(self.pleb.get_sagebrew_donations())
class DonationEndpointTests(APITestCase): def setUp(self): cache.clear() self.unit_under_test_name = 'goal' self.email = "*****@*****.**" self.pleb = create_user_util_test(self.email) self.user = User.objects.get(email=self.email) self.email2 = "*****@*****.**" self.pleb2 = create_user_util_test(self.email2) self.user2 = User.objects.get(email=self.email2) self.url = "http://testserver" self.donation = Donation(completed=False, amount=1000, owner_username=self.user.username).save() def test_unauthorized(self): url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.post(url, {}, format='json') self.assertIn( response.status_code, [status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN]) def test_save_int_data(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.post(url, 98897965, format='json') self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) def test_save_string_data(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.post(url, 'asfonosdnf', format='json') self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) def test_save_list_data(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.post(url, [], format='json') self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) def test_save_float_data(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.post(url, 1.010101010, format='json') self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) def test_create_on_detail(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.post(url, {}, format='json') response_data = { 'status_code': status.HTTP_405_METHOD_NOT_ALLOWED, 'detail': 'Method "POST" not allowed.' } self.assertEqual(response.data, response_data) self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) def test_list(self): self.client.force_authenticate(user=self.user) url = reverse('donation-list') response = self.client.get(url) self.assertEqual( response.data['detail'], "Sorry, we currently do not allow for users to " "query all donations for every quest.") self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) def test_get(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) def test_get_id(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.get(url) self.assertEqual(response.data['id'], self.donation.object_uuid) def test_get_type(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.get(url) self.assertEqual(response.data['type'], 'donation') def test_get_completed(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.get(url) self.assertEqual(response.data['completed'], self.donation.completed) def test_get_amount(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.get(url) self.assertEqual(response.data['amount'], self.donation.amount) def test_get_owner_username(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.get(url) self.assertEqual(response.data['owner_username'], self.donation.owner_username) def test_get_quest(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.get(url) self.assertIsNone(response.data['quest']) def test_get_mission(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.get(url) self.assertIsNone(response.data['mission']) def test_put(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.put(url, data={}, format='json') self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) def test_patch(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.patch(url, data={}, format='json') self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) def test_post(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.post(url, data={}, format='json') self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) def test_delete(self): self.client.force_authenticate(user=self.user) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.delete(url, data={}, format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) def test_delete_not_owner(self): self.client.force_authenticate(user=self.user2) self.donation.owned_by.connect(self.pleb2) url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.delete(url, data={}, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_delete_completed(self): self.client.force_authenticate(user=self.user) self.donation.completed = True self.donation.save() url = reverse('donation-detail', kwargs={'object_uuid': self.donation.object_uuid}) response = self.client.delete(url, data={}, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_get_donations(self): donation = Donation(amount=500).save() donation.mission.connect(self.mission) res = Mission.get_donations(self.mission.object_uuid) self.assertEqual(res[0].object_uuid, donation.object_uuid)