Completed
Push — master ( 7b8fc2...c09cea )
by Jeffrey
03:35
created

AprsTest.test_more()   A

Complexity

Conditions 1

Size

Total Lines 22

Duplication

Lines 8
Ratio 36.36 %

Importance

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