Esempio n. 1
0
    def test_initiator_should_see_appropriate_credit_trades(self):
        """
        As a fuel supplier, I should see all credit trades where:
        I'm the initiator, regardless of status
        I'm the respondent, if the status is "submitted" or greater
        """

        # setup some test data
        DataCreationUtilities.create_possible_credit_trades(
            self.users['fs_user_1'].organization,
            self.users['fs_user_2'].organization)

        response = self.clients['fs_user_1'].get('/api/credit_trades')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        fs_credit_trades = response.json()
        for credit_trade in fs_credit_trades:
            correct_view = False

            if credit_trade['initiator']['id'] == \
               self.users['fs_user_1'].organization.id:
                correct_view = True
            elif (credit_trade['respondent']['id']
                  == self.users['fs_user_1'].organization.id
                  and credit_trade['status']['id'] >=
                  self.statuses['submitted'].id):
                correct_view = True

            self.assertTrue(correct_view)
    def test_signing_authority_confirmations(self):
        """Test permissions for signing authority confirmations """

        initiator = \
            self.users[self.user_map[TestSigningAuthorityConfirmations.UserRelationship.INITIATOR]]
        respondent = \
            self.users[self.user_map[TestSigningAuthorityConfirmations.UserRelationship.RESPONDENT]]

        trades = DataCreationUtilities.create_possible_credit_trades(
            initiator.organization,
            respondent.organization
        )

        expected_results = defaultdict(lambda: False)

        # key (relationship, status, rescinded?)

        expected_results[(
            TestSigningAuthorityConfirmations.UserRelationship.INITIATOR,
            self.statuses['draft'].status,
            False
        )] = True

        expected_results[(
            TestSigningAuthorityConfirmations.UserRelationship.INITIATOR,
            self.statuses['submitted'].status,
            False
        )] = True

        expected_results[(
            TestSigningAuthorityConfirmations.UserRelationship.RESPONDENT,
            self.statuses['accepted'].status,
            False
        )] = True

        expected_results[(
            TestSigningAuthorityConfirmations.UserRelationship.RESPONDENT,
            self.statuses['submitted'].status,
            False
        )] = True

        for (trade, relationship) in \
                product(trades, TestSigningAuthorityConfirmations.UserRelationship):

            # Sign an array of assertions, like the frontend does
            with self.subTest(
                "Testing signing confirmation permissions as array",
                relationship=relationship,
                status=trade['status'],
                rescinded=trade['rescinded']
            ):
                payload = list(map(lambda assertion, trade_id=trade['id']: {
                    'hasAccepted': True,
                    'signingAuthorityAssertion': assertion.id,
                    'creditTrade': trade_id
                }, SigningAuthorityAssertion.objects.all()))

                response = self.clients[self.user_map[relationship]].post(
                    '/api/signing_authority_confirmations',
                    content_type='application/json',
                    data=json.dumps(payload)
                )

                valid = status.is_success(response.status_code)

                self.assertEqual(
                    valid,
                    expected_results[(relationship, trade['status'], trade['rescinded'])]
                )

            # also try one at a time (not a JSON array)
            with self.subTest(
                "Testing signing confirmation permissions one at a time",
                relationship=relationship,
                status=trade['status'],
                rescinded=trade['rescinded']
            ):
                assertion_id = SigningAuthorityAssertion.objects.first().id

                payload = {
                    'hasAccepted': True,
                    'signingAuthorityAssertion': assertion_id,
                    'creditTrade': trade['id']
                }

                response = self.clients[self.user_map[relationship]].post(
                    '/api/signing_authority_confirmations',
                    content_type='application/json',
                    data=json.dumps(payload)
                )

                valid = status.is_success(response.status_code)

                self.assertEqual(
                    valid,
                    expected_results[(relationship, trade['status'], trade['rescinded'])]
                )