Beispiel #1
0
    def test_approved_buy(self, **kwargs):
        # get fuel supplier balance for fs 1
        initiator_bal = fake_api_calls.get_organization_balance(id=2)
        respondent_bal = fake_api_calls.get_organization_balance(id=3)

        num_of_credits = 50

        credit_trade = fake_api_calls.create_credit_trade(
            user_id=self.user_id,
            status=STATUS_SUBMITTED,
            fair_market_value_per_credit=1000,
            initiator=2,
            respondent=3,
            number_of_credits=num_of_credits,
            type=2)

        credit_trade['status'] = STATUS_ACCEPTED
        updated_response = fake_api_calls.update_credit_trade_dict(
            credit_trade, credit_trade['id'])

        response = self.gov_client.put("{}/{}/approve".format(
            self.test_url, credit_trade['id']),
                                       content_type='application/json')

        # print(response.status_code)
        # print(response.content.decode("utf-8"))

        assert status.HTTP_200_OK == response.status_code

        # TODO: Make sure two credit histories are created

        initiator_bal_after = fake_api_calls.get_organization_balance(id=2)
        respondent_bal_after = fake_api_calls.get_organization_balance(id=3)

        init_final_bal = initiator_bal['validatedCredits'] + num_of_credits
        resp_final_bal = respondent_bal['validatedCredits'] - num_of_credits

        ct_completed = self.client.get("{}/{}".format(self.test_url,
                                                      credit_trade['id']),
                                       content_type='application/json')

        completed_response = json.loads(ct_completed.content.decode("utf-8"))

        # response_data should have status == completed
        today = datetime.datetime.today().strftime('%Y-%m-%d')

        # Status of Credit Trade should be 'completed'
        self.assertEqual(completed_response['status']['id'], STATUS_COMPLETED)

        # Effective date should be today
        self.assertEqual(initiator_bal_after['effectiveDate'], today)
        self.assertEqual(respondent_bal_after['effectiveDate'], today)

        # Credits should be subtracted/added
        self.assertEqual(init_final_bal,
                         initiator_bal_after['validatedCredits'])
        self.assertEqual(resp_final_bal,
                         respondent_bal_after['validatedCredits'])
Beispiel #2
0
    def setUp(self):

        # Initialize Foreign keys
        # self.ct_status_id = fake_api_calls?.create_credit_trade_status()
        self.ct_type_id = fake_api_calls.create_credit_trade_type()
        self.fs1_id, self.fs1_status_id, self.fs1_action_type_id = (
            fake_api_calls.create_organization())
        self.user_id = fake_api_calls.create_user(self.fs1_id)

        self.credit_trade = fake_api_calls.create_credit_trade(
            initiator=self.fs1_id,
            respondent=self.fs1_id,
            type=self.ct_type_id,
            status=STATUS_DRAFT,
            user_id=self.user_id,
        )

        self.client = Client(
            HTTP_SMGOV_USERGUID='c9804c52-05f1-4a6a-9d24-332d9d8be2a9',
            HTTP_SMGOV_USERDISPLAYNAME='Brad Smith',
            HTTP_SMGOV_USEREMAIL='*****@*****.**',
            HTTP_SM_UNIVERSALID='BSmith')
        self.test_url = "/api/credit_trades"

        self.test_data_fail = [{
            'data': {'number_of_credits': 1},
            'response': {
                "status": ["This field is required."],
                "respondent": ["This field is required."],
                "type": ["This field is required."]
            }
        }, {
            'data': {'number_of_credits': 1,
                     'status': STATUS_DRAFT},
            'response': {
                "respondent": ["This field is required."],
                "type": ["This field is required."],
            }
        }, {
            'data': {'number_of_credits': 1,
                     'status': STATUS_DRAFT,
                     'respondent': self.fs1_id},
            'response': {
                "type": ["This field is required."]
            }
        }]

        self.test_data_success = [{
            'data': {'number_of_credits': 1,
                     'status': STATUS_DRAFT,
                     'respondent': self.fs1_id,
                     'type': self.ct_type_id},
        }]
Beispiel #3
0
    def setUp(self):

        # Initialize Foreign keys
        # self.ct_status_id = fake_api_calls?.create_credit_trade_status()
        self.ct_type_id = fake_api_calls.create_credit_trade_type()
        self.fs1_id, self.fs1_status_id, self.fs1_action_type_id = (
            fake_api_calls.create_organization())
        self.user_id = fake_api_calls.create_user(self.fs1_id)
        resp_user = User.objects.get(id=self.user_id)
        resp_user.authorization_guid = 'e46435c1-7d69-489f-9dde-83005cd77744'
        resp_user.save()
        '''
        Apply a fuel supplier role to the default user
        '''
        fs_user = User.objects.get(username='******')
        fs_role = Role.objects.get(name='FSManager')
        UserRole.objects.create(user_id=fs_user.id, role_id=fs_role.id)
        '''
        Apply a fuel supplier role to the respondent user
        '''
        fs_role = Role.objects.get(name='FSManager')
        UserRole.objects.create(user_id=resp_user.id, role_id=fs_role.id)

        self.credit_trade = fake_api_calls.create_credit_trade(
            initiator=2,
            respondent=self.fs1_id,
            type=self.ct_type_id,
            status=STATUS_DRAFT,
            user_id=self.user_id,
        )

        self.client = Client(
            HTTP_SMGOV_USERGUID='c9804c52-05f1-4a6a-9d24-332d9d8be2a9',
            HTTP_SMGOV_USERDISPLAYNAME='Brad Smith',
            HTTP_SMGOV_USEREMAIL='*****@*****.**',
            HTTP_SM_UNIVERSALID='BSmith')

        self.gov_client = Client(
            HTTP_SMGOV_USERGUID='c2971372-3a96-4704-9b9c-18e4e9298ee3',
            HTTP_SMGOV_USERDISPLAYNAME='Test Person',
            HTTP_SMGOV_USEREMAIL='*****@*****.**',
            HTTP_SM_UNIVERSALID='Teperson',
            HTTP_SMGOV_USERTYPE='Internal',
            HTTP_SM_AUTHDIRNAME='IDIR')

        self.respondent_client = Client(
            HTTP_SMGOV_USERGUID='e46435c1-7d69-489f-9dde-83005cd77744',
            HTTP_SMGOV_USERDISPLAYNAME=resp_user.display_name,
            HTTP_SMGOV_USEREMAIL=resp_user.email)
        '''
        Apply a government role to Teperson
        '''
        gov_user = User.objects.get(username='******')
        gov_role = Role.objects.get(name='GovDirector')
        UserRole.objects.create(user_id=gov_user.id, role_id=gov_role.id)

        self.test_url = "/api/credit_trades"

        self.test_data_fail = [{
            'data': {
                'number_of_credits': 1
            },
            'response': {
                "status": ["This field is required."],
                "respondent": ["This field is required."],
                "type": ["This field is required."]
            }
        }, {
            'data': {
                'number_of_credits': 1,
                'status': STATUS_DRAFT
            },
            'response': {
                "respondent": ["This field is required."],
                "type": ["This field is required."],
            }
        }, {
            'data': {
                'number_of_credits': 1,
                'status': STATUS_DRAFT,
                'respondent': self.fs1_id
            },
            'response': {
                "type": ["This field is required."]
            }
        }]

        self.test_data_success = [{
            'data': {
                'number_of_credits': 1,
                'status': STATUS_DRAFT,
                'initiator': 2,
                'respondent': self.fs1_id,
                'type': self.ct_type_id
            },
        }]
Beispiel #4
0
    def test_approved_buy(self, **kwargs):
        # get fuel supplier balance for fs 1
        initiator_bal = fake_api_calls.get_organization_balance(id=2)
        respondent_bal, created = OrganizationBalance.objects.get_or_create(
            organization_id=self.fs1_id,
            expiration_date=None,
            defaults={'validated_credits': 10000})

        num_of_credits = 50

        credit_trade = fake_api_calls.create_credit_trade(
            user_id=self.user_id,
            status=STATUS_SUBMITTED,
            fair_market_value_per_credit=1000,
            initiator=2,
            respondent=self.fs1_id,
            number_of_credits=num_of_credits,
            type=2)

        data = {
            'number_of_credits': num_of_credits,
            'status': STATUS_ACCEPTED,
            'initiator': 2,
            'respondent': self.fs1_id,
            'type': 2,
            'fair_market_value_per_credit': 1000
        }

        resp_user = User.objects.get(id=self.user_id)
        resp_user.organization_id = self.fs1_id
        resp_user.save()

        response = self.respondent_client.put("{}/{}".format(
            self.test_url, credit_trade['id']),
                                              content_type='application/json',
                                              data=json.dumps(data))

        response = self.gov_client.put("{}/{}/approve".format(
            self.test_url, credit_trade['id']),
                                       content_type='application/json')

        assert status.HTTP_200_OK == response.status_code

        # TODO: Make sure two credit histories are created

        initiator_bal_after = fake_api_calls.get_organization_balance(id=2)
        respondent_bal_after = fake_api_calls.get_organization_balance(
            id=self.fs1_id)

        init_final_bal = initiator_bal['validatedCredits'] + num_of_credits
        resp_final_bal = respondent_bal.validated_credits - num_of_credits

        ct_completed = self.client.get("{}/{}".format(self.test_url,
                                                      credit_trade['id']),
                                       content_type='application/json')

        completed_response = json.loads(ct_completed.content.decode("utf-8"))

        # response_data should have status == completed
        today = datetime.datetime.today().strftime('%Y-%m-%d')

        # Status of Credit Trade should be 'completed'
        self.assertEqual(completed_response['status']['id'], STATUS_COMPLETED)

        # Effective date should be today
        self.assertEqual(initiator_bal_after['effectiveDate'], today)
        self.assertEqual(respondent_bal_after['effectiveDate'], today)

        # Credits should be subtracted/added
        self.assertEqual(init_final_bal,
                         initiator_bal_after['validatedCredits'])
        self.assertEqual(resp_final_bal,
                         respondent_bal_after['validatedCredits'])