Esempio n. 1
0
    def test_stripe_company_security_invoice_item(self,
                                                  mock_invoice_item_create):
        customer = self.company.get_customer()

        stripe_company_security_invoice_item(customer, 'test', None)
        mock_invoice_item_create.assert_not_called()

        from django.conf import settings

        plans = copy.deepcopy(settings.DJSTRIPE_PLANS)
        plans['test']['features']['securities']['price'] = 20

        with self.settings(DJSTRIPE_PLANS=plans):
            stripe_company_security_invoice_item(customer, 'test', None)
            mock_invoice_item_create.assert_not_called()

            SecurityGenerator().generate(company=self.company)

            # one security is free
            stripe_company_security_invoice_item(customer, 'test', None)
            mock_invoice_item_create.assert_not_called()

            SecurityGenerator().generate(company=self.company)

            stripe_company_security_invoice_item(customer, 'test', None)
            mock_invoice_item_create.assert_called()
            mock_invoice_item_create.assert_called_with(
                customer=customer.stripe_id,
                amount=20,
                currency=settings.DJSTRIPE_CURRENCIES[0][0],
                invoice=None,
                description=_(u'Extra Securities (1 x 0.20)')
            )
Esempio n. 2
0
    def test_call(self):

        with self.assertRaises(ValueError):
            self.validator('')

        plan_name = settings.DJSTRIPE_PLANS.keys()[0]

        self.assertIsNone(self.validator(plan_name))

        MODIFIED_DJSTRIPE_PLANS = copy.deepcopy(settings.DJSTRIPE_PLANS)
        MODIFIED_DJSTRIPE_PLANS[plan_name]['features']['securities'] = {
            'max': 1
        }

        with self.settings(DJSTRIPE_PLANS=MODIFIED_DJSTRIPE_PLANS):
            self.assertIsNone(self.validator(plan_name))

            # add security
            SecurityGenerator().generate(company=self.validator.company)

            self.assertIsNone(self.validator(plan_name))

            # add another security
            SecurityGenerator().generate(company=self.validator.company)

            with self.assertRaises(ValidationError):
                self.validator(plan_name)
Esempio n. 3
0
    def test_get_readable_number_segments(self):
        obj = SecurityGenerator().generate()

        obj.number_segments = u'[1,2,4-10, {}]'
        result = self.serializer.get_readable_number_segments(obj)
        self.assertNotIn('[', result)
        self.assertNotIn(']', result)
        self.assertNotIn('u', result)
        self.assertNotIn('{', result)
        self.assertNotIn('}', result)
Esempio n. 4
0
    def test_count_in_segments(self):
        """
        count shares in segments
        """
        security = SecurityGenerator().generate()

        segments = '1, 3,4, 99-1000'
        count = security.count_in_segments(segments)
        self.assertEqual(count, 905)

        segments = [1, 3, 4, 5, u'99-1000']
        count = security.count_in_segments(segments)
        self.assertEqual(count, 906)
Esempio n. 5
0
    def test_fields(self):
        security = SecurityGenerator().generate()

        self.assertTrue(hasattr(security, 'track_numbers'))
        self.assertTrue(hasattr(security, 'face_value'))
        self.assertTrue(hasattr(security, 'number_segments'))

        segments = [1, 2, 3, '4-6']
        security.number_segments = segments
        security.save()

        # refresh from db
        s = Security.objects.get(id=security.id)
        self.assertEqual(s.number_segments, segments)
Esempio n. 6
0
    def test_save_number_segments(self):
        company = CompanyGenerator().generate()
        operator = OperatorGenerator().generate(company=company)
        security = SecurityGenerator().generate(company=company)
        url = reverse('security-detail', kwargs={'pk': security.id})
        request = self.factory.get(url)

        data = SecuritySerializer(security, context={'request': request}).data
        data.update({'number_segments': '1,2,3,4,8-10'})

        self.client.force_authenticate(user=operator.user)

        res = self.client.put(url, data=data)

        security = Security.objects.get(id=security.id)
        self.assertEqual(security.number_segments, [u'1-4', u'8-10'])

        data.update({'number_segments': '1,2,3,4,4,,8-10'})

        res = self.client.put(url, data=data)

        self.assertEqual(res.status_code, 200)

        security = Security.objects.get(id=security.id)
        self.assertEqual(security.number_segments, [u'1-4', u'8-10'])
Esempio n. 7
0
    def test_share_value(self):
        """
        share value is last trated price
        """
        company = CompanyGenerator().generate(share_count=1000000)
        security = SecurityGenerator().generate(company=company)
        sc = ShareholderGenerator().generate(company=company)
        s1 = ShareholderGenerator().generate(company=company)
        now = datetime.datetime.now()

        PositionGenerator().generate(buyer=sc,
                                     count=1000000,
                                     value=1,
                                     security=security,
                                     bought_at=now -
                                     datetime.timedelta(days=11))
        p = PositionGenerator().generate(buyer=s1,
                                         seller=sc,
                                         count=500000,
                                         value=100,
                                         security=security,
                                         bought_at=now -
                                         datetime.timedelta(days=10))
        p.value = None
        p.save()

        self.assertEqual(s1.share_value(), Decimal('500000.0000'))
Esempio n. 8
0
    def test_transactions_xls(self):
        """ test download of all transactions data
        /company/3/download/transactions?from=2017-01-12T23:00:00.000Z&to=2017-01-13T23:00:00.000Z&security=56
        """
        company = CompanyGenerator().generate()
        security = SecurityGenerator().generate(company=company)
        # run test
        response = self.client.get(
            reverse('reports:transactions_xls',
                    kwargs={"company_id": company.id}), {
                        'to': '2017-01-12T23:00:00.000Z',
                        'from': '2011-01-12T23:00:00.000Z',
                        'security': security.pk
                    })

        # not logged in user
        self.assertEqual(response.status_code, 302)

        # login and retest
        user = UserGenerator().generate()
        self.client.force_login(user)

        response = self.client.get(
            reverse('reports:transactions_xls',
                    kwargs={"company_id": company.id}), {
                        'to': '2017-01-12T23:00:00.000Z',
                        'from': '2011-01-12T23:00:00.000Z',
                        'security': security.pk
                    })

        # assert response code
        self.assertEqual(response.status_code, 403)
Esempio n. 9
0
    def test_update(self):
        obj = SecurityGenerator().generate()

        obj = self.serializer.update(obj, dict())
        self.assertEqual(obj.number_segments, list())

        data = dict(number_segments='[1,2,4-10]')
        obj = self.serializer.update(obj, data)
        self.assertEqual(obj.number_segments, data.get('number_segments'))
Esempio n. 10
0
    def test_share_percent(self):
        """
        proper share percent math
        """
        company = CompanyGenerator().generate(share_count=1000000)
        security = SecurityGenerator().generate(company=company)
        sc = ShareholderGenerator().generate(company=company)
        s1 = ShareholderGenerator().generate(company=company)
        s2 = ShareholderGenerator().generate(company=company)
        s3 = ShareholderGenerator().generate(company=company)
        now = datetime.datetime.now()

        PositionGenerator().generate(buyer=sc,
                                     count=1000000,
                                     value=100,
                                     security=security,
                                     bought_at=now -
                                     datetime.timedelta(days=11))
        PositionGenerator().generate(buyer=s1,
                                     seller=sc,
                                     count=500000,
                                     value=100,
                                     security=security,
                                     bought_at=now -
                                     datetime.timedelta(days=10))
        PositionGenerator().generate(buyer=s2,
                                     seller=sc,
                                     count=5000,
                                     value=100,
                                     security=security,
                                     bought_at=now -
                                     datetime.timedelta(days=9))
        PositionGenerator().generate(buyer=s3,
                                     seller=sc,
                                     count=50,
                                     value=100,
                                     security=security,
                                     bought_at=now -
                                     datetime.timedelta(days=8))

        self.assertEqual(s1.share_percent(), '99.00')
        self.assertEqual(s2.share_percent(), '0.99')
        self.assertEqual(s3.share_percent(), '0.01')

        PositionGenerator().generate(buyer=s2,
                                     seller=s1,
                                     count=250000,
                                     value=100,
                                     security=security,
                                     bought_at=now -
                                     datetime.timedelta(days=7))

        self.assertEqual(s1.share_percent(), '49.50')
Esempio n. 11
0
    def _prepare_split_scenario(self):
        company = CompanyGenerator().generate()
        security = SecurityGenerator().generate(company=company)
        cshareholder = ShareholderGenerator().generate(company=company)
        shareholder1 = ShareholderGenerator().generate(company=company)
        shareholder2 = ShareholderGenerator().generate(company=company)
        shareholder3 = ShareholderGenerator().generate(company=company)
        shareholder4 = ShareholderGenerator().generate(company=company)
        PositionGenerator().generate(buyer=cshareholder,
                                     count=10000,
                                     value=1000,
                                     security=security,
                                     seller=None)
        p1 = PositionGenerator().generate(buyer=shareholder1,
                                          count=100,
                                          value=20,
                                          seller=cshareholder,
                                          security=security)
        p2 = PositionGenerator().generate(buyer=shareholder2,
                                          count=200,
                                          value=40,
                                          seller=cshareholder,
                                          security=security)
        p3 = PositionGenerator().generate(buyer=shareholder3,
                                          count=900,
                                          value=60,
                                          seller=cshareholder,
                                          security=security)
        p4 = PositionGenerator().generate(buyer=cshareholder,
                                          count=600,
                                          value=80,
                                          seller=shareholder3,
                                          security=security)
        PositionGenerator().generate(buyer=cshareholder,
                                     count=1000,
                                     value=1000,
                                     security=security,
                                     seller=None)
        company.share_count = 11000
        company.save()

        return dict(company=company,
                    cshareholder=cshareholder,
                    shareholder1=shareholder1,
                    shareholder2=shareholder2,
                    shareholder3=shareholder3,
                    shareholder4=shareholder4,
                    p1=p1,
                    p2=p2,
                    p3=p3,
                    p4=p4,
                    security=security)
Esempio n. 12
0
    def test_numbered_shares(self):

        company = CompanyGenerator().generate()
        SecurityGenerator().generate(
            company=company, track_numbers=True)
        operator = OperatorGenerator().generate(company=company)

        self.client.login(username=operator.user.username,
                          password=DEFAULT_TEST_DATA.get('password'))

        res = self.client.get(reverse(
            'company', kwargs={'company_id': company.id}))

        self.assertEqual(res.status_code, 200)
        self.assertTrue(
            _('tracking security numbers for owners enabled. segments:') in res.content)
Esempio n. 13
0
    def test_get_get_readable_title(self):
        obj = SecurityGenerator().generate()

        obj.face_value = 100
        self.assertIn(obj.get_title_display(),
                      self.serializer.get_readable_title(obj))
        self.assertIn(str(obj.face_value),
                      self.serializer.get_readable_title(obj))

        obj.face_value = 0
        self.assertEqual(self.serializer.get_readable_title(obj),
                         obj.get_title_display())
Esempio n. 14
0
    def test_call(self):

        self.assertIsNone(self.validator())

        # add subscription
        self.add_subscription(self.validator.company)

        self.assertIsNone(self.validator())

        plan_name = self.validator.company.get_current_subscription_plan()

        MODIFIED_DJSTRIPE_PLANS = copy.deepcopy(settings.DJSTRIPE_PLANS)
        MODIFIED_DJSTRIPE_PLANS[plan_name]['features']['securities'] = {
            'max': 1
        }

        with self.settings(DJSTRIPE_PLANS=MODIFIED_DJSTRIPE_PLANS):
            self.assertIsNone(self.validator())

            # add security
            SecurityGenerator().generate(company=self.validator.company)

            with self.assertRaises(ValidationError):
                self.validator()
Esempio n. 15
0
    def test_watter_split_61(self):
        """
        issue shares multiple times, sell some of them then do a split. see #61
        """
        company = CompanyGenerator().generate(share_count=10000000)
        sc = ShareholderGenerator().generate(company=company)  # aka Company
        s1 = ShareholderGenerator().generate(company=company)  # aka W
        s2 = ShareholderGenerator().generate(company=company)  # aka S
        OperatorGenerator().generate(company=company)
        security = SecurityGenerator().generate(company=company)
        now = datetime.datetime.now()
        PositionGenerator().generate(
            buyer=s2,
            seller=sc,
            count=10000,
            value=100,
            security=security,
            bought_at=now - datetime.timedelta(days=11))  # initial seed

        p1 = PositionGenerator().generate(buyer=s1,
                                          seller=sc,
                                          count=187,
                                          value=100,
                                          security=security,
                                          bought_at=now -
                                          datetime.timedelta(days=10))
        p2 = PositionGenerator().generate(buyer=s1,
                                          seller=sc,
                                          count=398,
                                          value=100,
                                          security=security,
                                          bought_at=now -
                                          datetime.timedelta(days=9))
        PositionGenerator().generate(buyer=s2,
                                     seller=s1,
                                     count=80,
                                     value=100,
                                     security=security,
                                     bought_at=now -
                                     datetime.timedelta(days=8))
        PositionGenerator().generate(buyer=s2,
                                     seller=s1,
                                     count=437,
                                     value=100,
                                     security=security,
                                     bought_at=now -
                                     datetime.timedelta(days=7))
        PositionGenerator().generate(buyer=s1,
                                     seller=sc,
                                     count=837,
                                     value=100,
                                     security=security,
                                     bought_at=now -
                                     datetime.timedelta(days=6))
        PositionGenerator().generate(buyer=s1,
                                     seller=sc,
                                     count=68,
                                     value=100,
                                     security=security,
                                     bought_at=now -
                                     datetime.timedelta(days=5))

        split_data = {
            'execute_at': now - datetime.timedelta(days=4),
            'dividend': 1,
            'divisor': 100,
            'comment': "MEGA SPLIT",
            'security': security,
        }
        company.split_shares(split_data)

        PositionGenerator().generate(buyer=s1,
                                     seller=sc,
                                     count=3350,
                                     value=100,
                                     security=security,
                                     bought_at=now -
                                     datetime.timedelta(days=3))

        self.assertEqual(s1.share_count(), 100650)
        positions = Position.objects.filter(
            comment__contains="split").filter(Q(buyer=s1) | Q(seller=s1))
        self.assertEqual(positions.count(), 2)
        p1 = positions[0]
        p2 = positions[1]
        self.assertEqual(p1.count, 973)
        self.assertEqual(p1.buyer, sc)
        self.assertEqual(p1.seller, s1)
        self.assertEqual(p2.count, 97300)
        self.assertEqual(p2.seller, sc)
        self.assertEqual(p2.buyer, s1)