def post_with_auth(self, data):
     return post_json(self.client,
                      '/request_funding',
                      data=data,
                      username=b'user4',
                      password=b'pass4',
                      timestamp=self.timestamp)
    def test_get_decision_decline(self, mock_get_requirements,
                                  mock_check_requirements,
                                  mock_evaluate_decision):
        mock_check_requirements.return_value = True
        mock_get_requirements.return_value = EXAMPLE_REQUIREMENTS
        mock_evaluate_decision.return_value = DecisionParams(approved=False, )

        tstamp = datetime(2018, 5, 4, 14, 3, 15)
        rv = post_json(self.client,
                       '/get_decision',
                       data={},
                       username=b'user2',
                       password=b'pass2',
                       timestamp=tstamp)
        assert rv.status == '200 OK'
        assert Decision.query.filter_by(user_id=self.test_user.id).count() == 1

        decision = Decision.query.filter_by(user_id=self.test_user.id)[0]
        expected_object = {'decision': DECLINED_STATE_NAME, 'datetime': tstamp}
        for k, v in expected_object.items():
            assert getattr(decision, k) == v

        expected_presentation = decision.to_dict()
        assert json.loads(rv.data) == {
            'requirements': EXAMPLE_REQUIREMENTS,
            'decision': expected_presentation
        }
 def test_register_new_user(self):
     payload = {
         'username': '******',
         'password': '******',
         'bank_account': 'abcdef'
     }
     rv = post_json(self.client, '/register', data=payload)
     assert b'new_user' in rv.data
     assert User.query.filter_by(username='******').scalar()
 def test_register_existing_user_fails(self):
     params = ('existing_user', 'bar', 'abcdef')
     create_user(*params)
     payload = {
         k: v
         for k, v in zip(['username', 'password', 'bank_account'], params)
     }
     rv = post_json(self.client, '/register', data=payload)
     assert b'existing_user' not in rv.data
     assert rv.status == '400 BAD REQUEST'
     assert User.query.filter_by(username='******').count() == 1
 def test_get_decision_post_no_decision(self, mock_get_requirements,
                                        mock_check_requirements):
     mock_check_requirements.return_value = False
     mock_get_requirements.return_value = EXAMPLE_REQUIREMENTS
     rv = post_json(self.client,
                    '/get_decision',
                    data={},
                    username=b'user2',
                    password=b'pass2')
     assert rv.status == '200 OK'
     assert json.loads(rv.data) == {'requirements': EXAMPLE_REQUIREMENTS}
 def test_requires_authorization(self, mock_send_cash):
     mock_send_cash.return_value = {
         'amount': 3500,
         'timestamp': self.timestamp + timedelta(minutes=1),
         'bank_ref': 'foo'
     }
     ApprovalFactory(user=self.test_user,
                     datetime=self.timestamp - timedelta(minutes=5),
                     amount=5000,
                     id=9)
     payload = {'amount': 3500, 'approval_reference': 9}
     response = post_json(self.client,
                          '/request_funding',
                          data=payload,
                          timestamp=self.timestamp)
     assert '200' not in response.status
     assert not mock_send_cash.called
    def test_get_decision_fails(self, mock_get_requirements,
                                mock_check_requirements,
                                mock_evaluate_decision):
        @staticmethod
        def raise_value_error():
            raise ValueError

        mock_check_requirements.return_value = True
        mock_get_requirements.return_value = EXAMPLE_REQUIREMENTS

        mock_evaluate_decision.side_effect = raise_value_error
        mock_evaluate_decision.return_value = DecisionParams(
            approved=True,
            params={
                'amount': 5000,
                'interest_rate': 0.0005,
                'fee_amount': 0,
                'fee_rate': 0
            })
        tstamp = datetime(2018, 5, 4, 14, 3, 12)
        rv = post_json(self.client,
                       '/get_decision',
                       data={},
                       username=b'user2',
                       password=b'pass2',
                       timestamp=tstamp)
        assert rv.status == '200 OK'
        assert Decision.query.filter_by(user_id=self.test_user.id).count() == 1

        decision = Decision.query.filter_by(user_id=self.test_user.id)[0]
        expected_object = {
            'decision': DECLINED_STATE_NAME,
            'datetime': tstamp,
            'interest_daily': None,
            'amount': None,
            'duration_days': None,
            'repayment_frequency_days': None,
            'fee_rate': None,
            'fee_amount': None
        }

        for k, v in expected_object.items():
            assert getattr(decision, k) == v

        expected_presentation = decision.to_dict()
        assert json.loads(rv.data)['decision'] == expected_presentation
    def test_full_decline(self):
        test_user = create_user('user4', 'pass4', 'acc4')
        data = {
            'basic_questions': {
                'first_name': 'Shady',
                'last_name': 'McShadyFace',
                'date_of_birth': '1994-12-21',
            },
            'credit_report': {
                'score': 1
            },
            'company_report': {
                'opinion': 'passable'
            },
        }

        tstamp = datetime(2018, 7, 3, 4, 3, 1)

        rv = post_json(self.client,
                       '/get_decision',
                       data=data,
                       username=b'user4',
                       password=b'pass4',
                       timestamp=tstamp)

        assert rv.status == '200 OK'

        assert Decision.query.filter_by(user_id=test_user.id).count() == 1
        decision = Decision.query.filter_by(user_id=test_user.id)[0]
        expected_object = {
            'decision': DECLINED_STATE_NAME,
            'datetime': tstamp,
            'interest_daily': None,
            'amount': None,
            'duration_days': None,
            'repayment_frequency_days': None,
            'fee_rate': None,
            'fee_amount': None
        }

        for k, v in expected_object.items():
            assert getattr(decision, k) == v, k
 def test_register_incomplete_details_fails(self):
     payload = {'username': '******', 'password': '******'}
     rv = post_json(self.client, '/register', payload)
     assert b'incomplete_user' not in rv.data
     assert rv.status == '400 BAD REQUEST'
     assert User.query.filter_by(username='******').count() == 0
    def test_full_approve(self):
        test_user = create_user('user3', 'pass3', 'acc3')
        data = {
            'basic_questions': {
                'first_name': 'Trusty',
                'last_name': 'McTrustFace',
                'date_of_birth': '1974-12-21',
            },
            'credit_report': {
                'score': 1
            },
            'company_report': {
                'opinion': 'passable'
            },
        }
        tstamp = datetime(2018, 7, 3, 4, 3, 1)

        rv = post_json(self.client,
                       '/get_decision',
                       data=data,
                       username=b'user3',
                       password=b'pass3',
                       timestamp=tstamp)

        assert rv.status == '200 OK'

        assert Decision.query.filter_by(user_id=test_user.id).count() == 1
        decision = Decision.query.filter_by(user_id=test_user.id)[0]
        expected_object = {
            'decision': APPROVED_STATE_NAME,
            'datetime': tstamp,
            'interest_daily': 0.0005,
            'amount': 5000.,
            'duration_days': 360,
            'repayment_frequency_days': 30,
            'fee_rate': 0,
            'fee_amount': 0
        }

        for k, v in expected_object.items():
            assert getattr(decision, k) == v, k

        expected_response = {
            'requirements': EXAMPLE_REQUIREMENTS,
            'decision': {
                'status': 'Approved',
                'reference': str(decision.id),
                'product': PRODUCT_NAME,
                'amount_min': MIN_LOAN_AMOUNT,
                'amount_max': 5000.,
                'duration': 360,
                'interest_type': 'Compound',
                'interest': 0.20016,
                'fee_flat': 0,
                'fee_rate': 0,
                'repayment_type': 'Equal Repayment',
                'repayment_frequency': '30d',
                'valid_until': datetime(2018, 7, 10).date().isoformat(),
            }
        }
        assert json.loads(rv.data) == expected_response