diff --git a/src/apex/aprs/aprs_internet_service.py b/src/apex/aprs/aprs_internet_service.py index 4061e957b778a7413e169cfccf1e4629c1ee71c1..8dccedf1579a88295b17e2fb8d89f4d95d2ef778 100644 --- a/src/apex/aprs/aprs_internet_service.py +++ b/src/apex/aprs/aprs_internet_service.py @@ -99,7 +99,7 @@ class AprsInternetService(object): content = "\n".join([self._auth, aprsUtil.format_aprs_frame(frame)]) headers = headers or aprsConstants.APRSIS_HTTP_HEADERS result = requests.post(self._url, data=content, headers=headers) - return 204 in result.status_code + return 204 == result.status_code elif 'UDP' in protocol: content = "\n".join([self._auth, aprsUtil.format_aprs_frame(frame)]) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) diff --git a/src/apex/aprs/util.py b/src/apex/aprs/util.py index 503df7ce8494f4a114341fc0ab6dab0cdddee236..8c803ec2efc924f5acb016c4c0592c506cae4917 100755 --- a/src/apex/aprs/util.py +++ b/src/apex/aprs/util.py @@ -138,7 +138,7 @@ def format_aprs_frame(frame): if frame['path']: formatted_frame = ','.join([formatted_frame, format_path(frame['path'])]) formatted_frame += ':' - for frame_byte in frame['text']: + for frame_byte in bytearray(frame['text'], 'Ascii'): formatted_frame += chr(frame_byte) return formatted_frame diff --git a/tests/test_aprs.py b/tests/test_aprs.py index 88b58be8a23ab5aaf775389d0af6467d33b1d4ed..acd5c62cb6f1bb90ad341bdd7441a548917d0507 100644 --- a/tests/test_aprs.py +++ b/tests/test_aprs.py @@ -11,13 +11,16 @@ from __future__ import unicode_literals import logging import logging.handlers -# import httpretty import random +import sys import unittest import apex.aprs.aprs_internet_service import apex.aprs.constants +if sys.version_info < (3, 0): + import httpretty + __author__ = 'Jeffrey Phillips Freeman (WI2ARD)' __maintainer__ = "Jeffrey Phillips Freeman (WI2ARD)" __email__ = "jeffrey.freeman@syncleus.com" @@ -79,77 +82,96 @@ class APRSTest(unittest.TestCase): # pylint: disable=R0904 self.fake_callsign ) - # @httpretty.httprettified - # def test_fake_good_auth(self): - # """ - # Tests authenticating against APRS-IS using a valid call+pass. - # """ - # httpretty.HTTPretty.register_uri( - # httpretty.HTTPretty.POST, - # self.fake_server, - # status=204 - # ) - # - # aprs_conn = apex.aprs.aprs_internet_service.AprsInternetService( - # user=self.fake_callsign, - # input_url=self.fake_server - # ) - # aprs_conn.connect() - # - # msg = '>'.join([ - # self.fake_callsign, - # 'APRS,TCPIP*:=3745.00N/12227.00W-Simulated Location' - # ]) - # self.logger.debug(locals()) - # - # result = aprs_conn.send(msg) - # - # self.assertTrue(result) - # - # @httpretty.httprettified - # def test_fake_bad_auth_http(self): - # """ - # Tests authenticating against APRS-IS using an invalid call+pass. - # """ - # httpretty.HTTPretty.register_uri( - # httpretty.HTTPretty.POST, - # self.fake_server, - # status=401 - # ) - # - # aprs_conn = apex.aprs.aprs_internet_service.AprsInternetService( - # user=self.fake_callsign, - # input_url=self.fake_server - # ) - # aprs_conn.connect() - # - # msg = '>'.join([ - # self.fake_callsign, - # 'APRS,TCPIP*:=3745.00N/12227.00W-Simulated Location' - # ]) - # self.logger.debug(locals()) - # - # result = aprs_conn.send(msg, protocol='HTTP') - # - # self.assertFalse(result) - # - # @unittest.skip('Test only works with real server.') - # def test_more(self): - # """ - # Tests APRS-IS binding against a real APRS-IS server. - # """ - # aprs_conn = apex.aprs.aprs_internet_service.AprsInternetService( - # user=self.real_callsign, - # input_url=self.real_server - # ) - # aprs_conn.connect() - # - # msg = '>'.join([ - # self.real_callsign, - # 'APRS,TCPIP*:=3745.00N/12227.00W-Simulated Location' - # ]) - # self.logger.debug(locals()) - # - # result = aprs_conn.send(msg) - # - # self.assertFalse(result) + if sys.version_info < (3, 0): + @httpretty.httprettified + def test_fake_good_auth(self): + """ + Tests authenticating against APRS-IS using a valid call+pass. + """ + httpretty.HTTPretty.register_uri( + httpretty.HTTPretty.POST, + self.fake_server, + status=204 + ) + + aprs_conn = apex.aprs.aprs_internet_service.AprsInternetService( + user=self.fake_callsign, + input_url=self.fake_server + ) + aprs_conn.connect() + + # msg = '>'.join([ + # self.fake_callsign, + # 'APRS,TCPIP*:=3745.00N/12227.00W-Simulated Location' + # ]) + msg = { + 'source': self.fake_callsign, + 'destination': 'APRS', + 'path': ['TCPIP*'], + 'text': '=3745.00N/12227.00W-Simulated Location' + } + self.logger.debug(locals()) + + result = aprs_conn.send(msg) + + self.assertTrue(result) + + @httpretty.httprettified + def test_fake_bad_auth_http(self): + """ + Tests authenticating against APRS-IS using an invalid call+pass. + """ + httpretty.HTTPretty.register_uri( + httpretty.HTTPretty.POST, + self.fake_server, + status=401 + ) + + aprs_conn = apex.aprs.aprs_internet_service.AprsInternetService( + user=self.fake_callsign, + input_url=self.fake_server + ) + aprs_conn.connect() + + # msg = '>'.join([ + # self.fake_callsign, + # 'APRS,TCPIP*:=3745.00N/12227.00W-Simulated Location' + # ]) + msg = { + 'source': self.fake_callsign, + 'destination': 'APRS', + 'path': ['TCPIP*'], + 'text': '=3745.00N/12227.00W-Simulated Location' + } + self.logger.debug(locals()) + + result = aprs_conn.send(msg, protocol='HTTP') + + self.assertFalse(result) + + @unittest.skip('Test only works with real server.') + def test_more(self): + """ + Tests APRS-IS binding against a real APRS-IS server. + """ + aprs_conn = apex.aprs.aprs_internet_service.AprsInternetService( + user=self.real_callsign, + input_url=self.real_server + ) + aprs_conn.connect() + + # msg = '>'.join([ + # self.real_callsign, + # 'APRS,TCPIP*:=3745.00N/12227.00W-Simulated Location' + # ]) + msg = { + 'source': self.fake_callsign, + 'destination': 'APRS', + 'path': ['TCPIP*'], + 'text': '=3745.00N/12227.00W-Simulated Location' + } + self.logger.debug(locals()) + + result = aprs_conn.send(msg) + + self.assertFalse(result) diff --git a/tox.ini b/tox.ini index 9d1059b283a5f9093726f786c28801175124f375..2301f01de261f5684ff5bb04b60599ab5d8e3bfb 100644 --- a/tox.ini +++ b/tox.ini @@ -125,9 +125,13 @@ commands = deps = {[testenv]deps} pytest-cov + httpretty [testenv:2.7-nocov] basepython = {env:TOXPYTHON:python2.7} +deps = + {[testenv]deps} + httpretty [testenv:3.3-cover] basepython = {env:TOXPYTHON:python3.3} @@ -185,9 +189,12 @@ commands = deps = {[testenv]deps} pytest-cov + httpretty [testenv:pypy-nocov] basepython = {env:TOXPYTHON:pypy} - +deps = + {[testenv]deps} + httpretty