Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #294 from VNG-Realisatie/feature/modify-env-name
Browse files Browse the repository at this point in the history
Allow modification of environment name in endpoint update form
  • Loading branch information
alextreme authored Dec 16, 2019
2 parents 4244528 + 9d10ad5 commit 10cd494
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/vng/servervalidation/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,12 @@ def clean_name(self):
"A test scenario with this name already exists"
))
return name


class EnvironmentUpdateForm(forms.ModelForm):

name = forms.CharField(required=False, help_text=_("The name of the environment"))

class Meta:
model = Environment
fields = ['name']
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% load sniplates %}
{% load django_bootstrap_breadcrumbs %}
{% load i18n %}
{% load crispy_forms_tags %}

{% block title %}{% trans "Update environment" %}{% endblock%}

Expand All @@ -23,6 +24,9 @@
</div>
<div class='card-body'>
{% load_widgets form='forms.html' %}
{{ env_form|crispy }}

<h4>Variables:</h4>
{% for field in form %}
{% form_field field %}
{% endfor %}
Expand All @@ -47,10 +51,14 @@

<script>
var form = document.getElementsByTagName("form")[1];
var inputs = form.getElementsByTagName("input");
var submit = document.getElementById("submit");

form.addEventListener("input", function () {
var submit = document.getElementById("submit");
submit.onclick = function() { return confirm('Modifying an environment will remove all of the previous provider runs for this environment.'); };
});
// Modifying only the environment name should not delete previous runs
for(i=2; i<inputs.length; i++) {
inputs[i].addEventListener("input", function () {
submit.onclick = function() { return confirm('Modifying an environment will remove all of the previous provider runs for this environment.'); };
});
}
</script>
{% endblock %}
31 changes: 31 additions & 0 deletions src/vng/servervalidation/tests/tests_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,20 @@ def test_update_environment_view_initial_data_correct(self):
self.assertEqual(form['url'].value, self.var1.url)
self.assertEqual(form['var'].value, self.var2.url)

def test_update_environment_modify_name(self):
response = self.app.get(reverse('server_run:endpoints_update', kwargs={
'api_id': self.test_scenario.api.id,
'test_id': self.test_scenario.id,
'env_id': self.environment.id
}), user=self.user)

form = response.forms[1]
form["name"] = "modified_name"
form.submit().follow()

self.environment.refresh_from_db()
self.assertEqual(self.environment.name, "modified_name")

def test_update_environment_modifies_variables(self):
response = self.app.get(reverse('server_run:endpoints_update', kwargs={
'api_id': self.test_scenario.api.id,
Expand Down Expand Up @@ -1200,6 +1214,23 @@ def test_update_environment_keeps_previous_provider_runs_if_not_modified(self):

self.assertEqual(self.environment.serverrun_set.count(), 3)

def test_update_environment_keeps_previous_provider_runs_if_only_name_modified(self):
ServerRunFactory.create_batch(3, environment=self.environment)

self.assertEqual(self.environment.serverrun_set.count(), 3)

response = self.app.get(reverse('server_run:endpoints_update', kwargs={
'api_id': self.test_scenario.api.id,
'test_id': self.test_scenario.id,
'env_id': self.environment.id
}), user=self.user)

form = response.forms[1]
form["name"] = "modified_name"
form.submit().follow()

self.assertEqual(self.environment.serverrun_set.count(), 3)

def test_placeholders_correct_order_after_update(self):
response = self.app.get(reverse('server_run:endpoints_update', kwargs={
'api_id': self.test_scenario.api.id,
Expand Down
13 changes: 12 additions & 1 deletion src/vng/servervalidation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
CreateServerRunForm, CreateEndpointForm,
SelectEnvironmentForm, CreateTestScenarioForm,
TestScenarioUrlFormSet, PostmanTestFormSet,
TestScenarioUrlUpdateFormSet, PostmanTestUpdateFormSet
TestScenarioUrlUpdateFormSet, PostmanTestUpdateFormSet, EnvironmentUpdateForm
)
from .models import (
API, ServerRun, Endpoint, TestScenarioUrl, TestScenario, PostmanTest,
Expand Down Expand Up @@ -783,6 +783,9 @@ def get_success_url(self):

def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)

data['env_form'] = EnvironmentUpdateForm()

pre_form = data['form']

self.env = self.get_object()
Expand Down Expand Up @@ -822,6 +825,14 @@ def get_context_data(self, **kwargs):
def post(self, request, *args, **kwargs):
data = request.POST
env = self.get_object()

env_form = EnvironmentUpdateForm(data)
if env_form.is_valid():
name = env_form.cleaned_data["name"]
if name:
env.name = name
env.save()

endpoints = env.endpoint_set.all()
tsu_names = endpoints.values_list('test_scenario_url__name', flat=True)

Expand Down

0 comments on commit 10cd494

Please sign in to comment.