push
This commit is contained in:
108
tests/test_settings_ksef.py
Normal file
108
tests/test_settings_ksef.py
Normal file
@@ -0,0 +1,108 @@
|
||||
import base64
|
||||
from io import BytesIO
|
||||
|
||||
from app.models.company import Company
|
||||
from app.models.setting import AppSetting
|
||||
from app.services.ksef_service import RequestsKSeFAdapter
|
||||
|
||||
|
||||
def test_save_ksef_token_and_certificate(auth_client, app):
|
||||
response = auth_client.post(
|
||||
'/settings/',
|
||||
data={
|
||||
'ksef-base_url': ' https://api.ksef.mf.gov.pl ',
|
||||
'ksef-auth_mode': 'token',
|
||||
'ksef-client_id': ' client-1 ',
|
||||
'ksef-token': ' secret-token ',
|
||||
'ksef-submit': '1',
|
||||
},
|
||||
content_type='multipart/form-data',
|
||||
follow_redirects=True,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
certificate_bytes = b"""-----BEGIN CERTIFICATE-----
|
||||
TEST
|
||||
-----END CERTIFICATE-----
|
||||
"""
|
||||
response = auth_client.post(
|
||||
'/settings/',
|
||||
data={
|
||||
'ksef-base_url': 'https://api.ksef.mf.gov.pl/docs/v2',
|
||||
'ksef-auth_mode': 'certificate',
|
||||
'ksef-client_id': 'client-2',
|
||||
'ksef-token': '',
|
||||
'ksef-certificate_file': (BytesIO(certificate_bytes), 'cert.pem'),
|
||||
'ksef-submit': '1',
|
||||
},
|
||||
content_type='multipart/form-data',
|
||||
follow_redirects=True,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
with app.app_context():
|
||||
company = Company.query.first()
|
||||
prefix = f'company.{company.id}.ksef'
|
||||
assert AppSetting.get(f'{prefix}.base_url') == 'https://api.ksef.mf.gov.pl/docs/v2'
|
||||
assert AppSetting.get(f'{prefix}.auth_mode') == 'certificate'
|
||||
assert AppSetting.get(f'{prefix}.client_id') == 'client-2'
|
||||
assert AppSetting.get(f'{prefix}.token', decrypt=True) == 'secret-token'
|
||||
assert AppSetting.get(f'{prefix}.certificate_name') == 'cert.pem'
|
||||
stored = AppSetting.get(f'{prefix}.certificate_data', decrypt=True)
|
||||
assert base64.b64decode(stored) == certificate_bytes
|
||||
|
||||
|
||||
def test_ksef_settings_page_shows_saved_status(auth_client, app):
|
||||
with app.app_context():
|
||||
company = Company.query.first()
|
||||
AppSetting.set(f'company.{company.id}.ksef.token', 'abc', encrypt=True)
|
||||
AppSetting.set(f'company.{company.id}.ksef.certificate_name', 'cert.pem')
|
||||
AppSetting.set(f'company.{company.id}.ksef.certificate_data', 'Y2VydA==', encrypt=True)
|
||||
AppSetting.set(f'company.{company.id}.ksef.mock_mode', 'true')
|
||||
from app.extensions import db
|
||||
db.session.commit()
|
||||
|
||||
response = auth_client.get('/settings/')
|
||||
body = response.get_data(as_text=True)
|
||||
assert response.status_code == 200
|
||||
assert 'Token KSeF jest zapisany w konfiguracji tej firmy.' in body
|
||||
assert 'Certyfikat KSeF jest zapisany w konfiguracji tej firmy.' in body
|
||||
assert 'Wgrany plik: cert.pem' in body
|
||||
|
||||
|
||||
def test_requests_adapter_uses_supported_invoice_endpoints(app, monkeypatch):
|
||||
calls = []
|
||||
|
||||
def fake_request(self, method, path, params=None, json=None, accept='application/json'):
|
||||
calls.append((method, path, params, json, accept))
|
||||
if path == '/invoices/query/metadata':
|
||||
return {
|
||||
'invoices': [
|
||||
{
|
||||
'ksefNumber': '5555555555-20250828-010080615740-E4',
|
||||
'invoiceNumber': 'FV/1',
|
||||
'issueDate': '2025-08-27',
|
||||
'acquisitionDate': '2025-08-28T09:22:56+00:00',
|
||||
'seller': {'nip': '5555555555', 'name': 'Test Company 1'},
|
||||
'netAmount': 100,
|
||||
'vatAmount': 23,
|
||||
'grossAmount': 123,
|
||||
}
|
||||
]
|
||||
}
|
||||
if path == '/invoices/ksef/5555555555-20250828-010080615740-E4':
|
||||
return '<Invoice/>'
|
||||
raise AssertionError(path)
|
||||
|
||||
with app.app_context():
|
||||
company = Company.query.first()
|
||||
AppSetting.set(f'company.{company.id}.ksef.mock_mode', 'false')
|
||||
from app.extensions import db
|
||||
db.session.commit()
|
||||
monkeypatch.setattr(RequestsKSeFAdapter, '_request', fake_request)
|
||||
documents = RequestsKSeFAdapter(company_id=company.id).list_documents()
|
||||
|
||||
assert len(documents) == 1
|
||||
assert calls[0][1] == '/invoices/query/metadata'
|
||||
assert calls[1][1] == '/invoices/ksef/5555555555-20250828-010080615740-E4'
|
||||
assert calls[1][4] == 'application/xml'
|
||||
Reference in New Issue
Block a user