def test_post_twibbon_with_2x1_image(self): campaign = CampaignFactory() response = self.client.post( reverse('twibbon-list-create', kwargs={'campaign_url': campaign.campaign_url}), {'img': open(get_sample_image_file_path('2x1.png'), 'rb')}, secure=True) self.assertEqual(response.status_code, 400) self.assertEqual(response.data['img'], ['image ratio must be 1:1'])
class TwibbonFactory(DjangoModelFactory): class Meta: model = Twibbon user = SubFactory(UserFactory) campaign = SubFactory(CampaignFactory) img = SimpleUploadedFile(name='jajaja.png', content=open( get_sample_image_file_path('1x1.png'), 'rb').read())
def test_post_campaign_without_user(self): response = self.client.post( reverse('campaign-list-create'), { 'name': 'Nama', 'campaign_url': 'url', 'twibbon_img': open(get_sample_image_file_path('1x1.png'), 'rb') }, secure=True) self.assertEqual(response.status_code, 401)
def test_post_twibbon_without_user(self): user = UserFactory() campaign = CampaignFactory(user=user) response = self.client.post( reverse('twibbon-list-create', kwargs={'campaign_url': campaign.campaign_url}), {'img': open(get_sample_image_file_path('1x1.png'), 'rb')}, secure=True) self.assertEqual(response.status_code, 201) self.assertEqual(Twibbon.objects.all().count(), 1)
class CampaignFactory(DjangoModelFactory): class Meta: model = Campaign name = "name" campaign_url = "asdfurl" twibbon_img = SimpleUploadedFile(name='jajaja.png', content=open( get_sample_image_file_path('1x1.png'), 'rb').read()) user = SubFactory(UserFactory)
def test_post_campaign_owner_is_valid(self): campaign = CampaignFactory() user = UserFactory(email="*****@*****.**") self.client.force_authenticate(user=user) response = self.client.post( reverse('twibbon-list-create', kwargs={'campaign_url': campaign.campaign_url}), {'img': open(get_sample_image_file_path('1x1.png'), 'rb')}, secure=True) twibbon = Twibbon.objects.first() self.assertEqual(response.status_code, 201) self.assertEqual(twibbon.owner_id, user.id)
def test_post_twibbon_success(self): user = UserFactory(email="[email protected]") campaign1 = CampaignFactory(user=user) self.client.force_authenticate(user=user) response = self.client.post( reverse('twibbon-list-create', kwargs={'campaign_url': campaign1.campaign_url}), {'img': open(get_sample_image_file_path('1x1.png'), 'rb')}, secure=True) self.assertEqual(response.status_code, 201) self.assertEqual(Twibbon.objects.all().count(), 1) self.assertEqual(campaign1.twibbons.all().count(), 1)
def test_put_campaign_user_unauthorized(self): campaign = CampaignFactory(name="campaign1") response = self.client.put( reverse('campaign-retrieve-update-destroy', kwargs={'campaign_url': campaign.campaign_url}), { 'name': 'Nama', 'campaign_url': 'url', 'twibbon_img': open( get_sample_image_file_path('1x1.png'), 'rb') }, secure=True) self.assertEqual(response.status_code, 401)
def test_post_campaign_with_invalid_campaign_url(self): user = UserFactory() self.client.force_authenticate(user=user) response = self.client.post( reverse('campaign-list-create'), { 'name': 'Nama', 'campaign_url': 'url!', 'twibbon_img': open(get_sample_image_file_path('1x1.png'), 'rb') }, secure=True) self.assertEqual(response.status_code, 400) self.assertEqual(response.data['campaign_url'], ["url must be alphanumeric"])
def test_post_campaign_with_2x1_image(self): user = UserFactory() self.client.force_authenticate(user=user) response = self.client.post( reverse('campaign-list-create'), { 'name': 'Nama', 'campaign_url': 'url', 'twibbon_img': open(get_sample_image_file_path('2x1.png'), 'rb') }, secure=True) self.assertEqual(response.status_code, 400) self.assertEqual(response.data['twibbon_img'], ['image ratio must be 1:1'])
def test_post_campaign_owner_is_valid(self): user = UserFactory() self.client.force_authenticate(user=user) response = self.client.post( reverse('campaign-list-create'), { 'name': 'Nama', 'campaign_url': 'url', 'twibbon_img': open(get_sample_image_file_path('1x1.png'), 'rb') }, secure=True) campaign = Campaign.objects.first() self.assertEqual(campaign.owner_id, user.id)
def test_post_campaign_success(self): user = UserFactory() self.client.force_authenticate(user=user) response = self.client.post( reverse('campaign-list-create'), { 'name': 'Nama', 'campaign_url': 'url', 'twibbon_img': open(get_sample_image_file_path('1x1.png'), 'rb') }, secure=True) self.assertEqual(response.status_code, 201) self.assertEqual(Campaign.objects.all().count(), 1) self.assertEqual(Campaign.objects.first().name, 'Nama')
def test_put_campaign_user_forbidden(self): user1 = UserFactory(email="*****@*****.**") user2 = UserFactory(email="*****@*****.**") campaign = CampaignFactory(name="campaign1", user=user1) self.client.force_authenticate(user=user2) response = self.client.put( reverse('campaign-retrieve-update-destroy', kwargs={'campaign_url': campaign.campaign_url}), { 'name': 'Nama', 'campaign_url': 'url', 'twibbon_img': open( get_sample_image_file_path('1x1.png'), 'rb') }, secure=True) self.assertEqual(response.status_code, 403)
def test_put_campaign_success(self): campaign = CampaignFactory(name="campaign1") self.client.force_authenticate(user=campaign.user) response = self.client.put( reverse('campaign-retrieve-update-destroy', kwargs={'campaign_url': campaign.campaign_url}), { 'name': 'Nama', 'campaign_url': 'url', 'twibbon_img': open( get_sample_image_file_path('1x1.png'), 'rb') }, secure=True) new_campaign = Campaign.objects.first() self.assertEqual(response.status_code, 200) self.assertEqual(new_campaign.name, 'Nama')
def test_put_twibbon_user_unauthorized(self): user = UserFactory(email="[email protected]") campaign = CampaignFactory(name="campaign1", user=user) twibbon = TwibbonFactory(campaign=campaign, caption="heho", user=user) response = self.client.put( reverse('twibbon-retrieve-update-destroy', kwargs={ 'campaign_url': campaign.campaign_url, 'twibbon_id': twibbon.id }), { 'caption': 'Hehe', 'img': open(get_sample_image_file_path('1x1.png'), 'rb') }, secure=True) new_twibbon = Twibbon.objects.first() self.assertEqual(response.status_code, 401) self.assertEqual(new_twibbon.caption, "heho")