Freebox thin client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.3 KiB

  1. #!/usr/bin/env python3
  2. import time
  3. import os
  4. import sys
  5. import requests
  6. import json
  7. import pytest
  8. from mock import (patch, PropertyMock, MagicMock)
  9. from pyfbx import Fbx
  10. from pyfbx.client import (ResponseError, Transport)
  11. from pyfbx.mdns import FbxMDNS
  12. def test_fbx_register():
  13. f = Fbx("http://192.168.1.254/api/v6")
  14. with pytest.raises(Exception):
  15. f.mksession()
  16. with patch('pyfbx.client.Transport.api_exec',
  17. side_effect=iter([
  18. {"track_id": "id", "app_token": "tok"},
  19. {"status": "pending"},
  20. {"status": "granted"}])):
  21. f.register(app_id="id", app_name="name", device="device")
  22. @pytest.mark.skipif('TEST_SKIP_LOCAL' in os.environ, reason="No freebox")
  23. def test_fbx_session_local():
  24. tokname = os.path.join(os.path.dirname(__file__), 'token.txt')
  25. with open(tokname) as tokfile:
  26. token, app_id = tokfile.read().splitlines()
  27. print("token:{}, app_id:{}".format(token, app_id))
  28. f = Fbx()
  29. f.mksession(app_id=app_id, token=token)
  30. r = f.Contacts.Create_a_contact(post_data={"display_name": "Sandy Kilo",
  31. "first_name": "Sandy",
  32. "last_name": "Kilo"})
  33. f.Contacts.Delete_a_contact(id=r['id'])
  34. with pytest.raises(ResponseError):
  35. try:
  36. f.Contacts.Access_a_given_contact_entry(1)
  37. except ResponseError as e:
  38. print(e)
  39. raise
  40. with patch('requests.Response', PropertyMock) as mock_resp:
  41. mock_resp.raise_for_status = MagicMock()
  42. mock_resp.json = MagicMock()
  43. f.Contacts.Get_a_list_of_contacts()
  44. f.mksession()
  45. def test_mdns():
  46. prop = {
  47. 'https_available': '0',
  48. 'api_base_url': '/api',
  49. 'api_version': '6.6',
  50. 'api_domain': 'example.com'}
  51. m = FbxMDNS()
  52. m.search(svc_name="_foobar._tcp.local.")
  53. with patch('pyfbx.mdns.FbxMDNS.svc_prop', new_callable=PropertyMock) as mock_prop:
  54. mock_prop.return_value = prop
  55. m.search(svc_name="_foobar._tcp.local.")
  56. m.search()
  57. def test_nonet():
  58. with patch('pyfbx.client.Transport._session', new_callable=PropertyMock, return_value=MagicMock(), create=True) as mock_session:
  59. f = Fbx("http://192.168.12.34/api/v9")
  60. f.System.Reboot_the_system()
  61. f = Fbx()