about summary refs log tree commit diff
path: root/test/python/test_dataset_probeset_values.py
blob: 7a934d0942cb9a218b5e96e7c7df125536c92628 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""Mirrors tests/dataset_probeset_values.rs. Needs a live MariaDB
(SQL_URI); skips with a message rather than failing when none is
reachable, same as test_group_by_name.py. Also needs a live gn-auth
(AUTH_SERVER_URL) and a token (see docs/gn-auth.md), since this
endpoint now requires a bearer token; skips when either is missing.
auth_token() tries this server's own POST /get-test-token first
(EXTRA_ENV below makes that possible), falling back to
GN4_TEST_AUTH_TOKEN.
"""

import http.client
import json
import socket
import unittest

from auth_helpers import TOKEN_ENV, auth_server_reachable, auth_token
from server_test_case import HOST, PORT, ServerTestCase


def database_reachable():
    try:
        with socket.create_connection(("127.0.0.1", 3306), timeout=0.5):
            return True
    except OSError:
        return False


class DatasetProbesetValuesTest(ServerTestCase):
    EXTRA_ENV = TOKEN_ENV

    def require_fixtures(self):
        if not database_reachable():
            self.skipTest("no database on 127.0.0.1:3306")
        if not auth_token():
            self.skipTest("no token (see docs/gn-auth.md)")
        if not auth_server_reachable():
            self.skipTest("no gn-auth reachable")

    def get(self, path, with_token=True):
        conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10)
        try:
            headers = {"Authorization": f"Bearer {auth_token()}"} if with_token else {}
            conn.request("GET", path, headers=headers)
            response = conn.getresponse()
            return response, response.read()
        finally:
            conn.close()

    def test_dataset_probeset_values_returns_strain_values(self):
        self.require_fixtures()

        response, body = self.get("/datasets/Br_U_0803_M/probesets/100001_at/values")
        self.assertEqual(response.status, 200)

        values = json.loads(body)
        self.assertTrue(values)
        bxd1 = next(v for v in values if v["name"] == "BXD1")
        self.assertIsInstance(bxd1["value"], float)

    def test_dataset_probeset_values_without_a_token_returns_401(self):
        if not database_reachable():
            self.skipTest("no database on 127.0.0.1:3306")

        response, _body = self.get(
            "/datasets/Br_U_0803_M/probesets/100001_at/values", with_token=False
        )
        self.assertEqual(response.status, 401)

    def test_dataset_probeset_values_unknown_dataset_returns_404(self):
        self.require_fixtures()

        response, body = self.get(
            "/datasets/not-a-real-dataset/probesets/100001_at/values"
        )
        self.assertEqual(response.status, 404)

        error = json.loads(body)
        self.assertEqual(error["status"], 404)
        self.assertEqual(error["code"], "dataset_not_found")
        self.assertEqual(error["operation_id"], "dataset_probeset_values")

    def test_dataset_probeset_values_unknown_probeset_in_dataset_returns_404(self):
        self.require_fixtures()

        response, body = self.get(
            "/datasets/Br_U_0803_M/probesets/not-a-real-probeset/values"
        )
        self.assertEqual(response.status, 404)

        error = json.loads(body)
        self.assertEqual(error["status"], 404)
        self.assertEqual(error["code"], "probeset_not_found")
        self.assertEqual(error["operation_id"], "dataset_probeset_values")

    def test_dataset_probeset_values_non_public_unlinked_dataset_returns_403(self):
        """UTHSC_BXD_Cochlea_0225 (public=1, confidentiality=1, created
        2025-02-13) is neither publicly readable by the flags nor old
        enough for the embargo-age rule, and has never been linked to a
        gn-auth resource -- so even the token's owner has no privilege
        on it.
        """
        self.require_fixtures()

        response, body = self.get(
            "/datasets/UTHSC_BXD_Cochlea_0225/probesets/ENSMUSG00000028180/values"
        )
        self.assertEqual(response.status, 403)

        error = json.loads(body)
        self.assertEqual(error["code"], "forbidden")


if __name__ == "__main__":
    unittest.main()