Exemple #1
0
 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))
Exemple #2
0
 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))
Exemple #3
0
 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))
Exemple #4
0
 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))
Exemple #5
0
    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)
Exemple #6
0
    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)
Exemple #7
0
    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)
Exemple #8
0
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
Exemple #9
0
    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)
Exemple #10
0
    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])
Exemple #11
0
    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])
Exemple #12
0
    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)
Exemple #13
0
    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)
Exemple #14
0
 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)
Exemple #15
0
 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)
Exemple #16
0
 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)
Exemple #17
0
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)
Exemple #18
0
 def setUp(self):
     super(APITestCase, self).setUp()
     self.logged_in_user = factory.make_user(
         username='******', password='******')
     self.client = OAuthAuthenticatedClient(self.logged_in_user)
Exemple #19
0
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)
Exemple #20
0
 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)