Exemplo n.º 1
0
 def test_list_active_campaigns(self):
     active_campaigns = CampaignFactory.create_batch(is_draft=False,
                                                     size=10)
     for campaign in active_campaigns:
         campaign.state = APPROVED
         campaign.save()
     inactive_campaigns = CampaignFactory.create_batch(is_draft=False,
                                                       size=5)
     for campaign in inactive_campaigns:
         campaign.state = APPROVED
         campaign.save()
         campaign.started = datetime.date.today() - datetime.timedelta(
             days=10)
         campaign.finished = datetime.date.today() - datetime.timedelta(
             days=1)
         campaign.save()
     response = self.client.get("/api/v1/campaigns/?active=True",
                                format="json")
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     data = response.json()
     self.assertEqual(len(active_campaigns), data["count"])
     response = self.client.get("/api/v1/campaigns/?active=False",
                                format="json")
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     data = response.json()
     self.assertEqual(len(inactive_campaigns), data["count"])
Exemplo n.º 2
0
 def test_followed_is_draft(self):
     user = UserFactory()
     CampaignFactory.create_batch(is_draft=True, size=10)
     campaign = CampaignFactory(is_draft=False)
     user.follow(campaign)
     self.client.force_authenticate(user)
     response = self.client.get("/api/v1/campaigns/?is_draft=False",
                                format="json")
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     data = response.json()
     self.assertEqual(1, data["count"])
Exemplo n.º 3
0
 def test_followed_by_filter(self):
     user = UserFactory()
     CampaignFactory.create_batch(size=10)
     campaign = CampaignFactory()
     user.follow(campaign)
     self.client.force_authenticate(user)
     response = self.client.get("/api/v1/campaigns/?followed_by={}".format(
         user.pk),
                                format="json")
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     data = response.json()
     self.assertEqual(1, data["count"])
Exemplo n.º 4
0
 def test_purchase_token(self):
     campaign = CampaignFactory(is_draft=False, funds=1000)
     self.client.force_authenticate(self.user)
     data = {"token": campaign.token.pk, "amount": 10}
     response = self.client.post("/api/v1/purchases/",
                                 data=data,
                                 format="json")
     self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Exemplo n.º 5
0
 def test_not_allow_edit_campaign(self):
     campaign = CampaignFactory(user=self.user)
     self.client.force_authenticate(UserFactory())
     data = {"title": "new title"}
     response = self.client.patch("/api/v1/campaigns/{}/".format(
         campaign.pk),
                                  format="json",
                                  data=data)
     self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
Exemplo n.º 6
0
    def test_create_alert(self):
        campaign = CampaignFactory()

        self.client.force_authenticate(self.user)
        data = {"rule": UP, "amount": 100, "campaign": campaign.pk}
        response = self.client.post("/api/v1/alerts/",
                                    format="json",
                                    data=data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Exemplo n.º 7
0
 def test_add_campaign_revenues(self):
     campaign = CampaignFactory(user=self.user)
     data = {"campaign": campaign.pk, "year": 2014, "amount": 100.0}
     self.client.force_authenticate(self.user)
     response = self.client.post("/api/v1/revenues/",
                                 format="json",
                                 data=data)
     self.assertEqual(response.status_code, status.HTTP_201_CREATED)
     campaign = Campaign.objects.get(pk=campaign.pk)
     self.assertEqual(1, campaign.revenues.count())
Exemplo n.º 8
0
 def test_follow_campaign(self):
     campaign = CampaignFactory()
     user = UserFactory()
     self.client.force_authenticate(user)
     response = self.client.post("/api/v1/campaigns/{}/follow/".format(
         campaign.pk),
                                 format="json")
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     user = User.objects.get(pk=user.pk)
     self.assertEqual(1, user.following.all().count())
Exemplo n.º 9
0
 def test_edit_campaign_tags(self):
     campaign = CampaignFactory(user=self.user)
     self.client.force_authenticate(self.user)
     data = {"tags": ["tag 1", "tag 2", "tag 3"]}
     response = self.client.patch("/api/v1/campaigns/{}/".format(
         campaign.pk),
                                  format="json",
                                  data=data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     edited_campaign = Campaign.objects.get(pk=campaign.pk)
     self.assertEqual(len(data["tags"]), edited_campaign.tags.count())
Exemplo n.º 10
0
 def test_list_tokens_by_campaign(self):
     campaigns = CampaignFactory.create_batch(is_draft=False,
                                              funds=1000,
                                              size=5)
     self.client.force_authenticate(self.user)
     response = self.client.get("/api/v1/tokens/?campaign_id={}".format(
         campaigns[0].id),
                                format="json")
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     data = response.json()
     self.assertEqual(data["count"], 1)
Exemplo n.º 11
0
 def test_list_search_campaigns(self):
     other_campaigns = CampaignFactory.create_batch(
         title="other title", description="other description", size=10)
     foo_campaigns = CampaignFactory.create_batch(
         title="foo title", description="foo description", size=5)
     CampaignFactory(title="one title", description="one description")
     response = self.client.get("/api/v1/campaigns/?search=other",
                                format="json")
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     data = response.json()
     self.assertEqual(len(other_campaigns), data["count"])
     response = self.client.get("/api/v1/campaigns/?search=foo",
                                format="json")
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     data = response.json()
     self.assertEqual(len(foo_campaigns), data["count"])
     response = self.client.get("/api/v1/campaigns/?search=one",
                                format="json")
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     data = response.json()
     self.assertEqual(1, data["count"])
Exemplo n.º 12
0
 def test_purchase_amount_limit(self):
     campaign = CampaignFactory(is_draft=False, funds=100000)
     token = campaign.token
     purchase = Purchase(token=token,
                         amount=token.amount + 1,
                         user=UserFactory())
     try:
         purchase.save()
         raised_exception = False
     except IntegrityError:
         raised_exception = True
     self.assertTrue(raised_exception)
Exemplo n.º 13
0
 def test_add_campaign_links(self):
     campaign = CampaignFactory(user=self.user)
     data = {
         "campaign": campaign.pk,
         "name": "foo",
         "url": "http://example.com"
     }
     self.client.force_authenticate(self.user)
     response = self.client.post("/api/v1/links/", format="json", data=data)
     self.assertEqual(response.status_code, status.HTTP_201_CREATED)
     campaign = Campaign.objects.get(pk=campaign.pk)
     self.assertEqual(1, campaign.links.count())
Exemplo n.º 14
0
 def test_edit_campaign(self):
     campaign = CampaignFactory(user=self.user)
     self.client.force_authenticate(self.user)
     data = {"title": "new title"}
     response = self.client.patch("/api/v1/campaigns/{}/".format(
         campaign.pk),
                                  format="json",
                                  data=data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     edited_campaign = Campaign.objects.get(pk=campaign.pk)
     self.assertNotEqual(campaign.title, edited_campaign.title)
     self.assertEqual(data["title"], edited_campaign.title)
Exemplo n.º 15
0
 def test_list_countries(self):
     CampaignFactory.create_batch(is_draft=False, country="ES", size=11)
     CampaignFactory.create_batch(is_draft=False, country="US", size=3)
     CampaignFactory.create_batch(is_draft=False, country="FR", size=7)
     response = self.client.get("/api/v1/countries/", format="json")
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     data = response.json()
     self.assertEqual(3, len(data))
Exemplo n.º 16
0
 def test_add_campaign_pictures(self):
     campaign = CampaignFactory(user=self.user)
     self.client.force_authenticate(self.user)
     picture = Image.new("RGB", (100, 100))
     picture_tmp_file = NamedTemporaryFile(suffix=".jpg")
     picture.save(picture_tmp_file)
     with open(picture_tmp_file.name, "rb") as picture:
         data = {"campaign": campaign.pk, "image": picture}
         response = self.client.post("/api/v1/pictures/",
                                     format="multipart",
                                     data=data)
     self.assertEqual(response.status_code, status.HTTP_201_CREATED)
     campaign = Campaign.objects.get(pk=campaign.pk)
     self.assertEqual(1, campaign.pictures.count())
Exemplo n.º 17
0
 def test_purchase(self):
     campaign = CampaignFactory(is_draft=False, funds=100000)
     token = campaign.token
     purchase = Purchase(token=token,
                         amount=token.amount / 2,
                         user=UserFactory())
     try:
         purchase.save()
         raised_exception = False
     except IntegrityError:
         raised_exception = True
     self.assertFalse(raised_exception)
     self.assertAlmostEqual(purchase.total,
                            token.unit_price * purchase.amount)
Exemplo n.º 18
0
    def test_list_pictures(self):
        campaigns = CampaignFactory.create_batch(2)
        PictureFactory.create_batch(2, campaign=campaigns[0])
        PictureFactory.create_batch(2, campaign=campaigns[1])

        response = self.client.get("/api/v1/pictures/", format="json")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        self.assertEqual(4, data['count'])

        response = self.client.get(
            f"/api/v1/pictures/?campaign={campaigns[0].id}", format="json")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        self.assertEqual(2, data['count'])
Exemplo n.º 19
0
 def test_list_purchases(self):
     campaigns = CampaignFactory.create_batch(is_draft=False,
                                              funds=1000,
                                              size=5)
     purchases = PurchaseFactory.create_batch(token=campaigns[0].token,
                                              user=self.user,
                                              amount=10,
                                              size=5)
     PurchaseFactory.create_batch(token=campaigns[0].token,
                                  amount=10,
                                  size=6)
     self.client.force_authenticate(self.user)
     response = self.client.get("/api/v1/purchases/", format="json")
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     data = response.json()
     self.assertEqual(data["count"], len(purchases))