def setUp(self):
        self.sp = CredReceiver()
        self.sp.factory = CredAMPServerFactory()
        self.transport = StringTransportWithDisconnection()
        self.sp.makeConnection(self.transport)

        self.transport.protocol = self.sp
Exemple #2
0
class TestProtocolLogin(TestCase):
    def setUp(self):
        self.sp = CredReceiver()
        self.sp.factory = CredAMPServerFactory()

    def tearDown(self):
        self.sp.factory.active_protocols = {}
        self.sp.factory.active_connections = {}

    def test_successfulLoginWithTestUser(self):
        print ""
        res = self.sp.login('test-user-sc', 'test-password')
        return self.assertTrue(res['bAuthenticated'])

    # FIX-ME
    def _test_unsuccessfulLoginWithTestUser(self):
        print ""
        self.sp.rpc = MagicMock(return_value=False)
        res = self.sp.login('wrong-user', 'wrong-pass')
        return self.assertFalse(res['bAuthenticated'])

    @patch.object(CredReceiver,
                  'start_remote_user',
                  return_value=[{
                      'ending_time': 1234,
                      'id': '-1'
                  }, 'gs', 'sc', 'client_a', 'client_c'])
    @patch.object(CredReceiver, 'check_expiration', return_value=1000)
    def _test_startRemoteSucessful(self, start_remote_user, check_expiration):
        res = self.sp.i_start_remote()

        return self.assertEqual(str(res['iResult']), '-2')
Exemple #3
0
    def setUp(self):
        self.sp = CredReceiver()
        self.sp.factory = CredAMPServerFactory()

        self.correctFrame = (
            "00:82:a0:00:00:53:45:52:50:2d:42:30:91:1d:1b:03:" +
            "8d:0b:5c:03:02:28:01:9c:01:ab:02:4c:02:98:01:da:" +
            "02:40:00:00:00:10:0a:46:58:10:00:c4:9d:cb:a2:21:39")
    def setUp(self):
        self.sp = CredReceiver()
        self.sp.factory = CredAMPServerFactory()
        self.sp.factory.sUsername = '******'
        self.transport = StringTransportWithDisconnection()
        self.sp.makeConnection(self.transport)

        self.transport.protocol = self.sp

        self.testFrame = ("00:82:a0:00:00:53:45:52:50:2d:42:30:91:1d:1b:03:" +
                          "8d:0b:5c:03:02:28:01:9c:01:ab:02:4c:02:98:01:da:" +
                          "02:40:00:00:00:10:0a:46:58:10:00:c4:9d:cb:a2:21:39")
class TestSlotsNotification(TestCase):
    def setUp(self):
        self.sp = CredReceiver()
        self.sp.factory = CredAMPServerFactory()

    def tearDown(self):
        pass

    @patch.object(CredReceiver, 'callRemote')
    def test_slotEndingCallRemoteCalled(self, callRemote):
        self.sp.slot_end(-1)
        return self.assertEqual(int(callRemote.call_count), 1)
class TestLogin(TestCase):
    def setUp(self):
        self.sp = CredReceiver()
        self.sp.factory = CredAMPServerFactory()
        self.sp.factory.sUsername = '******'
        self.transport = StringTransportWithDisconnection()
        self.sp.makeConnection(self.transport)

        self.transport.protocol = self.sp

        self.testFrame = ("00:82:a0:00:00:53:45:52:50:2d:42:30:91:1d:1b:03:" +
                          "8d:0b:5c:03:02:28:01:9c:01:ab:02:4c:02:98:01:da:" +
                          "02:40:00:00:00:10:0a:46:58:10:00:c4:9d:cb:a2:21:39")

    def tearDown(self):
        self.sp.factory.active_protocols = {}
        self.sp.factory.active_connections = {}

    def test_serverSendsAnythingWhenReceiveFrame(self):
        self.sp.dataReceived(self.testFrame)
        return self.assertEquals('', self.transport.value())

    @patch.object(CredReceiver, 'resetTimeout')
    def test_timeoutResetWhenReceivesMessage(self, resetTimeout):
        self.sp.dataReceived(self.testFrame)
        return self.assertEqual(resetTimeout.call_count, 1)
class TestServerProtocolConnectionDown(TestCase):
    """
    Testing multiple client connections
    TDOD. Test multiple valid connections
    """
    def setUp(self):
        self.sp = CredReceiver()
        self.sp.factory = CredAMPServerFactory()
        self.transport = StringTransportWithDisconnection()
        self.sp.makeConnection(self.transport)

        self.transport.protocol = self.sp

    def tearDown(self):
        self.sp.factory.active_protocols = {}
        self.sp.factory.active_connections = {}

    def test_localProtocolRemovesWhenTimeoutReachesWithRemoteUserConnected(
            self):
        sc_user = '******'
        gs_user = '******'
        username_test = 'test-user-sc'

        mockSC = Mock()
        mockSC.callRemote = MagicMock(return_value=True)
        mockGS = Mock()
        mockGS.callRemote = MagicMock(return_value=True)

        self.sp.factory.active_protocols[str(sc_user)] = mockSC
        self.sp.factory.active_protocols[str(gs_user)] = mockGS
        self.sp.factory.active_connections[str(sc_user)] = str(gs_user)
        self.sp.factory.active_connections[str(gs_user)] = str(sc_user)
        self.sp.username = username_test

        self.sp.timeoutConnection()

        return self.assertFalse(self.transport.connected), \
               self.assertIs(len(self.sp.factory.active_protocols), 1), \
               self.assertIs(len(self.sp.factory.active_connections), 0)

    def test_localProtocolRemovesWhenTimeoutReachesWithoutRemoteUserDisconnected(
            self):
        sc_user = '******'
        username_test = 'test-user-sc'

        mockSC = Mock()
        mockSC.callRemote = MagicMock(return_value=True)
        mockGS = Mock()
        mockGS.callRemote = MagicMock(return_value=True)

        # self.sp.factory.active_protocols[str(sc_user)] = mockSC
        self.sp.factory.active_protocols[str(sc_user)] = mockSC
        self.sp.username = username_test
        self.sp.timeoutConnection()

        return self.assertFalse(self.transport.connected), \
               self.assertIs(len(self.sp.factory.active_protocols), 1), \
               self.assertIs(len(self.sp.factory.active_connections), 0)
Exemple #8
0
class TestServerProtocolConnectionEstablished(TestCase):
    """
    Testing multiple client connections
    TDOD. Test multiple valid connections
    """
    def setUp(self):
        self.sp = CredReceiver()
        self.sp.factory = CredAMPServerFactory()
        self.transport = StringTransportWithDisconnection()

    def tearDown(self):
        self.transport.loseConnection()

    def test_connectionProtocolEstablished(self):
        """
        Checks if transport is working an clients list has the last client.
        :return:
        """
        self.sp.makeConnection(self.transport)
        self.transport.protocol = self.sp

        return self.assertTrue(self.transport.connected),\
               self.assertEqual(len(self.sp.factory.clients), 1)
Exemple #9
0
 def setUp(self):
     self.sp = CredReceiver()
     self.sp.factory = CredAMPServerFactory()
Exemple #10
0
class TestProtocolLoginAuxiliarMethods(TestCase):
    def setUp(self):
        self.sp = CredReceiver()
        self.sp.factory = CredAMPServerFactory()

    def tearDown(self):
        self.sp.factory.active_protocols = {}
        self.sp.factory.active_connections = {}

    def test_getsASlotUsingARPCCall(self):
        self.sp.username = '******'
        self.sp.rpc = Mock()
        self.sp.rpc.testing = Mock

        print ""
        response = self.sp.start_remote_user()
        value = [
            'test-user-gs', 'test-user-sc', 'test-user-sc', 'test-user-gs'
        ]

        self.assertIs(type(response[0]), dict)
        self.assertEqual(str(response[1]), str(value[0]))
        self.assertEqual(str(response[2]), str(value[1]))
        self.assertEqual(str(response[3]), str(value[2]))
        self.assertEqual(str(response[4]), str(value[3]))

    def test_decodeUserWithSlotUserIsSC(self):
        test_slot = {
            'gs_username': '******',
            'sc_username': '******'
        }
        self.sp.username = '******'
        response = self.sp.decode_user(test_slot)

        value = [
            'gs_user_test', 'sc_user_test', 'sc_user_test', 'gs_user_test'
        ]

        for i in range(len(response)):
            self.assertIs(value[i], response[i])

    def test_decodeUserWithSlotUserIsGS(self):
        test_slot = {
            'gs_username': '******',
            'sc_username': '******'
        }
        self.sp.username = '******'
        response = self.sp.decode_user(test_slot)

        value = [
            'gs_user_test', 'sc_user_test', 'gs_user_test', 'sc_user_test'
        ]

        for i in range(len(response)):
            self.assertIs(value[i], response[i])

    def test_decodeUserWithoutSlot(self):
        """
        Tries to decode an user without having a slot.
        :return: assertRaises statement.
        """
        return self.assertRaises(SlotErrorNotification,
                                 self.sp.decode_user,
                                 slot=None)

    def test_checkSlotOwnershipRight(self):
        """
        Checks if the current slot belongs to spacecraft user or groundstation user.
        :return: assertIsNone statement.
        """
        self.sp.username = '******'

        sc_user = '******'
        gs_user = '******'

        print ""
        return self.assertIsNone(self.sp.check_slot_ownership(
            sc_user, gs_user))

    def test_checkSlotOwnershipWrong(self):
        """
        Checks if the current slot belongs to spacecraft user or groundstation user.
        :return: assertRaises statement.
        """
        self.sp.username = '******'

        sc_user = '******'
        gs_user = '******'

        print ""
        return self.assertRaises(SlotErrorNotification,
                                 self.sp.check_slot_ownership, sc_user,
                                 gs_user)

    def test_checkExpirationSlotOnTime(self):
        time_now = misc.localize_datetime_utc(datetime.utcnow())
        time_now = int(time.mktime(time_now.timetuple()))
        time_now = time_now + 60

        return self.assertEqual(self.sp.check_expiration(-1, time_now), 60)

    def test_checkExpirationSlotExpired(self):
        time_now = misc.localize_datetime_utc(datetime.utcnow())
        time_now = int(time.mktime(time_now.timetuple()))
        time_now = time_now - 60

        return self.assertRaises(SlotErrorNotification,
                                 self.sp.check_expiration, -1, time_now)
Exemple #11
0
class TestManageMessages(TestCase):
    def setUp(self):
        self.sp = CredReceiver()
        self.sp.factory = CredAMPServerFactory()

        self.correctFrame = (
            "00:82:a0:00:00:53:45:52:50:2d:42:30:91:1d:1b:03:" +
            "8d:0b:5c:03:02:28:01:9c:01:ab:02:4c:02:98:01:da:" +
            "02:40:00:00:00:10:0a:46:58:10:00:c4:9d:cb:a2:21:39")

    def tearDown(self):
        self.sp.factory.active_protocols = {}
        self.sp.factory.active_connections = {}
        self.sp.username = None

    def test_messageSentLocalAndRemoteUserOk(self):
        """
        Remove local protocol and the active connections which are involving it.
        callRemote methods are mocked for testing purposes.

        :return: assertTrue statement
        """
        sc_user = '******'
        gs_user = '******'
        username_test = 'test-user-sc'

        mockSC = Mock()
        mockSC.callRemote = MagicMock(return_value=True)
        mockGS = Mock()

        self.sp.factory.active_protocols[str(sc_user)] = mockSC
        self.sp.factory.active_protocols[str(gs_user)] = mockGS
        self.sp.factory.active_connections[str(sc_user)] = str(gs_user)
        self.sp.factory.active_connections[str(gs_user)] = str(sc_user)
        self.sp.username = username_test

        self.sp.rpc = Mock()
        self.sp.rpc.testing = MagicMock(return_value=True)

        time = misc.get_utc_timestamp()

        res = self.sp.vSendMsg(self.correctFrame, time)
        return self.assertTrue(res['bResult'])

    def test_messageNotSentLocalProtocolNotConnected(self):
        """
        Remove local protocol and the active connections which are involving it.
        callRemote methods are mocked for testing purposes.

        :return: assertRaises statement
        """
        sc_user = '******'
        username_test = 'test-user-sc'

        mockGS = Mock()
        mockGS.callRemote = MagicMock(return_value=True)

        self.sp.factory.active_protocols[str(sc_user)] = mockGS
        self.sp.username = username_test

        self.sp.rpc = Mock()
        self.sp.rpc.testing = Mock()
        self.sp.rpc.store_message = MagicMock(return_value=True)

        time = misc.get_utc_timestamp()

        return self.assertRaises(errors.SlotErrorNotification,
                                 self.sp.vSendMsg, self.correctFrame, time)

    # FIX-ME When it's run with the complete tests bunch raises True instead ValueError
    def _test_messageNotSentRemoteProtocolNotConnected(self):
        """
        Remove local protocol and the active connections which are involving it.
        callRemote methods are mocked for testing purposes.

        :return: assertRaises statement
        """
        sc_user = '******'
        gs_user = '******'
        username_test = 'test-user-sc'

        mockSC = Mock()
        mockSC.callRemote = MagicMock(return_value=True)

        self.sp.factory.active_protocols[str(sc_user)] = mockSC
        self.sp.factory.active_connections[str(sc_user)] = str(gs_user)
        self.sp.factory.active_connections[str(gs_user)] = str(sc_user)
        self.sp.username = username_test

        self.sp.rpc = Mock()
        self.sp.rpc.testing = Mock()
        self.sp.rpc.store_message = MagicMock(return_value=True)

        time = misc.get_utc_timestamp()

        return self.assertRaises(errors.ValueError, self.sp.vSendMsg,
                                 self.correctFrame, time)
class TestManageActiveConnections(TestCase):
    def setUp(self):
        self.sp = CredReceiver()
        self.sp.factory = CredAMPServerFactory()
        self.sp.factory.sUsername = '******'
        self.transport = StringTransportWithDisconnection()
        self.sp.makeConnection(self.transport)

        self.transport.protocol = self.sp

    def tearDown(self):
        self.sp.factory.active_protocols = {}
        self.sp.factory.active_connections = {}

    def test_localProtocolRemoveWhenSuddenlyDisconnects(self):
        """
        Remove local protocol and the active connections which are involving it.
        callRemote methods are mocked for testing purposes.

        :return: Assertion statement
        """
        sc_user = '******'
        gs_user = '******'
        username_test = 'test-user-sc'

        mockSC = Mock()
        mockSC.callRemote = MagicMock(return_value=True)
        mockGS = Mock()
        mockGS.callRemote = MagicMock(return_value=True)

        self.sp.factory.active_protocols[str(sc_user)] = mockSC
        self.sp.factory.active_protocols[str(gs_user)] = mockGS
        self.sp.factory.active_connections[str(sc_user)] = str(gs_user)
        self.sp.factory.active_connections[str(gs_user)] = str(sc_user)
        self.sp.username = username_test

        res = self.sp.vEndRemote()

        return self.assertTrue(res['bResult']), \
               self.assertIs(len(self.sp.factory.active_protocols), 1), \
               self.assertIs(len(self.sp.factory.active_connections), 0)

    def test_localProtocolRemoveWhenRemoteUserIsDisconnected(self):
        """
        Remove local protocol when remote user has already detached the connection.

        :return: Assertion statement
        """

        # Must fill active_protocols with mock objects
        sc_user = '******'
        username_test = 'test-user-sc'

        mockSC = Mock()
        mockSC.callRemote = MagicMock(return_value=True)
        mockGS = Mock()
        mockGS.callRemote = MagicMock(return_value=True)

        # self.sp.factory.active_protocols[str(sc_user)] = mockSC
        self.sp.factory.active_protocols[str(sc_user)] = mockSC
        self.sp.username = username_test

        res = self.sp.vEndRemote()

        return self.assertTrue(res['bResult']), \
               self.assertIs(len(self.sp.factory.active_protocols), 0), \
               self.assertIs(len(self.sp.factory.active_connections), 0)