Esempio n. 1
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()
Esempio n. 2
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()
Esempio n. 3
0
 def test_creates_license_key(self):
     osystem, release = self.make_os_with_license_key()
     key = factory.make_name("key")
     self.patch_autospec(forms, "validate_license_key").return_value = True
     definition = {
         "osystem": osystem["name"],
         "distro_series": release["name"],
         "license_key": key,
     }
     data = definition.copy()
     data["distro_series"] = "%s/%s" % (osystem["name"], release["name"])
     form = LicenseKeyForm(data=data)
     form.save()
     license_key_obj = LicenseKey.objects.get(
         osystem=osystem["name"], distro_series=release["name"]
     )
     self.assertAttributes(license_key_obj, definition)
Esempio n. 4
0
 def test_includes_all_osystems_sorted(self):
     osystems = [self.make_os_with_license_key()[0] for _ in range(3)]
     choices = [
         (osystem["name"], osystem["title"])
         for osystem in sorted(osystems, key=itemgetter("title"))
     ]
     form = LicenseKeyForm()
     self.assertEqual(choices, form.fields["osystem"].choices)
Esempio n. 5
0
 def test_includes_osystem_in_choices(self):
     osystems = []
     for _ in range(3):
         release = make_rpc_release(requires_license_key=True)
         osystems.append(make_rpc_osystem(releases=[release]))
     patch_usable_osystems(self, osystems=osystems)
     choices = [(osystem['name'], osystem['title']) for osystem in osystems]
     form = LicenseKeyForm()
     self.assertItemsEqual(choices, form.fields['osystem'].choices)
Esempio n. 6
0
 def test_includes_only_osystems_that_require_license_keys(self):
     osystems = [self.make_os_with_license_key()[0] for _ in range(2)]
     factory.make_BootResource()
     choices = [
         (osystem['name'], osystem['title'])
         for osystem in sorted(osystems, key=itemgetter('title'))
         ]
     form = LicenseKeyForm()
     self.assertEqual(choices, form.fields['osystem'].choices)
Esempio n. 7
0
 def test_includes_all_distro_series(self):
     releases = [
         make_rpc_release(requires_license_key=True) for _ in range(3)
     ]
     osystem = make_rpc_osystem(releases=releases)
     patch_usable_osystems(self, osystems=[osystem])
     choices = [('%s/%s' % (osystem['name'], release['name']),
                 release['title']) for release in releases]
     form = LicenseKeyForm()
     self.assertItemsEqual(choices, form.fields['distro_series'].choices)
Esempio n. 8
0
 def test_includes_only_osystems_that_require_license_keys(self):
     osystems = []
     for _ in range(2):
         release = make_rpc_release(requires_license_key=True)
         osystems.append(make_rpc_osystem(releases=[release]))
     patch_usable_osystems(self, osystems=osystems + [make_rpc_osystem()])
     choices = [(osystem['name'], osystem['title'])
                for osystem in sorted(osystems, key=itemgetter('title'))]
     form = LicenseKeyForm()
     self.assertEqual(choices, form.fields['osystem'].choices)
Esempio n. 9
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)
Esempio n. 10
0
 def test_includes_only_distro_series_that_require_license_keys(self):
     osystem = factory.make_name("osystem")
     osystem_title = factory.make_name("osystem_title")
     releases = [
         self.make_os_with_license_key(osystem, osystem_title, release)[1]
         for release in REQUIRE_LICENSE_KEY
     ]
     factory.make_BootResource()
     choices = [("%s/%s" % (osystem, release["name"]), release["title"])
                for release in releases]
     form = LicenseKeyForm()
     self.assertItemsEqual(choices, form.fields["distro_series"].choices)
Esempio n. 11
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()
Esempio n. 12
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())
Esempio n. 13
0
 def test_doesnt_include_default_distro_series(self):
     form = LicenseKeyForm()
     self.assertNotIn(('', 'Default OS Release'),
                      form.fields['distro_series'].choices)
Esempio n. 14
0
 def test_doesnt_include_default_osystem(self):
     form = LicenseKeyForm()
     self.assertNotIn(('', 'Default OS'), form.fields['osystem'].choices)
Esempio n. 15
0
 def test_doesnt_include_default_distro_series(self):
     form = LicenseKeyForm()
     self.assertNotIn(("", "Default OS Release"),
                      form.fields["distro_series"].choices)
Esempio n. 16
0
 def test_doesnt_include_default_osystem(self):
     form = LicenseKeyForm()
     self.assertNotIn(("", "Default OS"), form.fields["osystem"].choices)