test_credentials.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import os
  2. import tempfile
  3. import pytest
  4. from room_cal.service_api.credentials import check_credentials, set_credentials
  5. @pytest.fixture(autouse=True)
  6. def isolated_data_home(tmp_path, monkeypatch):
  7. monkeypatch.setenv('ROOM_CAL_DATA_HOME', str(tmp_path))
  8. def test_set_credentials_single_key(tmp_path):
  9. set_credentials('airbnb', {'AIRBNB_COOKIES': 'bev=abc; cdn=xyz'})
  10. text = (tmp_path / 'airbnb.env').read_text()
  11. assert 'AIRBNB_COOKIES=bev=abc; cdn=xyz' in text
  12. assert 'AIRBNB_UPDATED_AT=' in text
  13. def test_set_credentials_multi_key(tmp_path):
  14. set_credentials('booking', {
  15. 'BOOKING_COOKIES': 'bui=1',
  16. 'BOOKING_SESSION': 'ses123',
  17. 'BOOKING_CSRF_TOKEN': 'csrf456',
  18. })
  19. text = (tmp_path / 'booking.env').read_text()
  20. assert 'BOOKING_COOKIES=bui=1' in text
  21. assert 'BOOKING_SESSION=ses123' in text
  22. assert 'BOOKING_CSRF_TOKEN=csrf456' in text
  23. assert 'BOOKING_UPDATED_AT=' in text
  24. def test_set_credentials_upserts_in_place(tmp_path):
  25. (tmp_path / 'airbnb.env').write_text('AIRBNB_LISTING_ID=99\nAIRBNB_COOKIES=old\n')
  26. set_credentials('airbnb', {'AIRBNB_COOKIES': 'new-cookie'})
  27. text = (tmp_path / 'airbnb.env').read_text()
  28. assert 'AIRBNB_COOKIES=new-cookie' in text
  29. assert 'old' not in text
  30. assert 'AIRBNB_LISTING_ID=99' in text
  31. def test_set_credentials_updates_timestamp_on_refresh(tmp_path):
  32. set_credentials('airbnb', {'AIRBNB_COOKIES': 'v1'})
  33. first = (tmp_path / 'airbnb.env').read_text()
  34. set_credentials('airbnb', {'AIRBNB_COOKIES': 'v2'})
  35. second = (tmp_path / 'airbnb.env').read_text()
  36. # Only one AIRBNB_UPDATED_AT line
  37. assert second.count('AIRBNB_UPDATED_AT=') == 1
  38. assert 'AIRBNB_COOKIES=v2' in second
  39. def test_check_credentials_missing_file(tmp_path):
  40. result = check_credentials('airbnb')
  41. assert result['valid'] is False
  42. assert result['age_days'] is None
  43. assert 'not found' in result['reason']
  44. def test_check_credentials_no_timestamp(tmp_path):
  45. (tmp_path / 'airbnb.env').write_text('AIRBNB_COOKIES=abc\n')
  46. result = check_credentials('airbnb')
  47. assert result['valid'] is True
  48. assert result['age_days'] is None
  49. assert 'timestamp' in result['reason']
  50. def test_check_credentials_with_timestamp(tmp_path):
  51. set_credentials('airbnb', {'AIRBNB_COOKIES': 'abc'})
  52. result = check_credentials('airbnb')
  53. assert result['valid'] is True
  54. assert result['age_days'] == 0
  55. assert 'refreshed' in result['reason']