Beispiel #1
0
    def test_oauth_headers(self):
        now = time.time()
        is_about_now = MatchesAll(
            GreaterThanOrEqual(int(now)),
            LessThanOrEqual(int(now) + 3),
        )

        url = factory.make_name("url")
        consumer_key = factory.make_name("consumer_key")
        token_key = factory.make_name("token_key")
        token_secret = factory.make_name("token_secret")
        consumer_secret = factory.make_name("consumer_secret")
        headers = maas_api_helper.oauth_headers(
            url, consumer_key, token_key, token_secret, consumer_secret)
        authorization = headers['Authorization']
        self.assertRegex(authorization, '^OAuth .*')
        authorization = authorization.replace('OAuth ', '')
        oauth_arguments = {}
        for argument in authorization.split(', '):
            key, value = argument.split('=')
            oauth_arguments[key] = value.replace('"', '')

        self.assertIn('oauth_nonce', oauth_arguments)
        oauth_arguments.pop('oauth_nonce', None)

        self.assertThat(oauth_arguments, MatchesDict({
            'oauth_timestamp': AfterPreprocessing(int, is_about_now),
            'oauth_version': Equals('1.0'),
            'oauth_signature_method': Equals('PLAINTEXT'),
            'oauth_consumer_key': Equals(consumer_key),
            'oauth_token': Equals(token_key),
            'oauth_signature': Equals(
                "%s%%26%s" % (consumer_secret, token_secret)),
        }))
    def test_oauth_headers(self):
        now = time.time()
        is_about_now = MatchesAll(GreaterThanOrEqual(int(now)),
                                  LessThanOrEqual(int(now) + 3))

        url = factory.make_name("url")
        consumer_key = factory.make_name("consumer_key")
        token_key = factory.make_name("token_key")
        token_secret = factory.make_name("token_secret")
        consumer_secret = factory.make_name("consumer_secret")
        headers = maas_api_helper.oauth_headers(url, consumer_key, token_key,
                                                token_secret, consumer_secret)
        authorization = headers["Authorization"]
        self.assertRegex(authorization, "^OAuth .*")
        authorization = authorization.replace("OAuth ", "")
        oauth_arguments = {}
        for argument in authorization.split(", "):
            key, value = argument.split("=")
            oauth_arguments[key] = value.replace('"', "")

        self.assertIn("oauth_nonce", oauth_arguments)
        oauth_arguments.pop("oauth_nonce", None)

        self.assertThat(
            oauth_arguments,
            MatchesDict({
                "oauth_timestamp":
                AfterPreprocessing(int, is_about_now),
                "oauth_version":
                Equals("1.0"),
                "oauth_signature_method":
                Equals("PLAINTEXT"),
                "oauth_consumer_key":
                Equals(consumer_key),
                "oauth_token":
                Equals(token_key),
                "oauth_signature":
                Equals("%s%%26%s" % (consumer_secret, token_secret)),
            }),
        )