def test_password_required(self): """ if either username or password are entered both are required """ relation = ['username', 'password'] for property in relation: data_ = self.data.copy() del data_[property] form = EditClusterForm(data_) self.assertFalse(form.is_valid())
def test_read_only(self): """ success without username or password """ data_ = self.data del data_['username'] del data_['password'] form = EditClusterForm(data_) self.assertTrue(form.is_valid()) cluster = form.save() for k, v in data_.items(): self.assertEqual(v, getattr(cluster, k))
def test_valid_edit(self): """ successfully edit a cluster """ data = self.data form = EditClusterForm(data, instance=self.cluster) self.assertTrue(form.is_valid()) cluster = form.save() for k, v in data.items(): self.assertEqual(v, getattr(cluster, k))
def test_read_only(self): """ success without username or password """ data_ = self.data del data_['username'] del data_['password'] form = EditClusterForm(data_) self.assertTrue(form.is_valid()) cluster = form.save() for k, v in data_.items(): self.assertEqual(v, getattr(cluster, k)) cluster.delete()
def test_required_fields(self): """ Tests adding a new cluster """ data = self.data required = ['hostname', 'port'] for property in required: data_ = data.copy() del data_[property] form = EditClusterForm(data_) self.assertFalse(form.is_valid())
def test_optional_fields(self): data = self.data non_required = ['slug', 'description', 'virtual_cpus', 'disk', 'ram'] for property in non_required: data_ = data.copy() del data_[property] form = EditClusterForm(data_) self.assertTrue(form.is_valid()) cluster = form.save() for k, v in data_.items(): self.assertEqual(v, getattr(cluster, k)) cluster.delete()
def test_no_username_or_password_for_read_only(self): """ tests that username and password are optional for read only cluster """ data = self.data del data['username'] del data['password'] form = EditClusterForm(data, instance=self.cluster) self.assertTrue(form.is_valid()) cluster = form.save() self.assertEqual('', cluster.username) self.assertEqual('', cluster.password)
def test_username_required_for_writeable_new_password(self): """ if password is entered for a cluster, username is required always """ self.cluster.username = '******' self.cluster.password = '******' self.cluster.save() data = self.data del data['username'] form = EditClusterForm(data, instance=self.cluster) self.assertFalse(form.is_valid())
def test_optional_fields(self): data = self.data non_required = ['slug','description','virtual_cpus','disk','ram'] for property in non_required: data_ = data.copy() del data_[property] form = EditClusterForm(data_) self.assertTrue(form.is_valid()) cluster = form.save() for k, v in data_.items(): self.assertEqual(v, getattr(cluster, k)) Cluster.objects.all().delete()
def test_no_password_for_writeable_new_username(self): """ tests that the password is required when the username has changed """ self.cluster.username = '******' self.cluster.password = '******' self.cluster.save() data = self.data del data['password'] data['username'] = '******' form = EditClusterForm(data, instance=self.cluster) self.assertFalse(form.is_valid())
def test_optional_fields(self): data = self.data non_required = ['slug', 'description', 'virtual_cpus', 'disk', 'ram'] for field in non_required: data_ = data.copy() del data_[field] form = EditClusterForm(data_) self.assertTrue(form.is_valid(), "Field %r was incorrectly required" % field) cluster = form.save() for k, v in data_.items(): self.assertEqual(v, getattr(cluster, k)) cluster.delete()
def test_unique_fields(self): # test unique fields form = EditClusterForm(self.data) form.is_valid() form.save() for property in ['hostname', 'slug']: data_ = self.data.copy() data_[property] = 'different' form = EditClusterForm(data_) self.assertFalse(form.is_valid())
def edit(request, cluster_slug=None): """ Edit a cluster """ if cluster_slug: cluster = get_object_or_404(Cluster, slug=cluster_slug) else: cluster = None user = request.user if not (user.is_superuser or (cluster and user.has_perm( 'admin', cluster))): raise PermissionDenied(NO_PRIVS) if request.method == 'POST': form = EditClusterForm(request.POST, instance=cluster) if form.is_valid(): cluster = form.save() # TODO Create post signal to import # virtual machines on edit of cluster if cluster.info is None: try: cluster.sync_nodes() cluster.sync_virtual_machines() except GanetiApiError: # ganeti errors here are silently discarded. It's # valid to enter bad info. A user might be adding # info for an offline cluster. pass log_action('EDIT' if cluster_slug else 'CREATE', user, cluster) return HttpResponseRedirect(reverse('cluster-detail', args=[cluster.slug])) elif request.method == 'DELETE': cluster.delete() return HttpResponse('1', mimetype='application/json') else: form = EditClusterForm(instance=cluster) return render_to_response("ganeti/cluster/edit.html", { 'form': form, 'cluster': cluster, }, context_instance=RequestContext(request), )
def test_username_and_password_change(self): """ tests changing the password for a cluster that already had username and password set """ self.cluster.username = '******' self.cluster.password = '******' self.cluster.save() data = self.data form = EditClusterForm(data, instance=self.cluster) self.assertTrue(form.is_valid()) cluster = form.save() self.assertEqual('tester', cluster.username) self.assertEqual('secret', cluster.password)
def test_no_password_for_writeable(self): """ Tests that password is not required when the cluster already has the password available. assumes username is the same """ self.cluster.username = '******' self.cluster.password = '******' self.cluster.save() data = self.data del data['password'] form = EditClusterForm(data, instance=self.cluster) self.assertTrue(form.is_valid()) cluster = form.save() self.assertEqual('tester', cluster.username) self.assertEqual('secret', cluster.password)
def test_username_and_password_added(self): """ tests setting a username and password for a cluster that did not previously have a username and password """ self.cluster.username = "" self.cluster.password = "" self.cluster.save() data = self.data data['username'] = '******' data['password'] = '******' form = EditClusterForm(data, instance=self.cluster) self.assertTrue(form.is_valid()) cluster = form.save() self.assertEqual('foo', cluster.username) self.assertEqual('bar', cluster.password)
def test_username_and_password_added(self): """ tests setting a username and password for a cluster that did not previously have a username and password """ self.cluster.username = None self.cluster.password = None self.cluster.save() data = self.data data['username'] = '******' data['password'] = '******' form = EditClusterForm(data, instance=self.cluster) self.assertTrue(form.is_valid()) cluster = form.save() self.assertEqual('foo', cluster.username) self.assertEqual('bar', cluster.password)
def edit(request, cluster_slug=None): """ Edit a cluster """ if cluster_slug: cluster = get_object_or_404(Cluster, slug=cluster_slug) else: cluster = None user = request.user if not (user.is_superuser or (cluster and user.has_perm('admin', cluster))): raise Http403(NO_PRIVS) if request.method == 'POST': form = EditClusterForm(request.POST, instance=cluster) if form.is_valid(): cluster = form.save() # TODO Create post signal to import # virtual machines on edit of cluster if cluster.info is None: try: cluster.sync_nodes() cluster.sync_virtual_machines() except GanetiApiError: # ganeti errors here are silently discarded. It's # valid to enter bad info. A user might be adding # info for an offline cluster. pass log_action('EDIT' if cluster_slug else 'CREATE', user, cluster) return HttpResponseRedirect( reverse('cluster-detail', args=[cluster.slug])) elif request.method == 'DELETE': cluster.delete() return HttpResponse('1', mimetype='application/json') else: form = EditClusterForm(instance=cluster) return render_to_response( "ganeti/cluster/edit.html", { 'form': form, 'cluster': cluster, }, context_instance=RequestContext(request), )
def test_bound_form(self): """ tests binding form with existing instance """ form = EditClusterForm(self.data) self.assertTrue(form.is_valid())
def test_bound(self): form = EditClusterForm(self.data) self.assertTrue(form.is_valid())
def test_unbound(self): form = EditClusterForm()