def test_bool(self): self.assertTrue( maas_api_helper.Credentials( token_key="token_key", consumer_key="consumer_key" ) ) self.assertFalse(maas_api_helper.Credentials())
def test_from_stringempty(self): self.assertEqual( maas_api_helper.Credentials.from_string(""), maas_api_helper.Credentials(), ) self.assertEqual( maas_api_helper.Credentials.from_string(None), maas_api_helper.Credentials(), )
def test_eq(self): creds = maas_api_helper.Credentials( token_key="token_key", consumer_secret="consumer_secret" ) self.assertEqual( creds, maas_api_helper.Credentials( token_key="token_key", consumer_secret="consumer_secret" ), ) self.assertNotEqual( creds, maas_api_helper.Credentials( token_key="token_key2", consumer_key="consumer_key" ), )
def test_update_no_update_already_set(self): creds = maas_api_helper.Credentials() 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") creds.update( { "consumer_key": consumer_key, "token_key": token_key, "token_secret": token_secret, "consumer_secret": consumer_secret, } ) creds.update( { "consumer_key": factory.make_name(), "token_key": factory.make_name(), "token_secret": factory.make_name(), "consumer_secret": factory.make_name(), } ) self.assertEqual(creds.consumer_key, consumer_key) self.assertEqual(creds.token_key, token_key) self.assertEqual(creds.token_secret, token_secret) self.assertEqual(creds.consumer_secret, consumer_secret)
def test_with_args(self): creds = maas_api_helper.Credentials( token_key="token_key", consumer_secret="consumer_secret" ) self.assertEqual(creds.consumer_key, "") self.assertEqual(creds.token_key, "token_key") self.assertEqual(creds.token_secret, "") self.assertEqual(creds.consumer_secret, "consumer_secret")
def test_all(self): creds = maas_api_helper.Credentials.from_string("ck:tk:ts:cs") self.assertEqual( creds, maas_api_helper.Credentials( consumer_key="ck", token_key="tk", token_secret="ts", consumer_secret="cs", ), )
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) ), } ), )
def test_defaults(self): creds = maas_api_helper.Credentials() self.assertEqual(creds.consumer_key, "") self.assertEqual(creds.token_key, "") self.assertEqual(creds.token_secret, "") self.assertEqual(creds.consumer_secret, "")
def test_oauth_headers_empty(self): creds = maas_api_helper.Credentials() self.assertEqual(creds.oauth_headers("http://example.com"), {})