okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0215-accesskey: launch: a key that only the Access step's highlight made goes again on "This computer only" (#5812)3235191e2

main
3.4 KiB90 linesraw
1"""#5812: on the launch's Access step, moving the highlight past "This computer only" made an API key, and nothing took it back when the
2highlight went back: a launch that ended on this computer served with a key while the screen said none is needed there."""
3
4import unittest
5from types import SimpleNamespace
6from unittest import mock
7
8try:
9    from strata_cli.launch import screens
10except ImportError:                                   # the optional Textual dependency is absent
11    screens = None
12
13
14class FakeAccess:
15    """The parts of the Access screen its key logic uses: the highlighted choice, the launch settings and the status line."""
16
17    def __init__(self, api_key=None, no_key=False):
18        self.ctx = SimpleNamespace(settings=SimpleNamespace(access="local", api_key=api_key, no_key=no_key))
19        self.key = "local"
20        self.said = []
21
22    def menu(self):
23        return SimpleNamespace(current=SimpleNamespace(key=self.key))
24
25    def say(self, text, warn=False):
26        self.said.append(text)
27
28    def drop_cursor_key(self):
29        return screens.AccessScreen.drop_cursor_key(self)
30
31    def build(self):
32        pass
33
34    def paint(self):
35        pass
36
37
38@unittest.skipIf(screens is None, "optional Textual dependency absent")
39class CursorKey(unittest.TestCase):
40    def setUp(self):
41        for name, value in (("kept_key", None), ("new_key", "sk-made-by-the-cursor")):
42            patch = mock.patch.object(screens.acc, name, return_value=value)
43            patch.start()
44            self.addCleanup(patch.stop)
45
46    def move(self, screen, kind):
47        screen.key = kind
48        screens.AccessScreen.on_cursor(screen)
49
50    def test_reading_the_choices_and_ending_on_this_computer_leaves_no_key(self):
51        screen = FakeAccess()
52        screens.AccessScreen.on_mount(screen)
53        self.move(screen, "lan")
54        self.assertEqual(screen.ctx.settings.api_key, "sk-made-by-the-cursor")      # beyond this computer: a key, as before
55        self.move(screen, "tailscale")
56        self.move(screen, "local")
57        self.assertIsNone(screen.ctx.settings.api_key)
58        screens.AccessScreen.commit(screen)
59        self.assertEqual((screen.ctx.settings.access, screen.ctx.settings.api_key), ("local", None))
60
61    def test_a_key_the_launch_already_had_stays(self):
62        screen = FakeAccess(api_key="sk-given-with-api-key")
63        screens.AccessScreen.on_mount(screen)
64        self.move(screen, "lan")
65        self.move(screen, "local")
66        screens.AccessScreen.commit(screen)
67        self.assertEqual(screen.ctx.settings.api_key, "sk-given-with-api-key")
68
69    def test_a_key_the_person_made_on_the_step_stays(self):
70        screen = FakeAccess()
71        screens.AccessScreen.on_mount(screen)
72        self.move(screen, "lan")
73        with mock.patch.object(screens.acc, "new_key", return_value="sk-pressed-k"):
74            screens.AccessScreen.action_newkey(screen)
75        self.move(screen, "local")
76        screens.AccessScreen.commit(screen)
77        self.assertEqual(screen.ctx.settings.api_key, "sk-pressed-k")
78
79    def test_a_launch_beyond_this_computer_still_gets_its_key(self):
80        screen = FakeAccess()
81        screens.AccessScreen.on_mount(screen)
82        self.move(screen, "lan")
83        self.move(screen, "local")
84        self.move(screen, "lan")
85        screens.AccessScreen.commit(screen)
86        self.assertEqual((screen.ctx.settings.access, screen.ctx.settings.api_key), ("lan", "sk-made-by-the-cursor"))
87
88
89if __name__ == "__main__":
90    unittest.main()