Esempio n. 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)),
        }))
Esempio n. 2
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")
        credentials = maas_api_helper.Credentials()
        credentials.update(
            {
                "consumer_key": consumer_key,
                "token_key": token_key,
                "token_secret": token_secret,
                "consumer_secret": consumer_secret,
            }
        )
        headers = credentials.oauth_headers(url)
        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)
                    ),
                }
            ),
        )
Esempio n. 3
0
 def test__does_not_match_greater_than(self):
     self.assertMismatch(LessThanOrEqual(4).match(5), "Differences:")
     self.assertMismatch(
         LessThanOrEqual("aaa").match("bbb"), "Differences:")
Esempio n. 4
0
 def test__matches_equal_to(self):
     self.assertThat(5, LessThanOrEqual(5))
     self.assertThat("bbb", LessThanOrEqual("bbb"))
Esempio n. 5
0
 def test__matches_less_than(self):
     self.assertThat(5, LessThanOrEqual(6))
     self.assertThat("bbb", LessThanOrEqual("ccc"))