diff --git a/autoscale/tests.py b/autoscale/tests.py index 934733a2..9fc3c4ce 100644 --- a/autoscale/tests.py +++ b/autoscale/tests.py @@ -3,6 +3,9 @@ from mock import patch +import os +import urllib + class IndexTestCase(TestCase): @patch("auth.views.token_is_valid") @@ -15,3 +18,21 @@ def test_index(self, token_is_valid): response = self.client.get(reverse("autoscale")) self.assertTemplateUsed(response, "autoscale/index.html") + + @patch("auth.views.token_is_valid") + def test_service_url(self, token_is_valid): + token_is_valid.return_value = True + + autoscale_dashboard_url = "http://localhost:123" + os.environ["AUTOSCALE_DASHBOARD_URL"] = autoscale_dashboard_url + session_engine = "django.contrib.sessions.backends.file" + token = "token/+12faslfkl12" + + with self.settings(SESSION_ENGINE=session_engine): + session = self.client.session + session['tsuru_token'] = "beare {}".format(token) + session.save() + + response = self.client.get(reverse("autoscale")) + expected = "{}?TSURU_TOKEN={}".format(autoscale_dashboard_url, urllib.quote(token)) + self.assertEqual(response.context_data["service_url"], expected) diff --git a/autoscale/views.py b/autoscale/views.py index eef9b5e6..d41129c3 100644 --- a/autoscale/views.py +++ b/autoscale/views.py @@ -3,6 +3,7 @@ from auth.views import LoginRequiredMixin import os +import urllib class Index(LoginRequiredMixin, TemplateView): @@ -11,6 +12,7 @@ class Index(LoginRequiredMixin, TemplateView): def get_context_data(self, *args, **kwargs): context = super(Index, self).get_context_data(*args, **kwargs) token = self.request.session.get('tsuru_token').split(' ')[1] + token = urllib.quote(token) service_url = "{}?TSURU_TOKEN={}".format(os.environ.get("AUTOSCALE_DASHBOARD_URL"), token) context["service_url"] = service_url return context