def test_url_with_appid(self):
        """Ensure that an url with an appid appends / to the appid url."""
        gae_req = AppEngineRequest(url="/foo", appid="test")

        url = gae_req.build_url()

        self.assertEqual(url, "http://test.appspot.com/foo")
    def test_home_url_with_no_appid(self):
        """Ensure that a / url with no appid appends / to the local url."""
        gae_req = AppEngineRequest(url="/")

        url = gae_req.build_url()

        self.assertEqual(url, "http://localhost/")
    def test_full_url_with_no_appid_or_ssl(self):
        """Ensure that a full url with no appid or ssl matches the url passed
        in.
        """
        gae_req = AppEngineRequest(url="http://test.appspot.com")

        url = gae_req.build_url()

        self.assertEqual(url, "http://test.appspot.com/")
    def test_no_url_with_hrd_and_full_domain_appid_passed_in(self):
        """Ensure that no url with an appid that has s~ and .appspot.com
        builds the correct GAE domain.
        """
        gae_req = AppEngineRequest(appid="s~test.appspot.com")

        url = gae_req.build_url()

        self.assertEqual(url, "http://test.appspot.com/")
    def test_no_url_or_appid_passed_in_and_is_ssl(self):
        """Ensure that if no url or appid are passed in that the default url
        is set to localhost. And with ssl it's set to https.
        """
        gae_req = AppEngineRequest(use_ssl=True)

        url = gae_req.build_url()

        self.assertEqual(url, "https://localhost/")
    def test_url_missing_scheme_with_no_appid_sets_local(self):
        """Ensure that a url with no scheme and no appid appends the url to
        a localhost url.
        """
        gae_req = AppEngineRequest(url="foo")

        url = gae_req.build_url()

        self.assertEqual(url, "http://localhost/foo")
    def test_full_url_with_appid_but_has_ssl(self):
        """Ensure that a full url with appid or ssl still  matches the url
        passed in.
        """
        gae_req = AppEngineRequest(url="http://test.appspot.com", appid="foo")

        url = gae_req.build_url()

        self.assertEqual(url, "http://test.appspot.com/")
    def test_response_has_auth_token_and_sid(self, request_post):
        """Ensure that if the response has an auth token and a sessionid that
        the token is returned correctly and the sid set on the class.
        """
        gae_req = AppEngineRequest(url="/foo", appid="test", source="test",
                                   email="*****@*****.**", password="******")

        request_post.return_value = Mock(text="Auth=my_token\nSID=my_sid")

        token = gae_req.get_auth_token()

        self.assertEqual(token, "my_token")
        self.assertEqual(gae_req.sid, "my_sid")

        data = {
            "Email": "*****@*****.**",
            "Passwd": "foobar",
            "service": "ah",
            "source": "test",
            "accountType": "HOSTED_OR_GOOGLE"
        }

        request_post.assert_called_once_with(
            "https://www.google.com/accounts/ClientLogin", data=data)