aboutsummaryrefslogtreecommitdiff
path: root/tests/test_scenarios.py
blob: f4fb8d392b460ba05a41112e75ec6e7a3fc4325d (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
# *****************************************************************************
# Copyright (C) 2021 Thomas Touhey <thomas@touhey.fr>
# This file is part of the fingerd project, which is MIT-licensed.
# *****************************************************************************
""" Tests for the fingerd server. """

from fingerd.fiction import (
    FingerScenario, FingerUserCreationAction, FingerUserDeletionAction,
    FingerUserEditionAction, FingerUserLoginAction, FingerUserLogoutAction,
)

import pytest


class TestScenarios:
    """ Test scenarios. """

    def test_no_ending_type(self):
        scenario = FingerScenario()
        scenario.duration = '1m'
        with pytest.raises(ValueError, match=r'ending type'):
            scenario.verify()

    def test_no_duration(self):
        scenario = FingerScenario()
        scenario.ending_type = FingerScenario.EndingType.FREEZE
        with pytest.raises(ValueError, match=r'ending time'):
            scenario.verify()

    def test_edit_without_create_user(self):
        scenario = FingerScenario()
        scenario.ending_type = FingerScenario.EndingType.FREEZE
        scenario.duration = '1m'

        scenario.add(
            FingerUserEditionAction(
                login='user',
                shell='/bin/sh',  # NOQA
            ),
            '0s',
        )

        with pytest.raises(ValueError, match=r'trying to edit user'):
            scenario.verify()

    def test_delete_without_create_user(self):
        scenario = FingerScenario()
        scenario.ending_type = FingerScenario.EndingType.FREEZE
        scenario.duration = '1m'

        scenario.add(
            FingerUserDeletionAction(
                login='user',
            ),
            '0s',
        )

        with pytest.raises(ValueError, match=r'trying to delete user'):
            scenario.verify()

    def test_edit_after_delete_user(self):
        scenario = FingerScenario()
        scenario.ending_type = FingerScenario.EndingType.FREEZE
        scenario.duration = '1m'

        scenario.add(
            FingerUserCreationAction(
                login='user',
                shell='/bin/sh',  # NOQA
            ),
            '0s',
        )
        scenario.add(
            FingerUserEditionAction(
                login='user',
                name='John Doe',
            ),
            '10s',
        )
        scenario.add(
            FingerUserDeletionAction(
                login='user',
            ),
            '5s',
        )

        with pytest.raises(ValueError, match=r'trying to edit user'):
            scenario.verify()

    def test_login_without_creation(self):
        scenario = FingerScenario()
        scenario.ending_type = FingerScenario.EndingType.FREEZE
        scenario.duration = '1m'

        scenario.add(
            FingerUserLoginAction(
                login='user',
            ),
            '0s',
        )

        with pytest.raises(ValueError, match=r'trying to log in as'):
            scenario.verify()

    def test_logout_without_creation(self):
        scenario = FingerScenario()
        scenario.ending_type = FingerScenario.EndingType.FREEZE
        scenario.duration = '1m'

        scenario.add(
            FingerUserLogoutAction(
                login='user',
            ),
            '0s',
        )

        with pytest.raises(
            ValueError, match=r'trying to delete session of non-existing',
        ):
            scenario.verify()

    def test_logout_without_login(self):
        scenario = FingerScenario()
        scenario.ending_type = FingerScenario.EndingType.FREEZE
        scenario.duration = '1m'

        scenario.add(
            FingerUserCreationAction(
                login='user',
                shell='/bin/sh',  # NOQA
            ),
            '0s',
        )
        scenario.add(
            FingerUserLogoutAction(
                login='user',
            ),
            '1s',
        )

        with pytest.raises(
            ValueError, match=r'trying to delete non existing session',
        ):
            scenario.verify()

# End of file.