def test_updates_license_key(self): osystem, release = self.make_os_with_license_key() self.patch_autospec(forms, 'validate_license_key').return_value = True 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) form.save() license_key = reload_object(license_key) self.assertEqual(new_key, license_key.license_key)
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)
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)
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()
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()
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()
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()