示例#1
0
def set_up_user(mobile_number,imei,token_string):


    print "set_up_user - setting up a user started."
    print "set_up_user - creating a new user."
    new_user = User.objects.create_user(username = mobile_number)

    print "set_up_user - creating a new profile."

    new_profile = UserProfile(user=new_user)

    if imei != None:
        new_profile.imei = imei

    new_profile.last_visit = timezone.now()
    new_profile.is_active = True
    new_profile.save()

    print "set_up_user - user profile set up completed. user profile id: %d" % new_profile.id
    print "set_up_user - creating MobileDevice"

    mobile_device = MobileDevice(user_profile = new_profile,
                                 imei = imei,
                                 token_string = token_string,
                                 sms_verification_code = generate_sms_verification_code(imei),
                                 sms_code_expiery = timezone.now() + datetime.timedelta(minutes=5)
                                )

    mobile_device.save()
    print "set_up_user - creating MobileDevice done, imei:%s" % imei

    return (new_profile,mobile_device)
示例#2
0
    def setUp(self):
        self.user = User.objects.create_user('test', '*****@*****.**', 'pw')
        token = '6F39E0E8BDED48269D5C3C55D43A6032A75F8D1AF0EE4A6E9F7516B40C6250EA'
        self.device = MobileDevice(user=self.user, devicetoken=token)
        self.device.save()
        self.old_get_connection = MobileDevice.get_push_service_connection

        @classmethod
        def mock_get_connection(cls):
            class MockSocket(object):
                def send(self, msg):
                    pass

                def close(self):
                    pass

            return MockSocket()

        MobileDevice.get_push_service_connection = mock_get_connection
示例#3
0
    def setUp(self):
        self.user = User.objects.create_user('test', '*****@*****.**', 'pw')
        token = '6F39E0E8BDED48269D5C3C55D43A6032A75F8D1AF0EE4A6E9F7516B40C6250EA'
        self.device = MobileDevice(user=self.user, devicetoken=token)
        self.device.save()
        self.old_get_connection = MobileDevice.get_push_service_connection

        @classmethod
        def mock_get_connection(cls):
            class MockSocket(object):
                 def send(self, msg):
                     pass

                 def close(self):
                     pass

            return MockSocket()

        MobileDevice.get_push_service_connection = mock_get_connection
示例#4
0
class PushMesageTests(TestCase):
    def setUp(self):
        self.user = User.objects.create_user('test', '*****@*****.**', 'pw')
        token = '6F39E0E8BDED48269D5C3C55D43A6032A75F8D1AF0EE4A6E9F7516B40C6250EA'
        self.device = MobileDevice(user=self.user, devicetoken=token)
        self.device.save()
        self.old_get_connection = MobileDevice.get_push_service_connection

        @classmethod
        def mock_get_connection(cls):
            class MockSocket(object):
                 def send(self, msg):
                     pass

                 def close(self):
                     pass

            return MockSocket()

        MobileDevice.get_push_service_connection = mock_get_connection

    def tearDown(self):
        MobileDevice.get_push_service_connection = self.old_get_connection

    def test_message_creation(self):
        PushMessage.queue_message(self.user, 'hi')
        self.assertTrue(PushMessage.objects.all().count() > 0)

    def test_message_sent(self):
        PushMessage.queue_message(self.user, 'hi')
        unsent = PushMessage.objects.filter(sent=False).count()
        self.assertEqual(1, unsent)

        PushMessage.send_pending_messages()

        unsent = PushMessage.objects.filter(sent=False).count()
        self.assertEqual(0, unsent)

    def test_adding_data_dict(self):
        PushMessage.queue_message(self.user, 'hi', data={'key': 'value'})
        self.assertTrue(PushMessage.objects.all().count() > 0)

        PushMessage.send_pending_messages()

        unsent = PushMessage.objects.filter(sent=False).count()
        self.assertEqual(0, unsent)

    def test_adding_data_str(self):
        PushMessage.queue_message(self.user, 'hi', data="string")
        self.assertTrue(PushMessage.objects.all().count() > 0)

        PushMessage.send_pending_messages()

        unsent = PushMessage.objects.filter(sent=False).count()
        self.assertEqual(0, unsent)

    def test_custom_badge_counter(self):
        self.msg_str = ''

        @classmethod
        def mock_get_connection(cls):
            class MockSocket(object):
                 def send(mockself, msg):
                     self.msg_str = str(msg)

                 def close(mockself):
                     pass

            return MockSocket()

        MobileDevice.get_push_service_connection = mock_get_connection
        PushMessage.queue_message(self.user, 'hi')
        with override_settings(NOTIFY_BADGE='notify.tests.MockBadgeCounter'):
            PushMessage.send_pending_messages()

        self.assertTrue(0 < self.msg_str.find('badge":42'))

    def test_future_schedule(self):
        future = datetime.max.replace(tzinfo=timezone.utc)
        PushMessage.queue_message(self.user, 'hi', readytime=future)
        self.assertTrue(PushMessage.objects.filter(sent=False).count() > 0)
        send_count = PushMessage.send_pending_messages()
        self.assertEqual(0, send_count)
        self.assertTrue(PushMessage.objects.filter(sent=False).count() > 0)

    def test_no_msg_no_connection(self):
        """
        Verifies that a socket is not opened if there are no messages to send
        """
        self.sock_created = False
        @classmethod
        def mock_get_connection(cls):
            class MockSocket(object):
                def __init__(other): self.sock_created = True
                def send(other, msg): return None
                def close(other): return None

            return MockSocket()

        MobileDevice.get_push_service_connection = mock_get_connection
        call_command('send_push_notifications')

        self.assertFalse(self.sock_created)
示例#5
0
class PushMesageTests(TestCase):
    def setUp(self):
        self.user = User.objects.create_user('test', '*****@*****.**', 'pw')
        token = '6F39E0E8BDED48269D5C3C55D43A6032A75F8D1AF0EE4A6E9F7516B40C6250EA'
        self.device = MobileDevice(user=self.user, devicetoken=token)
        self.device.save()
        self.old_get_connection = MobileDevice.get_push_service_connection

        @classmethod
        def mock_get_connection(cls):
            class MockSocket(object):
                def send(self, msg):
                    pass

                def close(self):
                    pass

            return MockSocket()

        MobileDevice.get_push_service_connection = mock_get_connection

    def tearDown(self):
        MobileDevice.get_push_service_connection = self.old_get_connection

    def test_message_creation(self):
        PushMessage.queue_message(self.user, 'hi')
        self.assertTrue(PushMessage.objects.all().count() > 0)

    def test_message_sent(self):
        PushMessage.queue_message(self.user, 'hi')
        unsent = PushMessage.objects.filter(sent=False).count()
        self.assertEqual(1, unsent)

        PushMessage.send_pending_messages()

        unsent = PushMessage.objects.filter(sent=False).count()
        self.assertEqual(0, unsent)

    def test_adding_data_dict(self):
        PushMessage.queue_message(self.user, 'hi', data={'key': 'value'})
        self.assertTrue(PushMessage.objects.all().count() > 0)

        PushMessage.send_pending_messages()

        unsent = PushMessage.objects.filter(sent=False).count()
        self.assertEqual(0, unsent)

    def test_adding_data_str(self):
        PushMessage.queue_message(self.user, 'hi', data="string")
        self.assertTrue(PushMessage.objects.all().count() > 0)

        PushMessage.send_pending_messages()

        unsent = PushMessage.objects.filter(sent=False).count()
        self.assertEqual(0, unsent)

    def test_custom_badge_counter(self):
        self.msg_str = ''

        @classmethod
        def mock_get_connection(cls):
            class MockSocket(object):
                def send(mockself, msg):
                    self.msg_str = str(msg)

                def close(mockself):
                    pass

            return MockSocket()

        MobileDevice.get_push_service_connection = mock_get_connection
        PushMessage.queue_message(self.user, 'hi')
        with override_settings(NOTIFY_BADGE='notify.tests.MockBadgeCounter'):
            PushMessage.send_pending_messages()

        self.assertTrue(0 < self.msg_str.find('badge":42'))

    def test_future_schedule(self):
        future = datetime.max.replace(tzinfo=timezone.utc)
        PushMessage.queue_message(self.user, 'hi', readytime=future)
        self.assertTrue(PushMessage.objects.filter(sent=False).count() > 0)
        send_count = PushMessage.send_pending_messages()
        self.assertEqual(0, send_count)
        self.assertTrue(PushMessage.objects.filter(sent=False).count() > 0)

    def test_no_msg_no_connection(self):
        """
        Verifies that a socket is not opened if there are no messages to send
        """
        self.sock_created = False

        @classmethod
        def mock_get_connection(cls):
            class MockSocket(object):
                def __init__(other):
                    self.sock_created = True

                def send(other, msg):
                    return None

                def close(other):
                    return None

            return MockSocket()

        MobileDevice.get_push_service_connection = mock_get_connection
        call_command('send_push_notifications')

        self.assertFalse(self.sock_created)
示例#6
0
def register(request,format=None):
    """
    creates a new user by having her mobile number
    """
    if request.method == 'POST':

        mobile_number = request.data.get('mobile_no')
        imei = request.data.get('imei')
        token_string = request.data.get('token')

        if is_mobile_valid(mobile_number):

            result = {}

            try:
                new_user = User.objects.get(username=mobile_number)
            except User.DoesNotExist:
                # this is a new user
                print "register - the user is new"
                (user_profile,mobile_device) = set_up_user(mobile_number,imei,token_string)
                result['outcome'] = "new_customer"
                result['sms_code'] = mobile_device.sms_verification_code
                return Response(result,status=status.HTTP_201_CREATED)

            else:
                # the user already exists. check if there is a new token.
                try:
                    print "register - the user already exists. Checking the token"
                    existing_user = new_user

                    print "register - get user profile"

                    try:
                        user_profile = UserProfile.objects.get(user=existing_user)
                    except UserProfile.DoesNotExist:
                        user_profile = UserProfile(user=existing_user,
                                                   last_visit = timezone.now(),
                                                   is_active = True)
                        result['alert'] = "user existed, but the user profile did not, so we created it. "
                        user_profile.save()

                    print "register - get user mobile device"
                    mobile_device = MobileDevice.objects.get(user_profile=user_profile,imei=imei)

                except MobileDevice.DoesNotExist:
                    # user exists, imei does not
                    # the user has changed his phone
                    # and then new imei and token must be stored.
                    mobile_device = MobileDevice(user_profile = user_profile,
                                                 imei = imei,
                                                 token_string = token_string,
                                                 sms_verification_code = generate_sms_verification_code(imei),
                                                 sms_code_expiery = timezone.now() + datetime.timedelta(minutes=5)
                                                 )
                    mobile_device.save()
                    result['outcome'] = "returning_customer with new imei (new phone)"
                    result['sms_code'] = mobile_device.sms_verification_code

                else:
                    # new token for the same mobile phone and user
                    # and has uninstalled/installed the program.
                    mobile_device.token_string = token_string
                    mobile_device.sms_verification_code = generate_sms_verification_code(imei)
                    mobile_device.sms_code_expiery = timezone.now() + datetime.timedelta(minutes=5)
                    mobile_device.save()
                    result['outcome'] = "returning_customer who has unistalled and installed the program (same imei new token)"
                    result['sms_code'] = mobile_device.sms_verification_code


                finally:
                    return Response(result, status=status.HTTP_200_OK)

        else:
            return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)