Exemple #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()
Exemple #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()
 def setUpClass(cls):
     """Add some test data."""
     # Setup a User and UserProfile.
     user = models.User(username="******", email="*****@*****.**")
     user.set_password("testpw")
     user.save()
     user_profile = models.UserProfile.objects.get(user=user)
     # Setup two Networks.
     cls.network_a = models.Network(name='network-a')
     cls.network_a.save()
     cls.network_b = models.Network(name='network-b')
     cls.network_b.save()
     # Setup two BTS on different networks with the same user.
     bts_one = models.BTS(uuid='59216199-d664-4b7a-a2db-6f26e9a5d204',
                          nickname='tower-nickname-100',
                          inbound_url='http://localhost:8090',
                          network=cls.network_a)
     bts_one.save()
     bts_two = models.BTS(uuid='59216199-d664-4b7a-a2db-6f26e9a5d205',
                          nickname='tower-nickname-200',
                          inbound_url='http://localhost:8090',
                          network=cls.network_b)
     bts_two.save()
     # Add two Subscribers.
     subscriber_a = models.Subscriber(
         network=bts_one.network, imsi='IMSI999990000000000',
         name='subscriber a', balance=10000, state='active')
     subscriber_a.save()
     subscriber_b = models.Subscriber(
         network=bts_two.network, imsi='IMSI999990000000001',
         name='subscriber b', balance=20000, state='active')
     subscriber_b.save()
     # Add some UsageEvents for the BTS and Subscribers.
     cls.number_of_outside_sms_bts_one = 10
     cls.number_of_local_sms_bts_two = 20
     cls.number_of_outside_calls_bts_one = 30
     cls.number_of_local_calls_bts_two = 40
     cls.number_of_gprs_events_bts_one = 50
     cls.number_of_gprs_events_bts_two = 60
     _add_usage_events(subscriber_a, bts_one, 'outside_sms',
                       cls.number_of_outside_sms_bts_one)
     _add_usage_events(subscriber_b, bts_two, 'local_sms',
                       cls.number_of_local_sms_bts_two)
     _add_usage_events(subscriber_a, bts_one, 'outside_call',
                       cls.number_of_outside_calls_bts_one)
     _add_usage_events(subscriber_b, bts_two, 'local_call',
                       cls.number_of_local_calls_bts_two)
     _add_usage_events(subscriber_a, bts_one, 'gprs',
                       cls.number_of_gprs_events_bts_one)
     _add_usage_events(subscriber_b, bts_two, 'gprs',
                       cls.number_of_gprs_events_bts_two)
     # Keep references to these objects so we can destroy them later.
     # TODO(matt): also grab all the usage_events that are generated.
     cls.objects = [user, user_profile, cls.network_a, cls.network_b,
                    bts_one, bts_two, subscriber_a, subscriber_b]
Exemple #4
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)
     uuid = "59216199-d664-4b7a-a2db-6f26e9a3d323"
     inbound_url = "http://localhost:8090"
     name = "test-tower-user-one"
     package_versions = {'endaga_version': '00000.00003.00020'}
     cls.bts = models.BTS(uuid=uuid,
                          nickname=name,
                          inbound_url=inbound_url,
                          secret='ok',
                          network=cls.user_profile.network,
                          package_versions=json.dumps(package_versions))
     cls.bts.save()
     # Create an attached Subscriber and a UsageEvent.
     cls.sub = models.Subscriber(balance=100000,
                                 name='sub-one',
                                 imsi='IMSI999990000000555',
                                 network=cls.bts.network,
                                 bts=cls.bts)
     cls.sub.save()
     cls.event = models.UsageEvent(subscriber=cls.sub,
                                   bts=cls.bts,
                                   kind='local_sms',
                                   reason='test',
                                   date=datetime.datetime.now(pytz.utc))
     cls.event.save()
    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()
    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()
    def post(self, request, format=None):
        """ Request a number and associate with a subscriber. """
        if not ("bts_uuid" in request.POST or "imsi" in request.POST):
            return Response("", status=status.HTTP_400_BAD_REQUEST)

        bts_uuid = str(request.POST['bts_uuid'])
        imsi = str(request.POST['imsi'])

        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 network,
        # prevent the registration of a new number. If it's associated with
        # this network, simply return the currently-associated number. N.B.,
        # this effectively enforces a 1-1 mapping of subscriber to number
        # currently.
        try:
            subscriber = models.Subscriber.objects.get(imsi=imsi)
            if subscriber.network != network:
                return Response("IMSI already registered to another network",
                                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()

        # If the subscriber already exists, we should return the associated
        # phone number and update the BTS to match what is being used.
        n = models.Number.objects.filter(subscriber=subscriber).first()
        if not n:  # Otherwise, pick a random available number and associate it.
            with transaction.atomic():
                n = models.Number.objects.filter(
                    state="available",
                    country_id=network.number_country).first()

                if not n:
                    return Response("No number available",
                                    status=status.HTTP_404_NOT_FOUND)

                n.state = "inuse"
                n.network = network
                n.subscriber = subscriber
                n.save()
                n.charge()

        return Response(
            {
                'number': n.number,
                'subscriber': subscriber.imsi,
                'balance': subscriber.balance
            },
            status=status.HTTP_200_OK)
Exemple #8
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)
    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()