def test_new_credits_display(self): with responses.RequestsMock() as rsps: rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(), status=200, ) # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(), status=200, match_querystring=True, ) self.login() response = self.client.get(self.url, follow=True) self.assertContains(response, '52.00') self.assertContains(response, '45.00')
def test_new_credits_display(self): with responses.RequestsMock() as rsps: rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(), status=200, ) # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(), status=200, match_querystring=True, ) self.login() response = self.client.get(self.url, follow=True) self.assertContains(response, '52.00') self.assertContains(response, '45.00')
def test_processing_credits_displays_continue_when_done(self): with responses.RequestsMock() as rsps: # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(), status=200, match_querystring=True, ) self.login() response = self.client.get(reverse('processing-credits'), follow=True) self.assertContains(response, 'Continue')
def test_processing_credits_displays_continue_when_done(self): with responses.RequestsMock() as rsps: # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(), status=200, match_querystring=True, ) self.login() response = self.client.get(reverse('processing-credits'), follow=True) self.assertContains(response, 'Continue')
def test_processing_credits_redirects_to_new_for_expired_batch(self): with responses.RequestsMock() as rsps: # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(EXPIRED_PROCESSING_BATCH), status=200, ) # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(EXPIRED_PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(CREDIT_2), status=200, match_querystring=True, ) # get complete credits rsps.add( rsps.GET, api_url('/credits/?resolution=credited&pk=1&pk=2'), json=wrap_response_data(CREDIT_1), status=200, match_querystring=True, ) rsps.add( rsps.DELETE, api_url('/credits/batches/%s/' % PROCESSING_BATCH['id']), status=200, ) # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(CREDIT_2), status=200, match_querystring=True, ) self.login() response = self.client.get(self.url, follow=True) self.assertRedirects(response, reverse('new-credits'))
def test_processing_credits_redirects_to_new_for_expired_batch(self): with responses.RequestsMock() as rsps: # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(EXPIRED_PROCESSING_BATCH), status=200, ) # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(EXPIRED_PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(CREDIT_2), status=200, match_querystring=True, ) # get complete credits rsps.add( rsps.GET, api_url('/credits/?resolution=credited&pk=1&pk=2'), json=wrap_response_data(CREDIT_1), status=200, match_querystring=True, ) rsps.add( rsps.DELETE, api_url('/credits/batches/%s/' % PROCESSING_BATCH['id']), status=200, ) # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(CREDIT_2), status=200, match_querystring=True, ) self.login() response = self.client.get(self.url, follow=True) self.assertRedirects(response, reverse('new-credits'))
def test_new_credits_redirects_to_processing_when_batch_active(self): with responses.RequestsMock() as rsps: # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) self.login() response = self.client.get(reverse('new-credits'), follow=True) self.assertRedirects(response, self.url)
def test_new_credits_redirects_to_processing_when_batch_active(self): with responses.RequestsMock() as rsps: # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) self.login() response = self.client.get(reverse('new-credits'), follow=True) self.assertRedirects(response, self.url)
def test_manual_credits_submit(self, _): with responses.RequestsMock() as rsps: # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # credit credit to API rsps.add( rsps.POST, api_url('/credits/actions/credit/'), status=204, ) # REDIRECT after success # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(), status=200, ) # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(CREDIT_2), status=200, match_querystring=True, ) self.login() response = self.client.post( self.url, data={'submit_manual_1': ''}, follow=True ) self.assertEqual( json.loads(rsps.calls[2].request.body.decode('utf-8')), [{'id': 1, 'credited': True}] ) self.assertEqual(response.status_code, 200) self.assertContains(response, '1 credit manually input by you into NOMIS')
def test_new_credits_submit_with_uncreditable(self, *_): with responses.RequestsMock() as rsps: # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(), status=200, match_querystring=True, ) # create batch rsps.add( rsps.POST, api_url('/credits/batches/'), status=201, ) # credit credits to NOMIS and API rsps.add( rsps.POST, api_url('/credits/actions/setmanual/'), status=204, ) rsps.add( rsps.POST, api_url('/credits/actions/credit/'), status=204, ) # REDIRECT after success # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(), status=200, match_querystring=True, ) # get complete credits rsps.add( rsps.GET, api_url('/credits/?resolution=credited&pk=1&pk=2'), json=wrap_response_data(CREDIT_2), status=200, match_querystring=True, ) # delete completed batch rsps.add( rsps.DELETE, api_url('/credits/batches/%s/' % PROCESSING_BATCH['id']), status=200, ) # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(CREDIT_1), status=200, match_querystring=True, ) self.login() with silence_logger(name='mtp', level=logging.CRITICAL): response = self.client.post( self.url, data={'credits': [1, 2], 'submit_new': 'submit'}, follow=True ) self.assertEqual(response.status_code, 200) self.assertContains(response, '1 credit sent to NOMIS') self.assertContains(response, '1 credit needs your manual input in NOMIS') self.assertEqual(len(mail.outbox), 1)
def test_balance_check_after_credit(self, mock_nomis, mock_logger): balances = { # credit 1 – £52 – within cap 'A1234BC': {'cash': 800, 'spends': 0, 'savings': 4000}, # credit 2 – £45 – exceeds cap 'A1234GG': {'cash': 1000, 'spends': 200, 'savings': 5500}, } def create_transaction(**kwargs): prisoner_number = kwargs['prisoner_number'] amount = kwargs['amount'] balances[prisoner_number]['cash'] += amount return {'id': f'{prisoner_number}-1'} def get_account_balances(prison, prisoner_number): self.assertEqual(prison, 'BXI', msg='Unexpected test data') return balances[prisoner_number] mock_nomis.create_transaction = create_transaction mock_nomis.get_account_balances = get_account_balances with responses.RequestsMock() as rsps: # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(), status=200, match_querystring=True, ) # create batch rsps.add( rsps.POST, api_url('/credits/batches/'), status=201, ) rsps.add( rsps.POST, api_url('/credits/actions/credit/'), status=204, ) rsps.add( rsps.POST, api_url('/credits/actions/credit/'), status=204, ) # REDIRECT after success # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(), status=200, match_querystring=True, ) # get complete credits rsps.add( rsps.GET, api_url('/credits/?resolution=credited&pk=1&pk=2'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # delete completed batch rsps.add( rsps.DELETE, api_url('/credits/batches/%s/' % PROCESSING_BATCH['id']), status=200, ) # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(), status=200, match_querystring=True, ) self.login() response = self.client.post( self.url, data={'credits': [1, 2], 'submit_new': 'submit'}, follow=True ) self.assertEqual(response.status_code, 200) self.assertContains(response, '2 credits sent to NOMIS') logger_error_messages = [call[0][0] for call in mock_logger.error.call_args_list] self.assertEqual(len(logger_error_messages), 1) self.assertEqual(logger_error_messages[0], 'NOMIS account balance for A1234GG exceeds cap')
def test_new_credits_submit(self, mock_create_transaction): with responses.RequestsMock() as rsps: # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(), status=200, match_querystring=True, ) # create batch rsps.add( rsps.POST, api_url('/credits/batches/'), status=201, ) rsps.add( rsps.POST, api_url('/credits/actions/credit/'), status=204, ) rsps.add( rsps.POST, api_url('/credits/actions/credit/'), status=204, ) # REDIRECT after success # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(), status=200, match_querystring=True, ) # get complete credits rsps.add( rsps.GET, api_url('/credits/?resolution=credited&pk=1&pk=2'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # delete completed batch rsps.add( rsps.DELETE, api_url('/credits/batches/%s/' % PROCESSING_BATCH['id']), status=200, ) # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(), status=200, match_querystring=True, ) self.login() response = self.client.post( self.url, data={'credits': [1, 2], 'submit_new': 'submit'}, follow=True ) self.assertEqual(response.status_code, 200) expected_calls = [ [{'id': 1, 'credited': True, 'nomis_transaction_id': 'A1234BC-1'}], [{'id': 2, 'credited': True, 'nomis_transaction_id': 'A1234GG-1'}] ] self.assertTrue( json.loads(rsps.calls[3].request.body.decode('utf-8')) in expected_calls and json.loads(rsps.calls[4].request.body.decode('utf-8')) in expected_calls ) request_nomis_session_used = mock_create_transaction.call_args_list[0][1]['session'] mock_create_transaction.assert_has_calls( [ mock.call( amount=5200, description='Sent by Fred Smith', prison_id='BXI', prisoner_number='A1234BC', record_id='1', retries=1, session=request_nomis_session_used, transaction_type='MTDS', ), mock.call( amount=4500, description='Sent by Fred Jones', prison_id='BXI', prisoner_number='A1234GG', record_id='2', retries=1, session=request_nomis_session_used, transaction_type='MTDS', ), ], any_order=True, ) self.assertContains(response, '2 credits sent to NOMIS') self.assertEqual(len(mail.outbox), 2) self.assertTrue( '£52.00' in mail.outbox[0].body and '£45.00' in mail.outbox[1].body or '£52.00' in mail.outbox[1].body and '£45.00' in mail.outbox[0].body ) self.assertTrue( 'BAAB65F6' in mail.outbox[0].body or 'BAAB65F6' in mail.outbox[1].body )
def test_new_credits_submit(self): with responses.RequestsMock() as rsps: # get new credits rsps.add( rsps.GET, api_url( '/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending' ), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url( '/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at' ), json=wrap_response_data(), status=200, match_querystring=True, ) # create batch rsps.add( rsps.POST, api_url('/credits/batches/'), status=201, ) # credit credits to NOMIS and API rsps.add( rsps.POST, nomis_url('/prison/BXI/offenders/A1234BC/transactions/'), json={'id': '6244779-1'}, status=200, ) rsps.add( rsps.POST, api_url('/credits/actions/credit/'), status=204, ) rsps.add( rsps.POST, nomis_url('/prison/BXI/offenders/A1234GG/transactions/'), json={'id': '6244780-1'}, status=200, ) rsps.add( rsps.POST, api_url('/credits/actions/credit/'), status=204, ) # REDIRECT after success # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(), status=200, match_querystring=True, ) # get complete credits rsps.add( rsps.GET, api_url('/credits/?resolution=credited&pk=1&pk=2'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # delete completed batch rsps.add( rsps.DELETE, api_url('/credits/batches/%s/' % PROCESSING_BATCH['id']), status=200, ) # get new credits rsps.add( rsps.GET, api_url( '/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending' ), json=wrap_response_data(), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url( '/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at' ), json=wrap_response_data(), status=200, match_querystring=True, ) self.login() response = self.client.post(self.url, data={ 'credits': [1, 2], 'submit_new': None }, follow=True) self.assertEqual(response.status_code, 200) expected_calls = [[{ 'id': 1, 'credited': True, 'nomis_transaction_id': '6244779-1' }], [{ 'id': 2, 'credited': True, 'nomis_transaction_id': '6244780-1' }]] self.assertTrue( json.loads(rsps.calls[4].request.body.decode('utf-8')) in expected_calls and json.loads(rsps.calls[6].request.body.decode('utf-8')) in expected_calls) self.assertContains(response, '2 credits sent to NOMIS') self.assertEqual(len(mail.outbox), 2) self.assertTrue('£52.00' in mail.outbox[0].body and '£45.00' in mail.outbox[1].body or '£52.00' in mail.outbox[1].body and '£45.00' in mail.outbox[0].body)
def test_manual_credits_submit(self): with responses.RequestsMock() as rsps: # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # credit credit to API rsps.add( rsps.POST, api_url('/credits/actions/credit/'), status=204, ) # REDIRECT after success # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(), status=200, ) # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(CREDIT_2), status=200, match_querystring=True, ) self.login() response = self.client.post( self.url, data={'submit_manual_1': None}, follow=True ) self.assertEqual( json.loads(rsps.calls[2].request.body.decode('utf-8')), [{'id': 1, 'credited': True}] ) self.assertEqual(response.status_code, 200) self.assertContains(response, '1 credit manually input by you into NOMIS')
def test_new_credits_submit_with_uncreditable(self): with responses.RequestsMock() as rsps: # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(), status=200, match_querystring=True, ) # create batch rsps.add( rsps.POST, api_url('/credits/batches/'), status=201, ) # credit credits to NOMIS and API rsps.add( rsps.POST, nomis_url('/prison/BXI/offenders/A1234BC/transactions/'), status=400, ) rsps.add( rsps.POST, api_url('/credits/actions/setmanual/'), status=204, ) rsps.add( rsps.POST, nomis_url('/prison/BXI/offenders/A1234GG/transactions/'), json={'id': '6244780-1'}, status=200, ) rsps.add( rsps.POST, api_url('/credits/actions/credit/'), status=204, ) # REDIRECT after success # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(), status=200, match_querystring=True, ) # get complete credits rsps.add( rsps.GET, api_url('/credits/?resolution=credited&pk=1&pk=2'), json=wrap_response_data(CREDIT_2), status=200, match_querystring=True, ) # delete completed batch rsps.add( rsps.DELETE, api_url('/credits/batches/%s/' % PROCESSING_BATCH['id']), status=200, ) # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(CREDIT_1), status=200, match_querystring=True, ) rsps.add( rsps.GET, nomis_url('/offenders/A1234BC/location/'), json={ 'establishment': { 'code': 'LEI', 'desc': 'LEEDS (HMP)' } }, status=200, ) self.login() with silence_logger(name='mtp', level=logging.CRITICAL): response = self.client.post( self.url, data={'credits': [1, 2], 'submit_new': None}, follow=True ) self.assertEqual(response.status_code, 200) self.assertContains(response, '1 credit sent to NOMIS') self.assertContains(response, '1 credit needs your manual input in NOMIS') self.assertEqual(len(mail.outbox), 1)
def test_new_credits_submit(self): with responses.RequestsMock() as rsps: # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(), status=200, match_querystring=True, ) # create batch rsps.add( rsps.POST, api_url('/credits/batches/'), status=201, ) # credit credits to NOMIS and API rsps.add( rsps.POST, nomis_url('/prison/BXI/offenders/A1234BC/transactions/'), json={'id': '6244779-1'}, status=200, ) rsps.add( rsps.POST, api_url('/credits/actions/credit/'), status=204, ) rsps.add( rsps.POST, nomis_url('/prison/BXI/offenders/A1234GG/transactions/'), json={'id': '6244780-1'}, status=200, ) rsps.add( rsps.POST, api_url('/credits/actions/credit/'), status=204, ) # REDIRECT after success # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(), status=200, match_querystring=True, ) # get complete credits rsps.add( rsps.GET, api_url('/credits/?resolution=credited&pk=1&pk=2'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # delete completed batch rsps.add( rsps.DELETE, api_url('/credits/batches/%s/' % PROCESSING_BATCH['id']), status=200, ) # get new credits rsps.add( rsps.GET, api_url('/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending'), json=wrap_response_data(), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url('/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at'), json=wrap_response_data(), status=200, match_querystring=True, ) self.login() response = self.client.post( self.url, data={'credits': [1, 2], 'submit_new': None}, follow=True ) self.assertEqual(response.status_code, 200) expected_calls = [ [{'id': 1, 'credited': True, 'nomis_transaction_id': '6244779-1'}], [{'id': 2, 'credited': True, 'nomis_transaction_id': '6244780-1'}] ] self.assertTrue( json.loads(rsps.calls[4].request.body.decode('utf-8')) in expected_calls and json.loads(rsps.calls[6].request.body.decode('utf-8')) in expected_calls ) self.assertContains(response, '2 credits sent to NOMIS') self.assertEqual(len(mail.outbox), 2) self.assertTrue( '£52.00' in mail.outbox[0].body and '£45.00' in mail.outbox[1].body or '£52.00' in mail.outbox[1].body and '£45.00' in mail.outbox[0].body )
def test_new_credits_submit_with_conflict(self): with responses.RequestsMock() as rsps: # get new credits rsps.add( rsps.GET, api_url( '/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending' ), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url( '/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at' ), json=wrap_response_data(), status=200, match_querystring=True, ) # create batch rsps.add( rsps.POST, api_url('/credits/batches/'), status=201, ) # credit credits to NOMIS and API rsps.add( rsps.POST, nomis_url('/prison/BXI/offenders/A1234BC/transactions/'), status=409, ) rsps.add( rsps.POST, api_url('/credits/actions/credit/'), status=204, ) rsps.add( rsps.POST, nomis_url('/prison/BXI/offenders/A1234GG/transactions/'), json={'id': '6244780-1'}, status=200, ) rsps.add( rsps.POST, api_url('/credits/actions/credit/'), status=204, ) # REDIRECT after success # get active batches rsps.add( rsps.GET, api_url('/credits/batches/'), json=wrap_response_data(PROCESSING_BATCH), status=200, ) # get incomplete credits rsps.add( rsps.GET, api_url('/credits/?resolution=pending&pk=1&pk=2'), json=wrap_response_data(), status=200, match_querystring=True, ) # get complete credits rsps.add( rsps.GET, api_url('/credits/?resolution=credited&pk=1&pk=2'), json=wrap_response_data(CREDIT_1, CREDIT_2), status=200, match_querystring=True, ) # delete completed batch rsps.add( rsps.DELETE, api_url('/credits/batches/%s/' % PROCESSING_BATCH['id']), status=200, ) # get new credits rsps.add( rsps.GET, api_url( '/credits/?ordering=-received_at&offset=0&limit=100&status=credit_pending&resolution=pending' ), json=wrap_response_data(), status=200, match_querystring=True, ) # get manual credits rsps.add( rsps.GET, api_url( '/credits/?resolution=manual&status=credit_pending&offset=0&limit=100&ordering=-received_at' ), json=wrap_response_data(), status=200, match_querystring=True, ) self.login() with silence_logger(name='mtp', level=logging.ERROR): response = self.client.post(self.url, data={ 'credits': [1, 2], 'submit_new': None }, follow=True) self.assertEqual(response.status_code, 200) self.assertContains(response, '2 credits sent to NOMIS') self.assertEqual(len(mail.outbox), 2)