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 test_POST_is_prohibited(self):
     self.become_admin()
     license_key = factory.make_LicenseKey()
     response = self.client.post(
         self.get_url(license_key.osystem, license_key.distro_series),
         {'osystem': "New osystem"})
     self.assertEqual(http.client.METHOD_NOT_ALLOWED, response.status_code)
 def test_can_update_license_key(self):
     self.client.login(user=factory.make_admin())
     key = factory.make_LicenseKey(
         distro_series=random.choice(REQUIRE_LICENSE_KEY))
     make_osystem_requiring_license_key(self, key.osystem,
                                        key.distro_series)
     self.patch(forms, "validate_license_key").return_value = True
     new_key = factory.make_name("key")
     edit_link = reverse("license-key-edit",
                         args=[key.osystem, key.distro_series])
     definition = {
         "osystem": key.osystem,
         "distro_series": "%s/%s" % (key.osystem, key.distro_series),
         "license_key": new_key,
     }
     response = self.client.post(edit_link, definition)
     self.assertEqual(
         (http.client.FOUND, reverse("settings_license_keys")),
         (response.status_code, extract_redirect(response)),
     )
     expected_result = {
         "osystem": key.osystem,
         "distro_series": key.distro_series,
         "license_key": new_key,
     }
     self.assertAttributes(reload_object(key), expected_result)
Example #4
0
 def test_DELETE_deletes_license_key(self):
     self.become_admin()
     license_key = factory.make_LicenseKey()
     response = self.client.delete(
         self.get_url(license_key.osystem, license_key.distro_series))
     self.assertEqual(http.client.NO_CONTENT, response.status_code)
     self.assertIsNone(reload_object(license_key))
Example #5
0
 def test_errors_on_not_unique(self):
     key = factory.make_LicenseKey()
     new_key = factory.make_name('key')
     self.assertRaises(IntegrityError,
                       LicenseKey.objects.create,
                       osystem=key.osystem,
                       distro_series=key.distro_series,
                       license_key=new_key)
Example #6
0
 def make_license_key_with_os(self, license_key=None):
     release = make_rpc_release(requires_license_key=True)
     osystem = make_rpc_osystem(releases=[release])
     patch_usable_osystems(self, osystems=[osystem])
     license_key = factory.make_LicenseKey(osystem=osystem['name'],
                                           distro_series=release['name'],
                                           license_key=license_key)
     return license_key
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)
 def make_license_key_with_os(self,
                              osystem=None,
                              distro_series=None,
                              license_key=None):
     license_key = factory.make_LicenseKey(osystem=osystem,
                                           distro_series=distro_series,
                                           license_key=license_key)
     osystem = make_osystem_requiring_license_key(license_key.osystem,
                                                  license_key.distro_series)
     return license_key, osystem
Example #9
0
 def make_license_key_with_os(
         self, osystem=None, release=None, license_key=None):
     if release is None:
         release = random.choice(REQUIRE_LICENSE_KEY)
     license_key = factory.make_LicenseKey(
         osystem=osystem, distro_series=release,
         license_key=license_key)
     osystem = make_osystem_requiring_license_key(
         self, license_key.osystem, license_key.distro_series)
     return license_key, osystem
 def test_can_delete_license_key(self):
     self.client.login(user=factory.make_admin())
     key = factory.make_LicenseKey()
     delete_link = reverse('license-key-delete',
                           args=[key.osystem, key.distro_series])
     response = self.client.post(delete_link, {'post': 'yes'})
     self.assertEqual((http.client.FOUND, reverse('settings_license_keys')),
                      (response.status_code, extract_redirect(response)))
     self.assertFalse(
         LicenseKey.objects.filter(
             osystem=key.osystem, distro_series=key.distro_series).exists())
Example #11
0
 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)
Example #12
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 #13
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 #14
0
    def test_GET_returns_license_key(self):
        self.become_admin()
        license_key = factory.make_LicenseKey()

        response = self.client.get(
            self.get_url(license_key.osystem, license_key.distro_series))
        self.assertEqual(http.client.OK, response.status_code)

        parsed_result = json_load_bytes(response.content)
        self.assertEqual((
            license_key.osystem,
            license_key.distro_series,
            license_key.license_key,
        ), (
            parsed_result['osystem'],
            parsed_result['distro_series'],
            parsed_result['license_key'],
        ))
Example #15
0
    def test_GET_returns_license_keys(self):
        self.become_admin()
        orig_license_key = factory.make_LicenseKey()

        response = self.client.get(reverse('license_keys_handler'))
        self.assertEqual(http.client.OK, response.status_code,
                         response.content)

        parsed_result = json_load_bytes(response.content)
        self.assertEqual(1, len(parsed_result))
        [returned_network] = parsed_result
        fields = {'osystem', 'distro_series', 'license_key'}
        self.assertItemsEqual(fields.union({'resource_uri'}),
                              returned_network.keys())
        expected_values = {
            field: getattr(orig_license_key, field)
            for field in fields if field != 'resource_uri'
        }
        expected_values['resource_uri'] = reverse(
            'license_key_handler',
            args=[orig_license_key.osystem, orig_license_key.distro_series])
        self.assertEqual(expected_values, returned_network)
 def test_can_update_license_key(self):
     self.client.login(user=factory.make_admin())
     key = factory.make_LicenseKey()
     osystem = make_osystem_requiring_license_key(key.osystem,
                                                  key.distro_series)
     patch_usable_osystems(self, osystems=[osystem])
     self.patch(forms, 'validate_license_key').return_value = True
     new_key = factory.make_name('key')
     edit_link = reverse('license-key-edit',
                         args=[key.osystem, key.distro_series])
     definition = {
         'osystem': key.osystem,
         'distro_series': "%s/%s" % (key.osystem, key.distro_series),
         'license_key': new_key,
     }
     response = self.client.post(edit_link, definition)
     self.assertEqual((http.client.FOUND, reverse('settings_license_keys')),
                      (response.status_code, extract_redirect(response)))
     expected_result = {
         'osystem': key.osystem,
         'distro_series': key.distro_series,
         'license_key': new_key,
     }
     self.assertAttributes(reload_object(key), expected_result)
Example #17
0
 def test_get_license_key(self):
     key = factory.make_LicenseKey()
     license_key = LicenseKey.objects.get_license_key(
         key.osystem, key.distro_series
     )
     self.assertEqual(key.license_key, license_key)
Example #18
0
 def test_GET_requires_admin(self):
     license_key = factory.make_LicenseKey()
     response = self.client.get(
         self.get_url(license_key.osystem, license_key.distro_series))
     self.assertEqual(http.client.FORBIDDEN, response.status_code)
Example #19
0
 def make_license_key_with_os(self, license_key=None):
     osystem, release = make_os(self)
     license_key = factory.make_LicenseKey(osystem=osystem,
                                           distro_series=release,
                                           license_key=license_key)
     return license_key
Example #20
0
 def test_GET_requires_admin(self):
     factory.make_LicenseKey()
     response = self.client.get(reverse("license_keys_handler"))
     self.assertEqual(http.client.FORBIDDEN, response.status_code)
Example #21
0
 def test_DELETE_requires_admin(self):
     license_key = factory.make_LicenseKey()
     response = self.client.delete(
         self.get_url(license_key.osystem, license_key.distro_series))
     self.assertEqual(http.client.FORBIDDEN, response.status_code)
     self.assertIsNotNone(reload_object(license_key))
Example #22
0
 def test_get_by_osystem_series(self):
     key = factory.make_LicenseKey()
     expected = LicenseKey.objects.get_by_osystem_series(
         key.osystem, key.distro_series
     )
     self.assertEqual(key, expected)
Example #23
0
 def test_has_license_key_False(self):
     factory.make_LicenseKey()
     osystem = factory.make_name("osystem")
     series = factory.make_name("distro_series")
     self.assertFalse(LicenseKey.objects.has_license_key(osystem, series))
Example #24
0
 def test_has_license_key_True(self):
     key = factory.make_LicenseKey()
     self.assertTrue(
         LicenseKey.objects.has_license_key(key.osystem, key.distro_series)
     )