class TestClient_OAuth_1_0_Example:
    def setUp(self):
        self.client_credentials = Credentials(identifier="dpf43f3p2l4k3l03", shared_secret="kd94hf93k423kf44")
        self.client = Client(self.client_credentials,
                             temporary_credentials_request_uri="https://photos.example.net/initiate",
                             resource_owner_authorization_uri="https://photos.example.net/authorize",
                             token_credentials_request_uri="https://photos.example.net/token",
                             use_authorization_header=True)
        self.temporary_credentials = Credentials(identifier="hh5s93j4hdidpola", shared_secret="hdhd0244k9j7ao03")
        self.token_credentials = Credentials(identifier="nnch734d00sl2jdk", shared_secret="pfkkdhi9sl3r4s00")

    def test___init__(self):
        c = self.client
        assert_equal(c._temporary_credentials_request_uri, "https://photos.example.net/initiate")
        assert_equal(c._resource_owner_authorization_uri, "https://photos.example.net/authorize")
        assert_equal(c._token_credentials_request_uri, "https://photos.example.net/token")
        assert_equal(c._use_authorization_header, True)
        assert_equal(c._client_credentials.identifier, "dpf43f3p2l4k3l03")
        assert_equal(c._client_credentials.shared_secret, "kd94hf93k423kf44")

    def test_oauth_version(self):
        # OAuth version MUST be set to "1.0". Anything else is the responsibility of the API user.
        assert_equal(self.client.oauth_version, "1.0")

    def test_get_authorization_url(self):
        url = self.client.get_authorization_url(self.temporary_credentials, a="something here", b=["another thing", 5], oauth_ignored="ignored")
        assert_equal(url, "https://photos.example.net/authorize?a=something%20here&b=5&b=another%20thing&oauth_token=" + self.temporary_credentials.identifier)

    def test_parse_temporary_credentials_response(self):
        headers = {
            "Content-Type": "application/x-www-form-urlencoded",
        }
        assert_raises(ValueError, self.client.parse_temporary_credentials_response, ResponseProxy(200, "OK", "oauth_token=hh5s93j4hdidpola&oauth_token_secret=hdhd0244k9j7ao03&oauth_callback_confirmed=", headers))
        assert_raises(ValueError, self.client.parse_temporary_credentials_response, ResponseProxy(200, "OK", "oauth_token=hh5s93j4hdidpola&oauth_token_secret=hdhd0244k9j7ao03&oauth_callback_confirmed=false", headers))

        params, credentials = self.client.parse_temporary_credentials_response(ResponseProxy(200, "OK", "oauth_token=hh5s93j4hdidpola&oauth_token_secret=hdhd0244k9j7ao03&oauth_callback_confirmed=true", headers=headers))
        assert_dict_equal(params, {
            "oauth_token": ["hh5s93j4hdidpola"],
            "oauth_token_secret": ["hdhd0244k9j7ao03"],
            "oauth_callback_confirmed": ["true"],
        })
        assert_equal(credentials, self.temporary_credentials)

    def test_parse_token_credentials_response(self):
        headers = {
            "Content-Type": "application/x-www-form-urlencoded",
        }
        params, credentials = self.client.parse_token_credentials_response(ResponseProxy(200, "OK", "oauth_token=nnch734d00sl2jdk&oauth_token_secret=pfkkdhi9sl3r4s00", headers=headers))
        assert_dict_equal(params, {
            "oauth_token": ["nnch734d00sl2jdk"],
            "oauth_token_secret": ["pfkkdhi9sl3r4s00"],
        })
        assert_equal(credentials, self.token_credentials)

    def test__parse_credentials_response(self):
        headers = {
            "Content-Type": "application/x-www-form-urlencoded",
        }
        params, credentials = self.client._parse_credentials_response(ResponseProxy(200, "OK", "oauth_token=hh5s93j4hdidpola&oauth_token_secret=hdhd0244k9j7ao03&oauth_callback_confirmed=true", headers=headers))
        assert_dict_equal(params, {
            "oauth_token": ["hh5s93j4hdidpola"],
            "oauth_token_secret": ["hdhd0244k9j7ao03"],
            "oauth_callback_confirmed": ["true"],
        })
        assert_equal(credentials, self.temporary_credentials)

        params, credentials = self.client._parse_credentials_response(ResponseProxy(200, "OK", "oauth_token=nnch734d00sl2jdk&oauth_token_secret=pfkkdhi9sl3r4s00", headers=headers))
        assert_dict_equal(params, {
            "oauth_token": ["nnch734d00sl2jdk"],
            "oauth_token_secret": ["pfkkdhi9sl3r4s00"],
        })
        assert_equal(credentials, self.token_credentials)


    def test_parse_credentials_response_validation(self):
        status_code = 200
        status = "OK"
        body = "oauth_token=nnch734d00sl2jdk&oauth_token_secret=pfkkdhi9sl3r4s00"
        headers = {
            "Content-Type": "application/x-www-form-urlencoded",
        }

        assert_raises(InvalidHttpResponseError, self.client._parse_credentials_response, ResponseProxy(status_code, None, body, headers))
        assert_raises(InvalidHttpResponseError, self.client._parse_credentials_response, ResponseProxy(None, status, body, headers))
        assert_raises(InvalidHttpResponseError, self.client._parse_credentials_response, ResponseProxy(status_code, status, None, headers))
        assert_raises(InvalidHttpResponseError, self.client._parse_credentials_response, ResponseProxy(status_code, status, body, None))

        assert_raises(HttpError, self.client._parse_credentials_response, ResponseProxy(300, "Multiple choices", body, headers))
        assert_raises(HttpError, self.client._parse_credentials_response, ResponseProxy(199, "continue", body, headers))

        assert_raises(InvalidHttpResponseError, self.client._parse_credentials_response, ResponseProxy(200, "OK" , "", headers))
        assert_raises(InvalidContentTypeError, self.client._parse_credentials_response, ResponseProxy(200, "OK", body, {"Content-Type": "invalid"}))
class Test__OAuthClient_urls(unittest2.TestCase):
    def setUp(self):
        self.client_credentials = \
            Credentials(identifier=RFC_CLIENT_IDENTIFIER,
                        shared_secret=RFC_CLIENT_SECRET)
        self.temporary_credentials = \
            Credentials(identifier=RFC_TEMPORARY_IDENTIFIER,
                        shared_secret=RFC_TEMPORARY_SECRET)
        self.token_credentials = Credentials(identifier=RFC_TOKEN_IDENTIFIER,
                                             shared_secret=RFC_TOKEN_SECRET)
        args = dict(
            temporary_credentials_uri=RFC_TEMP_URI,
            token_credentials_uri=RFC_TOKEN_URI,
            authorization_uri=RFC_AUTHORIZATION_URI,
            authentication_uri=RFC_AUTHENTICATION_URI,
            use_authorization_header=True
        )
        self.client = Client(None, self.client_credentials, **args)

    def test___init__(self):
        c = self.client
        self.assertEqual(c._temporary_credentials_uri,
                         RFC_TEMP_URI)
        self.assertEqual(c._token_credentials_uri,
                         RFC_TOKEN_URI)
        self.assertEqual(c._authorization_uri,
                         RFC_AUTHORIZATION_URI)
        self.assertEqual(c._authentication_uri,
                         RFC_AUTHENTICATION_URI)
        self.assertEqual(c._use_authorization_header, True)
        self.assertEqual(c._client_credentials.identifier,
                         RFC_CLIENT_IDENTIFIER)
        self.assertEqual(c._client_credentials.shared_secret,
                         RFC_CLIENT_SECRET)

    def test_get_authorization_url(self):
        url = self.client.get_authorization_url(self.temporary_credentials,
                                                a="something here",
                                                b=["another thing", 5],
                                                oauth_ignored="ignored")
        self.assertEqual(url,
                         RFC_AUTHORIZATION_URI +
                         b("?a=something%20here"
                         "&b=5"
                         "&b=another%20thing&oauth_token=") +
                         self.temporary_credentials.identifier)

    def test_get_authentication_url(self):
        url = self.client.get_authentication_url(self.temporary_credentials,
                                                a="something here",
                                                b=["another thing", 5],
                                                oauth_ignored=b("ignored"))
        self.assertEqual(url,
                         RFC_AUTHENTICATION_URI +
                         b("?a=something%20here"
                         "&b=5"
                         "&b=another%20thing&oauth_token=") +
                         self.temporary_credentials.identifier)

    def test_no_authentication_url(self):
        args = dict(
            temporary_credentials_uri=RFC_TEMP_URI,
            token_credentials_uri=RFC_TOKEN_URI,
            authorization_uri=RFC_AUTHORIZATION_URI,
            authentication_uri=None,
            use_authorization_header=True
        )
        client = Client(None, self.client_credentials, **args)
        self.assertRaises(NotImplementedError,
                          client.get_authentication_url,
                          self.temporary_credentials,
                          a=b("something here"),
                          b=[b("another thing"), 5],
                          oauth_ignored=b("ignored"))