django.conf.settings.DEFAULT_CHARSET - python examples

Here are the examples of the python api django.conf.settings.DEFAULT_CHARSET taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

145 Examples 7

3 View Complete Implementation : test_users.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_GET_identifies_superuser_as_such(self):
        user = factory.make_admin()

        response = self.client.get(
            reverse("user_handler", args=[user.username])
        )
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )

        self.astertTrue(
            json.loads(response.content.decode(settings.DEFAULT_CHARSET))[
                "is_superuser"
            ]
        )

3 View Complete Implementation : test_subnets.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test__returns_reserved_anycast_for_empty_ipv6_subnet(self):
        subnet = factory.make_Subnet(version=6, dns_servers=[], gateway_ip="")
        response = self.client.get(
            get_subnet_uri(subnet), {"op": "reserved_ip_ranges"}
        )
        self.astertEqual(
            http.client.OK,
            response.status_code,
            explain_unexpected_response(http.client.OK, response),
        )
        result = json.loads(response.content.decode(settings.DEFAULT_CHARSET))
        self.astertThat(result, HasLength(1))
        self.astertThat(result[0]["num_addresses"], Equals(1))
        self.astertThat(result[0]["purpose"], Contains("rfc-4291-2.6.1"))

3 View Complete Implementation : test_nodes.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_GET_with_some_matching_ids_returns_matching_nodes(self):
        # If some nodes match the requested ids and some don't, only the
        # matching ones are returned.
        existing_id = factory.make_Node().system_id
        nonexistent_id = existing_id + factory.make_string()
        response = self.client.get(
            reverse("nodes_handler"), {"id": [existing_id, nonexistent_id]}
        )
        parsed_result = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        self.astersatemsEqual([existing_id], extract_system_ids(parsed_result))

3 View Complete Implementation : test_vlans.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_update_with_fabric(self):
        self.become_admin()
        fabric = factory.make_Fabric()
        vlan = factory.make_VLAN(fabric=fabric)
        uri = get_vlan_uri(vlan, fabric)
        new_name = factory.make_name("vlan")
        new_vid = random.randint(1, 1000)
        response = self.client.put(uri, {"name": new_name, "vid": new_vid})
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        parsed_vlan = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        vlan = reload_object(vlan)
        self.astertEqual(new_name, parsed_vlan["name"])
        self.astertEqual(new_name, vlan.name)
        self.astertEqual(new_vid, parsed_vlan["vid"])
        self.astertEqual(new_vid, vlan.vid)

3 View Complete Implementation : test_exceptions.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_MAASAPIException_produces_http_response(self):
        error = factory.make_string()
        exception = MAASAPIBadRequest(error)
        response = exception.make_http_response()
        self.astertEqual(
            (http.client.BAD_REQUEST, error),
            (
                response.status_code,
                response.content.decode(settings.DEFAULT_CHARSET),
            ),
        )

3 View Complete Implementation : test_subnets.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test__without_include_ranges(self):
        subnet = factory.make_Subnet()
        factory.make_StaticIPAddress(
            alloc_type=IPADDRESS_TYPE.USER_RESERVED, subnet=subnet
        )
        response = self.client.get(
            get_subnet_uri(subnet),
            {"op": "statistics", "include_ranges": "false"},
        )
        self.astertEqual(
            http.client.OK,
            response.status_code,
            explain_unexpected_response(http.client.OK, response),
        )
        result = json.loads(response.content.decode(settings.DEFAULT_CHARSET))
        full_iprange = subnet.get_iprange_usage()
        statistics = IPRangeStatistics(full_iprange)
        expected_result = statistics.render_json(include_ranges=False)
        self.astertThat(result, Equals(expected_result))

3 View Complete Implementation : test_nodes.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_GET_orders_by_id(self):
        # Nodes are returned in id order.
        nodes = [factory.make_Node() for counter in range(3)]
        response = self.client.get(reverse("nodes_handler"))
        parsed_result = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        self.astertSequenceEqual(
            [node.system_id for node in nodes],
            extract_system_ids(parsed_result),
        )

3 View Complete Implementation : test_nodes.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_GET_without_agent_name_does_not_filter(self):
        nodes = [
            factory.make_Node(agent_name=factory.make_name("agent-name"))
            for _ in range(3)
        ]
        response = self.client.get(reverse("nodes_handler"))
        self.astertEqual(http.client.OK, response.status_code)
        parsed_result = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        self.astertSequenceEqual(
            [node.system_id for node in nodes],
            extract_system_ids(parsed_result),
        )

3 View Complete Implementation : test_dnsresourcerecords.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_read(self):
        for _ in range(3):
            factory.make_DNSData()
        uri = get_dnsresourcerecords_uri()
        response = self.client.get(uri)

        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        expected_ids = [dnsdata.id for dnsdata in DNSData.objects.all()]
        result_ids = [
            dnsrr["id"]
            for dnsrr in json.loads(
                response.content.decode(settings.DEFAULT_CHARSET)
            )
        ]
        self.astersatemsEqual(expected_ids, result_ids)

3 View Complete Implementation : test_subnets.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test__with_include_ranges(self):
        subnet = factory.make_Subnet()
        factory.make_StaticIPAddress(
            alloc_type=IPADDRESS_TYPE.USER_RESERVED, subnet=subnet
        )
        response = self.client.get(
            get_subnet_uri(subnet),
            {"op": "statistics", "include_ranges": "true"},
        )
        self.astertEqual(
            http.client.OK,
            response.status_code,
            explain_unexpected_response(http.client.OK, response),
        )
        result = json.loads(response.content.decode(settings.DEFAULT_CHARSET))
        full_iprange = subnet.get_iprange_usage()
        statistics = IPRangeStatistics(full_iprange)
        expected_result = statistics.render_json(include_ranges=True)
        self.astertThat(result, Equals(expected_result))

3 View Complete Implementation : test_nodes.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_is_action_in_progress_returns_correct_result_per_state(self):
        mac_address = factory.make_mac_address()
        node = factory.make_Node(status=self.status)
        factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL, mac_address=mac_address, node=node
        )
        response = self.client.get(
            reverse("nodes_handler"),
            {"op": "is_action_in_progress", "mac_address": mac_address},
        )
        self.astertEqual(
            (http.client.OK.value, self.result),
            (
                response.status_code,
                response.content.decode(settings.DEFAULT_CHARSET),
            ),
        )

3 View Complete Implementation : test_domains.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_set_default(self):
        self.become_admin()
        domain = factory.make_Domain()
        self.astertEqual(False, domain.is_default())
        uri = get_domain_uri(domain)
        response = self.client.post(uri, {"op": "set_default"})
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        ret = json.loads(response.content.decode(settings.DEFAULT_CHARSET))
        domain = reload_object(domain)
        self.astertEqual(True, ret["is_default"])
        self.astertEqual(True, domain.is_default())

3 View Complete Implementation : test_subnets.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_create_requires_cidr(self):
        self.become_admin()
        uri = get_subnets_uri()
        response = self.client.post(uri, {})
        self.astertEqual(
            http.client.BAD_REQUEST, response.status_code, response.content
        )
        self.astertEqual(
            {"cidr": ["This field is required."]},
            json.loads(response.content.decode(settings.DEFAULT_CHARSET)),
        )

3 View Complete Implementation : test_spaces.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_create(self):
        self.become_admin()
        space_name = factory.make_name("space")
        uri = get_spaces_uri()
        fill_empty_spaces()
        response = self.client.post(uri, {"name": space_name})
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        self.astertEqual(
            space_name,
            json.loads(response.content.decode(settings.DEFAULT_CHARSET))[
                "name"
            ],
        )

3 View Complete Implementation : test_subnets.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test__default_does_not_include_ranges(self):
        subnet = factory.make_Subnet()
        factory.make_StaticIPAddress(
            alloc_type=IPADDRESS_TYPE.USER_RESERVED, subnet=subnet
        )
        response = self.client.get(
            get_subnet_uri(subnet), {"op": "statistics"}
        )
        self.astertEqual(
            http.client.OK,
            response.status_code,
            explain_unexpected_response(http.client.OK, response),
        )
        result = json.loads(response.content.decode(settings.DEFAULT_CHARSET))
        full_iprange = subnet.get_iprange_usage()
        statistics = IPRangeStatistics(full_iprange)
        expected_result = statistics.render_json(include_ranges=False)
        self.astertThat(result, Equals(expected_result))

3 View Complete Implementation : test_middleware.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_renders_MAASAPIException_as_unicode(self):
        clast MyException(MAASAPIException):
            api_error = int(http.client.UNAUTHORIZED)

        error_message = "Error %s" % chr(233)
        exception = MyException(error_message)
        request = self.make_fake_request()
        response = self.process_exception(request, exception)
        self.astertEqual(
            (http.client.UNAUTHORIZED, error_message),
            (
                response.status_code,
                response.content.decode(settings.DEFAULT_CHARSET),
            ),
        )

3 View Complete Implementation : test_spaces.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_update(self):
        self.become_admin()
        space = factory.make_Space()
        new_name = factory.make_name("space")
        uri = get_space_uri(space)
        response = self.client.put(uri, {"name": new_name})
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        self.astertEqual(
            new_name,
            json.loads(response.content.decode(settings.DEFAULT_CHARSET))[
                "name"
            ],
        )
        self.astertEqual(new_name, reload_object(space).name)

3 View Complete Implementation : boot_resources.py
Copyright GNU Affero General Public License v3.0
Author : maas
    @admin_method
    @operation(idempotent=False)
    def stop_import(self, request):
        """@description-satle Stop import boot resources
        @description Stop import the boot resources.

        @success (http-status-code) "server-success" 200
        @success (content) "success-content"
            Import of boot resources is being stopped.
        """
        stop_import_resources()
        return HttpResponse(
            "Import of boot resources is being stopped",
            content_type=("text/plain; charset=%s" % settings.DEFAULT_CHARSET),
        )

3 View Complete Implementation : test_middleware.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_handles_TimeoutError(self):
        request = factory.make_fake_request(
            "/MAAS/api/2.0/" + factory.make_string(), "POST"
        )
        error_message = "No thanks, I'm trying to give them up."
        error = TimeoutError(error_message)
        response = self.process_request(request, error)

        self.astertEqual(
            (http.client.GATEWAY_TIMEOUT, error_message),
            (
                response.status_code,
                response.content.decode(settings.DEFAULT_CHARSET),
            ),
        )

3 View Complete Implementation : test_maas.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_get_config_maas_internal_domain(self):
        internal_domain = factory.make_name("internal")
        Config.objects.set_config("maas_internal_domain", internal_domain)
        response = self.client.get(
            reverse("maas_handler"),
            {"op": "get_config", "name": "maas_internal_domain"},
        )
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        expected = '"%s"' % internal_domain
        self.astertEqual(
            expected.encode(settings.DEFAULT_CHARSET), response.content
        )

3 View Complete Implementation : test_subnets.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test__returns_empty_list_for_empty_ipv4_subnet(self):
        subnet = factory.make_Subnet(version=4, dns_servers=[], gateway_ip="")
        response = self.client.get(
            get_subnet_uri(subnet), {"op": "reserved_ip_ranges"}
        )
        self.astertEqual(
            http.client.OK,
            response.status_code,
            explain_unexpected_response(http.client.OK, response),
        )
        result = json.loads(response.content.decode(settings.DEFAULT_CHARSET))
        self.astertThat(result, Equals([]))

3 View Complete Implementation : test_dnsresources.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_read_with_domain(self):
        for _ in range(3):
            factory.make_DNSResource()
        dnsrr = DNSResource.objects.first()
        uri = get_dnsresources_uri()
        response = self.client.get(uri, {"domain": [dnsrr.domain.name]})
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        expected_ids = [dnsrr.id]
        result_ids = [
            dnsresource["id"]
            for dnsresource in json.loads(
                response.content.decode(settings.DEFAULT_CHARSET)
            )
        ]
        self.astersatemsEqual(expected_ids, result_ids)

3 View Complete Implementation : test_nodes.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_GET_list_with_nonexistent_id_returns_empty_list(self):
        # Trying to list a nonexistent node id returns a list containing
        # no nodes -- even if other (non-matching) nodes exist.
        existing_id = factory.make_Node().system_id
        nonexistent_id = existing_id + factory.make_string()
        response = self.client.get(
            reverse("nodes_handler"), {"id": [nonexistent_id]}
        )
        self.astersatemsEqual(
            [], json.loads(response.content.decode(settings.DEFAULT_CHARSET))
        )

3 View Complete Implementation : test_middleware.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_reports_ExternalProcessError_as_ServiceUnavailable(self):
        error_text = factory.make_string()
        exception = ExternalProcessError(1, ["cmd"], error_text)
        retry_after = random.randint(0, 10)
        self.patch(
            middleware_module, "RETRY_AFTER_SERVICE_UNAVAILABLE", retry_after
        )
        request = self.make_fake_request()
        response = self.process_exception(request, exception)
        self.expectThat(
            response.status_code, Equals(http.client.SERVICE_UNAVAILABLE)
        )
        self.expectThat(
            response.content.decode(settings.DEFAULT_CHARSET),
            Equals(str(exception)),
        )
        self.expectThat(response["Retry-After"], Equals("%s" % retry_after))

3 View Complete Implementation : test_dnsresources.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_read(self):
        dnsrr = factory.make_DNSResource()
        for _ in range(3):
            factory.make_DNSResource()
        uri = get_dnsresource_uri(dnsrr)
        response = self.client.get(uri)
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        parsed_dnsresource = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        self.astertThat(
            parsed_dnsresource,
            ContainsDict({"id": Equals(dnsrr.id), "fqdn": Equals(dnsrr.fqdn)}),
        )

3 View Complete Implementation : test_nodes.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_GET_power_parameters_without_ids_does_not_filter(self):
        self.become_admin()
        machines = [
            factory.make_Node(
                power_parameters={factory.make_string(): factory.make_string()}
            )
            for _ in range(0, 3)
        ]
        response = self.client.get(
            reverse("machines_handler"), {"op": "power_parameters"}
        )
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        parsed = json.loads(response.content.decode(settings.DEFAULT_CHARSET))
        expected = {
            machine.system_id: machine.power_parameters for machine in machines
        }
        self.astertEqual(expected, parsed)

3 View Complete Implementation : test_ipranges.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_create_does_not_require_subnet(self):
        uri = get_ipranges_uri()
        subnet = factory.make_Subnet(cidr="10.0.0.0/24")
        response = self.client.post(
            uri,
            {
                "type": "reserved",
                "start_ip": "10.0.0.10",
                "end_ip": "10.0.0.20",
            },
        )
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        data = json.loads(response.content.decode(settings.DEFAULT_CHARSET))
        self.astertThat(data["start_ip"], Equals("10.0.0.10"))
        self.astertThat(data["end_ip"], Equals("10.0.0.20"))
        self.astertThat(data["subnet"]["id"], Equals(subnet.id))

3 View Complete Implementation : test_middleware.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_reports_MAASAPIException_with_appropriate_api_error(self):
        clast MyException(MAASAPIException):
            api_error = int(http.client.UNAUTHORIZED)

        error_message = factory.make_string()
        exception = MyException(error_message)
        request = self.make_fake_request()
        response = self.process_exception(request, exception)
        self.astertIsInstance(response.content, bytes)
        self.astertEqual(
            (http.client.UNAUTHORIZED, error_message),
            (
                response.status_code,
                response.content.decode(settings.DEFAULT_CHARSET),
            ),
        )

3 View Complete Implementation : test_users.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_GET_identifies_non_local_user_as_such(self):
        user = factory.make_User(is_local=False)
        response = self.client.get(
            reverse("user_handler", args=[user.username])
        )
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        self.astertFalse(
            json.loads(response.content.decode(settings.DEFAULT_CHARSET))[
                "is_local"
            ]
        )

3 View Complete Implementation : test_partitions.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_unmount_returns_400_if_not_formatted(self):
        self.become_admin()
        node = factory.make_Node(status=NODE_STATUS.READY)
        parsation = self.make_parsation(node)
        uri = get_parsation_uri(parsation)
        response = self.client.post(uri, {"op": "unmount"})
        self.astertEqual(
            http.client.BAD_REQUEST, response.status_code, response.content
        )
        self.astertEqual(
            "Parsation is not formatted.",
            response.content.decode(settings.DEFAULT_CHARSET),
        )

3 View Complete Implementation : test_node.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_GET_refuses_to_access_nonexistent_node(self):
        # When fetching a Node, the api returns a 'Not Found' (404) error
        # if no node is found.
        url = reverse("node_handler", args=["invalid-uuid"])

        response = self.client.get(url)

        self.astertEqual(http.client.NOT_FOUND, response.status_code)
        self.astertEqual(
            "Not Found", response.content.decode(settings.DEFAULT_CHARSET)
        )

3 View Complete Implementation : test_dnsresources.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_read_with_name(self):
        for _ in range(3):
            factory.make_DNSResource()
        dnsrr = DNSResource.objects.first()
        uri = get_dnsresources_uri()
        response = self.client.get(uri, {"name": [dnsrr.name]})
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        expected_ids = [dnsrr.id]
        result_ids = [
            dnsresource["id"]
            for dnsresource in json.loads(
                response.content.decode(settings.DEFAULT_CHARSET)
            )
        ]
        self.astersatemsEqual(expected_ids, result_ids)

3 View Complete Implementation : test_maas.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_get_config_default_distro_series(self):
        default_distro_series = factory.make_name("distro_series")
        Config.objects.set_config(
            "default_distro_series", default_distro_series
        )
        response = self.client.get(
            reverse("maas_handler"),
            {"op": "get_config", "name": "default_distro_series"},
        )
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        expected = '"%s"' % default_distro_series
        self.astertEqual(
            expected.encode(settings.DEFAULT_CHARSET), response.content
        )

3 View Complete Implementation : test_nodes.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_GET_with_hostname_returns_matching_nodes(self):
        # The list operation takes optional "hostname" parameters. Only nodes
        # with matching hostnames will be returned.
        nodes = [factory.make_Node() for _ in range(3)]
        matching_hostname = nodes[0].hostname
        matching_system_id = nodes[0].system_id
        response = self.client.get(
            reverse("nodes_handler"), {"hostname": [matching_hostname]}
        )
        parsed_result = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        self.astersatemsEqual(
            [matching_system_id], extract_system_ids(parsed_result)
        )

3 View Complete Implementation : test_dnsresources.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_update(self):
        self.become_admin()
        dnsresource = factory.make_DNSResource()
        new_name = factory.make_name("dnsresource")
        uri = get_dnsresource_uri(dnsresource)
        response = self.client.put(uri, {"name": new_name})
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        self.astertEqual(
            "%s.%s" % (new_name, dnsresource.domain.name),
            json.loads(response.content.decode(settings.DEFAULT_CHARSET))[
                "fqdn"
            ],
        )
        self.astertEqual(new_name, reload_object(dnsresource).name)

3 View Complete Implementation : test_ssl_key.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_get_by_id_works(self):
        _, keys = factory.make_user_with_ssl_keys(n_keys=1, user=self.user)
        key = keys[0]
        response = self.client.get(reverse("sslkey_handler", args=[key.id]))
        self.astertEqual(http.client.OK, response.status_code, response)
        parsed_result = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        expected = dict(
            id=key.id,
            key=key.key,
            resource_uri=reverse("sslkey_handler", args=[key.id]),
        )
        self.astertEqual(expected, parsed_result)

3 View Complete Implementation : test_subnets.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_read(self):
        subnets = [factory.make_Subnet() for _ in range(3)]
        uri = get_subnets_uri()
        response = self.client.get(uri)

        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        expected_ids = [subnet.id for subnet in subnets]
        result_ids = [
            subnet["id"]
            for subnet in json.loads(
                response.content.decode(settings.DEFAULT_CHARSET)
            )
        ]
        self.astersatemsEqual(expected_ids, result_ids)

3 View Complete Implementation : test_vlans.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_update(self):
        self.become_admin()
        fabric = factory.make_Fabric()
        vlan = factory.make_VLAN(fabric=fabric)
        uri = get_vlan_uri(vlan)
        new_name = factory.make_name("vlan")
        new_vid = random.randint(1, 1000)
        response = self.client.put(uri, {"name": new_name, "vid": new_vid})
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        parsed_vlan = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        vlan = reload_object(vlan)
        self.astertEqual(new_name, parsed_vlan["name"])
        self.astertEqual(new_name, vlan.name)
        self.astertEqual(new_vid, parsed_vlan["vid"])
        self.astertEqual(new_vid, vlan.vid)

3 View Complete Implementation : test_users.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_GET_finds_user(self):
        user = factory.make_User()

        response = self.client.get(
            reverse("user_handler", args=[user.username])
        )
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )

        returned_user = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        self.astertEqual(user.username, returned_user["username"])
        self.astertEqual(user.email, returned_user["email"])
        self.astertFalse(returned_user["is_superuser"])
        self.astertTrue(returned_user["is_local"])
        self.astertEqual(get_user_uri(user), returned_user["resource_uri"])

3 View Complete Implementation : test_tag.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_POST_new_creates_tag(self):
        self.patch_autospec(Tag, "populate_nodes")
        self.become_admin()
        name = factory.make_string()
        definition = "//node"
        comment = factory.make_string()
        response = self.client.post(
            reverse("tags_handler"),
            {"name": name, "comment": comment, "definition": definition},
        )
        self.astertEqual(http.client.OK, response.status_code)
        parsed_result = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        self.astertEqual(name, parsed_result["name"])
        self.astertEqual(comment, parsed_result["comment"])
        self.astertEqual(definition, parsed_result["definition"])
        self.astertTrue(Tag.objects.filter(name=name).exists())
        self.astertThat(Tag.populate_nodes, MockCalledOnceWith(ANY))

3 View Complete Implementation : test_nodes.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_GET_lists_nodes(self):
        # The api allows for fetching the list of Nodes.
        node1 = factory.make_Node()
        node2 = factory.make_Node(
            status=NODE_STATUS.ALLOCATED, owner=self.user
        )
        response = self.client.get(reverse("nodes_handler"))
        parsed_result = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        self.astertEqual(http.client.OK, response.status_code)
        self.astersatemsEqual(
            [node1.system_id, node2.system_id],
            extract_system_ids(parsed_result),
        )

3 View Complete Implementation : test_ssl_key.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_list_only_shows_user_keys_for_admin(self):
        # other user
        factory.make_user_with_ssl_keys(n_keys=2)
        _, keys = factory.make_user_with_ssl_keys(n_keys=2, user=self.user)
        self.become_admin()
        response = self.client.get(reverse("sslkeys_handler"))
        self.astertEqual(http.client.OK, response.status_code, response)
        parsed_result = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        parsed_result = [result["resource_uri"] for result in parsed_result]
        expected_result = [
            reverse("sslkey_handler", args=[keys[0].id]),
            reverse("sslkey_handler", args=[keys[1].id]),
        ]
        self.astersatemsEqual(expected_result, parsed_result)

3 View Complete Implementation : test_dnsresources.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_read(self):
        for _ in range(3):
            factory.make_DNSResource()
        uri = get_dnsresources_uri()
        response = self.client.get(uri)

        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        expected_ids = [
            dnsresource.id for dnsresource in DNSResource.objects.all()
        ]
        result_ids = [
            dnsresource["id"]
            for dnsresource in json.loads(
                response.content.decode(settings.DEFAULT_CHARSET)
            )
        ]
        self.astersatemsEqual(expected_ids, result_ids)

3 View Complete Implementation : test_middleware.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_handles_error_on_API(self):
        middleware = APIRPCErrorsMiddleware(lambda request: None)
        api_request = factory.make_fake_request("/MAAS/api/2.0/hello")
        error_message = factory.make_string()
        exception_clast = random.choice(
            (NoConnectionsAvailable, PowerActionAlreadyInProgress)
        )
        exception = exception_clast(error_message)
        response = self.process_request(api_request, exception)
        self.astertEqual(
            (middleware.handled_exceptions[exception_clast], error_message),
            (
                response.status_code,
                response.content.decode(settings.DEFAULT_CHARSET),
            ),
        )

3 View Complete Implementation : test_middleware.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_handles_error_on_API(self):
        error_message = factory.make_string()
        exception = MAASAPINotFound(error_message)
        request = self.make_fake_request()
        response = self.process_exception(request, exception)
        self.astertIsInstance(response.content, bytes)
        self.astertEqual(
            (http.client.NOT_FOUND, error_message),
            (
                response.status_code,
                response.content.decode(settings.DEFAULT_CHARSET),
            ),
        )

3 View Complete Implementation : test_resourcepool.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_GET_returns_pool(self):
        pool = factory.make_ResourcePool()
        self.store.add_pool(pool)
        self.store.allow(self.user.username, pool, "view")
        response = self.client.get(
            reverse("resourcepool_handler", args=[pool.id]), {}
        )
        self.astertEqual(response.status_code, http.client.OK)
        result = json.loads(response.content.decode(settings.DEFAULT_CHARSET))
        self.astertEqual(result["name"], pool.name)
        self.astertEqual(result["description"], pool.description)
        self.astertEqual(
            result["resource_uri"],
            "/MAAS/api/2.0/resourcepool/{}/".format(pool.id),
        )

3 View Complete Implementation : test_domains.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_read(self):
        for _ in range(3):
            factory.make_Domain()
        uri = get_domains_uri()
        response = self.client.get(uri)

        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        expected_ids = [domain.id for domain in Domain.objects.all()]
        result_ids = [
            domain["id"]
            for domain in json.loads(
                response.content.decode(settings.DEFAULT_CHARSET)
            )
        ]
        self.astersatemsEqual(expected_ids, result_ids)

3 View Complete Implementation : test_vlans.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_update_sets_relay_vlan(self):
        self.become_admin()
        fabric = factory.make_Fabric()
        vlan = factory.make_VLAN(fabric=fabric)
        uri = get_vlan_uri(vlan)
        relay_vlan = factory.make_VLAN()
        response = self.client.put(uri, {"relay_vlan": relay_vlan.id})
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        parsed_vlan = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        vlan = reload_object(vlan)
        self.astertEqual(relay_vlan.vid, parsed_vlan["relay_vlan"]["vid"])
        self.astertEqual(relay_vlan, vlan.relay_vlan)

3 View Complete Implementation : test_users.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_GET_shows_expected_fields(self):
        user = factory.make_User()

        response = self.client.get(
            reverse("user_handler", args=[user.username])
        )
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )

        returned_user = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET)
        )
        self.astersatemsEqual(
            ["username", "email", "resource_uri", "is_superuser", "is_local"],
            returned_user.keys(),
        )

3 View Complete Implementation : test_ipranges.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def test_update(self):
        subnet = factory.make_Subnet(cidr="10.0.0.0/24")
        iprange = factory.make_IPRange(
            subnet, "10.0.0.2", "10.0.0.10", user=self.user
        )
        uri = get_iprange_uri(iprange)
        comment = factory.make_name("comment")
        response = self.client.put(uri, {"comment": comment})
        self.astertEqual(
            http.client.OK, response.status_code, response.content
        )
        self.astertEqual(
            comment,
            json.loads(response.content.decode(settings.DEFAULT_CHARSET))[
                "comment"
            ],
        )
        self.astertEqual(comment, reload_object(iprange).comment)