|
| 1 | +# |
| 2 | +# Gramps Web API - A RESTful API for the Gramps genealogy program |
| 3 | +# |
| 4 | +# Copyright (C) 2025 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 /people/<handle>/ydna/ endpoint.""" |
| 21 | + |
| 22 | +import os |
| 23 | +import unittest |
| 24 | +import uuid |
| 25 | +from unittest.mock import patch |
| 26 | + |
| 27 | +from gramps.cli.clidbman import CLIDbManager |
| 28 | +from gramps.gen.dbstate import DbState |
| 29 | + |
| 30 | +from gramps_webapi.app import create_app |
| 31 | +from gramps_webapi.auth import add_user, user_db |
| 32 | +from gramps_webapi.auth.const import ROLE_GUEST, ROLE_OWNER |
| 33 | +from gramps_webapi.const import ENV_CONFIG_FILE, TEST_AUTH_CONFIG |
| 34 | + |
| 35 | + |
| 36 | +def get_headers(client, user: str, password: str): |
| 37 | + rv = client.post("/api/token/", json={"username": user, "password": password}) |
| 38 | + access_token = rv.json["access_token"] |
| 39 | + return {"Authorization": f"Bearer {access_token}"} |
| 40 | + |
| 41 | + |
| 42 | +def make_handle(): |
| 43 | + return str(uuid.uuid4()) |
| 44 | + |
| 45 | + |
| 46 | +class TestYDnaEndpoint(unittest.TestCase): |
| 47 | + @classmethod |
| 48 | + def setUpClass(cls): |
| 49 | + cls.name = "Test Web API YDNA" |
| 50 | + cls.dbman = CLIDbManager(DbState()) |
| 51 | + dbpath, _ = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite") |
| 52 | + tree = os.path.basename(dbpath) |
| 53 | + with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}): |
| 54 | + cls.app = create_app(config_from_env=False) |
| 55 | + cls.app.config["TESTING"] = True |
| 56 | + cls.client = cls.app.test_client() |
| 57 | + with cls.app.app_context(): |
| 58 | + user_db.create_all() |
| 59 | + add_user(name="user", password="123", role=ROLE_GUEST, tree=tree) |
| 60 | + add_user(name="admin", password="123", role=ROLE_OWNER, tree=tree) |
| 61 | + |
| 62 | + @classmethod |
| 63 | + def tearDownClass(cls): |
| 64 | + cls.dbman.remove_database(cls.name) |
| 65 | + |
| 66 | + def test_without_token(self): |
| 67 | + rv = self.client.get("/api/people/nope/ydna") |
| 68 | + assert rv.status_code == 401 |
| 69 | + |
| 70 | + def test_person_not_found(self): |
| 71 | + headers = get_headers(self.client, "user", "123") |
| 72 | + rv = self.client.get("/api/people/nope/ydna", headers=headers) |
| 73 | + assert rv.status_code == 404 |
| 74 | + |
| 75 | + def test_no_ydna(self): |
| 76 | + headers = get_headers(self.client, "admin", "123") |
| 77 | + person = { |
| 78 | + "primary_name": { |
| 79 | + "surname_list": [{"_class": "Surname", "surname": "Doe"}], |
| 80 | + "first_name": "John", |
| 81 | + }, |
| 82 | + "gender": 1, |
| 83 | + } |
| 84 | + rv = self.client.post("/api/people/", json=person, headers=headers) |
| 85 | + assert rv.status_code == 201 |
| 86 | + handle = rv.json[0]["handle"] |
| 87 | + rv = self.client.get(f"/api/people/{handle}/ydna", headers=headers) |
| 88 | + assert rv.status_code == 200 |
| 89 | + assert rv.json == {} |
| 90 | + |
| 91 | + def test_with_ydna(self): |
| 92 | + headers = get_headers(self.client, "admin", "123") |
| 93 | + handle = make_handle() |
| 94 | + ydna_string = "M269+, CTS1078+, P312+, U106-, L21-, Z290-, Z2103+, FGC3845-" |
| 95 | + person = { |
| 96 | + "_class": "Person", |
| 97 | + "handle": handle, |
| 98 | + "primary_name": { |
| 99 | + "_class": "Name", |
| 100 | + "surname_list": [{"_class": "Surname", "surname": "Smith"}], |
| 101 | + "first_name": "Adam", |
| 102 | + }, |
| 103 | + "gender": 1, |
| 104 | + "attribute_list": [ |
| 105 | + {"_class": "Attribute", "type": "Y-DNA", "value": ydna_string} |
| 106 | + ], |
| 107 | + } |
| 108 | + rv = self.client.post("/api/objects/", json=[person], headers=headers) |
| 109 | + assert rv.status_code == 201 |
| 110 | + rv = self.client.get(f"/api/people/{handle}/ydna", headers=headers) |
| 111 | + assert rv.status_code == 200 |
| 112 | + assert "clade_lineage" in rv.json |
| 113 | + assert all( |
| 114 | + "name" in item and "age_info" in item for item in rv.json["clade_lineage"] |
| 115 | + ) |
| 116 | + assert "raw_data" not in rv.json |
| 117 | + rv = self.client.get(f"/api/people/{handle}/ydna?raw=1", headers=headers) |
| 118 | + assert "raw_data" in rv.json |
| 119 | + assert rv.json["raw_data"] == ydna_string |
0 commit comments