aboutsummaryrefslogtreecommitdiff
path: root/tests/test_fingerd.py
blob: 848e0d5f4e12c1dca5fb68e2a2b3286fe06cae6a (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
#!/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. """

import socket

from datetime import timedelta
from time import sleep

from fingerd.core import FingerServer
from fingerd.fiction import (
    FingerScenario, FingerScenarioInterface,
    FingerUserCreationAction, FingerUserLoginAction, FingerUserLogoutAction,
)

import pytest


class TestFingerConnection:
    """ Test basic finger connections. """

    @pytest.fixture
    def fingerserver(self):
        """ Start a finger server.

            A fixture starting a finger server on ``localhost:3099``
            and stopping it after the test.
        """

        scenario = FingerScenario()
        scenario.ending_type = 'freeze'
        scenario.duration = timedelta(seconds=5)
        scenario.add(
            FingerUserCreationAction(
                login='john',
                name='John Doe',
                home='/home/john',
                shell='/bin/bash',  # NOQA
                office='84.6',
            ),
            timedelta(seconds=-5))
        scenario.add(
            FingerUserLoginAction(
                login='john',
                line='tty1',
            ),
            timedelta(seconds=0))
        scenario.add(
            FingerUserLogoutAction(
                login='john',
            ),
            timedelta(seconds=1))

        server = FingerServer(
            'localhost:3099',
            hostname='example.org',
            interface=FingerScenarioInterface(scenario),
        )
        server.start()

        sleep(.1)
        yield

        server.stop()

    def _send_command(self, command):
        conn = socket.create_connection(('localhost', 3099))
        conn.send(command)
        return conn.recv(1024)

    # ---
    # Tests.
    # ---

    def test_no_user_list(self, fingerserver):
        """ Test if an unknown user returns an empty result. """

        result = self._send_command(b'user\r\n')
        assert result == b'No user list available.\r\n'

    def test_existing_user_list(self, fingerserver):
        """ Test the user list before and after the cron is executed. """

        result = self._send_command(b'\r\n')

        assert result != b''
        assert result != b'No user list available.\r\n'

        sleep(2)
        result = self._send_command(b'\r\n')

        assert result == b'No user list available.\r\n'

# End of file.