def test_Deploy_is_actionable_if_user_has_ssh_keys(self): owner = factory.make_User() factory.make_SSHKey(owner) node = factory.make_Node( interface=True, status=NODE_STATUS.ALLOCATED, power_type='manual', owner=owner) self.assertTrue(Deploy(node, owner).is_actionable())
def test_migrate_ancillary_data_to_legacy_user_when_multiple_users(self): factory.make_FileStorage(owner=None) # Create two users, both with API credentials, an SSH key and a node. user1 = factory.make_User() consumer1, token1 = user1.userprofile.create_authorisation_token() key1 = factory.make_SSHKey(user1, get_ssh_key_string(1)) node1 = factory.make_Node(owner=user1) user2 = factory.make_User() consumer2, token2 = user2.userprofile.create_authorisation_token() key2 = factory.make_SSHKey(user2, get_ssh_key_string(2)) node2 = factory.make_Node(owner=user2) migrate() # The SSH keys have been copied to the legacy user. legacy_user = get_legacy_user() legacy_users_ssh_keys = get_ssh_keys(legacy_user) self.assertSetEqual({key1.key, key2.key}, set(legacy_users_ssh_keys)) # The API credentials have been moved to the legacy user. legacy_users_nodes = Node.objects.filter(owner=legacy_user) self.assertSetEqual({node1, node2}, set(legacy_users_nodes)) self.assertEqual( (legacy_user, legacy_user, legacy_user, legacy_user), ( reload_object(consumer1).user, reload_object(token1).user, reload_object(consumer2).user, reload_object(token2).user, ), )
def test_do_not_delete_keysource_when_keys(self): user = factory.make_User() protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]) auth_id = factory.make_name("auth_id") keysource = factory.make_KeySource(protocol=protocol, auth_id=auth_id) factory.make_SSHKey(user=user, keysource=keysource) self.assertIsNotNone(reload_object(keysource))
def test_list(self): user = factory.make_User() handler = SSHKeyHandler(user, {}, None) factory.make_SSHKey(user) expected_sshkeys = [ self.dehydrate_sshkey(sshkey) for sshkey in SSHKey.objects.all() ] self.assertItemsEqual(expected_sshkeys, handler.list({}))
def test_copy_does_not_clobber(self): # When the destination user already has some keys, copy_ssh_keys() # adds to them; it does not remove them. user1 = factory.make_User() key1 = factory.make_SSHKey(user1, get_ssh_key_string(1)) user2 = factory.make_User() key2 = factory.make_SSHKey(user2, get_ssh_key_string(2)) copy_ssh_keys(user1, user2) user2s_ssh_keys = SSHKey.objects.filter(user=user2) self.assertSetEqual({key1.key, key2.key}, {ssh_key.key for ssh_key in user2s_ssh_keys})
def test_copy_is_idempotent(self): # When the destination user already has a key, copy_ssh_keys() is a # noop for that key. user1 = factory.make_User() key1 = factory.make_SSHKey(user1) user2 = factory.make_User() key2 = factory.make_SSHKey(user2, key1.key) copy_ssh_keys(user1, user2) user2s_ssh_keys = SSHKey.objects.filter(user=user2) self.assertSetEqual({key2.key}, {ssh_key.key for ssh_key in user2s_ssh_keys})
def test_get_doesnt_work_if_not_owned(self): user = factory.make_User() handler = SSHKeyHandler(user, {}, None) not_owned_sshkey = factory.make_SSHKey(factory.make_User()) self.assertRaises( HandlerDoesNotExistError, handler.get, {"id": not_owned_sshkey.id} )
def test_get(self): user = factory.make_User() handler = SSHKeyHandler(user, {}, None) sshkey = factory.make_SSHKey(user) self.assertEqual( self.dehydrate_sshkey(sshkey), handler.get({"id": sshkey.id}) )
def test_DELETE_user_with_sshkey_deletes_key(self): self.become_admin() user = factory.make_User() key_id = factory.make_SSHKey(user=user).id response = self.client.delete( reverse('user_handler', args=[user.username])) self.assertEqual(http.client.NO_CONTENT, response.status_code, response.status_code) self.assertFalse(SSHKey.objects.filter(id=key_id).exists())
def test_delete_keysource_deleted_when_no_more_keys(self): user = factory.make_User() protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]) auth_id = factory.make_name('auth_id') keysource = factory.make_KeySource(protocol=protocol, auth_id=auth_id) sshkey = factory.make_SSHKey(user=user, keysource=keysource) sshkey.delete() self.assertIsNone(reload_object(keysource))
def test_copy(self): user1 = factory.make_User() key1 = factory.make_SSHKey(user1) user2 = factory.make_User() copy_ssh_keys(user1, user2) user2s_ssh_keys = SSHKey.objects.filter(user=user2) self.assertSetEqual({key1.key}, {ssh_key.key for ssh_key in user2s_ssh_keys})
def test_perform_action_catches_start_action_errors(self): error_text = factory.make_string(prefix="NodeActionError") exc = NodeActionError(error_text) self.patch(PowerOn, "execute").side_effect = exc with transaction.atomic(): user = factory.make_User() factory.make_SSHKey(user) node = factory.make_Node(status=NODE_STATUS.READY, owner=user) form = BulkNodeActionForm(user=user, data=dict(action=PowerOn.name, system_id=[node.system_id])) self.assertTrue(form.is_valid(), form._errors) with transaction.atomic(): done, not_actionable, not_permitted = form.save() self.assertEqual([0, 1, 0], [done, not_actionable, not_permitted])
def create_sshkey(self, params=None): if params is None: params = {} return factory.make_SSHKey(**params)
def test_delete(self): user = factory.make_User() sshkey = factory.make_SSHKey(user=user) handler = SSHKeyHandler(user, {}, None) handler.delete({"id": sshkey.id}) self.assertIsNone(get_one(SSHKey.objects.filter(id=sshkey.id)))
def test_Deploy_inhibit_allows_user_with_SSH_key(self): user_with_key = factory.make_User() factory.make_SSHKey(user_with_key) self.assertIsNone(Deploy(factory.make_Node(), user_with_key).inhibit())