Exemplo n.º 1
0
 def test_create(self):
     data = {
         'avatar': gen_image_field(),
         'country': self.country_random.id,
         'first_name': gen_string(max_length=10),
         'last_name': gen_string(max_length=10),
         'password': gen_string(max_length=10),
         'paypal_email': gen_email(),
         'username': gen_email()
     }
     response = self.client.post(self.proxies_list_url, data)
     self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Exemplo n.º 2
0
    def test_send_statement_email(self, mock_send_mail,
                                  mock_email_message_send,
                                  mock_email_multi_alternatives_send):

        self.assertFalse(send_statement_email(0))

        statement = mommy.make(ShareholderStatement, pdf_file='example.pdf')
        mommy.make('shareholder.Shareholder', user=statement.user)

        # no user email
        self.assertFalse(statement.user.email)
        self.assertFalse(send_statement_email(statement.pk))
        mock_send_mail.assert_called_once()
        mock_send_mail.reset_mock()

        statement.user.email = random_gen.gen_email()
        statement.user.save()

        # pdf file is no file
        self.assertFalse(os.path.isfile(statement.pdf_file))
        self.assertFalse(send_statement_email(statement.pk))
        mock_send_mail.assert_called_once()
        mock_send_mail.reset_mock()

        statement.pdf_file = os.path.join(
            os.path.dirname(__file__), 'files', 'example.pdf'
        )
        statement.save()

        with self.settings(MANDRILL_SHAREHOLDER_STATEMENT_TEMPLATE=None):
            self.assertIsNone(statement.email_sent_at)
            self.assertTrue(send_statement_email(statement.pk))
            statement.refresh_from_db()
            self.assertIsNotNone(statement.email_sent_at)
            mock_email_multi_alternatives_send.assert_called_once()

        statement.email_sent_at = None
        statement.save()

        with self.settings(MANDRILL_SHAREHOLDER_STATEMENT_TEMPLATE='foo',
                           EMAIL_BACKEND='djrill'):
            self.assertTrue(send_statement_email(statement.pk))
            statement.refresh_from_db()
            self.assertIsNotNone(statement.email_sent_at)
            mock_email_message_send.assert_called_once()
            self.assertFalse(statement.remote_email_id)

            with mock.patch('shareholder.tasks.EmailMessage') as mock_message:
                remote_id = random_gen.gen_uuid().hex
                mandrill_response_mock = mock.PropertyMock(
                    return_value=[dict(_id=remote_id)])
                type(mock_message.return_value).mandrill_response = (
                    mandrill_response_mock)

                self.assertTrue(send_statement_email(statement.pk))
                statement.refresh_from_db()
                self.assertIn(remote_id, statement.remote_email_id)
                self.assertIn('mandrill', statement.remote_email_id)
Exemplo n.º 3
0
    def test_call(self):

        with self.assertRaises(ValidationError):
            self.validator()

        self.validator.company.email = random_gen.gen_string(20)

        with self.assertRaises(ValidationError):
            self.validator()

        self.validator.company.email = random_gen.gen_email()

        self.assertIsNone(self.validator())
Exemplo n.º 4
0
    def test_post(self, mock_security_invoice_item,
                  mock_shareholder_invoice_item, mock_sync_card):
        operator = OperatorGenerator().generate()
        operator.company.get_customer()

        view_url = reverse('djstripe:confirm',
                           args=[operator.company_id, 'test'])

        req = self.factory.post(view_url)
        req.user = operator.user
        req.resolver_match = resolve(view_url)
        self.view.request = req
        self.view.kwargs = dict(plan='test', company_id=operator.company_id)

        self.view.form_invalid = mock.Mock(
            side_effect=ValidationError('ERROR'))
        with self.assertRaises(ValidationError):
            res = self.view.post(req)
            self.assertIn('ERROR', res.content)  # pragma: nocover

        post_data = dict(plan='test', email=random_gen.gen_email())
        req = self.factory.post(view_url, data=post_data)
        req.user = operator.user
        req.resolver_match = resolve(view_url)
        self.view.request = req

        with mock.patch('djstripe.models.Customer.subscribe'):
            res = self.view.post(req)
            self.assertEqual(res.status_code, 302)

        mock_shareholder_invoice_item.assert_called()
        mock_security_invoice_item.assert_called()

        company = operator.company.__class__.objects.get(
            pk=operator.company_id)
        self.assertEqual(company.email, post_data['email'])

        # test unsubscribable
        with mock.patch('shareholder.models.Company.validate_plan',
                        return_value=(False, [])):

            with self.assertRaises(ValidationError):
                self.view.post(req)

        with mock.patch('djstripe.models.Customer.subscribe',
                        side_effect=StripeError('STRIPEERROR')):
            with self.assertRaises(ValidationError):
                res = self.view.post(req)
                self.assertIn('STRIPEERROR', res.content)  # pragma: nocover
Exemplo n.º 5
0
 def test_partial_update(self):
     self.client.force_authenticate(self.proxy_random.user)
     paypal_email = gen_email()
     first_name = gen_string(max_length=20)
     last_name = gen_string(max_length=20)
     data = {
         'country': self.country_random.id,
         'paypal_email': paypal_email,
         'first_name': first_name,
         'last_name': last_name
     }
     response = self.client.patch(self.proxy_detail_url, data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(response.data['country'], self.country_random.id)
     self.assertEqual(response.data['paypal_email'], paypal_email)
     self.assertEqual(response.data['user']['first_name'], first_name)
     self.assertEqual(response.data['user']['last_name'], last_name)
Exemplo n.º 6
0
    def setUp(self):
        super(ShareholderSerializerTestCase, self).setUp()

        self.factory = RequestFactory()
        self.client = Client()
        self.session = self.client.session

        self.shareholder = ShareholderGenerator().generate()
        self.operator = OperatorGenerator().generate(
            company=self.shareholder.company)

        add_company_to_session(self.session, self.operator.company)
        self.request = self.factory.get('/services/rest/shareholders')
        self.request.user = self.operator.user
        self.request.session = self.session
        self.new_data = ShareholderSerializer(self.shareholder,
                                              context={
                                                  'request': self.request
                                              }).data
        self.new_data['user']['email'] = random_gen.gen_email()
        self.new_data.update(
            {'number': self.operator.company.get_new_shareholder_number()})
        self.new_data['user']['userprofile'].update(
            {'birthday': '2013-05-05T00:00'})
Exemplo n.º 7
0
def generate_duration():
    return random.randrange(1, 20)


def generate_duration_or_zero():
    if random.uniform(0, 10) > 8:
        return generate_duration()
    else:
        return 0


def generate_bool():
    return random.uniform(0, 1) > 0


def generate_color_hex():
    """
    See http://stackoverflow.com/a/14019260
    """
    def r():
        return random.randint(0, 255)

    return '#%02X%02X%02X' % (r(), r(), r())


UserRecipe = Recipe(get_user_model(), email=gen_email())
EmailAddressRecipe = Recipe(EmailAddress,
                            user=foreign_key(UserRecipe),
                            verified=True,
                            primary=True)
Exemplo n.º 8
0
 def test_is_valid_email(self):
     self.assertFalse(is_valid_email(random_gen.gen_string(10)))
     self.assertTrue(is_valid_email(random_gen.gen_email()))
Exemplo n.º 9
0
    def test_send_receipt(self, mock_attach, mock_send):

        self.assertFalse(self.charge.receipt_sent)
        self.assertIsNone(self.charge.amount)

        # don't send receipt if no amount
        self.charge.send_receipt()
        self.assertFalse(self.charge.receipt_sent)
        mock_send.assert_not_called()

        # don't send receipt if already sent
        self.charge.amount = abs(random_gen.gen_float())
        self.charge.save()
        self.charge.receipt_sent = True
        self.charge.save()
        self.assertTrue(self.charge.receipt_sent)
        self.assertIsNotNone(self.charge.amount)
        self.charge.send_receipt()
        mock_send.assert_not_called()

        self.charge.receipt_sent = False
        self.charge.save()

        # create subscriber (with no email)
        subscriber = mommy.make(settings.DJSTRIPE_SUBSCRIBER_MODEL)
        self.charge.customer.subscriber = subscriber
        self.charge.customer.save()
        self.assertFalse(bool(subscriber.email))

        with patch.object(self.charge, '_get_customer_data') as mock_data:
            mock_data.return_value = self.fake_responser.get_response()

            self.charge.send_receipt()
            mock_send.assert_called()
            self.assertTrue(bool(self.charge.customer.subscriber.email))
            # check postal error
            self.assertEqual(len(self._log_messages.get('error')), 1)
            self._test_logging_handler.reset()

            # reset email for next test
            self.charge.customer.subscriber.email = ''
            self.charge.customer.subscriber.save()
            self.charge.receipt_sent = False

            mock_send.reset_mock()

            with patch('utils.mail.is_valid_email', return_value=False):
                self.charge.send_receipt()
                # check log error
                self.assertEqual(len(self._log_messages.get('error')), 1)
                self._test_logging_handler.reset()
                # no email sent
                mock_send.assert_not_called()

        # add company address and email
        self.add_address(subscriber)

        subscriber.email = random_gen.gen_email()
        subscriber.save()
        self.assertTrue(self.charge.customer.subscriber.has_address)
        self.assertTrue(bool(self.charge.customer.subscriber.email))

        self.charge.send_receipt()
        mock_send.assert_called()

        mock_send.reset_mock()

        # reset sent
        self.charge.receipt_sent = False

        # add invoice
        self.charge.invoice = mommy.make(Invoice)
        self.charge.save()

        # check attachments
        mock_attach.assert_not_called()

        example_invoice = os.path.join(
            os.path.dirname(__file__), 'files', 'example.pdf')
        with patch.object(self.charge.invoice, '_generate_invoice_pdf',
                          return_value=example_invoice):
            self.charge.send_receipt()
            mock_send.assert_called()
            mock_attach.assert_called()

        mock_send.reset_mock()

        # check receipt_sent when email fails
        self.charge.receipt_sent = False
        self.charge.invoice = None
        self.charge.save()

        self.charge.send_receipt()
        # self.assertFalse(self.charge.receipt_sent)  # FIXME
        mock_send.assert_called()