/*
 * Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com>
 *
 * This file is part of Symfony CLI project
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package envs

import (
	"encoding/base64"
	"os"

	. "gopkg.in/check.v1"
)

type RemoteSuite struct{}

var _ = Suite(&RemoteSuite{})

func (s *RemoteSuite) TestMailer(c *C) {
	r := &Remote{}

	os.Setenv("MAILER_URL", "smtp://foobar")
	c.Assert(r.Mailer(), DeepEquals, Envs{"MAILER_ENABLED": "1"})
	os.Unsetenv("MAILER_URL")

	os.Setenv("MAILER_DSN", "smtp://foobar")
	c.Assert(r.Mailer(), DeepEquals, Envs{"MAILER_ENABLED": "1"})
	os.Unsetenv("MAILER_DSN")

	os.Setenv("MAILER_HOST", "foobar")
	c.Assert(r.Mailer(), DeepEquals, Envs{"MAILER_ENABLED": "1"})
	os.Unsetenv("MAILER_HOST")

	os.Setenv("PLATFORM_SMTP_HOST", "")
	c.Assert(r.Mailer(), DeepEquals, Envs{
		"MAILER_ENABLED":   "0",
		"MAILER_USER":      "",
		"MAILER_PASSWORD":  "",
		"MAILER_URL":       "null://localhost",
		"MAILER_DSN":       "null://localhost",
		"MAILER_HOST":      "localhost",
		"MAILER_PORT":      "25",
		"MAILER_TRANSPORT": "smtp",
		"MAILER_AUTH_MODE": "plain",
	})

	os.Setenv("PLATFORM_SMTP_HOST", "246.0.64.1")
	c.Assert(r.Mailer(), DeepEquals, Envs{
		"MAILER_ENABLED":   "1",
		"MAILER_USER":      "",
		"MAILER_PASSWORD":  "",
		"MAILER_URL":       "smtp://246.0.64.1:25?verify_peer=0",
		"MAILER_DSN":       "smtp://246.0.64.1:25?verify_peer=0",
		"MAILER_HOST":      "246.0.64.1",
		"MAILER_PORT":      "25",
		"MAILER_TRANSPORT": "smtp",
		"MAILER_AUTH_MODE": "plain",
	})

	os.Setenv("PLATFORM_SMTP_HOST", "246.0.64.1:25")
	c.Assert(r.Mailer(), DeepEquals, Envs{
		"MAILER_ENABLED":   "1",
		"MAILER_USER":      "",
		"MAILER_PASSWORD":  "",
		"MAILER_URL":       "smtp://246.0.64.1:25?verify_peer=0",
		"MAILER_DSN":       "smtp://246.0.64.1:25?verify_peer=0",
		"MAILER_HOST":      "246.0.64.1",
		"MAILER_PORT":      "25",
		"MAILER_TRANSPORT": "smtp",
		"MAILER_AUTH_MODE": "plain",
	})
}

func (s *RemoteSuite) TestSymfonyLegacyFallbacks(c *C) {
	c.Assert(os.Getenv("SYMFONY_ENV"), Equals, "")
	c.Assert(os.Getenv("APP_ENV"), Equals, "")

	r := &Remote{}
	c.Assert(r.Extra(), DeepEquals, Envs{"APP_ENV": "prod", "APP_DEBUG": "0", "SYMFONY_ENV": "prod", "SYMFONY_DEBUG": "0"})

	os.Setenv("SYMFONY_ENV", "dev")
	c.Check(r.Extra(), DeepEquals, Envs{"APP_ENV": "dev", "APP_DEBUG": "0", "SYMFONY_DEBUG": "0"})
	os.Unsetenv("SYMFONY_ENV")

	os.Setenv("APP_ENV", "dev")
	c.Check(r.Extra(), DeepEquals, Envs{"APP_DEBUG": "0", "SYMFONY_ENV": "dev", "SYMFONY_DEBUG": "0"})
	os.Unsetenv("APP_ENV")

	os.Setenv("SYMFONY_DEBUG", "1")
	c.Check(r.Extra(), DeepEquals, Envs{"APP_ENV": "prod", "SYMFONY_ENV": "prod", "APP_DEBUG": "1"})
	os.Unsetenv("SYMFONY_DEBUG")

	os.Setenv("APP_DEBUG", "1")
	c.Check(r.Extra(), DeepEquals, Envs{"APP_ENV": "prod", "SYMFONY_ENV": "prod", "SYMFONY_DEBUG": "1"})
	os.Unsetenv("APP_DEBUG")
}

func (s *RemoteSuite) TestIsWorker(c *C) {
	r := &Remote{}
	// By default: nothing
	c.Assert(r.Extra()["SYMFONY_IS_WORKER"], Equals, "")

	os.Setenv("PLATFORM_APPLICATION_NAME", "app")
	defer os.Unsetenv("PLATFORM_APPLICATION_NAME")
	c.Assert(r.Extra()["SYMFONY_IS_WORKER"], Equals, "0")

	os.Setenv("PLATFORM_APPLICATION_NAME", "app--mails")
	defer os.Unsetenv("PLATFORM_APPLICATION_NAME")
	c.Assert(r.Extra()["SYMFONY_IS_WORKER"], Equals, "1")
}

func (s *RemoteSuite) TestDefaultRoute(c *C) {
	r := &Remote{}
	_, hasUrl := r.Extra()["SYMFONY_DEFAULT_ROUTE_URL"]
	_, hasProjectUrl := r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"]
	_, hasApplicationUrl := r.Extra()["SYMFONY_APPLICATION_DEFAULT_ROUTE_URL"]
	// By default: nothing
	c.Assert(hasUrl, Equals, false)
	c.Assert(hasProjectUrl, Equals, false)
	c.Assert(hasApplicationUrl, Equals, false)
	defer os.Unsetenv("PLATFORM_ROUTES")

	os.Setenv("PLATFORM_ROUTES", "[encoded data, 8712 characters]")
	// it should work the same from the application container or from the workers...
	for _, suffix := range []string{"", "--worker"} {
		os.Setenv("PLATFORM_APPLICATION_NAME", "app"+suffix)
		// {default} has priority
		for _, s := range []string{"APPLICATION_", "PROJECT_"} {
			c.Check(r.Extra()["SYMFONY_"+s+"DEFAULT_ROUTE_URL"], Equals, "https://symfony.com/")
			c.Check(r.Extra()["SYMFONY_"+s+"DEFAULT_ROUTE_SCHEME"], Equals, "https")
			c.Check(r.Extra()["SYMFONY_"+s+"DEFAULT_ROUTE_PORT"], Equals, "443")
			c.Check(r.Extra()["SYMFONY_"+s+"DEFAULT_ROUTE_HOST"], Equals, "symfony.com")
			c.Check(r.Extra()["SYMFONY_"+s+"DEFAULT_ROUTE_PATH"], Equals, "/")
		}

		c.Check(r.Extra()["SYMFONY_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])

		os.Unsetenv("PLATFORM_APPLICATION_NAME")
	}

	// it should work the same from the application container or from the workers...
	for _, suffix := range []string{"", "--worker"} {
		os.Setenv("PLATFORM_ROUTES", "[encoded data, 6040 characters]")
		// App does not match, https://{all}/ is the match for the project
		c.Check(r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"], Equals, "https://git.sensiolabs.com/")
		c.Check(r.Extra()["SYMFONY_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])
		//nothing for the application as it does not match
		c.Check(r.Extra()["SYMFONY_APPLICATION_DEFAULT_ROUTE_URL"], Equals, "")

		os.Setenv("PLATFORM_APPLICATION_NAME", "test-app"+suffix)
		// then {all}
		c.Check(r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"], Equals, "https://git.sensiolabs.com/")
		c.Check(r.Extra()["SYMFONY_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])
		c.Check(r.Extra()["SYMFONY_APPLICATION_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])

		os.Setenv("PLATFORM_ROUTES", "[encoded data, 4492 characters]")
		// then {default}/path
		c.Check(r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"], Equals, "https://tls-1-3.tucksaun.net/bar")
		c.Check(r.Extra()["SYMFONY_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])
		c.Check(r.Extra()["SYMFONY_APPLICATION_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])

		os.Setenv("PLATFORM_ROUTES", "[encoded data, 4520 characters]")
		// then {all}/path
		c.Check(r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"], Equals, "https://test2.tucksaun.net/foo")
		c.Check(r.Extra()["SYMFONY_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])
		c.Check(r.Extra()["SYMFONY_APPLICATION_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])

		os.Setenv("PLATFORM_ROUTES", "eyJodHRwOi8vdGVzdDIudHVja3NhdW4ubmV0LyI6IHsidG8iOiAiaHR0cHM6Ly90ZXN0Mi50dWNrc2F1bi5uZXQvIiwgIm9yaWdpbmFsX3VybCI6ICJodHRwOi8vdGVzdDIudHVja3NhdW4ubmV0LyIsICJ0eXBlIjogInJlZGlyZWN0IiwgInByaW1hcnkiOiBmYWxzZSwgImlkIjogbnVsbH0sICJodHRwczovL3Rlc3QyLnR1Y2tzYXVuLm5ldC8iOiB7InByaW1hcnkiOiBmYWxzZSwgImlkIjogbnVsbCwgImF0dHJpYnV0ZXMiOiB7fSwgInR5cGUiOiAidXBzdHJlYW0iLCAidGxzIjogeyJjbGllbnRfYXV0aGVudGljYXRpb24iOiBudWxsLCAibWluX3ZlcnNpb24iOiBudWxsLCAiY2xpZW50X2NlcnRpZmljYXRlX2F1dGhvcml0aWVzIjogW10sICJzdHJpY3RfdHJhbnNwb3J0X3NlY3VyaXR5IjogeyJwcmVsb2FkIjogbnVsbCwgImluY2x1ZGVfc3ViZG9tYWlucyI6IG51bGwsICJlbmFibGVkIjogbnVsbH19LCAiY2FjaGUiOiB7ImRlZmF1bHRfdHRsIjogMCwgImNvb2tpZXMiOiBbIioiXSwgImVuYWJsZWQiOiB0cnVlLCAiaGVhZGVycyI6IFsiQWNjZXB0IiwgIkFjY2VwdC1MYW5ndWFnZSJdfSwgInNzaSI6IHsiZW5hYmxlZCI6IGZhbHNlfSwgInVwc3RyZWFtIjogInRlc3QtYXBwIiwgIm9yaWdpbmFsX3VybCI6ICJodHR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BudWxsLCAiYXR0cmlidXRlcyI6IHt9LCAidHlwZSI6ICJ1cHN0cmVhbSIsICJ0bHMiOiB7ImNsaWVudF9hdXRoZW50aWNhdGlvbiI6IG51bGwsICJtaW5fdmVyc2lvbiI6IG51bGwsICJjbGllbnRfY2VydGlmaWNhdGVfYXV0aG9yaXRpZXMiOiBbXSwgInN0cmljdF90cmFuc3BvcnRfc2VjdXJpdHkiOiB7InByZWxvYWQiOiBudWxsLCAiaW5jbHVkZV9zdWJkb21haW5zIjogbnVsbCwgImVuYWJsZWQiOiBudWxsfX0sICJjYWNoZSI6IHsiZGVmYXVsdF90dGwiOiAwLCAiY29va2llcyI6IFsiKiJdLCAiZW5hYmxlZCI6IHRydWUsICJoZWFkZXJzIjogWyJBY2NlcHQiLCAiQWNjZXB0LUxhbmd1YWdlIl19LCAic3NpIjogeyJlbmFibGVkIjogZmFsc2V9LCAidXBzdHJlYW0iOiAidGVzdC1hcHAiLCAib3JpZ2luYWxfdXJsIjogImh0dHBzOi8vdGxzLTEtMy50dWNrc2F1bi5uZXQvYmFyIn0sICJodHRwOi8vdGxzLTEtMy50dWNrc2F1bi5uZXQvYmFyIjogeyJ0byI6ICJodHRwczovL3Rscy0xLTMudHVja3NhdW4ubmV0L2JhciIsICJvcmlnaW5hbF91cmwiOiAiaHR0cDovL3Rscy0xLTMudHVja3NhdW4ubmV0L2JhciIsICJ0eXBlIjogInJlZGlyZWN0IiwgInByaW1hcnkiOiBmYWxzZSwgImlkIjogbnVsbH19")
		// Fallback to first route available
		c.Check(r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"], Equals, "https://test2.tucksaun.net/")
		c.Check(r.Extra()["SYMFONY_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])
		c.Check(r.Extra()["SYMFONY_APPLICATION_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])

		os.Setenv("PLATFORM_ROUTES", "[encoded data, 4412 characters]")
		// https://{default}/ is the match
		c.Check(r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"], Equals, "https://tls-1-3.tucksaun.net/")
		c.Check(r.Extra()["SYMFONY_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])
		c.Check(r.Extra()["SYMFONY_APPLICATION_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])

		os.Setenv("PLATFORM_ROUTES", "[encoded data, 6652 characters]")
		// should work the same way in environments
		c.Check(r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"], Equals, "https://test-routes-ezema3q-c6yyhsnvmdojk.eu.s5y.io/")
		c.Check(r.Extra()["SYMFONY_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])
		c.Check(r.Extra()["SYMFONY_APPLICATION_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])

		os.Unsetenv("PLATFORM_APPLICATION_NAME")
	}

	// ... and with varnish?
	{
		os.Setenv("PLATFORM_ROUTES", "eyJodHRwOi8vcXNsemszaXJheS1qNGMyMno0dWJpNmZrLmV1LnM1eS5pby8iOiB7InByaW1hcnkiOiBmYWxzZSwgImlkIjogbnVsbCwgImF0dHJpYnV0ZXMiOiB7fSwgInR5cGUiOiAicmVkaXJlY3QiLCAidG8iOiAiaHR0cHM6Ly9xc2x6azNpcmF5LWo0YzIyejR1Ymk2ZmsuZXUuczV5LmlvLyIsICJvcmlnaW5hbF91cmwiOiAiaHR0cDovL3thbGx9LyJ9LCAiaHR0cHM6Ly9xc2x6azNpcmF5LWo0YzIyejR1Ymk2ZmsuZXUuczV5LmlvLyI6IHsicHJpbWFyeSI6IHRydWUsICJpZCI6IG51bGwsICJhdHRyaWJ1dGVzIjoge30sICJ0eXBlIjogInVw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iB7ImNvbW1hbmRzIjogeyJzdGFydCI6ICJzZXQgLXggLWUgXG5cbig+JjIgc3ltZm9ueS1kZXBsb3kpXG5waHAgYmluL2NvbnNvbGUgbWVzc2VuZ2VyOmNvbnN1bWUgYXN5bmMgLXZ2IC0tdGltZS1saW1pdCAzNjAwIC0tbWVtb3J5LWxpbWl0PTEyOE1cbiJ9fX0=")
		url, hasUrl := r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"]
		// App does not match
		c.Check(hasUrl, Equals, true)
		c.Check(url, Equals, "https://qslzk3iray-j4c22z4ubi6fk.eu.s5y.io/")
		c.Check(r.Extra()["SYMFONY_DEFAULT_ROUTE_URL"], Equals, url)
		c.Check(r.Extra()["SYMFONY_APPLICATION_DEFAULT_ROUTE_URL"], Equals, "")
	}

	// when there's no clear winner, the one with `www` is the winner
	{
		// wwww prefix wins
		os.Setenv("PLATFORM_ROUTES", "eyJodHRwczovL21hcmtldC40M3pxYXN0Z3g0LWM2eXloc252bWRvamsuZXUuczV5LmlvLyI6IHsicHJpbWFyeSI6IGZhbHNlLCAiaWQiOiBudWxsLCAiYXR0cmlidXRlcyI6IHt9LCAidHlwZSI6ICJ1cHN0cmVhbSIsICJ1cHN0cmVhbSI6ICJhcHAiLCAib3JpZ2luYWxfdXJsIjogImh0dHBzOi8vbWFya2V0LnthbGx9LyJ9LCAiaHR0cHM6Ly93d3cuNDN6cWFzdGd4NC1jNnl5aHNudm1kb2prLmV1LnM1eS5pby8iOiB7InByaW1hcnkiOiB0cnVlLCAiaWQiOiBudWxsLCAiYXR0cmlidXRlcyI6IHt9LCAidHlwZSI6ICJ1cHN0cmVhbSIsICJ1cHN0cmVhbSI6ICJhcHAiLCAib3JpZ2luYWxfdXJsIjogImh0dHBzOi8vd3d3LnthbGx9LyJ9LCAiaHR0cHM6Ly9hZG1pbi40M3pxYXN0Z3g0LWM2eXloc252bWRvamsuZXUuczV5LmlvLyI6IHsicHJpbWFyeSI6IGZhbHNlLCAiaWQiOiBudWxsLCAiYXR0cmlidXRlcyI6IHt9LCAidHlwZSI6ICJ1cHN0cmVhbSIsICJ1cHN0cmVhbSI6ICJhcHAiLCAib3JpZ2luYWxfdXJsIjogImh0dHBzOi8vYWRtaW4ue2FsbH0vIn0sICJodHRwOi8vbWFya2V0LjQzenFhc3RneDQtYzZ5eWhzbnZtZG9qay5ldS5zNXkuaW8vIjogeyJ0byI6ICJodHRwczovL21hcmtldC40M3pxYXN0Z3g0LWM2eXloc252bWRvamsuZXUuczV5LmlvLyIsICJvcmlnaW5hbF91cmwiOiAiaHR0cDovL21hcmtldC57YWxsfS8iLCAidHlwZSI6ICJyZWRpcmVjdCIsICJwcmltYXJ5IjogZmFsc2UsICJpZCI6IG51bGx9LCAiaHR0cDovL3d3dy40M3pxYXN0Z3g0LWM2eXloc252bWRvamsuZXUuczV5LmlvLyI6IHsidG8iOiAiaHR0cHM6Ly93d3cuNDN6cWFzdGd4NC1jNnl5aHNudm1kb2prLmV1LnM1eS5pby8iLCAib3JpZ2luYWxfdXJsIjogImh0dHA6Ly93d3cue2FsbH0vIiwgInR5cGUiOiAicmVkaXJlY3QiLCAicHJpbWFyeSI6IGZhbHNlLCAiaWQiOiBudWxsfSwgImh0dHA6Ly9hZG1pbi40M3pxYXN0Z3g0LWM2eXloc252bWRvamsuZXUuczV5LmlvLyI6IHsidG8iOiAiaHR0cHM6Ly9hZG1pbi40M3pxYXN0Z3g0LWM2eXloc252bWRvamsuZXUuczV5LmlvLyIsICJvcmlnaW5hbF91cmwiOiAiaHR0cDovL2FkbWluLnthbGx9LyIsICJ0eXBlIjogInJlZGlyZWN0IiwgInByaW1hcnkiOiBmYWxzZSwgImlkIjogbnVsbH19")
		os.Setenv("PLATFORM_APPLICATION_NAME", "app")
		url, hasUrl := r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"]
		c.Check(hasUrl, Equals, true)
		c.Check(url, Equals, "https://www.43zqastgx4-c6yyhsnvmdojk.eu.s5y.io/")
		c.Check(r.Extra()["SYMFONY_DEFAULT_ROUTE_URL"], Equals, url)
		c.Check(r.Extra()["SYMFONY_APPLICATION_DEFAULT_ROUTE_URL"], Equals, url)

		// the APEX should wine
		os.Setenv("PLATFORM_ROUTES", "eyJodHRwczovL21hcmtldC40M3pxYXN0Z3g0LWM2eXloc252bWRvamsuZXUuczV5LmlvLyI6IHsicHJpbWFyeSI6IGZhbHNlLCAiaWQiOiBudWxsLCAiYXR0cmlidXRlcyI6IHt9LCAidHlwZSI6ICJ1cHN0cmVhbSIsICJ1cHN0cmVhbSI6ICJhcHAiLCAib3JpZ2luYWxfdXJsIjogImh0dHBzOi8vbWFya2V0LnthbGx9LyJ9LCAiaHR0cHM6Ly80M3pxYXN0Z3g0LWM2eXloc252bWRvamsuZXUuczV5LmlvLyI6IHsicHJpbWFyeSI6IHRydWUsICJpZCI6IG51bGwsICJhdHRyaWJ1dGVzIjoge30sICJ0eXBlIjogInVwc3RyZWFtIiwgInVwc3RyZWFtIjogImFwcCIsICJvcmlnaW5hbF91cmwiOiAiaHR0cHM6Ly97YWxsfS8ifSwgImh0dHBzOi8vYWRtaW4uNDN6cWFzdGd4NC1jNnl5aHNudm1kb2prLmV1LnM1eS5pby8iOiB7InByaW1hcnkiOiBmYWxzZSwgImlkIjogbnVsbCwgImF0dHJpYnV0ZXMiOiB7fSwgInR5cGUiOiAidXBzdHJlYW0iLCAidXBzdHJlYW0iOiAiYXBwIiwgIm9yaWdpbmFsX3VybCI6ICJodHRwczovL2FkbWluLnthbGx9LyJ9LCAiaHR0cDovL21hcmtldC40M3pxYXN0Z3g0LWM2eXloc252bWRvamsuZXUuczV5LmlvLyI6IHsidG8iOiAiaHR0cHM6Ly9tYXJrZXQuNDN6cWFzdGd4NC1jNnl5aHNudm1kb2prLmV1LnM1eS5pby8iLCAib3JpZ2luYWxfdXJsIjogImh0dHA6Ly9tYXJrZXQue2FsbH0vIiwgInR5cGUiOiAicmVkaXJlY3QiLCAicHJpbWFyeSI6IGZhbHNlLCAiaWQiOiBudWxsfSwgImh0dHA6Ly9hZG1pbi40M3pxYXN0Z3g0LWM2eXloc252bWRvamsuZXUuczV5LmlvLyI6IHsidG8iOiAiaHR0cHM6Ly9hZG1pbi40M3pxYXN0Z3g0LWM2eXloc252bWRvamsuZXUuczV5LmlvLyIsICJvcmlnaW5hbF91cmwiOiAiaHR0cDovL2FkbWluLnthbGx9LyIsICJ0eXBlIjogInJlZGlyZWN0IiwgInByaW1hcnkiOiBmYWxzZSwgImlkIjogbnVsbH0sICJodHRwOi8vNDN6cWFzdGd4NC1jNnl5aHNudm1kb2prLmV1LnM1eS5pby8iOiB7InRvIjogImh0dHBzOi8vNDN6cWFzdGd4NC1jNnl5aHNudm1kb2prLmV1LnM1eS5pby8iLCAib3JpZ2luYWxfdXJsIjogImh0dHA6Ly97YWxsfS8iLCAidHlwZSI6ICJyZWRpcmVjdCIsICJwcmltYXJ5IjogZmFsc2UsICJpZCI6IG51bGx9fQ==")
		url, hasUrl = r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"]
		c.Check(hasUrl, Equals, true)
		c.Check(url, Equals, "https://43zqastgx4-c6yyhsnvmdojk.eu.s5y.io/")
		c.Check(r.Extra()["SYMFONY_DEFAULT_ROUTE_URL"], Equals, url)
		c.Check(r.Extra()["SYMFONY_APPLICATION_DEFAULT_ROUTE_URL"], Equals, url)

		os.Unsetenv("PLATFORM_APPLICATION_NAME")
	}

	// When no matches, the {default} one wins
	{
		os.Setenv("PLATFORM_ROUTES", "[encoded data, 8712 characters]")
		url, hasUrl := r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"]
		c.Assert(url, Equals, "https://symfony.com/")
		c.Assert(hasUrl, Equals, true)
		c.Check(r.Extra()["SYMFONY_DEFAULT_ROUTE_URL"], Equals, url)
	}

	// A more complicated use-case with multi app and several applications being public
	{
		os.Setenv("PLATFORM_ROUTES", "eyJodHRwOi8vd3d3LnN5bWZvbnljYXN0cy5jb20vIjogeyJwcmltYX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JzeW1mb255Y2FzdHMiLCAib3JpZ2luYWxfdXJsIjogImh0dHBzOi8ve2RlZmF1bHR9LyJ9LCAiaHR0cHM6Ly9jaGFsbGVuZ2VydW5uZXIuc3ltZm9ueWNhc3RzLmNvbS8iOiB7InByaW1hcnkiOiBmYWxzZSwgImlkIjogbnVsbCwgImF0dHJpYnV0ZXMiOiB7fSwgInR5cGUiOiAidXBzdHJlYW0iLCAidXBzdHJlYW0iOiAiY2hhbGxlbmdlcnVubmVyIiwgIm9yaWdpbmFsX3VybCI6ICJodHRwczovL2NoYWxsZW5nZXJ1bm5lci57ZGVmYXVsdH0vIn19")
		os.Setenv("PLATFORM_APPLICATION_NAME", "challengerunner")

		c.Check(r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"], Equals, "https://symfonycasts.com/")
		c.Check(r.Extra()["SYMFONY_APPLICATION_DEFAULT_ROUTE_URL"], Equals, "https://challengerunner.symfonycasts.com/")
		c.Check(r.Extra()["SYMFONY_DEFAULT_ROUTE_URL"], Equals, r.Extra()["SYMFONY_PROJECT_DEFAULT_ROUTE_URL"])
		os.Unsetenv("PLATFORM_APPLICATION_NAME")
	}
}

func (s *RemoteSuite) TestRelationships(c *C) {
	r := &Remote{}
	os.Setenv("PLATFORM_RELATIONSHIPS", "")
	c.Assert(extractRelationshipsEnvs(r), DeepEquals, Envs{})

	os.Setenv("PLATFORM_RELATIONSHIPS", "eyJzZWN1cml0eS1zZXJ2ZXIiOiBbeyJpcCI6ICIxNjkuMjU0LjI2LjIzMSIsICJob3N0IjogInNlY3VyaXR5LXNlcnZlci5pbnRlcm5hbCIsICJzY2hlbWUiOiAiaHR0cCIsICJwb3J0IjogODAsICJyZWwiOiAiaHR0cCJ9XSwgImRhdGFiYXNlIjogW3sidXNlcm5hbWUiOiAibWFpbiIsICJzY2hlbWUiOiAicGdzcWwiLCAiaXAiOiAiMTY5LjI1NC4xMjAuNDgiLCAiaG9zdCI6ICJkYXRhYmFzZS5pbnRlcm5hbCIsICJyZWwiOiAicG9zdGdyZXNxbCIsICJwYXRoIjogIm1haW4iLCAicXVlcnkiOiB7ImlzX21hc3RlciI6IHRydWV9LCAicGFzc3dvcmQiOiAibWFpbiIsICJwb3J0IjogNTQzMn1dfQ==")
	c.Assert(extractRelationshipsEnvs(r), DeepEquals, Envs{
		"SECURITY_SERVER_IP":     "169.254.26.231",
		"SECURITY_SERVER_SCHEME": "http",
		"SECURITY_SERVER_HOST":   "security-server.internal",
		"SECURITY_SERVER_URL":    "http://security-server.internal:80",
		"SECURITY_SERVER_SERVER": "http://security-server.internal:80",
		"SECURITY_SERVER_PORT":   "80",
		"DATABASE_URL":           "postgres://main:main@database.internal:5432/main?sslmode=disable&charset=utf8",
		"DATABASE_DRIVER":        "postgres",
		"DATABASE_NAME":          "main",
		"DATABASE_DATABASE":      "main",
		"DATABASE_SERVER":        "postgres://database.internal:5432",
		"DATABASE_HOST":          "database.internal",
		"DATABASE_PORT":          "5432",
		"DATABASE_USER":          "main",
		"DATABASE_USERNAME":      "main",
		"DATABASE_PASSWORD":      "main",
	})

	os.Setenv("PLATFORM_RELATIONSHIPS", "eyJzZWN1cml0eS1zZXJ2ZXIiOiBbeyJpcCI6ICIxNjkuMjU0LjI2LjIzMSIsICJob3N0IjogInNlY3VyaXR5LXNlcnZlci5pbnRlcm5hbCIsICJzY2hlbWUiOiAiaHR0cCIsICJwb3J0IjogODAsICJyZWwiOiAiaHR0cCJ9XSwgImRhdGFiYXNlIjogW3sidXNlcm5hbWUiOiAibWFpbiIsICJzY2hlbWUiOiAicGdzcWwiLCAiaXAiOiAiMTY5LjI1NC4xMjAuNDgiLCAiaG9zdCI6ICJkYXRhYmFzZS5pbnRlcm5hbCIsICJyZWwiOiAicG9zdGdyZXNxbCIsICJwYXRoIjogIm1haW4iLCAicXVlcnkiOiB7ImlzX21hc3RlciI6IHRydWV9LCAicGFzc3dvcmQiOiAibWFpbiIsICJwb3J0IjogNTQzMn1dfQ==")
	c.Assert(extractRelationshipsEnvs(r), DeepEquals, Envs{
		"SECURITY_SERVER_IP":     "169.254.26.231",
		"SECURITY_SERVER_SCHEME": "http",
		"SECURITY_SERVER_HOST":   "security-server.internal",
		"SECURITY_SERVER_URL":    "http://security-server.internal:80",
		"SECURITY_SERVER_SERVER": "http://security-server.internal:80",
		"SECURITY_SERVER_PORT":   "80",
		"DATABASE_URL":           "postgres://main:main@database.internal:5432/main?sslmode=disable&charset=utf8",
		"DATABASE_DRIVER":        "postgres",
		"DATABASE_NAME":          "main",
		"DATABASE_DATABASE":      "main",
		"DATABASE_SERVER":        "postgres://database.internal:5432",
		"DATABASE_HOST":          "database.internal",
		"DATABASE_PORT":          "5432",
		"DATABASE_USER":          "main",
		"DATABASE_USERNAME":      "main",
		"DATABASE_PASSWORD":      "main",
	})

	os.Setenv("PLATFORM_RELATIONSHIPS", "eyJkYXRhYmFzZSI6IFt7InVzZXJuYW1lIjogIm1haW4iLCAicGFzc3dvcmQiOiAibWFpbiIsICJzZXJ2aWNlIjogImRiIiwgImlwIjogIjE2OS4yNTQuMTI3LjExOSIsICJob3N0bmFtZSI6ICJ2dXJ6c3l5a2xzeGRiNnVhbDduaWVsYmtxdS5kYi5zZXJ2aWNlLl8uczV5LmlvIiwgImNsdXN0ZXIiOiAia2JqdW5hYjN6Z2V6Zy1kZW1vLWZrbHZjM2EiLCAiaG9zdCI6ICJkYXRhYmFzZS5pbnRlcm5hbCIsICJyZWwiOiAicG9zdGdyZXNxbCIsICJxdWVyeSI6IHsiaXNfbWFzdGVyIjogdHJ1ZX0sICJwYXRoIjogIm1haW4iLCAic2NoZW1lIjogInBnc3FsIiwgInR5cGUiOiAicG9zdGdyZXNxbDo5LjYiLCAicG9ydCI6IDU0MzJ9XX0=")
	c.Assert(extractRelationshipsEnvs(r), DeepEquals, Envs{
		"DATABASE_URL":      "postgres://main:main@database.internal:5432/main?sslmode=disable&charset=utf8&serverVersion=9.6",
		"DATABASE_DRIVER":   "postgres",
		"DATABASE_NAME":     "main",
		"DATABASE_DATABASE": "main",
		"DATABASE_SERVER":   "postgres://database.internal:5432",
		"DATABASE_HOST":     "database.internal",
		"DATABASE_PORT":     "5432",
		"DATABASE_USER":     "main",
		"DATABASE_USERNAME": "main",
		"DATABASE_PASSWORD": "main",
		"DATABASE_VERSION":  "9.6",
	})

	os.Setenv("PLATFORM_RELATIONSHIPS", "eyJyZWRpcyI6IFt7InVzZXJuYW1lIjogbnVsbCwgInBhc3N3b3JkIjogbnVsbCwgInNlcnZpY2UiOiAicmVkaXNfc2Vzc2lvbnMiLCAiZnJhZ21lbnQiOiBudWxsLCAiaXAiOiAiMTY5LjI1NC4yMy4yMDQiLCAiaG9zdG5hbWUiOiAidGVncTdqY3BqMjVuM3VnYjN0cm9rY2w1anEucmVkaXNfc2Vzc2lvbnMuc2VydmljZS5fLnM1eS5pbyIsICJwb3J0IjogNjM3OSwgImNsdXN0ZXIiOiAiN2NhbTRtbTUzN2ViZS1jbGVhbi11cC10bWVpd2hxIiwgImhvc3QiOiAicmVkaXMuaW50ZXJuYWwiLCAicmVsIjogInJlZGlzIiwgInBhdGgiOiBudWxsLCAicXVlcnkiOiB7fSwgInNjaGVtZSI6ICJyZWRpcyIsICJ0eXBlIjogInJlZGlzOjMuMiIsICJwdWJsaWMiOiBmYWxzZX1dLCAiZGF0YWJhc2UiOiBbeyJ1c2VybmFtZSI6ICJtYWluIiwgInBhc3N3b3JkIjogIjZlNjAyODg4NTc2NzAzMDMwZjUzYzE1NDA1MWJkNzc4IiwgInNlcnZpY2UiOiAibXlzcWwiLCAiaXAiOiAiMTY5LjI1NC4xMzQuMTEiLCAiaG9zdG5hbWUiOiAiaTNvNjJkbzV0eXh5MzV3NXdzdTY1YmdjcnUubXlzcWwuc2VydmljZS5fLnM1eS5pbyIsICJjbHVzdGVyIjogIjdjYW00bW01MzdlYmUtY2xlYW4tdXAtdG1laXdocSIsICJob3N0IjogImRhdGFiYXNlLmludGVybmFsIiwgInJlbCI6ICJtYWluIiwgInF1ZXJ5IjogeyJpc19tYXN0ZXIiOiB0cnVlfSwgInBhdGgiOiAibWFpbiIsICJzY2hlbWUiOiAibXlzcWwiLCAidHlwZSI6ICJteXNxbDoxMC4wIiwgInBvcnQiOiAzMzA2fV19")
	c.Assert(extractRelationshipsEnvs(r), DeepEquals, Envs{
		"DATABASE_URL":      "mysql://main:6e602888576703030f53c154051bd778@database.internal:3306/main?sslmode=disable&charset=utf8mb4&serverVersion=10.0.0-MariaDB",
		"DATABASE_DRIVER":   "mysql",
		"DATABASE_NAME":     "main",
		"DATABASE_DATABASE": "main",
		"DATABASE_SERVER":   "mysql://database.internal:3306",
		"DATABASE_HOST":     "database.internal",
		"DATABASE_PORT":     "3306",
		"DATABASE_USER":     "main",
		"DATABASE_USERNAME": "main",
		"DATABASE_PASSWORD": "6e602888576703030f53c154051bd778",
		"DATABASE_VERSION":  "10.0.0-MariaDB",
		"REDIS_URL":         "redis://redis.internal:6379",
		"REDIS_HOST":        "redis.internal",
		"REDIS_PORT":        "6379",
		"REDIS_SCHEME":      "redis",
	})

	os.Setenv("PLATFORM_RELATIONSHIPS", "eyJtcSI6IFt7InVzZXJuYW1lIjogImd1ZXN0IiwgInBhc3N3b3JkIjogImd1ZXN0IiwgInNlcnZpY2UiOiAibXlyYWJiaXRtcSIsICJpcCI6ICIxNjkuMjU0LjIxLjEzNCIsICJob3N0bmFtZSI6ICI3dWQydGRtcG5tYXoyZGQ3bHBmaW5henJkeS5teXJhYmJpdG1xLnNlcnZpY2UuXy5zNXkuaW8iLCAiY2x1c3RlciI6ICJyNWFpcW12bzViaHFlLW1hc3Rlci03cnF0d3RpIiwgImhvc3QiOiAibXEuaW50ZXJuYWwiLCAicmVsIjogInJhYmJpdG1xIiwgInNjaGVtZSI6ICJhbXFwIiwgInR5cGUiOiAicmFiYml0bXE6My43IiwgInBvcnQiOiA1NjcyfV0sICJkYXRhYmFzZSI6IFt7InVzZXJuYW1lIjogIm1haW4iLCAicGFzc3dvcmQiOiAibWFpbiIsICJzZXJ2aWNlIjogImRhdGFiYXNlIiwgImlwIjogIjE2OS4yNTQuMTUwLjE2IiwgImhvc3RuYW1lIjogInplNTM2bDZlZGtnbXRiZGVlcmttc3Zkenh1LmRhdGFiYXNlLnNlcnZpY2UuXy5zNXkuaW8iLCAiY2x1c3RlciI6ICJyNWFpcW12bzViaHFlLW1hc3Rlci03cnF0d3RpIiwgImhvc3QiOiAiZGF0YWJhc2UuaW50ZXJuYWwiLCAicmVsIjogInBvc3RncmVzcWwiLCAicXVlcnkiOiB7ImlzX21hc3RlciI6IHRydWV9LCAicGF0aCI6ICJtYWluIiwgInNjaGVtZSI6ICJwZ3NxbCIsICJ0eXBlIjogInBvc3RncmVzcWw6MTAiLCAicG9ydCI6IDU0MzJ9XX0=")
	c.Assert(extractRelationshipsEnvs(r), DeepEquals, Envs{
		"DATABASE_URL":           "postgres://main:main@database.internal:5432/main?sslmode=disable&charset=utf8&serverVersion=10",
		"DATABASE_SERVER":        "postgres://database.internal:5432",
		"DATABASE_DRIVER":        "postgres",
		"DATABASE_HOST":          "database.internal",
		"DATABASE_PORT":          "5432",
		"DATABASE_USER":          "main",
		"DATABASE_USERNAME":      "main",
		"DATABASE_PASSWORD":      "main",
		"DATABASE_VERSION":       "10",
		"DATABASE_DATABASE":      "main",
		"DATABASE_NAME":          "main",
		"MQ_SERVER":              "amqp://mq.internal:5672",
		"MQ_DSN":                 "amqp://guest:guest@mq.internal:5672",
		"MQ_URL":                 "amqp://guest:guest@mq.internal:5672",
		"MQ_SCHEME":              "amqp",
		"MQ_HOST":                "mq.internal",
		"MQ_PORT":                "5672",
		"MQ_USER":                "guest",
		"MQ_USERNAME":            "guest",
		"MQ_PASSWORD":            "guest",
		"MQ_MANAGEMENT_SERVER":   "http://mq.internal:15672",
		"MQ_MANAGEMENT_URL":      "http://guest:guest@mq.internal:15672",
		"MQ_MANAGEMENT_SCHEME":   "http",
		"MQ_MANAGEMENT_IP":       "169.254.21.134",
		"MQ_MANAGEMENT_HOST":     "mq.internal",
		"MQ_MANAGEMENT_PORT":     "15672",
		"MQ_MANAGEMENT_USER":     "guest",
		"MQ_MANAGEMENT_USERNAME": "guest",
		"MQ_MANAGEMENT_PASSWORD": "guest",
	})

	os.Setenv("PLATFORM_RELATIONSHIPS", "eyJkYXRhYmFzZSI6IFt7InNlcnZpY2UiOiAiZGF0YWJhc2VfbXlzcWxfbWFpbiIsICJpcCI6ICIxNjkuMjU0LjE4MC4yMjUiLCAiaG9zdG5hbWUiOiAibG03aDJld2Zvemc2cHVyamlhYmI1ZzRpYnUuZGF0YWJhc2VfbXlzcWxfbWFpbi5zZXJ2aWNlLl8uczV5LmlvIiwgImNsdXN0ZXIiOiAieHBzZm9vdGFwanc0YS1tYXN0ZXItN3JxdHd0aSIsICJob3N0IjogImRhdGFiYXNlLmludGVybmFsIiwgInJlbCI6ICJteXNxbCIsICJxdWVyeSI6IHt9LCAic2NoZW1lIjogIm15c3FsIiwgInR5cGUiOiAibXlzcWw6MTAuMSIsICJwb3J0IjogMzMwNn1dfQ==")
	c.Assert(extractRelationshipsEnvs(r), DeepEquals, Envs{
		"DATABASE_URL":      "mysql://database.internal:3306/main?sslmode=disable&charset=utf8mb4&serverVersion=10.1.0-MariaDB",
		"DATABASE_SERVER":   "mysql://database.internal:3306",
		"DATABASE_DRIVER":   "mysql",
		"DATABASE_HOST":     "database.internal",
		"DATABASE_PORT":     "3306",
		"DATABASE_VERSION":  "10.1.0-MariaDB",
		"DATABASE_DATABASE": "main",
		"DATABASE_NAME":     "main",
	})

	os.Setenv("PLATFORM_RELATIONSHIPS", "eyJkYXRhYmFzZSI6IFt7InNlcnZpY2UiOiAiZGF0YWJhc2VfbXlzcWxfbWFpbiIsICJpcCI6ICIxNjkuMjU0LjE4MC4yMjUiLCAiaG9zdG5hbWUiOiAibG03aDJld2Zvemc2cHVyamlhYmI1ZzRpYnUuZGF0YWJhc2VfbXlzcWxfbWFpbi5zZXJ2aWNlLl8uczV5LmlvIiwgImNsdXN0ZXIiOiAieHBzZm9vdGFwanc0YS1tYXN0ZXItN3JxdHd0aSIsICJob3N0IjogImRhdGFiYXNlLmludGVybmFsIiwgInJlbCI6ICJteXNxbCIsICJxdWVyeSI6IHt9LCAic2NoZW1lIjogIm15c3FsIiwgInR5cGUiOiAibXlzcWw6MTAuMiIsICJwb3J0IjogMzMwNn1dfQo=")
	// Maria DB 10.2 == 10.2.7
	c.Assert(extractRelationshipsEnvs(r), DeepEquals, Envs{
		"DATABASE_URL":      "mysql://database.internal:3306/main?sslmode=disable&charset=utf8mb4&serverVersion=10.2.7-MariaDB",
		"DATABASE_SERVER":   "mysql://database.internal:3306",
		"DATABASE_DRIVER":   "mysql",
		"DATABASE_HOST":     "database.internal",
		"DATABASE_PORT":     "3306",
		"DATABASE_VERSION":  "10.2.7-MariaDB",
		"DATABASE_DATABASE": "main",
		"DATABASE_NAME":     "main",
	})

	os.Setenv("DATABASE_VERSION", "10.2.19-MariaDB")
	os.Setenv("DATABASE_CHARSET", "utf8")
	c.Assert(extractRelationshipsEnvs(r), DeepEquals, Envs{
		"DATABASE_URL":      "mysql://database.internal:3306/main?sslmode=disable&charset=utf8&serverVersion=10.2.19-MariaDB",
		"DATABASE_SERVER":   "mysql://database.internal:3306",
		"DATABASE_DRIVER":   "mysql",
		"DATABASE_HOST":     "database.internal",
		"DATABASE_PORT":     "3306",
		"DATABASE_VERSION":  "10.2.19-MariaDB",
		"DATABASE_DATABASE": "main",
		"DATABASE_NAME":     "main",
	})
	os.Unsetenv("DATABASE_VERSION")
	os.Unsetenv("DATABASE_CHARSET")
}

func (s *RemoteSuite) TestMySQLReadReplicaForDedicated(c *C) {
	r := &Remote{}
	value, err := os.ReadFile("testdata/dedicated/relationships_with_read_replica.json")
	if err != nil {
		panic(err)
	}
	if err := os.Setenv("PLATFORM_RELATIONSHIPS", base64.StdEncoding.EncodeToString(value)); err != nil {
		panic(err)
	}

	e := extractRelationshipsEnvs(r)

	c.Assert("mysql://mysql:xxx@dbread.internal:3306/main?sslmode=disable&charset=utf8mb4&serverVersion=10.6.0-MariaDB", DeepEquals, e["DBREAD_URL"])
	c.Assert("mysql://mysql:xxx@db.internal:3306/main?sslmode=disable&charset=utf8mb4&serverVersion=10.6.0-MariaDB", DeepEquals, e["DB_URL"])
}

func (s *RemoteSuite) TestNoIPsForDedicated(c *C) {
	r := &Remote{}
	value, err := os.ReadFile("testdata/dedicated/no_ips_for_dedicated.json")
	if err != nil {
		panic(err)
	}
	if err := os.Setenv("PLATFORM_RELATIONSHIPS", base64.StdEncoding.EncodeToString(value)); err != nil {
		panic(err)
	}

	rels := extractRelationshipsEnvs(r)
	c.Assert(rels, DeepEquals, Envs{
		"DATABASE_DATABASE":            "x_stg",
		"DATABASE_DRIVER":              "mysql",
		"DATABASE_HOST":                "127.0.0.1",
		"DATABASE_NAME":                "x_stg",
		"DATABASE_PASSWORD":            "x",
		"DATABASE_PORT":                "3306",
		"DATABASE_SERVER":              "mysql://127.0.0.1:3306",
		"DATABASE_URL":                 "mysql://xstg:x@127.0.0.1:3306/x_stg?sslmode=disable&charset=utf8mb4",
		"DATABASE_USER":                "xstg",
		"DATABASE_USERNAME":            "xstg",
		"RABBITMQ_VHOST":               "x_stg",
		"RABBITMQ_DSN":                 "amqp://x_stg:x@localhost:5672/x_stg",
		"RABBITMQ_HOST":                "localhost",
		"RABBITMQ_MANAGEMENT_HOST":     "localhost",
		"RABBITMQ_MANAGEMENT_PASSWORD": "x",
		"RABBITMQ_MANAGEMENT_PORT":     "15672",
		"RABBITMQ_MANAGEMENT_SCHEME":   "http",
		"RABBITMQ_MANAGEMENT_SERVER":   "http://localhost:15672",
		"RABBITMQ_MANAGEMENT_URL":      "http://x_stg:x@localhost:15672",
		"RABBITMQ_MANAGEMENT_USER":     "x_stg",
		"RABBITMQ_MANAGEMENT_USERNAME": "x_stg",
		"RABBITMQ_PASSWORD":            "x",
		"RABBITMQ_PORT":                "5672",
		"RABBITMQ_SCHEME":              "amqp",
		"RABBITMQ_SERVER":              "amqp://localhost:5672",
		"RABBITMQ_URL":                 "amqp://x_stg:x@localhost:5672/x_stg",
		"RABBITMQ_USER":                "x_stg",
		"RABBITMQ_USERNAME":            "x_stg",
		"REDISCACHE_HOST":              "localhost",
		"REDISCACHE_PORT":              "6379",
		"REDISCACHE_SCHEME":            "redis",
		"REDISCACHE_URL":               "redis://localhost:6379",
	})
}
