Ejemplo n.º 1
0
    def setUp(self):
        self.port = self._get_free_port()
        self.path, fn = split(abspath(__file__))

        self.server = StubServer(self.port)
        self.server.run()

        # Testing urls are fictional, but it does not really matter
        self.s = NajdiSiSms(username="******", password="******")
        self.s.logout_url = "http://localhost:%d/logout" % self.port
        self.s.login_url = "http://localhost:%d/login" % self.port
        self.s.session_url = "http://localhost:%d/session" % self.port
        self.s.home_url = "http://localhost:%d/" % self.port
        self.s.send_url = "http://localhost:%d" \
                          "/{session}/{prefix}/{number}/{data}" %self.port
Ejemplo n.º 2
0
class dispatcher(object):
    def __init__(self, config, warnings, messengers = None):
        self.config = config
        self.warnings = warnings
        self.messengers = messengers

        self.sms = NajdiSiSms("username", "password")

    def start(self):
        self.run = True
        self.dispatcher_worker = gevent.spawn(self.dispatcher)

    def stop(self):
        self.run = False
        self.dispatcher_worker.join()

    def dispatcher(self):
        while self.run:
            warnings = []
            warning = True
            while warning:
                try:
                    warning = self.warnings.get(timeout = 1)
                except Empty:
                    break
                warnings.append(warning)

            if not warnings: continue
            print "processing new warnings"

            clients = self.config.getClients()
            for devices in warnings:
                time = devices[0]
                for device in devices[1]:
                    client = filter(lambda c: c[1]["mac"] == device.mac,
                                    clients)
                    if not client: client = [("unknown", {"mac": device.mac})]

                    client= client[0]

                    for number in self.config.getNumbers():
                        try:
                            self.sms.send(number,
                                "Device %s with mac %s got missing at %s"
                                %(client[0], client[1]["mac"], time.strftime("%H:%M:%S")))
                        except SmsException:
                            pass
Ejemplo n.º 3
0
    def setUp(self):
        self.port = self._get_free_port()
        self.path, fn = split(abspath(__file__))

        self.server = StubServer(self.port)
        self.server.run()

        # Testing urls are fictional, but it does not really matter
        self.s = NajdiSiSms(username= "******", password= "******")
        self.s.logout_url = "http://localhost:%d/logout" %self.port
        self.s.login_url = "http://localhost:%d/login" %self.port
        self.s.session_url = "http://localhost:%d/session" %self.port
        self.s.home_url = "http://localhost:%d/" %self.port
        self.s.send_url = "http://localhost:%d" \
                          "/{session}/{prefix}/{number}/{data}" %self.port
Ejemplo n.º 4
0
 def setUp(self):
     self.s = NajdiSiSms(username="******", password="******", retries=1)
Ejemplo n.º 5
0
class unit_tests(TestCase):
    def setUp(self):
        self.s = NajdiSiSms(username="******", password="******", retries=1)

    def test_parse_balance(self):
        res = self.s._parse_balance(
            "<strong id=\"sms_left\" name=\"sms_left\">0 / 40</strong>")

        self.assertEqual(res, 40)

        with self.assertRaises(ResponseException):
            res = self.s._parse_balance(
                "<strong id=\"sms_left\" name=\"sms_left\"></strong>")

    def test_send_sms(self):
        def _login():
            self.s._session = '1361468289330'

        manager = Mock()
        self.s._login = manager._login
        self.s._login.side_effect = _login
        self.s._send_sms = manager._send_sms
        self.s._balance = 10

        self.s.send('041928491', 'test')

        expected_calls = [
            call._login(),
            call._send_sms('1361468289330', '41', '928491', 'test')
        ]
        self.assertEqual(expected_calls, manager.mock_calls)

        self.s.send('041928491', 'test')

        expected_calls = [
            call._login(),
            call._send_sms('1361468289330', '41', '928491', 'test'),
            call._send_sms('1361468289330', '41', '928491', 'test')
        ]
        self.assertEqual(expected_calls, manager.mock_calls)

    def test_send_sms_login_error_ok(self):
        def _login_first():
            def _login_second():
                self.s._session = '1361468289330'

            self.s._login.side_effect = _login_second
            raise AuthException

        manager = Mock()
        self.s._login = manager._login
        self.s._login.side_effect = _login_first
        self.s._send_sms = manager._send_sms
        self.s._balance = 10

        self.s.send('041928491', 'test')

        expected_calls = [
            call._login(),
            call._login(),
            call._send_sms('1361468289330', '41', '928491', 'test')
        ]
        self.assertEqual(expected_calls, manager.mock_calls)

    def test_send_sms_login_error_error(self):
        manager = Mock()
        self.s._login = manager._login
        self.s._login.side_effect = AuthException
        self.s._send_sms = manager._send_sms
        self.s._balance = 10

        with self.assertRaises(AuthException):
            self.s.send('041928491', 'test')

        expected_calls = [call._login(), call._login()]
        self.assertEqual(expected_calls, manager.mock_calls)

    def test_send_sms_send_error_error(self):
        def _login():
            self.s._session = '1361468289330'

        manager = Mock()
        self.s._login = manager._login
        self.s._login.side_effect = _login
        self.s._send_sms = manager._send_sms
        self.s._send_sms.side_effect = SendException
        self.s._balance = 10

        with self.assertRaises(SendException):
            self.s.send('041928491', 'test')

        expected_calls = [
            call._login(),
            call._send_sms('1361468289330', '41', '928491', 'test'),
            call._login(),
            call._send_sms('1361468289330', '41', '928491', 'test')
        ]
        self.assertEqual(expected_calls, manager.mock_calls)

    def test_send_sms_balance_error_error(self):
        def _login():
            self.s._session = '1361468289330'

        manager = Mock()
        self.s._login = manager._login
        self.s._login.side_effect = _login
        self.s._balance = 0

        with self.assertRaises(SendException):
            self.s.send('041928491', 'test')

        expected_calls = [call._login(), call._login()]
        self.assertEqual(expected_calls, manager.mock_calls)
Ejemplo n.º 6
0
class functional_tests(TestCase):
    """
    We emulate webserver to perform real testing, because mocking mechanize is
    not a way to go.
    """
    def _get_free_port(self):
        """
        Gets random free port

        :returns: Random free port
        :rtype: int
        """

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind(('', 0))

        port = sock.getsockname()[1]
        sock.close()

        return port

    def setUp(self):
        self.port = self._get_free_port()
        self.path, fn = split(abspath(__file__))

        self.server = StubServer(self.port)
        self.server.run()

        # Testing urls are fictional, but it does not really matter
        self.s = NajdiSiSms(username="******", password="******")
        self.s.logout_url = "http://localhost:%d/logout" % self.port
        self.s.login_url = "http://localhost:%d/login" % self.port
        self.s.session_url = "http://localhost:%d/session" % self.port
        self.s.home_url = "http://localhost:%d/" % self.port
        self.s.send_url = "http://localhost:%d" \
                          "/{session}/{prefix}/{number}/{data}" %self.port

    def tearDown(self):
        self.server.stop()

    def test_login(self):
        capture = {}
        self.server.expect(method="GET", url="/logout").and_return()
        self.server.expect(method="GET", url="/login").and_return(
            file_content=join(self.path, "najdisi_login.html"))
        self.server.expect(
            method="POST",
            url="/j_spring_security_check",
            data_capture=capture,
        ).and_return(reply_code=203)
        self.server.expect(method="GET", url="/session").and_return(
            file_content=join(self.path, "najdisi_loggedin.html"))

        self.s._login()

        self.assertEqual(
            capture["body"],
            urlencode([('j_username', 'test'), ('j_password', 'test')]),
            "posted data incorrect")

        self.assertEqual(self.s._session, '1361468289330')

    def test_login_errors(self):
        # Test for non incorrect login form name
        self.server.expect(method="GET", url="/logout").and_return()
        self.server.expect(method="GET", url="/login").and_return(
            content="<html><body><form name='notfound'></body></html>")

        with self.assertRaisesRegexp(ResponseException,
                                     "Error extracting login form"):
            self.s._login()

        # Test for correct form name, but nonexisting fields
        self.server.expect(method="GET", url="/logout").and_return()
        self.server.expect(method="GET", url="/login").and_return(
            content="<html><body><form name='lgn'></body></html>")

        with self.assertRaisesRegexp(ResponseException,
                                     "Error getting username and password"):
            self.s._login()

        # Test for unsucessfull login, session id
        self.server.expect(method="GET", url="/logout").and_return()
        self.server.expect(method="GET", url="/login").and_return(
            file_content=join(self.path, "najdisi_login.html"))
        self.server.expect(
            method="POST",
            url="/j_spring_security_check").and_return(reply_code=203)
        self.server.expect(method="GET", url="/session").and_return(
            file_content=join(self.path, "najdisi_home.html"))

        with self.assertRaisesRegexp(ResponseException,
                                     "Error getting session id"):
            self.s._login()

    def test_send_sms(self):
        self.server.expect(
            method="GET", url="/1361468289330/41/441325/test").and_return(
                mime_type="text/json",
                content="{ \"msg_left\" : \"10\", \"msg_cnt\" : \"10\" }")
        self.s._send_sms("1361468289330", "41", "441325", "test")

        self.assertEqual(self.s.balance, 10)

    def test_send_sms_error(self):
        self.server.expect(
            method="GET",
            url="/1361468289330/41/441325/test").and_return(reply_code=404)
        with self.assertRaisesRegexp(CommunicationException,
                                     "Error sending sms"):
            self.s._send_sms("1361468289330", "41", "441325", "test")

        self.server.expect(method="GET",
                           url="/1361468289330/41/441325/test").and_return(
                               mime_type="text/json", content="error")
        with self.assertRaisesRegexp(ResponseException,
                                     "Error parsing response"):
            self.s._send_sms("1361468289330", "41", "441325", "test")
Ejemplo n.º 7
0
 def setUp(self):
     self.s = NajdiSiSms(username= "******", password= "******", retries = 1)
Ejemplo n.º 8
0
class unit_tests(TestCase):
    def setUp(self):
        self.s = NajdiSiSms(username= "******", password= "******", retries = 1)

    def test_parse_balance(self):
        res = self.s._parse_balance("<strong id=\"sms_left\" name=\"sms_left\">0 / 40</strong>")

        self.assertEqual(res, 40)

        with self.assertRaises(ResponseException):
            res = self.s._parse_balance("<strong id=\"sms_left\" name=\"sms_left\"></strong>")

    def test_send_sms(self):
        def _login():
            self.s._session = '1361468289330'

        manager = Mock()
        self.s._login = manager._login
        self.s._login.side_effect = _login
        self.s._send_sms = manager._send_sms
        self.s._balance = 10

        self.s.send('041928491', 'test')

        expected_calls = [call._login(), call._send_sms('1361468289330', '41','928491', 'test')]
        self.assertEqual(expected_calls, manager.mock_calls)

        self.s.send('041928491', 'test')

        expected_calls = [call._login(),
                          call._send_sms('1361468289330', '41','928491', 'test'),
                          call._send_sms('1361468289330', '41','928491', 'test')]
        self.assertEqual(expected_calls, manager.mock_calls)

    def test_send_sms_login_error_ok(self):
        def _login_first():
            def _login_second():
                self.s._session = '1361468289330'
            self.s._login.side_effect = _login_second
            raise AuthException

        manager = Mock()
        self.s._login = manager._login
        self.s._login.side_effect = _login_first
        self.s._send_sms = manager._send_sms
        self.s._balance = 10

        self.s.send('041928491', 'test')

        expected_calls = [call._login(), call._login(),
                          call._send_sms('1361468289330', '41','928491', 'test')]
        self.assertEqual(expected_calls, manager.mock_calls)

    def test_send_sms_login_error_error(self):
        manager = Mock()
        self.s._login = manager._login
        self.s._login.side_effect = AuthException
        self.s._send_sms = manager._send_sms
        self.s._balance = 10

        with self.assertRaises(AuthException):
            self.s.send('041928491', 'test')

        expected_calls = [call._login(), call._login()]
        self.assertEqual(expected_calls, manager.mock_calls)

    def test_send_sms_send_error_error(self):
        def _login():
            self.s._session = '1361468289330'

        manager = Mock()
        self.s._login = manager._login
        self.s._login.side_effect = _login
        self.s._send_sms = manager._send_sms
        self.s._send_sms.side_effect = SendException
        self.s._balance = 10

        with self.assertRaises(SendException):
            self.s.send('041928491', 'test')

        expected_calls = [call._login(),
                          call._send_sms('1361468289330', '41','928491', 'test'),
                          call._login(),
                          call._send_sms('1361468289330', '41','928491', 'test')]
        self.assertEqual(expected_calls, manager.mock_calls)

    def test_send_sms_balance_error_error(self):
        def _login():
            self.s._session = '1361468289330'

        manager = Mock()
        self.s._login = manager._login
        self.s._login.side_effect = _login
        self.s._balance = 0

        with self.assertRaises(SendException):
            self.s.send('041928491', 'test')

        expected_calls = [call._login(), call._login()]
        self.assertEqual(expected_calls, manager.mock_calls)
Ejemplo n.º 9
0
class functional_tests(TestCase):
    """
    We emulate webserver to perform real testing, because mocking mechanize is
    not a way to go.
    """

    def _get_free_port(self):
        """
        Gets random free port

        :returns: Random free port
        :rtype: int
        """

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind(('', 0))

        port = sock.getsockname()[1]
        sock.close()

        return port

    def setUp(self):
        self.port = self._get_free_port()
        self.path, fn = split(abspath(__file__))

        self.server = StubServer(self.port)
        self.server.run()

        # Testing urls are fictional, but it does not really matter
        self.s = NajdiSiSms(username= "******", password= "******")
        self.s.logout_url = "http://localhost:%d/logout" %self.port
        self.s.login_url = "http://localhost:%d/login" %self.port
        self.s.session_url = "http://localhost:%d/session" %self.port
        self.s.home_url = "http://localhost:%d/" %self.port
        self.s.send_url = "http://localhost:%d" \
                          "/{session}/{prefix}/{number}/{data}" %self.port

    def tearDown(self):
        self.server.stop()

    def test_login(self):
        capture= {}
        self.server.expect(method = "GET", url = "/logout").and_return()
        self.server.expect(method = "GET", url = "/login").and_return(
            file_content = join(self.path, "najdisi_login.html")
        )
        self.server.expect(method="POST", url="/j_spring_security_check",
                           data_capture=capture,
                          ).and_return(reply_code=203)
        self.server.expect(method="GET", url = "/session").and_return(
            file_content=join(self.path,"najdisi_loggedin.html")
        )

        self.s._login()

        self.assertEqual(capture["body"],
                         urlencode([('j_username', 'test'), ('j_password', 'test')]),
                         "posted data incorrect")

        self.assertEqual(self.s._session, '1361468289330')

    def test_login_errors(self):
        # Test for non incorrect login form name
        self.server.expect(method = "GET", url = "/logout").and_return()
        self.server.expect(method="GET", url="/login").and_return(
            content="<html><body><form name='notfound'></body></html>"
        )

        with self.assertRaisesRegexp(ResponseException, "Error extracting login form"):
            self.s._login()

        # Test for correct form name, but nonexisting fields
        self.server.expect(method = "GET", url = "/logout").and_return()
        self.server.expect(method="GET", url="/login").and_return(
            content="<html><body><form name='lgn'></body></html>"
        )

        with self.assertRaisesRegexp(ResponseException, "Error getting username and password"):
            self.s._login()

        # Test for unsucessfull login, session id
        self.server.expect(method = "GET", url = "/logout").and_return()
        self.server.expect(method="GET", url="/login").and_return(
            file_content=join(self.path,"najdisi_login.html")
        )
        self.server.expect(method="POST", url="/j_spring_security_check"
                          ).and_return(reply_code=203)
        self.server.expect(method="GET", url="/session").and_return(
            file_content=join(self.path,"najdisi_home.html")
        )

        with self.assertRaisesRegexp(ResponseException, "Error getting session id"):
            self.s._login()

    def test_send_sms(self):
        self.server.expect(method = "GET", url="/1361468289330/41/441325/test").and_return(
            mime_type="text/json",
            content="{ \"msg_left\" : \"10\", \"msg_cnt\" : \"10\" }"
        )
        self.s._send_sms("1361468289330", "41", "441325", "test")

        self.assertEqual(self.s.balance, 10)

    def test_send_sms_error(self):
        self.server.expect(method = "GET", url="/1361468289330/41/441325/test").and_return(
            reply_code=404)
        with self.assertRaisesRegexp(CommunicationException, "Error sending sms"):
            self.s._send_sms("1361468289330", "41", "441325", "test")

        self.server.expect(method = "GET", url="/1361468289330/41/441325/test").and_return(
            mime_type="text/json",
            content="error"
        )
        with self.assertRaisesRegexp(ResponseException, "Error parsing response"):
            self.s._send_sms("1361468289330", "41", "441325", "test")
Ejemplo n.º 10
0
# -*- coding: utf-8 -*-
import logging, sys
from pysms.providers import NajdiSiSms

loglevel = 'DEBUG'

datefmt = '%b %d %H:%M:%S'
logformat = '%(asctime)s %(levelname)s pysms: %(message)s'

logging.basicConfig(level=loglevel,
                    stream=sys.stdout,
                    format=logformat,
                    datefmt=datefmt)

provider = NajdiSiSms("username","password")
print provider.send("51385279", u"čžš")
print provider.send("51385279", u"čžš")
Ejemplo n.º 11
0
    def __init__(self, config, warnings, messengers = None):
        self.config = config
        self.warnings = warnings
        self.messengers = messengers

        self.sms = NajdiSiSms("username", "password")
Ejemplo n.º 12
0
# -*- coding: utf-8 -*-
import logging, sys
from pysms.providers import NajdiSiSms

loglevel = 'DEBUG'

datefmt = '%b %d %H:%M:%S'
logformat = '%(asctime)s %(levelname)s pysms: %(message)s'

logging.basicConfig(level=loglevel,
                    stream=sys.stdout,
                    format=logformat,
                    datefmt=datefmt)

provider = NajdiSiSms("username","password")

if len(sys.argv) == 2:
    print provider.send("41777777", sys.argv[1].decode("utf-8"))
else:
    print provider.send("41777777", u"test_sms")
provider.destruct()

# JSESSIONID
Ejemplo n.º 13
0
# -*- coding: utf-8 -*-
import logging, sys
from pysms.providers import NajdiSiSms

loglevel = 'DEBUG'

datefmt = '%b %d %H:%M:%S'
logformat = '%(asctime)s %(levelname)s pysms: %(message)s'

logging.basicConfig(level=loglevel,
                    stream=sys.stdout,
                    format=logformat,
                    datefmt=datefmt)

provider = NajdiSiSms("username", "password")
print provider.send("51385279", u"čžš")
print provider.send("51385279", u"čžš")