Ejemplo n.º 1
0
 def setUpClass(cls):
     cls.password = '******'
     cls.user = models.User(username='******', email='*****@*****.**')
     cls.user.set_password(cls.password)
     cls.user.save()
     cls.user2 = models.User(username='******', email='*****@*****.**')
     cls.user2.set_password(cls.password)
     cls.user2.save()
     cls.user_profile = models.UserProfile.objects.get(user=cls.user)
     cls.user_profile2 = models.UserProfile.objects.get(user=cls.user2)
     # Create a BTS for each user profile.
     uuid = "59216199-d664-4b7a-a2db-6f26e9a5d323"
     inbound_url = "http://localhost:8090"
     name = "test-tower-user-one"
     cls.bts = models.BTS(uuid=uuid,
                          nickname=name,
                          inbound_url=inbound_url,
                          secret='ok',
                          network=cls.user_profile.network)
     cls.bts.save()
     uuid = "1eac9487-fc7c-4674-8c38-dab66d612453"
     inbound_url = "http://localhost:8090"
     name = "test-tower-user-two"
     cls.bts2 = models.BTS(uuid=uuid,
                           nickname=name,
                           inbound_url=inbound_url,
                           network=cls.user_profile2.network)
     cls.bts2.save()
     # Create two subscribers, one for each tower, each with a number.
     cls.imsi = "IMSI999990000000555"
     cls.sub = models.Subscriber(balance=100000,
                                 name='sub-one',
                                 imsi=cls.imsi,
                                 network=cls.bts.network,
                                 bts=cls.bts)
     cls.sub.save()
     cls.number = models.Number(number='6285574719324',
                                state="inuse",
                                network=cls.user_profile.network,
                                subscriber=cls.sub,
                                kind="number.nexmo.monthly")
     cls.number.save()
     cls.imsi2 = "IMSI999990000000556"
     cls.sub2 = models.Subscriber(balance=100000,
                                  name='sub-two',
                                  imsi=cls.imsi2,
                                  network=cls.bts2.network,
                                  bts=cls.bts2)
     cls.sub2.save()
     cls.number2 = models.Number(number='6285574719443',
                                 state="inuse",
                                 network=cls.user_profile2.network,
                                 subscriber=cls.sub2,
                                 kind="number.nexmo.monthly")
     cls.number2.save()
     # Create one last number, unattached to a subscriber.
     cls.number3 = models.Number(number='5551234',
                                 state="available",
                                 kind="number.nexmo.monthly")
     cls.number3.save()
Ejemplo n.º 2
0
 def setUpClass(cls):
     cls.user = models.User(username='******', email='*****@*****.**')
     cls.user.save()
     cls.user2 = models.User(username='******', email='*****@*****.**')
     cls.user2.save()
     cls.user_profile = models.UserProfile.objects.get(user=cls.user)
     cls.user_profile2 = models.UserProfile.objects.get(user=cls.user2)
     # Create a BTS.
     uuid = "59216199-d664-4b7a-a2db-6f26e9a5d324"
     inbound_url = "http://localhost:8090"
     name = "test-tower-user-one"
     cls.bts = models.BTS(uuid=uuid,
                          nickname=name,
                          inbound_url=inbound_url,
                          secret='ok',
                          network=cls.user_profile.network)
     cls.bts.save()
     # Create two subscribers, one for each tower, each with a number or
     # two.
     cls.imsi = "IMSI999990000000555"
     cls.sub = models.Subscriber(balance=100000,
                                 name='sub-one',
                                 imsi=cls.imsi,
                                 network=cls.bts.network,
                                 bts=cls.bts)
     cls.sub.save()
     cls.number = models.Number(number='6285574719324',
                                state="inuse",
                                network=cls.user_profile.network,
                                subscriber=cls.sub,
                                kind="number.nexmo.monthly")
     cls.number.save()
     cls.number2 = models.Number(number='6285574719443',
                                 state="inuse",
                                 network=cls.user_profile.network,
                                 subscriber=cls.sub,
                                 kind="number.nexmo.monthly")
     cls.number2.save()
     cls.imsi2 = "IMSI999990000000556"
     cls.sub2 = models.Subscriber(balance=100000,
                                  name='sub-two',
                                  imsi=cls.imsi2,
                                  network=cls.user_profile2.network,
                                  bts=None)
     cls.sub2.save()
     cls.number3 = models.Number(number='6285574719444',
                                 state="inuse",
                                 network=cls.user_profile2.network,
                                 subscriber=cls.sub2,
                                 kind="number.nexmo.monthly")
     cls.number3.save()
     cls.pcu = models.PendingCreditUpdate(subscriber=cls.sub,
                                          amount=100,
                                          uuid='abc123')
     cls.pcu.save()
Ejemplo n.º 3
0
 def setUpClass(cls):
     cls.user = models.User(username='******', email='*****@*****.**')
     cls.user.save()
     cls.user2 = models.User(username='******', email='*****@*****.**')
     cls.user2.save()
     cls.user_profile = models.UserProfile.objects.get(user=cls.user)
     cls.user_profile2 = models.UserProfile.objects.get(user=cls.user2)
     # Give the users some credit so they don't try to recharge.
     cls.initial_credit = endagaweb.util.currency.dollars2mc(50)
     cls.user_profile.network.add_credit(cls.initial_credit)
     cls.user_profile2.network.add_credit(cls.initial_credit)
     # Refresh the objects so the ledger balances reload.
     cls.user_profile = models.UserProfile.objects.get(
         id=cls.user_profile.id)
     cls.user_profile2 = models.UserProfile.objects.get(
         id=cls.user_profile2.id)
     # Create a BTS for each user profile.
     uuid = "59216199-d664-4b7a-a2db-6f26e9a5e203"
     inbound_url = "http://localhost:8090"
     name = "user1_testtower"
     cls.bts = models.BTS(uuid=uuid,
                          nickname=name,
                          inbound_url=inbound_url,
                          network=cls.user_profile.network)
     cls.bts.save()
     uuid = "1eac9487-fc7c-4674-8c38-dab66d6125c4"
     inbound_url = "http://localhost:8090"
     name = "user2_testtower"
     cls.bts2 = models.BTS(uuid=uuid,
                           nickname=name,
                           inbound_url=inbound_url,
                           network=cls.user_profile2.network)
     cls.bts2.save()
     # Create two numbers to test intra-network calls.
     cls.imsi = "IMSI999990000000000"
     cls.num = 6285574719464
     cls.number = models.Number(number=cls.num,
                                state="available",
                                network=cls.user_profile.network,
                                kind="number.nexmo.monthly")
     cls.number.save()
     cls.imsi2 = "IMSI999990000000001"
     cls.num2 = 6285574719465
     cls.number2 = models.Number(number=cls.num2,
                                 state="available",
                                 network=cls.user_profile2.network,
                                 kind="number.nexmo.monthly")
     cls.number2.save()
Ejemplo n.º 4
0
 def setUpClass(cls):
     """Using setUpClass so we don't create duplicate objects."""
     # Create a user, user profile, BTS and subscriber.
     cls.user = models.User(username="******", email="*****@*****.**")
     cls.user.save()
     cls.user_profile = models.UserProfile.objects.get(user=cls.user)
     cls.bts = models.BTS(uuid="zyxw9876",
                          nickname="test-bts",
                          inbound_url="http://localhost/inbound-test",
                          network=cls.user_profile.network)
     cls.bts.save()
     cls.subscriber_imsi = 'IMSI000987123456789'
     cls.subscriber = models.Subscriber.objects.create(
         balance=1000,
         name='cam-test-name',
         imsi=cls.subscriber_imsi,
         network=cls.bts.network)
     cls.subscriber.save()
     # Register a number with this subscriber.
     cls.subscriber_number = '5559876'
     registered_number = models.Number(number=cls.subscriber_number,
                                       state='inuse',
                                       kind='number.nexmo.monthly',
                                       subscriber=cls.subscriber)
     registered_number.save()
     # Set the registration endpoint.
     cls.endpoint = '/api/v1/register/'
Ejemplo n.º 5
0
 def setUp(self):
     self.client = test.Client()
     # Create an "available" number.
     self.available_number = models.Number(
         number='5554433', state='available', kind='number.nexmo.monthly',
         country_id="US")
     self.available_number.save()
Ejemplo n.º 6
0
    def setUpClass(cls):
        """Create a Network and BTS."""
        cls.network= models.Network.objects.create()
        cls.header = {
            'HTTP_AUTHORIZATION': 'Token %s' % cls.network.api_token
        }

        """Create a User and BTS."""
        cls.username = '******'
        cls.password = '******'
        cls.user = models.User(username=cls.username, email='*****@*****.**')
        cls.user.set_password(cls.password)
        cls.user.save()

        # mock out notifications' celery
        cls.old_celery_app = notifications.celery_app
        notifications.celery_app = mock.MagicMock()

        cls.user_profile = models.UserProfile.objects.get(user=cls.user)
        cls.uuid = "59216199-d664-4b7a-a2db-6f26e9a5d203"
        inbound_url = "http://localhost:8090"
        cls.bts = models.BTS(
            uuid=cls.uuid, nickname='test-name', inbound_url=inbound_url,
            secret=cls.uuid, network=cls.network, band='GSM900', channel=51)
        cls.bts.save()
        cls.sub = models.Subscriber(
            network=cls.network, bts=cls.bts,
            imsi='IMSI901550000000001', balance=0)
        cls.sub.save()
        cls.number = models.Number(number='5551234', state="inuse",
                                   network=cls.bts.network,
                                   kind="number.nexmo.monthly",
                                   subscriber=cls.sub)
        cls.number.save()
Ejemplo n.º 7
0
    def setUpClass(cls):
        """Create a User and BTS."""
        cls.username = '******'
        cls.password = '******'
        cls.user = models.User(username=cls.username, email='*****@*****.**')
        cls.user.set_password(cls.password)
        cls.user.save()
        cls.user_profile = models.UserProfile.objects.get(user=cls.user)
        # mock out notifications' celery
        cls.old_celery_app = notifications.celery_app
        notifications.celery_app = mock.MagicMock()

        uuid1 = "59216199-d664-4b7a-a2cb-6f26e9a5d203"
        uuid2 = "0e3e8bbc-1614-11e5-9f31-0124d722f2e0"
        inbound_url = "http://localhost:8090"
        cls.bts1 = models.BTS(
            uuid=uuid1, nickname='bts1', inbound_url=inbound_url,
            secret=uuid1, network=cls.user_profile.network)
        cls.bts1.save()
        cls.bts2 = models.BTS(
            uuid=uuid2, nickname='bts2', inbound_url=inbound_url,
            secret=uuid2, network=cls.user_profile.network)
        cls.bts2.save()
        cls.sub = models.Subscriber(
            network=cls.user_profile.network, bts=cls.bts1,
            imsi='IMSI901550000000001', balance=0)
        cls.sub.save()
        cls.number = models.Number(number='5551234', state="inuse",
                                   network=cls.bts1.network,
                                   kind="number.nexmo.monthly",
                                   subscriber=cls.sub)
        cls.number.save()
Ejemplo n.º 8
0
    def setUpClass(cls):
        cls.user = models.User(username="******", email="*****@*****.**")
        cls.password = '******'
        cls.user.set_password(cls.password)
        cls.user.save()
        cls.user_profile = models.UserProfile.objects.get(user=cls.user)

        cls.bts = models.BTS(
            uuid="12345abcd", nickname="testbts",
            inbound_url="http://localhost/test",
            network=cls.user_profile.network)
        cls.bts.save()

        cls.subscriber_imsi = 'IMSI000123'
        cls.subscriber_num = '5551234'
        cls.subscriber = models.Subscriber.objects.create(
            balance=100, name='test-name', imsi=cls.subscriber_imsi,
            network=cls.bts.network, bts=cls.bts)
        cls.subscriber.save()

        cls.number = models.Number(number=cls.subscriber_num, state="inuse",
                                   network=cls.user_profile.network,
                                   kind="number.nexmo.monthly",
                                   subscriber=cls.subscriber)
        cls.number.save()

        cls.adjust_credit_endpoint = (
            '/dashboard/subscribers/%s/adjust-credit' % cls.subscriber_imsi)
        # Drop all PCUs.
        for pcu in models.PendingCreditUpdate.objects.all():
            pcu.delete()
Ejemplo n.º 9
0
    def setUpClass(cls):
        # Setup a UserProfile, BTS, Subscriber and Number.
        cls.user = models.User(username="******", email="*****@*****.**")
        cls.user.save()
        cls.user_profile = models.UserProfile.objects.get(user=cls.user)
        # Add some initial credit to the user profile and disable CC recharge.
        cls.credit_amount = 10 * 1e5
        cls.user_profile.network.add_credit(cls.credit_amount)
        cls.user_profile.network.autoload_enable = False
        cls.user_profile.save()

        # mock out notifications' celery
        cls.old_celery_app = notifications.celery_app
        notifications.celery_app = mock.MagicMock()

        cls.bts = models.BTS(uuid="332244abc",
                             nickname="testbts",
                             inbound_url="http://localhost/test",
                             network=cls.user_profile.network)
        cls.bts.save()
        # Mark the BTS active so that it's not in a no-data state and thus,
        # when we try to get the network's lowest tower version, this BTS will
        # be considered.  This lowest version matters when activating certain
        # billing features.
        cls.bts.mark_active()
        cls.subscriber_number = '19195550987'
        cls.subscriber = models.Subscriber.objects.create(
            balance=2 * 1e5,
            name='test-subscriber',
            imsi='IMSI000123000',
            network=cls.bts.network,
            bts=cls.bts)
        cls.subscriber.save()
        cls.number = models.Number(number=cls.subscriber_number,
                                   state="inuse",
                                   network=cls.user_profile.network,
                                   kind="number.nexmo.monthly",
                                   subscriber=cls.subscriber)
        cls.number.save()
        # Mock the Providers used by the outbound handler.
        cls.original_providers = endagaweb.views.api.SendSMS.HANDLERS
        #this is a really ugly hack to make mocks work here
        endagaweb.views.api.SendSMS.HANDLERS = {
            "number.nexmo.monthly":
            (mock.Mock(), "id", "pw", "ib_sms", "ob_sms", "ib_voice")
        }
        # Mock the tasks used by the inbound handler.
        cls.original_tasks = endagaweb.views.api.tasks
        endagaweb.views.api.tasks = mock.Mock()
        # Setup the api client, SMS endpoints and the token-based auth.
        cls.client = Client()
        cls.send_endpoint = '/api/v1/send/'
        cls.inbound_endpoint = '/api/v1/inbound/'
        cls.header = {
            'HTTP_AUTHORIZATION':
            'Token %s' % cls.user_profile.network.api_token
        }
Ejemplo n.º 10
0
    def get(self, request, bts_uuid=None, number=None, format=None):
        """ Associate a number to a BTS.

        DEPRECATED (shasan 2016jan5) -- use the POST endpoint instead
        """
        if not (number or bts_uuid or "imsi" in request.GET):
            return Response("", status=status.HTTP_400_BAD_REQUEST)
        network = get_network_from_user(request.user)
        try:
            bts = models.BTS.objects.get(uuid=bts_uuid, network=network)
        except models.BTS.DoesNotExist:
            return Response("User is not associated with that BTS.",
                            status=status.HTTP_403_FORBIDDEN)
        # If the IMSI is already in use, and associated with another BTS,
        # prevent the registration of a new number.  However, we allow IMSIs
        # to register a second number on the IMSI's original BTS.
        imsi = request.GET['imsi']
        try:
            subscriber = models.Subscriber.objects.get(imsi=imsi)
            if subscriber.network != network:
                return Response("IMSI already registered",
                                status=status.HTTP_409_CONFLICT)
        except models.Subscriber.DoesNotExist:
            # Create a new subscriber if one matching this IMSI didn't already
            # exist.
            subscriber = models.Subscriber(network=network,
                                           imsi=imsi,
                                           balance=0,
                                           bts=bts)
            subscriber.save()

        with transaction.atomic():
            q = models.Number.objects.filter(number__exact="%s" % number)
            if len(q) > 0:
                n = q[0]
                # This is tricky. Numbers that get marked 'pending' will have
                # the network id already set, so this check fails and we set
                # the number as in-use. This is an artifact of the two-step
                # number registration process. So don't remove the network ID
                # check!
                if n.state != "available" and n.network != bts.network:
                    return Response("Number already in use.",
                                    status=status.HTTP_400_BAD_REQUEST)
                n.network = bts.network
                n.state = "inuse"
            else:
                # FIXME this should never happen -- all numbers should already
                # be in the system, unless we're associating an old BTS for the
                # first time (like w/ Bok)
                n = models.Number(number=number,
                                  state="inuse",
                                  network=bts.network)
            # Associate it with the subscriber and save.
            n.subscriber = subscriber
            n.save()
            return Response(None, status=status.HTTP_200_OK)
Ejemplo n.º 11
0
    def test_deactivate_number(self):
        """We can deactivate one of the Subscriber's numbers via POST.

        Pushes number into the 'available' state and disassociates the number
        from the subscriber.  Starts an async post task (which we will mock).
        Should also create a 'deactivate_number' UsageEvent.
        """
        # This subscriber currently has only one number and deleting the last
        # number is verboten, so first we've gotta add another number.
        new_number = models.Number(number='6285574719987',
                                   state="inuse",
                                   network=self.user_profile.network,
                                   subscriber=self.sub,
                                   kind="number.nexmo.monthly")
        new_number.save()
        # Deactivate the original number.
        url = '/api/v2/numbers/%s' % self.number.number
        data = {'state': 'available'}
        header = {
            'HTTP_AUTHORIZATION':
            'Token %s' % self.user_profile.network.api_token
        }
        with mock.patch('endagaweb.tasks.async_post.delay') as mocked_task:
            response = self.client.post(url, data=data, **header)
        self.assertEqual(200, response.status_code)
        # Reload the original number from the db and check its state.
        number = models.Number.objects.get(id=self.number.id)
        self.assertEqual(data['state'], number.state)
        # The subscriber should now have one associated number.  Reload the
        # sub from the db to verify.
        subscriber = models.Subscriber.objects.get(id=self.sub.id)
        self.assertEqual(new_number.number, subscriber.numbers())
        # The original number should not be associated with a Subscriber or a
        # Network.
        self.assertEqual(None, number.subscriber)
        self.assertEqual(None, number.network)
        # The mocked task should have been called with specific arguments
        self.assertTrue(mocked_task.called)
        args, _ = mocked_task.call_args
        task_endpoint, task_data = args
        expected_url = '%s/config/deactivate_number' % self.bts.inbound_url
        self.assertEqual(expected_url, task_endpoint)
        # The task_data should be signed with the BTS UUID and should have a
        # jwt key which is a dict with a number key.
        serializer = itsdangerous.JSONWebSignatureSerializer(self.bts.secret)
        task_data = serializer.loads(task_data['jwt'])
        self.assertEqual(number.number, task_data['number'])
        # A 'deactivate_number' UsageEvent should have been created.
        event = models.UsageEvent.objects.get(to_number=number.number,
                                              kind='deactivate_number')
        self.assertEqual('deactivated phone number: %s' % number.number,
                         event.reason)
Ejemplo n.º 12
0
 def setUp(self):
     self.user = models.User(username="******", email="*****@*****.**")
     self.user.save()
     self.user_profile = models.UserProfile.objects.get(user=self.user)
     self.bts = models.BTS(uuid="133222",
                           nickname="test-bts-name!",
                           inbound_url="http://localhost/133222/test",
                           network=self.user_profile.network)
     self.bts.save()
     self.subscriber = models.Subscriber.objects.create(
         balance=10000,
         name='test-sub-name',
         imsi='IMSI00123',
         network=self.bts.network)
     self.subscriber.save()
     self.number = models.Number(number='5551234',
                                 state="inuse",
                                 network=self.bts.network,
                                 kind="number.nexmo.monthly",
                                 subscriber=self.subscriber)
     self.number.save()
    def setUpClass(cls):
        """Generate a User, BTS and Number."""
        cls.email = "*****@*****.**"
        cls.user = models.User(username=cls.email, email=cls.email)
        cls.user.save()
        cls.user_profile = models.UserProfile.objects.get(user=cls.user)
        cls.uuid = "59216199-d664-4b7a-a2db-6f26e9a5d209"
        cls.inbound_url = "http://*****:*****@endaga.com"
        cls.user2 = models.User(username=cls.email2, email=cls.email2)
        cls.user2.save()
        cls.user_profile2 = models.UserProfile.objects.get(user=cls.user2)
        cls.user_profile2.network.bypass_gateway_auth = True
        cls.user_profile2.network.save()
        cls.uuid_whitelist = "59216899-d664-4b7a-a2db-6f26e9a5f205"
        cls.bts_whitelist = models.BTS(uuid=cls.uuid_whitelist,
                                       nickname='bts_whitelist',
                                       network=cls.user_profile2.network,
                                       inbound_url='http://localhost:8091')
        cls.bts_whitelist.save()
        imsi2 = "IMSI999990010000001"
        cls.sub2 = models.Subscriber(balance=10000,
                                     name='sub-three',
                                     imsi=imsi2,
                                     network=cls.user_profile2.network,
                                     bts=cls.bts_whitelist)
        cls.sub2.save()
        cls.num2 = 6285574719465
        cls.kind = "number.nexmo.monthly"
        cls.number2 = models.Number(number=cls.num2,
                                    state="inuse",
                                    network=cls.user_profile2.network,
                                    kind=cls.kind,
                                    subscriber=cls.sub2)
        cls.number2.save()