def test_import_boot_images_denied_if_not_admin(self): user = factory.make_user() client = OAuthAuthenticatedClient(user) response = client.post(reverse('nodegroups_handler'), {'op': 'import_boot_images'}) self.assertEqual( httplib.FORBIDDEN, response.status_code, explain_unexpected_response(httplib.FORBIDDEN, response))
def test_import_boot_images_denied_if_not_admin(self): user = factory.make_user() client = OAuthAuthenticatedClient(user) response = client.post( reverse('nodegroups_handler'), {'op': 'import_boot_images'}) self.assertEqual( httplib.FORBIDDEN, response.status_code, explain_unexpected_response(httplib.FORBIDDEN, response))
def test_invalid_oauth_request(self): # An OAuth-signed request that does not validate is an error. user = factory.make_user() client = OAuthAuthenticatedClient(user) get_auth_tokens(user).delete() # Delete the user's API keys. response = client.post(reverse('nodes_handler'), {'op': 'start'}) observed = response.status_code, response.content expected = ( Equals(httplib.UNAUTHORIZED), Contains("Invalid access token:"), ) self.assertThat(observed, MatchesListwise(expected))
def test_invalid_oauth_request(self): # An OAuth-signed request that does not validate is an error. user = factory.make_user() client = OAuthAuthenticatedClient(user) get_auth_tokens(user).delete() # Delete the user's API keys. response = client.post(reverse('nodes_handler'), {'op': 'start'}) observed = response.status_code, response.content expected = ( Equals(httplib.UNAUTHORIZED), Contains("Invalid access token:"), ) self.assertThat(observed, MatchesListwise(expected))
def test_nodegroup_list_nodes_works_for_admin(self): nodegroup = factory.make_node_group() admin = factory.make_admin() client = OAuthAuthenticatedClient(admin) node = factory.make_node(nodegroup=nodegroup) response = client.get( reverse('nodegroup_handler', args=[nodegroup.uuid]), {'op': 'list_nodes'}) self.assertEqual(httplib.OK, response.status_code, explain_unexpected_response(httplib.OK, response)) parsed_result = json.loads(response.content) self.assertItemsEqual([node.system_id], parsed_result)
def test_nodegroup_list_nodes_works_for_admin(self): nodegroup = factory.make_node_group() admin = factory.make_admin() client = OAuthAuthenticatedClient(admin) node = factory.make_node(nodegroup=nodegroup) response = client.get( reverse('nodegroup_handler', args=[nodegroup.uuid]), {'op': 'list_nodes'}) self.assertEqual( httplib.OK, response.status_code, explain_unexpected_response(httplib.OK, response)) parsed_result = json.loads(response.content) self.assertItemsEqual([node.system_id], parsed_result)
def test_nodegroup_import_boot_images_calls_script(self): recorder = self.patch(tasks, 'call_and_check') proxy = factory.getRandomString() Config.objects.set_config('http_proxy', proxy) nodegroup = factory.make_node_group() admin = factory.make_admin() client = OAuthAuthenticatedClient(admin) response = client.post( reverse('nodegroup_handler', args=[nodegroup.uuid]), {'op': 'import_boot_images'}) self.assertEqual(httplib.OK, response.status_code, explain_unexpected_response(httplib.OK, response)) recorder.assert_called_once_with( ['sudo', '-n', '-E', 'maas-import-pxe-files'], env=ANY)
def get_nodegroup_worker_client(nodegroup_uuid): """Get a MAASClient that can do work for this nodegroup.""" nodegroup = get_one(NodeGroup.objects.filter(uuid=nodegroup_uuid)) django_client = OAuthAuthenticatedClient(get_worker_user(), token=nodegroup.api_token) maas_client = MAASDjangoTestClient(django_client) return maas_client
def test_nodegroup_import_boot_images_calls_script(self): recorder = self.patch(tasks, 'call_and_check') proxy = factory.getRandomString() Config.objects.set_config('http_proxy', proxy) nodegroup = factory.make_node_group() admin = factory.make_admin() client = OAuthAuthenticatedClient(admin) response = client.post( reverse('nodegroup_handler', args=[nodegroup.uuid]), {'op': 'import_boot_images'}) self.assertEqual( httplib.OK, response.status_code, explain_unexpected_response(httplib.OK, response)) recorder.assert_called_once_with( ['sudo', '-n', '-E', 'maas-import-pxe-files'], env=ANY)
def test_GET_nodes_hides_invisible_nodes(self): user2 = factory.make_user() node1 = factory.make_node() inject_lshw_result(node1, b'<node><foo/></node>') node2 = factory.make_node(status=NODE_STATUS.ALLOCATED, owner=user2) inject_lshw_result(node2, b'<node><bar/></node>') tag = factory.make_tag(definition='//node') response = self.client.get(self.get_tag_uri(tag), {'op': 'nodes'}) self.assertEqual(httplib.OK, response.status_code) parsed_result = json.loads(response.content) self.assertEqual([node1.system_id], [r['system_id'] for r in parsed_result]) # However, for the other user, they should see the result client2 = OAuthAuthenticatedClient(user2) response = client2.get(self.get_tag_uri(tag), {'op': 'nodes'}) self.assertEqual(httplib.OK, response.status_code) parsed_result = json.loads(response.content) self.assertItemsEqual([node1.system_id, node2.system_id], [r['system_id'] for r in parsed_result])
def test_GET_nodes_hides_invisible_nodes(self): user2 = factory.make_user() node1 = factory.make_node() inject_lshw_result(node1, b'<node><foo/></node>') node2 = factory.make_node(status=NODE_STATUS.ALLOCATED, owner=user2) inject_lshw_result(node2, b'<node><bar/></node>') tag = factory.make_tag(definition='//node') response = self.client.get(self.get_tag_uri(tag), {'op': 'nodes'}) self.assertEqual(httplib.OK, response.status_code) parsed_result = json.loads(response.content) self.assertEqual([node1.system_id], [r['system_id'] for r in parsed_result]) # However, for the other user, they should see the result client2 = OAuthAuthenticatedClient(user2) response = client2.get(self.get_tag_uri(tag), {'op': 'nodes'}) self.assertEqual(httplib.OK, response.status_code) parsed_result = json.loads(response.content) self.assertItemsEqual([node1.system_id, node2.system_id], [r['system_id'] for r in parsed_result])
def test_details_allows_admin(self): nodegroup = factory.make_node_group() node = factory.make_node(nodegroup=nodegroup) user = factory.make_admin() client = OAuthAuthenticatedClient(user) response = client.post( reverse('nodegroup_handler', args=[nodegroup.uuid]), {'op': 'details', 'system_ids': [node.system_id]}) self.assertEqual(httplib.OK, response.status_code) parsed_result = bson.BSON(response.content).decode() self.assertDictEqual( { node.system_id: { "lshw": None, "lldp": None, }, }, parsed_result)
def test_details_allows_admin(self): nodegroup = factory.make_node_group() node = factory.make_node(nodegroup=nodegroup) user = factory.make_admin() client = OAuthAuthenticatedClient(user) response = client.post( reverse('nodegroup_handler', args=[nodegroup.uuid]), { 'op': 'details', 'system_ids': [node.system_id] }) self.assertEqual(httplib.OK, response.status_code) parsed_result = bson.BSON(response.content).decode() self.assertDictEqual({ node.system_id: { "lshw": None, "lldp": None, }, }, parsed_result)
def test_import_boot_images_calls_script_for_all_accepted_clusters(self): recorder = self.patch(nodegroup_module, 'import_boot_images') proxy = factory.make_name('proxy') Config.objects.set_config('http_proxy', proxy) accepted_nodegroups = [ factory.make_node_group(status=NODEGROUP_STATUS.ACCEPTED), factory.make_node_group(status=NODEGROUP_STATUS.ACCEPTED), ] factory.make_node_group(status=NODEGROUP_STATUS.REJECTED) factory.make_node_group(status=NODEGROUP_STATUS.PENDING) admin = factory.make_admin() client = OAuthAuthenticatedClient(admin) response = client.post( reverse('nodegroups_handler'), {'op': 'import_boot_images'}) self.assertEqual( httplib.OK, response.status_code, explain_unexpected_response(httplib.OK, response)) queues = [ kwargs['queue'] for args, kwargs in recorder.apply_async.call_args_list] self.assertItemsEqual( [nodegroup.work_queue for nodegroup in accepted_nodegroups], queues)
def test_import_boot_images_calls_script_for_all_accepted_clusters(self): recorder = self.patch(nodegroup_module, 'import_boot_images') proxy = factory.make_name('proxy') Config.objects.set_config('http_proxy', proxy) accepted_nodegroups = [ factory.make_node_group(status=NODEGROUP_STATUS.ACCEPTED), factory.make_node_group(status=NODEGROUP_STATUS.ACCEPTED), ] factory.make_node_group(status=NODEGROUP_STATUS.REJECTED) factory.make_node_group(status=NODEGROUP_STATUS.PENDING) admin = factory.make_admin() client = OAuthAuthenticatedClient(admin) response = client.post(reverse('nodegroups_handler'), {'op': 'import_boot_images'}) self.assertEqual(httplib.OK, response.status_code, explain_unexpected_response(httplib.OK, response)) queues = [ kwargs['queue'] for args, kwargs in recorder.apply_async.call_args_list ] self.assertItemsEqual( [nodegroup.work_queue for nodegroup in accepted_nodegroups], queues)
def test_other_user_than_node_cannot_signal_commissioning_result(self): node = factory.make_node(status=NODE_STATUS.COMMISSIONING) client = OAuthAuthenticatedClient(factory.make_user()) response = call_signal(client) self.assertEqual(httplib.FORBIDDEN, response.status_code) self.assertEqual(NODE_STATUS.COMMISSIONING, reload_object(node).status)
def make_node_client(node=None): """Create a test client logged in as if it were `node`.""" if node is None: node = factory.make_node() token = NodeKey.objects.get_token_for_node(node) return OAuthAuthenticatedClient(get_node_init_user(), token)
def setUp(self): super(APITestCase, self).setUp() self.logged_in_user = factory.make_user( username='******', password='******') self.client = OAuthAuthenticatedClient(self.logged_in_user)
def make_worker_client(nodegroup): """Create a test client logged in as if it were `nodegroup`.""" return OAuthAuthenticatedClient( get_worker_user(), token=nodegroup.api_token)
def test_node_init_user_cannot_access(self): token = NodeKey.objects.get_token_for_node(factory.make_node()) client = OAuthAuthenticatedClient(get_node_init_user(), token) response = client.get(reverse('nodes_handler'), {'op': 'list'}) self.assertEqual(httplib.FORBIDDEN, response.status_code)