Completed
Push — master ( a8928e...605880 )
by Jeffrey
06:31
created

APRSTest.test_fake_bad_auth_http()   B

Complexity

Conditions 1

Size

Total Lines 32

Duplication

Lines 32
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 32
loc 32
rs 8.8571
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 = '>'.join([
104
            #     self.fake_callsign,
105
            #     'APRS,TCPIP*:=3745.00N/12227.00W-Simulated Location'
106
            # ])
107
            msg = {
108
                'source': self.fake_callsign,
109
                'destination': 'APRS',
110
                'path': ['TCPIP*'],
111
                'text': '=3745.00N/12227.00W-Simulated Location'
112
            }
113
            self.logger.debug(locals())
114
115
            result = aprs_conn.send(msg)
116
117
            self.assertTrue(result)
118
119 View Code Duplication
        @httpretty.httprettified
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
120
        def test_fake_bad_auth_http(self):
121
            """
122
            Tests authenticating against APRS-IS using an invalid call+pass.
123
            """
124
            httpretty.HTTPretty.register_uri(
125
                httpretty.HTTPretty.POST,
126
                self.fake_server,
127
                status=401
128
            )
129
130
            aprs_conn = apex.aprs.aprs_internet_service.AprsInternetService(
131
                user=self.fake_callsign,
132
                input_url=self.fake_server
133
            )
134
            aprs_conn.connect()
135
136
            # msg = '>'.join([
137
            #     self.fake_callsign,
138
            #     'APRS,TCPIP*:=3745.00N/12227.00W-Simulated Location'
139
            # ])
140
            msg = {
141
                'source': self.fake_callsign,
142
                'destination': 'APRS',
143
                'path': ['TCPIP*'],
144
                'text': '=3745.00N/12227.00W-Simulated Location'
145
            }
146
            self.logger.debug(locals())
147
148
            result = aprs_conn.send(msg, protocol='HTTP')
149
150
            self.assertFalse(result)
151
152
        @unittest.skip('Test only works with real server.')
153
        def test_more(self):
154
            """
155
            Tests APRS-IS binding against a real APRS-IS server.
156
            """
157
            aprs_conn = apex.aprs.aprs_internet_service.AprsInternetService(
158
                user=self.real_callsign,
159
                input_url=self.real_server
160
            )
161
            aprs_conn.connect()
162
163
            # msg = '>'.join([
164
            #     self.real_callsign,
165
            #     'APRS,TCPIP*:=3745.00N/12227.00W-Simulated Location'
166
            # ])
167
            msg = {
168
                'source': self.fake_callsign,
169
                'destination': 'APRS',
170
                'path': ['TCPIP*'],
171
                'text': '=3745.00N/12227.00W-Simulated Location'
172
            }
173
            self.logger.debug(locals())
174
175
            result = aprs_conn.send(msg)
176
177
            self.assertFalse(result)
178