|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +from app import create_app # Assuming your Flask app is created in app.py |
| 4 | +from models.payment import PaymentStatus |
| 5 | + |
| 6 | +class TestPaymentController(unittest.TestCase): |
| 7 | + def setUp(self): |
| 8 | + self.app = create_app('testing') # Use a testing configuration |
| 9 | + self.client = self.app.test_client() |
| 10 | + |
| 11 | + @patch('services.coinbase_service.CoinbaseService.create_payment') |
| 12 | + def test_create_payment(self, mock_create_payment): |
| 13 | + mock_create_payment.return_value = { |
| 14 | + 'id': 'mock_payment_id', |
| 15 | + 'status': 'pending' |
| 16 | + } |
| 17 | + response = self.client.post('/api/payments', json={ |
| 18 | + 'method': 'coinbase', |
| 19 | + 'amount': 100.00, |
| 20 | + 'currency': 'USD' |
| 21 | + }) |
| 22 | + self.assertEqual(response.status_code, 201) |
| 23 | + self.assertIn('mock_payment_id', response.get_data(as_text=True)) |
| 24 | + |
| 25 | + @patch('services.coinbase_service.CoinbaseService.get_payment_status') |
| 26 | + def test_get_payment_status(self, mock_get_payment_status): |
| 27 | + mock_get_payment_status.return_value = { |
| 28 | + 'id': 'mock_payment_id', |
| 29 | + 'status': PaymentStatus.COMPLETED.value |
| 30 | + } |
| 31 | + response = self.client.get('/api/payments/mock_payment_id') |
| 32 | + self.assertEqual(response.status_code, 200) |
| 33 | + self.assertIn(PaymentStatus.COMPLETED.value, response.get_data(as_text=True)) |
| 34 | + |
| 35 | + def test_create_payment_invalid(self): |
| 36 | + response = self.client.post('/api/payments', json={}) |
| 37 | + self.assertEqual(response.status_code, 400) |
| 38 | + self.assertIn('Missing required fields', response.get_data(as_text=True)) |
| 39 | + |
| 40 | +if __name__ == '__main__': |
| 41 | + unittest.main() |
0 commit comments