def user_keys(request): msg = None if request.method == "GET": form = SshKeyForm() else: form = SshKeyForm(request.POST) if form.is_valid(): dups = [] for key_type, key, comment in form.cleaned_data["ssh_pubkey"]: ssh_key = SshPublicKey(key_type=key_type, key=key, comment=comment, owner=request.user) fprint = ssh_key.compute_fingerprint() other_keys = SshPublicKey.objects.filter(owner=request.user, fingerprint=fprint) if not other_keys: ssh_key.fingerprint = fprint ssh_key.save() form = SshKeyForm() else: dups.append(fprint) if dups: msg = _("The following keys were skipped because" " they already exist:<br />%s") % "<br />".join(dups) msg = mark_safe(msg) keys = SshPublicKey.objects.filter(owner=request.user) return render( request, 'users/user_keys.html', { 'form': form, 'keys': keys, 'msg': msg } )
def test_user_application(self): self.client.login(username='******', password='******') # create an application data = { 'hostname': 'test2.example.com', 'memory': 1024, 'vcpus': 1, 'disk_size': 5, 'operating_system': 'noop', 'comments': 'test', 'accept_tos': 'on', 'admin_contact_name': 'asd', 'admin_contact_email': '*****@*****.**', 'admin_contact_phone': '12123489', } res = self.client.post(reverse('apply'), data) # redirect means form is valid self.assertEqual(res.status_code, 302) # lets make sure the application has been saved application = InstanceApplication.objects.get(hostname='test2.example.com') # lets see if the application shows up for the admin self.client.login(username='******', password='******') # make sure the admin can edit the application res = self.client.get( reverse( 'application-review', kwargs={ 'application_id': application.id }) ) self.assertEqual(res.status_code, 200) # approve form form = res.context['appform'] # make sure the form is currently invalid self.assertEqual(form.is_valid(), False) # make sure the form is InstanceApplicationReviewForm self.assertEqual(form.__class__.__name__, 'InstanceApplicationReviewForm') cluster = self.create_dummy_cluster() # try to accept instance with missing fields that are required # only in accept. # initialize the form and add the extra data. form = form.__class__(data) self.assertEqual(form.is_valid(), False) self.assertEqual('cluster' in form.errors.keys(), True) self.assertEqual('netw' in form.errors.keys(), True) self.assertEqual('node_group' in form.errors.keys(), True) self.assertEqual('disk_template' in form.errors.keys(), True) # initialize the form and add the extra data. data['cluster'] = cluster.id form = form.__class__(data) form.fields['cluster'].choices.append((100, 100)) form.fields['netw'].choices.append(('test::test', 'test::test')) form.fields['disk_template'].choices.append(('test', 'test')) form.fields['node_group'].choices.append(('test', 'test')) form.fields['vgs'].choices.append(('test', 'test')) form.data['netw'] = 'test::test' form.data['disk_template'] = 'test' form.data['node_group'] = 'test' form.data['vgs'] = 'test' self.assertEqual(form.is_valid(), True) # accept instance res = self.send_application_review(form.data, application) self.assertEqual(res.status_code, 302) # make sure the application has pending code self.assertEqual(application.status in PENDING_CODES, True) # reject instance form.data['reject'] = 'reject' # we have to make sure the form ignores cluster, netw etc # in case of rejection form.data['cluster'] = '' form.data['netw'] = '' form.data['disk_template'] = '' form.data['node_group'] = '' form.data['vgs'] = '' # make the request res = self.send_application_review(form.data, application) form = res.context['appform'] self.assertEqual('admin_comments' in form.errors.keys(), True) # send the form properly form.data['admin_comments'] = 'test' res = self.send_application_review(form.data, application) self.assertEqual(res.status_code, 302) # make sure the application has pending code self.assertEqual(application.status in PENDING_CODES, True) # ssh_keys res = self.client.get(reverse( "instance-ssh-keys", kwargs={ "application_id": application.id, "cookie": application.cookie } )) self.assertEqual(res.content, '') # create an ssh key for user key = SshPublicKey( key_type='test', key='test', comment='test', owner=User.objects.get(username='******'), fingerprint='test' ) key.save() res = self.client.get(reverse( "instance-ssh-keys", kwargs={ "application_id": application.id, "cookie": application.cookie } )) self.assertEqual(key.key_line(), res.content)