Example #1
0
 def test_errors_on_not_unique(self):
     osystem, release = self.make_os_with_license_key()
     self.patch_autospec(forms, "validate_license_key").return_value = True
     key = factory.make_name("key")
     factory.make_LicenseKey(
         osystem=osystem["name"],
         distro_series=release["name"],
         license_key=key,
     )
     definition = {
         "osystem": osystem["name"],
         "distro_series": "%s/%s" % (osystem["name"], release["name"]),
         "license_key": key,
     }
     form = LicenseKeyForm(data=definition)
     self.assertFalse(form.is_valid(), form.errors)
     self.assertEqual(
         {
             "__all__": [
                 "%s %s" % (
                     "License key with this operating system and distro series",
                     "already exists.",
                 )
             ]
         },
         form.errors,
     )
Example #2
0
    def create(self, request):
        """Define a license key.

        :param osystem: Operating system that the key belongs to.
        :param distro_series: OS release that the key belongs to.
        :param license_key: License key for osystem/distro_series combo.
        """
        # If the user provides no parametes to the create command, then
        # django will make the request.data=None. This will cause the form
        # to be valid, not returning all the missing fields.
        if request.data is None:
            data = {}
        else:
            data = request.data.copy()

        if "distro_series" in data:
            # Preprocess distro_series if present.
            if "/" in data["distro_series"]:
                if "osystem" not in data:
                    # Construct osystem value from distroseries.
                    data["osystem"] = data["distro_series"].split("/", 1)[0]
            else:
                # If distro_series is not of the form "os/series", we combine
                # osystem with distro_series since that is what LicenseKeyForm
                # expects.
                if "osystem" in data:
                    data["distro_series"] = "%s/%s" % (data["osystem"],
                                                       data["distro_series"])
        form = LicenseKeyForm(data=data)
        if not form.is_valid():
            raise MAASAPIValidationError(form.errors)
        return form.save()
Example #3
0
 def test_validates_license_key(self):
     osystem, release = self.make_os_with_license_key()
     self.patch_autospec(forms, 'validate_license_key').return_value = False
     license_key = factory.make_LicenseKey(
         osystem=osystem['name'],
         distro_series=release['name'],
         license_key=factory.make_name('key'))
     new_key = factory.make_name('key')
     form = LicenseKeyForm(data={'license_key': new_key},
                           instance=license_key)
     self.assertFalse(form.is_valid(), form.errors)
     self.assertEqual({'__all__': ['Invalid license key.']}, form.errors)
Example #4
0
 def test_validates_license_key(self):
     osystem, release = self.make_os_with_license_key()
     self.patch_autospec(forms, "validate_license_key").return_value = False
     license_key = factory.make_LicenseKey(
         osystem=osystem["name"],
         distro_series=release["name"],
         license_key=factory.make_name("key"),
     )
     new_key = factory.make_name("key")
     form = LicenseKeyForm(data={"license_key": new_key},
                           instance=license_key)
     self.assertFalse(form.is_valid(), form.errors)
     self.assertEqual({"__all__": ["Invalid license key."]}, form.errors)
Example #5
0
    def create(self, request):
        """@description-title Define a license key
        @description Define a license key.

        @param (string) "osystem" [required=true] Operating system that the key
        belongs to.

        @param (string) "distro_series" [required=true] OS release that the key
        belongs to.

        @param (string) "license_key" [required=true] License key for
        osystem/distro_series combo.

        @success (http-status-code) "server-success" 200
        @success (json) "success-json" A JSON object containing the new license
        key.
        @success-example "success-json" [exkey=license-keys-placeholder]
        placeholder text
        """
        # If the user provides no parametes to the create command, then
        # django will make the request.data=None. This will cause the form
        # to be valid, not returning all the missing fields.
        if request.data is None:
            data = {}
        else:
            data = request.data.copy()

        if "distro_series" in data:
            # Preprocess distro_series if present.
            if "/" in data["distro_series"]:
                if "osystem" not in data:
                    # Construct osystem value from distroseries.
                    data["osystem"] = data["distro_series"].split("/", 1)[0]
            else:
                # If distro_series is not of the form "os/series", we combine
                # osystem with distro_series since that is what LicenseKeyForm
                # expects.
                if "osystem" in data:
                    data["distro_series"] = "%s/%s" % (
                        data["osystem"],
                        data["distro_series"],
                    )
        form = LicenseKeyForm(data=data)
        if not form.is_valid():
            raise MAASAPIValidationError(form.errors)
        return form.save()
Example #6
0
    def update(self, request, osystem, distro_series):
        """Update license key.

        :param osystem: Operating system that the key belongs to.
        :param distro_series: OS release that the key belongs to.
        :param license_key: License key for osystem/distro_series combo.
        """
        license_key = get_object_or_404(LicenseKey,
                                        osystem=osystem,
                                        distro_series=distro_series)
        data = request.data
        if data is None:
            data = {}
        form = LicenseKeyForm(instance=license_key, data=data)
        if not form.is_valid():
            raise MAASAPIValidationError(form.errors)
        return form.save()
Example #7
0
 def test_errors_on_not_unique(self):
     osystem, release = self.make_os_with_license_key()
     self.patch_autospec(forms, 'validate_license_key').return_value = True
     key = factory.make_name('key')
     factory.make_LicenseKey(
         osystem=osystem['name'], distro_series=release['name'],
         license_key=key)
     definition = {
         'osystem': osystem['name'],
         'distro_series': "%s/%s" % (osystem['name'], release['name']),
         'license_key': key,
         }
     form = LicenseKeyForm(data=definition)
     self.assertFalse(form.is_valid(), form.errors)
     self.assertEqual({
         '__all__': ['%s %s' % (
             "License key with this operating system and distro series",
             "already exists.")]},
         form.errors)
Example #8
0
    def update(self, request, osystem, distro_series):
        """@description-title Update license key
        @description Update a license key for the given operating system and
        distro series.

        @param (string) "{osystem}" [required=true] Operating system that the
        key belongs to.

        @param (string) "{distro_series}" [required=true] OS release that the
        key belongs to.

        @param (string) "license_key" [required=false] License key for
        osystem/distro_series combo.

        @success (http-status-code) "server-success" 200
        @success (json) "success-json" A JSON object containing the updated
        license key.
        @success-example "success-json" [exkey=license-keys-placeholder]
        placeholder text

        @error (http-status-code) "404" 404
        @error (content) "not-found" The requested operating system and distro
        series combination is not found.
        @error-example "not-found"
            Unknown API endpoint: /MAAS/api/2.0/license-key/windows/win2012/.
        """
        license_key = get_object_or_404(LicenseKey,
                                        osystem=osystem,
                                        distro_series=distro_series)
        data = request.data
        if data is None:
            data = {}
        form = LicenseKeyForm(instance=license_key, data=data)
        if not form.is_valid():
            raise MAASAPIValidationError(form.errors)
        return form.save()
Example #9
0
 def test_requires_all_fields(self):
     form = LicenseKeyForm(data={})
     self.assertFalse(form.is_valid(), form.errors)
     self.assertItemsEqual(['osystem', 'distro_series', 'license_key'],
                           form.errors.keys())