|
| 1 | +# |
| 2 | +# Gramps Web API - A RESTful API for the Gramps genealogy program |
| 3 | +# |
| 4 | +# Copyright (C) 2024 David Straub |
| 5 | +# |
| 6 | +# This program is free software; you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Affero General Public License as published by |
| 8 | +# the Free Software Foundation; either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Affero General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Affero General Public License |
| 17 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | +# |
| 19 | + |
| 20 | +"""Tests for the database repair endpoint.""" |
| 21 | + |
| 22 | +import os |
| 23 | +import unittest |
| 24 | +from unittest.mock import patch |
| 25 | + |
| 26 | +from gramps.cli.clidbman import CLIDbManager |
| 27 | +from gramps.gen.dbstate import DbState |
| 28 | + |
| 29 | +from gramps_webapi.app import create_app |
| 30 | +from gramps_webapi.auth import add_user, user_db |
| 31 | +from gramps_webapi.auth.const import ROLE_GUEST, ROLE_OWNER |
| 32 | +from gramps_webapi.const import ENV_CONFIG_FILE, TEST_AUTH_CONFIG |
| 33 | + |
| 34 | + |
| 35 | +class TestRepair(unittest.TestCase): |
| 36 | + """Test database repair.""" |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def setUpClass(cls): |
| 40 | + cls.name = "Test Web API Repair" |
| 41 | + cls.dbman = CLIDbManager(DbState()) |
| 42 | + dirpath, _ = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite") |
| 43 | + tree = os.path.basename(dirpath) |
| 44 | + with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}): |
| 45 | + cls.app = create_app() |
| 46 | + cls.app.config["TESTING"] = True |
| 47 | + cls.client = cls.app.test_client() |
| 48 | + with cls.app.app_context(): |
| 49 | + user_db.create_all() |
| 50 | + add_user(name="user", password="123", role=ROLE_GUEST, tree=tree) |
| 51 | + add_user(name="owner", password="123", role=ROLE_OWNER, tree=tree) |
| 52 | + rv = cls.client.post( |
| 53 | + "/api/token/", json={"username": "owner", "password": "123"} |
| 54 | + ) |
| 55 | + access_token = rv.json["access_token"] |
| 56 | + cls.headers = {"Authorization": f"Bearer {access_token}"} |
| 57 | + |
| 58 | + @classmethod |
| 59 | + def tearDownClass(cls): |
| 60 | + cls.dbman.remove_database(cls.name) |
| 61 | + |
| 62 | + def test_repair_empty_database(self): |
| 63 | + """Test Repairing the empty database.""" |
| 64 | + rv = self.client.post("/api/trees/-/repair", headers=self.headers) |
| 65 | + assert rv.status_code == 201 |
| 66 | + assert rv.json["num_errors"] == 0 |
| 67 | + assert rv.json["message"] == "" |
| 68 | + |
| 69 | + def test_repair_empty_person(self): |
| 70 | + """Test Repairing an empty person.""" |
| 71 | + rv = self.client.post("/api/people/", json={}, headers=self.headers) |
| 72 | + assert rv.status_code == 201 |
| 73 | + rv = self.client.get("/api/people/", headers=self.headers) |
| 74 | + assert rv.status_code == 200 |
| 75 | + assert len(rv.json) == 1 |
| 76 | + rv = self.client.post("/api/trees/-/repair", headers=self.headers) |
| 77 | + assert rv.status_code == 201 |
| 78 | + assert rv.json["num_errors"] == 1 |
| 79 | + rv = self.client.get("/api/people/", headers=self.headers) |
| 80 | + assert rv.status_code == 200 |
| 81 | + assert len(rv.json) == 0 |
| 82 | + |
| 83 | + def test_repair_empty_event(self): |
| 84 | + """Test Repairing an empty event.""" |
| 85 | + rv = self.client.post("/api/events/", json={}, headers=self.headers) |
| 86 | + assert rv.status_code == 201 |
| 87 | + rv = self.client.get("/api/events/", headers=self.headers) |
| 88 | + assert rv.status_code == 200 |
| 89 | + assert len(rv.json) == 1 |
| 90 | + rv = self.client.post("/api/trees/-/repair", headers=self.headers) |
| 91 | + assert rv.status_code == 201 |
| 92 | + assert rv.json["num_errors"] == 1 |
| 93 | + rv = self.client.get("/api/events/", headers=self.headers) |
| 94 | + assert rv.status_code == 200 |
| 95 | + assert len(rv.json) == 0 |
0 commit comments