|
| 1 | +# SPDX-FileCopyrightText: Fondation RERO+ |
| 2 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 3 | + |
| 4 | +"""Test deposits JSON schema.""" |
| 5 | + |
| 6 | +from sonar.jsonschemas.deposits_json_schema import DepositsJSONSchema |
| 7 | + |
| 8 | + |
| 9 | +class MockUserRecord: |
| 10 | + """Mock user record exposing only what DepositsJSONSchema.process() needs.""" |
| 11 | + |
| 12 | + is_moderator = False |
| 13 | + |
| 14 | + def __init__(self, organisation): |
| 15 | + """Store the organisation to return.""" |
| 16 | + self.organisation = organisation |
| 17 | + |
| 18 | + def replace_refs(self): |
| 19 | + """Return self, no reference to resolve in this mock.""" |
| 20 | + return self |
| 21 | + |
| 22 | + def get(self, key): |
| 23 | + """Return the mocked organisation.""" |
| 24 | + return self.organisation if key == "organisation" else None |
| 25 | + |
| 26 | + |
| 27 | +def test_process_keeps_new_project_option_by_default(app, monkeypatch): |
| 28 | + """Test the "Add a new project" option is kept when no organisation is restricted.""" |
| 29 | + app.config["DEPOSITS_DISABLE_NEW_PROJECT_ORGANISATIONS"] = [] |
| 30 | + monkeypatch.setattr( |
| 31 | + "sonar.jsonschemas.deposits_json_schema.current_user_record", |
| 32 | + MockUserRecord({"code": "hepvs"}), |
| 33 | + ) |
| 34 | + |
| 35 | + schema = DepositsJSONSchema("deposits").process() |
| 36 | + |
| 37 | + assert len(schema["properties"]["projects"]["items"]["oneOf"]) == 2 |
| 38 | + |
| 39 | + |
| 40 | +def test_process_removes_new_project_option_for_restricted_organisation(app, monkeypatch): |
| 41 | + """Test the "Add a new project" option is removed for a restricted organisation.""" |
| 42 | + app.config["DEPOSITS_DISABLE_NEW_PROJECT_ORGANISATIONS"] = ["hepvs"] |
| 43 | + monkeypatch.setattr( |
| 44 | + "sonar.jsonschemas.deposits_json_schema.current_user_record", |
| 45 | + MockUserRecord({"code": "hepvs"}), |
| 46 | + ) |
| 47 | + |
| 48 | + schema = DepositsJSONSchema("deposits").process() |
| 49 | + |
| 50 | + one_of = schema["properties"]["projects"]["items"]["oneOf"] |
| 51 | + assert len(one_of) == 1 |
| 52 | + assert one_of[0]["title"] == "Existing project" |
| 53 | + |
| 54 | + |
| 55 | +def test_process_keeps_new_project_option_for_other_organisation(app, monkeypatch): |
| 56 | + """Test the "Add a new project" option is kept for a non-restricted organisation.""" |
| 57 | + app.config["DEPOSITS_DISABLE_NEW_PROJECT_ORGANISATIONS"] = ["hepvs"] |
| 58 | + monkeypatch.setattr( |
| 59 | + "sonar.jsonschemas.deposits_json_schema.current_user_record", |
| 60 | + MockUserRecord({"code": "other"}), |
| 61 | + ) |
| 62 | + |
| 63 | + schema = DepositsJSONSchema("deposits").process() |
| 64 | + |
| 65 | + assert len(schema["properties"]["projects"]["items"]["oneOf"]) == 2 |
0 commit comments