Completed
Branch master (d42b79)
by Jeffrey
05:09
created

APRSTest.test_more()   A

Complexity

Conditions 1

Size

Total Lines 22

Duplication

Lines 7
Ratio 31.82 %

Importance

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