class BalanceHistoryViewTest(test.APITransactionTestCase): def setUp(self): self.user = UserFactory() self.other = UserFactory() self.customer = CustomerFactory() self.customer.add_user(self.user, CustomerRole.OWNER) def test_other_user_can_not_see_balance_history(self): self.client.force_authenticate(user=self.other) response = self.client.get(self.get_url()) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_owner_can_see_balance_history(self): amounts = [10, 15, 20, 25] for amount in amounts: BalanceHistory.objects.create(customer=self.customer, amount=amount, created=timezone.now() - timedelta(days=1)) self.client.force_authenticate(user=self.user) response = self.client.get(self.get_url()) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assert_equal_decimals(amounts, [item['amount'] for item in response.data]) def test_balance_history_displayed_for_last_month(self): BalanceHistory.objects.create(customer=self.customer, amount=10, created=timezone.now() - timedelta(days=1)) BalanceHistory.objects.create(customer=self.customer, amount=20, created=timezone.now() - timedelta(days=60)) self.client.force_authenticate(user=self.user) response = self.client.get(self.get_url()) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assert_equal_decimals([10], [item['amount'] for item in response.data]) def get_url(self): return CustomerFactory.get_url(self.customer, 'balance_history') def assert_equal_decimals(self, xs, ys): self.assertEqual(map(Decimal, xs), map(Decimal, ys))
class ImageUploadTest(test.APITransactionTestCase): def setUp(self): self.staff = UserFactory(is_staff=True) self.owner = UserFactory() self.user = UserFactory() self.customer = CustomerFactory() self.customer.add_user(self.owner, CustomerRole.OWNER) self.url = reverse('customer_image', kwargs={'uuid': self.customer.uuid.hex}) def test_default_customer_logo(self): self.client.force_authenticate(user=self.staff) self.assert_default_logo() with dummy_image() as image: self.assert_can_upload_image(image) self.assert_can_delete_image() self.assert_default_logo() # Positive def test_staff_can_upload_and_delete_customer_logo(self): self.client.force_authenticate(user=self.staff) with dummy_image() as image: self.assert_can_upload_image(image) self.assert_can_delete_image() def test_customer_owner_can_upload_and_delete_customer_logo(self): self.client.force_authenticate(user=self.owner) with dummy_image() as image: self.assert_can_upload_image(image) self.assert_can_delete_image() # Negative def test_user_cannot_upload_logo_for_customer_he_is_not_owner_of(self): self.client.force_authenticate(user=self.user) with dummy_image() as image: response = self.upload_image(self.url, image) self.assertEqual(status.HTTP_403_FORBIDDEN, response.status_code) # Helpers def assert_can_upload_image(self, image): response = self.upload_image(self.url, image) self.assertEqual(status.HTTP_200_OK, response.status_code, response.data) self.assertIn('image', response.data) self.assertIsNotNone(response.data['image']) def assert_can_delete_image(self): response = self.client.delete(self.url) self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code, response.data) @override_nodeconductor_settings(DEFAULT_CUSTOMER_LOGO='default_logo.jpg') def assert_default_logo(self): url = reverse('customer-detail', kwargs={'uuid': self.customer.uuid.hex}) response = self.client.get(url) self.assertEqual(status.HTTP_200_OK, response.status_code) self.assertEqual('default_logo.jpg', response.data['image']) def upload_image(self, url, image): return self.client.put(self.url, {'image': image}, format='multipart')