Completed
Pull Request — master (#2)
by Jeffrey
04:10
created

APRSTest.test_more()   A

Complexity

Conditions 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
__author__ = 'Jeffrey Phillips Freeman WI2ARD <[email protected]>'
5
__license__ = 'Apache License, Version 2.0'
6
__copyright__ = 'Copyright 2016, Syncleus, Inc. and contributors'
7
8
"""Tests for Python APRS-IS Bindings."""
9
import logging
10
import logging.handlers
11
# import httpretty
12
import random
13
import unittest
14
15
import apex.aprs.aprs_internet_service
16
import apex.aprs.constants
17
18
ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
19
NUMBERS = '0123456789'
20
POSITIVE_NUMBERS = NUMBERS[1:]
21
ALPHANUM = ''.join([ALPHABET, NUMBERS])
22
23
24
class APRSTest(unittest.TestCase):  # pylint: disable=R0904
25
    """Tests for Python APRS-IS Bindings."""
26
27
    logger = logging.getLogger(__name__)
28
    logger.setLevel(apex.aprs.constants.LOG_LEVEL)
29
    console_handler = logging.StreamHandler()
30
    console_handler.setLevel(apex.aprs.constants.LOG_LEVEL)
31
    formatter = logging.Formatter(apex.aprs.constants.LOG_FORMAT)
32
    console_handler.setFormatter(formatter)
33
    logger.addHandler(console_handler)
34
    logger.propagate = False
35
36
    @classmethod
37
    def random(cls, length=8, alphabet=ALPHANUM):
38
        """
39
        Generates a random string for test cases.
40
41
        :param length: Length of string to generate.
42
        :param alphabet: Alphabet to use to create string.
43
        :type length: int
44
        :type alphabet: str
45
        """
46
        return ''.join(random.choice(alphabet) for _ in xrange(length))
47
48
    def setUp(self):  # pylint: disable=C0103
49
        self.fake_server = ''.join([
50
            'http://localhost:',
51
            self.random(4, POSITIVE_NUMBERS),
52
            '/'
53
        ])
54
55
        self.fake_callsign = ''.join([
56
            self.random(1, 'KWN'),
57
            self.random(1, NUMBERS),
58
            self.random(3, ALPHABET),
59
            '-',
60
            self.random(1, POSITIVE_NUMBERS)
61
        ])
62
63
        self.real_server = 'http://localhost:14580'
64
        self.real_callsign = '-'.join(['W2GMD', self.random(1, '123456789')])
65
66
        self.logger.debug(
67
            "fake_server=%s fake_callsign=%s",
68
            self.fake_server,
69
            self.fake_callsign
70
        )
71
72
    # @httpretty.httprettified
73
    # def test_fake_good_auth(self):
74
    #     """
75
    #     Tests authenticating against APRS-IS using a valid call+pass.
76
    #     """
77
    #     httpretty.HTTPretty.register_uri(
78
    #         httpretty.HTTPretty.POST,
79
    #         self.fake_server,
80
    #         status=204
81
    #     )
82
    #
83
    #     aprs_conn = apex.aprs.aprs_internet_service.AprsInternetService(
84
    #         user=self.fake_callsign,
85
    #         input_url=self.fake_server
86
    #     )
87
    #     aprs_conn.connect()
88
    #
89
    #     msg = '>'.join([
90
    #         self.fake_callsign,
91
    #         'APRS,TCPIP*:=3745.00N/12227.00W-Simulated Location'
92
    #     ])
93
    #     self.logger.debug(locals())
94
    #
95
    #     result = aprs_conn.send(msg)
96
    #
97
    #     self.assertTrue(result)
98
    #
99
    # @httpretty.httprettified
100
    # def test_fake_bad_auth_http(self):
101
    #     """
102
    #     Tests authenticating against APRS-IS using an invalid call+pass.
103
    #     """
104
    #     httpretty.HTTPretty.register_uri(
105
    #         httpretty.HTTPretty.POST,
106
    #         self.fake_server,
107
    #         status=401
108
    #     )
109
    #
110
    #     aprs_conn = apex.aprs.aprs_internet_service.AprsInternetService(
111
    #         user=self.fake_callsign,
112
    #         input_url=self.fake_server
113
    #     )
114
    #     aprs_conn.connect()
115
    #
116
    #     msg = '>'.join([
117
    #         self.fake_callsign,
118
    #         'APRS,TCPIP*:=3745.00N/12227.00W-Simulated Location'
119
    #     ])
120
    #     self.logger.debug(locals())
121
    #
122
    #     result = aprs_conn.send(msg, protocol='HTTP')
123
    #
124
    #     self.assertFalse(result)
125
    #
126
    # @unittest.skip('Test only works with real server.')
127
    # def test_more(self):
128
    #     """
129
    #     Tests APRS-IS binding against a real APRS-IS server.
130
    #     """
131
    #     aprs_conn = apex.aprs.aprs_internet_service.AprsInternetService(
132
    #         user=self.real_callsign,
133
    #         input_url=self.real_server
134
    #     )
135
    #     aprs_conn.connect()
136
    #
137
    #     msg = '>'.join([
138
    #         self.real_callsign,
139
    #         'APRS,TCPIP*:=3745.00N/12227.00W-Simulated Location'
140
    #     ])
141
    #     self.logger.debug(locals())
142
    #
143
    #     result = aprs_conn.send(msg)
144
    #
145
    #     self.assertFalse(result)
146