예제 #1
0
 def test_env_overrides(self, mock_settings_file, mock_env):
     get_os2sync_settings.cache_clear()
     settings = get_os2sync_settings()
     # Exists in both env and file, using env
     assert settings.municipality == env_municipality
     assert settings.os2sync_top_unit_uuid == env_uuid
     # Exists only in file
     assert settings.os2sync_api_url == file_api_url
예제 #2
0
 def test_minimal_settings_file(self, settings_mock):
     get_os2sync_settings.cache_clear()
     settings = get_os2sync_settings()
     assert settings.municipality == self.dummy_config["municipality.cvr"]
     assert (
         settings.os2sync_top_unit_uuid == self.dummy_config["os2sync_top_unit_uuid"]
     )
     assert settings.os2sync_api_url == self.dummy_config["os2sync.api_url"]
예제 #3
0
def get_mo_session():
    session = requests.Session()
    session.verify = get_os2sync_settings().os2sync_ca_verify_os2mo
    session.headers = {
        "User-Agent": "os2mo-data-import-and-export",
    }
    session_headers = TokenSettings().get_headers()
    if session_headers:
        session.headers.update(session_headers)
    return session
예제 #4
0
def os2mo_get(url, **params):
    #format url like {BASE}/service
    mora_base = get_os2sync_settings().mora_base
    
    url = url.format(BASE=f"{mora_base}/service")
    try:
        session = get_mo_session()
        r = session.get(url, params=params)
        r.raise_for_status()
        return r
    except Exception:
        logger.exception(url)
        raise
예제 #5
0
 def test_full_config(self):
     conf = {
         "municipality.cvr": "test",
         "mora.base": "http://testos2mo.dk",
         "os2sync.top_unit_uuid": uuid4(),
         "os2sync.api_url": "http://testos2sync.dk",
         "os2sync.use_lc_db": False,
         "os2sync.log_level": 1,
         "os2sync.log_file": "test.log",
         "os2sync.hash_cache": "test",
         "os2sync.xfer_cpr": True,
         "os2sync.autowash": False,
         "os2sync.ca_verify_os2sync": True,
         "os2sync.ca_verify_os2mo": True,
         "os2sync.phone_scope_classes": [uuid4(), uuid4()],
         "os2sync.email_scope_classes": [uuid4(), uuid4()],
         "os2sync.ignored.unit_levels": [uuid4(), uuid4()],
         "os2sync.ignored.unit_types": [uuid4(), uuid4()],
         "os2sync.templates": {"template": "test"},
         "os2sync.use_contact_for_tasks": False,
     }
     with patch("integrations.os2sync.config.load_settings", return_value=conf):
         get_os2sync_settings.cache_clear()
         assert get_os2sync_settings()
예제 #6
0
    prev_date = datetime.datetime.now() - datetime.timedelta(days=1)
    hash_cache_file = pathlib.Path(settings.os2sync_hash_cache)

    if hash_cache_file.exists():
        prev_date = datetime.datetime.fromtimestamp(
            hash_cache_file.stat().st_mtime)
    prev_date = prev_date.strftime("%Y-%m-%d")

    counter = collections.Counter()
    logger.info("mox_os2sync starting")
    log_mox_config(settings)

    if hash_cache_file and hash_cache_file.exists():
        os2sync.hash_cache.update(json.loads(hash_cache_file.read_text()))

    orgunit_uuids = sync_os2sync_orgunits(settings, counter, prev_date)
    sync_os2sync_users(settings, orgunit_uuids, counter, prev_date)

    if hash_cache_file:
        hash_cache_file.write_text(json.dumps(os2sync.hash_cache, indent=4))

    log_mox_counters(counter)
    log_mox_config(settings)
    logger.info("mox_os2sync done")


if __name__ == "__main__":
    settings = config.get_os2sync_settings()
    helper = MoraHelper(settings.mora_base)
    main(settings)
예제 #7
0
 def test_invalid_values(self, wrong_type_config, settings_mock):
     settings_mock.return_value.update(wrong_type_config)
     with pytest.raises(ValidationError):
         get_os2sync_settings.cache_clear()
         get_os2sync_settings()
예제 #8
0
 def test_minimal_settings_env(self, mock_settings_file, mock_env):
     get_os2sync_settings.cache_clear()
     settings = get_os2sync_settings()
     assert settings.municipality == env_municipality
     assert settings.os2sync_top_unit_uuid == env_uuid
예제 #9
0
 def test_no_settings(self, settings_mock):
     with pytest.raises(ValidationError):
         get_os2sync_settings.cache_clear()
         get_os2sync_settings()
예제 #10
0
from unittest.mock import patch

from integrations.os2sync.config import get_os2sync_settings

# Create dummy settings ignoring any settings.json file.
with patch("integrations.os2sync.config.load_settings", return_value={}):
    dummy_settings = get_os2sync_settings(
        municipality="1234",
        os2sync_top_unit_uuid="269a0339-0c8b-472d-9514-aef952a2b4df",
    )

NICKNAME_TEMPLATE = "{% if nickname -%}{{ nickname }}{%- else %}{{ name }}{%- endif %}"


class MoEmployeeMixin:
    def mock_employee(self, cpr="0101012222", nickname=False):
        # Mock the result of `os2mo_get("{BASE}/e/" + uuid + "/").json()`
        # Only contains the keys relevant for testing
        return {
            # Name
            "name": "Test Testesen",
            "givenname": "Test",
            "surname": "Testesen",
            # Nickname
            "nickname": "Kalde Navn" if nickname else "",
            "nickname_givenname": "Kalde" if nickname else "",
            "nickname_surname": "Navn" if nickname else "",
            # Other fields
            "cpr_no": cpr,
            "user_key": "testtestesen",
            "uuid": "mock-uuid",