test_rest_config.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import os
  12. from unittest.mock import patch
  13. import pytest
  14. from taipy.common.config.config import Config
  15. from taipy.common.config.exceptions import MissingEnvVariableError
  16. from taipy.rest.config.rest_config import RestConfig
  17. from tests.core.utils.named_temporary_file import NamedTemporaryFile
  18. def test_rest_config_no_values():
  19. assert Config.rest.port == 5000
  20. assert Config.rest.host == "127.0.0.1"
  21. assert Config.rest.use_https is False
  22. assert Config.rest.ssl_cert is None
  23. assert Config.rest.ssl_key is None
  24. def test_rest_config_default_values():
  25. Config.configure_rest()
  26. assert Config.rest.port == RestConfig._DEFAULT_PORT
  27. assert Config.rest.host == RestConfig._DEFAULT_HOST
  28. assert Config.rest.use_https is RestConfig._DEFAULT_USE_HTTPS
  29. assert Config.rest.ssl_cert is RestConfig._DEFAULT_SSL_CERT
  30. assert Config.rest.ssl_key is RestConfig._DEFAULT_SSL_KEY
  31. def test_rest_config_only_part_of_custom_values():
  32. Config.configure_rest(
  33. use_https=True,
  34. ssl_cert="cert.pem",
  35. ssl_key="key.pem"
  36. )
  37. assert Config.rest.port == RestConfig._DEFAULT_PORT
  38. assert Config.rest.host == RestConfig._DEFAULT_HOST
  39. assert Config.rest.use_https is True
  40. assert Config.rest.ssl_cert == "cert.pem"
  41. assert Config.rest.ssl_key == "key.pem"
  42. def test_rest_config_custom_values_and_toml_override():
  43. # We override some default values with the Python API
  44. Config.configure_rest(
  45. port=8080,
  46. host="0.0.0.0",
  47. )
  48. assert Config.rest.port == 8080
  49. assert Config.rest.host == "0.0.0.0"
  50. assert Config.rest.use_https is RestConfig._DEFAULT_USE_HTTPS
  51. assert Config.rest.ssl_cert is RestConfig._DEFAULT_SSL_CERT
  52. assert Config.rest.ssl_key is RestConfig._DEFAULT_SSL_KEY
  53. # now we load a toml file
  54. toml_cfg = NamedTemporaryFile(
  55. content="""
  56. [TAIPY]
  57. [REST]
  58. port = 2
  59. host = "192.168.0.87"
  60. use_https = "true:bool"
  61. ssl_cert = "cert.pem"
  62. ssl_key = "key.pem"
  63. """
  64. )
  65. Config.load(toml_cfg.filename)
  66. assert Config.rest.port == 2
  67. assert Config.rest.host == "192.168.0.87"
  68. assert Config.rest.use_https is True
  69. assert Config.rest.ssl_cert == "cert.pem"
  70. assert Config.rest.ssl_key == "key.pem"
  71. def test_rest_config_custom_values_and_missing_env_var_override():
  72. #we use env variables
  73. Config.configure_rest(
  74. port="ENV[PORT]:int",
  75. host="ENV[HOST]",
  76. ssl_cert="ENV[SSL_CERT]",
  77. ssl_key="ENV[SSL_KEY]"
  78. )
  79. Config.rest.use_https = "ENV[USE_HTTPS]"
  80. with pytest.raises(MissingEnvVariableError):
  81. _ = Config.rest.port
  82. with pytest.raises(MissingEnvVariableError):
  83. _ = Config.rest.host
  84. with pytest.raises(MissingEnvVariableError):
  85. _ = Config.rest.use_https
  86. with pytest.raises(MissingEnvVariableError):
  87. _ = Config.rest.ssl_cert
  88. with pytest.raises(MissingEnvVariableError):
  89. _ = Config.rest.ssl_key
  90. def test_rest_config_custom_values_and_env_var_override():
  91. with patch.dict(os.environ, {
  92. "PORT": "3",
  93. "HOST": "1.2.3.4",
  94. "USE_HTTPS": "true",
  95. "SSL_CERT": "cert.pem",
  96. "SSL_KEY": "key.pem"
  97. }):
  98. # we use env variables
  99. Config.configure_rest(
  100. port="ENV[PORT]:int",
  101. host="ENV[HOST]",
  102. use_https="ENV[USE_HTTPS]:bool",
  103. ssl_cert="ENV[SSL_CERT]",
  104. ssl_key="ENV[SSL_KEY]"
  105. )
  106. assert Config.rest.port == 3
  107. assert Config.rest.host == "1.2.3.4"
  108. assert Config.rest.use_https is True
  109. assert Config.rest.ssl_cert == "cert.pem"
  110. assert Config.rest.ssl_key == "key.pem"
  111. def test_rest_config_copy():
  112. rest_config = Config.configure_rest(
  113. port=8080, host="0.0.0.0", use_https=True, ssl_cert="cert.pem", ssl_key="key.pem"
  114. )
  115. rest_config_copy = rest_config.__copy__()
  116. assert rest_config_copy.port == 8080
  117. assert rest_config_copy.host == "0.0.0.0"
  118. assert rest_config_copy.use_https is True
  119. assert rest_config_copy.ssl_cert == "cert.pem"
  120. assert rest_config_copy.ssl_key == "key.pem"
  121. # Ensure it's a deep copy
  122. rest_config_copy.port = 9090
  123. assert rest_config.port == 8080
  124. def test_rest_default_config_is_valid():
  125. issues = Config.check()
  126. assert len(issues.errors) == 0
  127. assert len(issues.warnings) == 0
  128. assert len(issues.infos) == 0
  129. def test_rest_config_checker_valid_config():
  130. Config.configure_rest(port=8080, host="0.0.0.0", use_https=True, ssl_cert="cert.pem", ssl_key="key.pem")
  131. issues = Config.check()
  132. assert len(issues.errors) == 0
  133. assert len(issues.warnings) == 0
  134. assert len(issues.infos) == 0
  135. def test_rest_config_checker_invalid_port_and_host():
  136. Config.configure_rest(port=70000, host="") # Invalid port and host
  137. with pytest.raises(SystemExit):
  138. Config.check()
  139. issues = Config._collector
  140. assert len(issues.errors) == 2
  141. assert len(issues.warnings) == 0
  142. assert len(issues.infos) == 0
  143. assert "port" in issues.errors[0].field
  144. assert "host" in issues.errors[1].field
  145. def test_rest_config_checker_https_missing_cert_and_key():
  146. Config.configure_rest(use_https=True) # Missing ssl_cert and ssl_key
  147. with pytest.raises(SystemExit):
  148. Config.check()
  149. issues = Config._collector
  150. assert len(issues.errors) == 2
  151. assert len(issues.warnings) == 0
  152. assert len(issues.infos) == 0
  153. assert "ssl_cert" in issues.errors[0].field
  154. assert "ssl_key" in issues.errors[1].field
  155. def test_rest_config_checker_https_invalid_cert_and_key():
  156. Config.configure_rest(use_https=True, ssl_cert=123, ssl_key=456) # Invalid types for ssl_cert and ssl_key
  157. with pytest.raises(SystemExit):
  158. Config.check()
  159. issues = Config._collector
  160. assert len(issues.errors) == 2
  161. assert len(issues.warnings) == 0
  162. assert len(issues.infos) == 0
  163. assert "ssl_cert" in issues.errors[0].field
  164. assert "ssl_key" in issues.errors[1].field