def test_acquire_wan_vessels_some_vessels_fail(self):

    # Have every other vessel acquisition fail. We're going to acquire 50,
    # so we'll need 100 responses alternating between failure and success
    # (we're starting with failure, so 100, not 99).
    calls_results = [False, True] * 50
    mocklib.mock_backend_acquire_vessel(calls_results)
    
    # Create a user who will be doing the acquiring.
    user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", "1 2", "2 2 2", "3 4")
    userport = user.usable_vessel_port
    
    # We need to give the user some donations so they have enough credits.
    # We're assuming it's 10 credits per donation.
    self.assertEqual(10, maindb.VESSEL_CREDITS_FOR_DONATIONS_MULTIPLIER)
    
    # Also make sure the user started with 10 credits.
    self.assertEqual(10, maindb.get_user_free_vessel_credits(user))
    
    # We need 100 nodes the user can acquire vessels on as we're having half of
    # the node acquisitions fail.
    testutil.create_nodes_on_different_subnets(100, [userport])
    
    # Now credit the user for donations on 4 of these.
    for node in maindb.get_active_nodes()[:4]:
      maindb.create_donation(node, user, '')
    
    # Ok, the user now has 50 vessels the can acquire and there are 100 nodes
    # with vessels available for them. Let's try to acquire all 50 at once and
    # make sure this works even though we'll have to get through 100 requests
    # to the backend to make it happen.
    vessel_list = interface.acquire_vessels(user, 50, 'wan')
    
    self.assertEqual(50, len(vessel_list))
  def test_acquire_specific_vessels(self):
    # 8 vessels will ultimately be acquired.
    calls_results = [True] * 8
    mocklib.mock_backend_acquire_vessel(calls_results)
    
    # Create a user who will be doing the acquiring.
    user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", "1 2", "2 2 2", "3 4")
    userport = user.usable_vessel_port
    
    vesselcount = maindb.get_user_free_vessel_credits(user)
    
    # We use userport + 1 to make sure the user isn't being restricted to only
    # vessels that have their user port in their port list.
    testutil.create_nodes_on_different_subnets(vesselcount + 10, [userport + 1])
    
    vessels = list(maindb._get_queryset_of_all_available_vessels_for_a_port_include_nat_nodes(userport + 1))
    
    # Request the first 4 vessels in the list.
    first_vessel_list = interface.acquire_specific_vessels(user, vessels[:4])
    
    # Now request the first 6 vessels in the list. We should only get 2.
    second_vessel_list = interface.acquire_specific_vessels(user, vessels[:6])

    self.assertEqual(4, len(first_vessel_list))
    self.assertEqual(2, len(second_vessel_list))
    
    # Now ask for more vessels than the user has available, regardless of the
    # fact that some of the requested vessels aren't available.
    requestcount = vesselcount - 6 + 1
    
    func = interface.acquire_specific_vessels
    args = (user, vessels[:requestcount])
    self.assertRaises(InsufficientUserResourcesError, func, *args)
Esempio n. 3
0
 def test_renew_vessels_insufficient_vessel_credits(self):
   
   # Create a user who will be doing the acquiring.
   user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", "1 2", "2 2 2", "3 4")
   userport = user.usable_vessel_port
   
   vesselcount = 4
   
   # Have every vessel acquisition to the backend request succeed.
   calls_results = [True] * vesselcount
   mocklib.mock_backend_acquire_vessel(calls_results)
   
   testutil.create_nodes_on_different_subnets(vesselcount, [userport])
   
   # Acquire all of the vessels the user can acquire.
   vessel_list = interface.acquire_vessels(user, vesselcount, 'rand')
   
   # Decrease the user's vessel credits to one less than the number of vessels
   # they have acquired.
   user.free_vessel_credits = 0
   user.save()
   
   func = interface.renew_vessels
   args = (user, vessel_list)
   self.assertRaises(InsufficientUserResourcesError, func, *args)      
 
   func = interface.renew_all_vessels
   args = (user,)
   self.assertRaises(InsufficientUserResourcesError, func, *args)   
  def test_acquire_wan_vessels_multiple_calls_rand(self):

    # Have every vessel acquisition to the backend request succeed.
    calls_results = [True] * 10
    mocklib.mock_backend_acquire_vessel(calls_results)
    
    # Create a user who will be doing the acquiring.
    user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", "1 2", "2 2 2", "3 4")
    userport = user.usable_vessel_port
    
    vesselcount = maindb.get_user_free_vessel_credits(user)
    
    # Create vesselcount nodes split between the different types.
    testutil.create_nodes_on_different_subnets(4, [userport])
    testutil.create_nodes_on_same_subnet(4, [userport])
    testutil.create_nat_nodes(vesselcount - 8, [userport])
    
    # First request a single vessel.
    first_vessel_list = interface.acquire_vessels(user, 1, 'rand')
    
    # Now acquire all of the rest that the user can acquire.
    second_vessel_list = interface.acquire_vessels(user, vesselcount - 1, 'rand')

    self.assertEqual(1, len(first_vessel_list))
    self.assertEqual(vesselcount - 1, len(second_vessel_list))
Esempio n. 5
0
  def test_renew_some_of_users_vessel(self):
    
    # Create a user who will be doing the acquiring.
    user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", "1 2", "2 2 2", "3 4")
    userport = user.usable_vessel_port
    
    vesselcount = 4
    
    # Have every vessel acquisition to the backend request succeed.
    calls_results = [True] * vesselcount
    mocklib.mock_backend_acquire_vessel(calls_results)
    
    testutil.create_nodes_on_different_subnets(vesselcount, [userport])
    
    # Acquire all of the vessels the user can acquire.
    vessel_list = interface.acquire_vessels(user, vesselcount, 'rand')
    
    renew_vessels_list = vessel_list[:2]
    not_renewed_vessels_list = vessel_list[2:]
    
    interface.renew_vessels(user, renew_vessels_list)
  
    now = datetime.datetime.now()
    timedelta_oneday = datetime.timedelta(days=1)
  
    for vessel in renew_vessels_list:
      self.assertTrue(vessel.date_expires - now > timedelta_oneday)

    for vessel in not_renewed_vessels_list:
      self.assertTrue(vessel.date_expires - now < timedelta_oneday)
  def test_acquire_specific_vessels(self):
    # 8 vessels will ultimately be acquired.
    calls_results = [True] * 8
    mocklib.mock_backend_acquire_vessel(calls_results)
    
    # Create a user who will be doing the acquiring.
    user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", "1 2", "2 2 2", "3 4")
    userport = user.usable_vessel_port
    
    vesselcount = maindb.get_user_free_vessel_credits(user)
    
    # We use userport + 1 to make sure the user isn't being restricted to only
    # vessels that have their user port in their port list.
    testutil.create_nodes_on_different_subnets(vesselcount + 10, [userport + 1])
    
    vessels = list(maindb._get_queryset_of_all_available_vessels_for_a_port_include_nat_nodes(userport + 1))
    
    # Request the first 4 vessels in the list.
    first_vessel_list = interface.acquire_specific_vessels(user, vessels[:4])
    
    # Now request the first 6 vessels in the list. We should only get 2.
    second_vessel_list = interface.acquire_specific_vessels(user, vessels[:6])

    self.assertEqual(4, len(first_vessel_list))
    self.assertEqual(2, len(second_vessel_list))
    
    # Now ask for more vessels than the user has available, regardless of the
    # fact that some of the requested vessels aren't available.
    requestcount = vesselcount - 6 + 1
    
    func = interface.acquire_specific_vessels
    args = (user, vessels[:requestcount])
    self.assertRaises(InsufficientUserResourcesError, func, *args)
  def test_acquire_wan_vessels_some_vessels_fail(self):

    # Have every other vessel acquisition fail. We're going to acquire 50,
    # so we'll need 100 responses alternating between failure and success
    # (we're starting with failure, so 100, not 99).
    calls_results = [False, True] * 50
    mocklib.mock_backend_acquire_vessel(calls_results)
    
    # Create a user who will be doing the acquiring.
    user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", "1 2", "2 2 2", "3 4")
    userport = user.usable_vessel_port
    
    # We need to give the user some donations so they have enough credits.
    # We're assuming it's 10 credits per donation.
    self.assertEqual(10, maindb.VESSEL_CREDITS_FOR_DONATIONS_MULTIPLIER)
    
    # Also make sure the user started with 10 credits.
    self.assertEqual(10, maindb.get_user_free_vessel_credits(user))
    
    # We need 100 nodes the user can acquire vessels on as we're having half of
    # the node acquisitions fail.
    testutil.create_nodes_on_different_subnets(100, [userport])
    
    # Now credit the user for donations on 4 of these.
    for node in maindb.get_active_nodes()[:4]:
      maindb.create_donation(node, user, '')
    
    # Ok, the user now has 50 vessels the can acquire and there are 100 nodes
    # with vessels available for them. Let's try to acquire all 50 at once and
    # make sure this works even though we'll have to get through 100 requests
    # to the backend to make it happen.
    vessel_list = interface.acquire_vessels(user, 50, 'wan')
    
    self.assertEqual(50, len(vessel_list))
  def test_acquire_wan_vessels_multiple_calls_rand(self):

    # Have every vessel acquisition to the backend request succeed.
    calls_results = [True] * 10
    mocklib.mock_backend_acquire_vessel(calls_results)
    
    # Create a user who will be doing the acquiring.
    user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", "1 2", "2 2 2", "3 4")
    userport = user.usable_vessel_port
    
    vesselcount = maindb.get_user_free_vessel_credits(user)
    
    # Create vesselcount nodes split between the different types.
    testutil.create_nodes_on_different_subnets(4, [userport])
    testutil.create_nodes_on_same_subnet(4, [userport])
    testutil.create_nat_nodes(vesselcount - 8, [userport])
    
    # First request a single vessel.
    first_vessel_list = interface.acquire_vessels(user, 1, 'rand')
    
    # Now acquire all of the rest that the user can acquire.
    second_vessel_list = interface.acquire_vessels(user, vesselcount - 1, 'rand')

    self.assertEqual(1, len(first_vessel_list))
    self.assertEqual(vesselcount - 1, len(second_vessel_list))
  def test_regenerate_user_key(self):
    
    pubkey = "1 2"
    privkey = "3 4 5"
    donor_key = "6 7"
    
    # Create a user who will be doing the acquiring.
    user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", 
                              pubkey, privkey, donor_key)
    userport = user.usable_vessel_port

    vesselcount = 4
    
    # Have every vessel acquisition to the backend request succeed.
    calls_results = [True] * vesselcount
    mocklib.mock_backend_acquire_vessel(calls_results)
    
    testutil.create_nodes_on_different_subnets(vesselcount, [userport])

    # Acquire all vessels on behalf of this user.
    all_vessels_list = interface.acquire_vessels(user, vesselcount, 'rand')

    # Release 2 vessels.
    released_vessels_list = all_vessels_list[:2]
    kept_vessels_list = all_vessels_list[2:]
    interface.release_vessels(user, released_vessels_list)
    
    # Ensure all of the vessels are marked as having user keys in sync.
    for vessel in all_vessels_list:
      # Get a fresh vessel from the db.
      vessel = maindb.get_vessel(vessel.node.node_identifier, vessel.name)
      self.assertTrue(vessel.user_keys_in_sync)

    # We expect a single key to be generated through the keygen api (the new
    # user public key).
    mocklib.mock_keygen_generate_keypair([("55 66", "77 88 99")])
    
    interface.change_user_keys(user, pubkey=None)
    
    # Get a new user object from the database.
    user = maindb.get_user(user.username)
    
    # Make sure the user's key changed.
    self.assertEqual(user.user_pubkey, "55 66")
    self.assertEqual(user.user_privkey, "77 88 99")
    
    # Make sure that all of the vessels the user has access to (and no other
    # vessels) are marked as needing user keys to be sync'd.
    # Ensure all of the vessels are marked as having user keys in sync.
    for vessel in kept_vessels_list:
      # Get a fresh vessel from the db.
      vessel = maindb.get_vessel(vessel.node.node_identifier, vessel.name)
      self.assertFalse(vessel.user_keys_in_sync)

    for vessel in released_vessels_list:
      # Get a fresh vessel from the db.
      vessel = maindb.get_vessel(vessel.node.node_identifier, vessel.name)
      self.assertTrue(vessel.user_keys_in_sync)
Esempio n. 10
0
    def test_renew_vessels_dont_belong_to_user(self):

        # Create a user who will be doing the acquiring.
        user = maindb.create_user("testuser", "password",
                                  "*****@*****.**", "affiliation", "1 2",
                                  "2 2 2", "3 4")
        userport = user.usable_vessel_port

        # Create a second user.
        user2 = maindb.create_user("user2", "password", "*****@*****.**",
                                   "affiliation", "1 2", "2 2 2", "3 4")

        vesselcount = 4

        # Have every vessel acquisition to the backend request succeed.
        calls_results = [True] * vesselcount
        mocklib.mock_backend_acquire_vessel(calls_results)

        testutil.create_nodes_on_different_subnets(vesselcount, [userport])

        # Acquire all of the vessels the user can acquire.
        vessel_list = interface.acquire_vessels(user, vesselcount, 'rand')

        release_vessel = vessel_list[0]
        interface.release_vessels(user, [release_vessel])

        # Manually fiddle with one of the vessels to make it owned by user2.
        user2_vessel = vessel_list[1]
        user2_vessel.acquired_by_user = user2
        user2_vessel.save()

        # Try to renew all of the originally acquired vessels, including the ones
        # that were released. We expect these to just be ignored.
        interface.renew_vessels(user, vessel_list)

        # Get fresh vessel objects that reflect the renewal.
        remaining_vessels = interface.get_acquired_vessels(user)
        release_vessel = maindb.get_vessel(release_vessel.node.node_identifier,
                                           release_vessel.name)
        user2_vessel = maindb.get_vessel(user2_vessel.node.node_identifier,
                                         user2_vessel.name)

        now = datetime.datetime.now()
        timedelta_oneday = datetime.timedelta(days=1)

        # Ensure that the vessels the user still has were renewed but that the ones
        # the user released were ignored (not renewed).
        for vessel in remaining_vessels:
            self.assertTrue(vessel.date_expires - now > timedelta_oneday)

        self.assertTrue(user2_vessel.date_expires - now < timedelta_oneday)

        self.assertEqual(release_vessel.date_expires, None)
  def test_renew_vessels_dont_belong_to_user(self):
    
    # Create a user who will be doing the acquiring.
    user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", "1 2", "2 2 2", "3 4")
    userport = user.usable_vessel_port
    
    # Create a second user.
    user2 = maindb.create_user("user2", "password", "*****@*****.**", "affiliation", "1 2", "2 2 2", "3 4")
    
    vesselcount = 4
    
    # Have every vessel acquisition to the backend request succeed.
    calls_results = [True] * vesselcount
    mocklib.mock_backend_acquire_vessel(calls_results)
    
    testutil.create_nodes_on_different_subnets(vesselcount, [userport])
    
    # Acquire all of the vessels the user can acquire.
    vessel_list = interface.acquire_vessels(user, vesselcount, 'rand')
    
    release_vessel = vessel_list[0]
    interface.release_vessels(user, [release_vessel])
    
    # Manually fiddle with one of the vessels to make it owned by user2.
    user2_vessel = vessel_list[1]
    user2_vessel.acquired_by_user = user2
    user2_vessel.save()
    
    # Try to renew all of the originally acquired vessels, including the ones
    # that were released. We expect these to just be ignored.
    interface.renew_vessels(user, vessel_list)
  
    # Get fresh vessel objects that reflect the renewal.
    remaining_vessels = interface.get_acquired_vessels(user)
    release_vessel = maindb.get_vessel(release_vessel.node.node_identifier, release_vessel.name)
    user2_vessel = maindb.get_vessel(user2_vessel.node.node_identifier, user2_vessel.name)
   
    now = datetime.datetime.now()
    timedelta_oneday = datetime.timedelta(days=1)
  
    # Ensure that the vessels the user still has were renewed but that the ones
    # the user released were ignored (not renewed).
    for vessel in remaining_vessels:
      self.assertTrue(vessel.date_expires - now > timedelta_oneday)

    self.assertTrue(user2_vessel.date_expires - now < timedelta_oneday)
    
    self.assertEqual(release_vessel.date_expires, None)
    def test_set_user_key(self):

        pubkey = "1 2"
        privkey = "3 4 5"
        donor_key = "6 7"

        # Create a user who will be doing the acquiring.
        user = maindb.create_user("testuser", "password",
                                  "*****@*****.**", "affiliation", pubkey,
                                  privkey, donor_key)
        userport = user.usable_vessel_port

        vesselcount = 4

        # Have every vessel acquisition to the backend request succeed.
        calls_results = [True] * vesselcount
        mocklib.mock_backend_acquire_vessel(calls_results)

        testutil.create_nodes_on_different_subnets(vesselcount, [userport])

        # Acquire all vessels on behalf of this user.
        all_vessels_list = interface.acquire_vessels(user, vesselcount, 'rand')

        # Release 2 vessels.
        released_vessels_list = all_vessels_list[:2]
        kept_vessels_list = all_vessels_list[2:]
        interface.release_vessels(user, released_vessels_list)

        # Ensure all of the vessels are marked as having user keys in sync.
        for vessel in all_vessels_list:
            # Get a fresh vessel from the db.
            vessel = maindb.get_vessel(vessel.node.node_identifier,
                                       vessel.name)
            self.assertTrue(vessel.user_keys_in_sync)

        # We expect no keys to be generated through the keygen api.
        mocklib.mock_keygen_generate_keypair([])

        interface.change_user_keys(user, pubkey="55 66")

        # Get a new user object from the database.
        user = maindb.get_user(user.username)

        # Make sure the user's key changed.
        self.assertEqual(user.user_pubkey, "55 66")
        self.assertEqual(user.user_privkey, None)

        # Make sure that all of the vessels the user has access to (and no other
        # vessels) are marked as needing user keys to be sync'd.
        # Ensure all of the vessels are marked as having user keys in sync.
        for vessel in kept_vessels_list:
            # Get a fresh vessel from the db.
            vessel = maindb.get_vessel(vessel.node.node_identifier,
                                       vessel.name)
            self.assertFalse(vessel.user_keys_in_sync)

        for vessel in released_vessels_list:
            # Get a fresh vessel from the db.
            vessel = maindb.get_vessel(vessel.node.node_identifier,
                                       vessel.name)
            self.assertTrue(vessel.user_keys_in_sync)