def test_view_node_POST_performs_action(self): factory.make_sshkey(self.logged_in_user) self.set_up_oauth_token() node = factory.make_node(status=NODE_STATUS.READY) node_link = reverse('node-view', args=[node.system_id]) response = self.client.post( node_link, data={NodeActionForm.input_name: StartNode.display}) self.assertEqual(httplib.FOUND, response.status_code) self.assertEqual(NODE_STATUS.ALLOCATED, reload_object(node).status)
def test_view_node_POST_action_displays_message(self): factory.make_sshkey(self.logged_in_user) self.set_up_oauth_token() node = factory.make_node(status=NODE_STATUS.READY) response = self.perform_action_and_get_node_page( node, StartNode.display) self.assertIn( "This node is now allocated to you.", '\n'.join(msg.message for msg in response.context['messages']))
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_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_delete_key_POST(self): # A POST request deletes the key, and redirects to the prefs. key = factory.make_sshkey(self.logged_in_user) del_link = reverse('prefs-delete-sshkey', args=[key.id]) response = self.client.post(del_link, {'post': 'yes'}) self.assertEqual('/account/prefs/', extract_redirect(response)) self.assertFalse(SSHKey.objects.filter(id=key.id).exists())
def test_delete_key_GET_nonexistent_key_redirects_to_prefs(self): # Deleting a nonexistent key requires no confirmation. It just # "succeeds" instantaneously. key = factory.make_sshkey(self.logged_in_user) del_link = reverse('prefs-delete-sshkey', args=[key.id]) key.delete() response = self.client.get(del_link) self.assertEqual('/account/prefs/', extract_redirect(response))
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_delete_key_POST_ignores_nonexistent_key(self): # Deleting a key that's already been deleted? Basically that's # success. key = factory.make_sshkey(self.logged_in_user) del_link = reverse('prefs-delete-sshkey', args=[key.id]) key.delete() response = self.client.post(del_link, {'post': 'yes'}) self.assertEqual('/account/prefs/', extract_redirect(response))
def test_delete_key_GET(self): # The 'Delete key' page displays a confirmation page with a form. key = factory.make_sshkey(self.logged_in_user) del_link = reverse('prefs-delete-sshkey', args=[key.id]) response = self.client.get(del_link) doc = fromstring(response.content) self.assertIn("Are you sure you want to delete the following key?", response.content) # The page features a form that submits to itself. self.assertSequenceEqual(['.'], [ elem.get('action').strip() for elem in doc.cssselect('#content form') ])
def test_migrate_ancillary_data_to_legacy_user_when_multiple_users(self): factory.make_file_storage(owner=None) # Create two users, both with API credentials, an SSH key and a node. user1 = factory.make_user() consumer1, token1 = user1.get_profile().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.get_profile().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_delete_key_GET(self): # The 'Delete key' page displays a confirmation page with a form. key = factory.make_sshkey(self.logged_in_user) del_link = reverse('prefs-delete-sshkey', args=[key.id]) response = self.client.get(del_link) doc = fromstring(response.content) self.assertIn( "Are you sure you want to delete the following key?", response.content) # The page features a form that submits to itself. self.assertSequenceEqual( ['.'], [elem.get('action').strip() for elem in doc.cssselect( '#content form')])
def test_StartNode_inhibit_allows_user_with_SSH_key(self): user_with_key = factory.make_user() factory.make_sshkey(user_with_key) self.assertIsNone( StartNode(factory.make_node(), user_with_key).inhibit())
def test_delete_key_GET_cannot_access_someone_elses_key(self): key = factory.make_sshkey(factory.make_user()) del_link = reverse('prefs-delete-sshkey', args=[key.id]) response = self.client.get(del_link) self.assertEqual(httplib.FORBIDDEN, response.status_code)