def test_upload_with_invalid_credentials_raises_exception(self): conf = config.Config() conf.set('macaroon', 'inval"id') conf.save() self.assertRaises( errors.InvalidCredentialsError, self.client.upload, 'test-snap', self.snap_path)
def test_invalid_discharge_raises_exception(self): conf = config.Config() conf.set('macaroon', pymacaroons.Macaroon().serialize()) conf.set('unbound_discharge', 'inval*id') conf.save() with self.assertRaises(errors.InvalidCredentialsError): storeapi._macaroon_auth(conf)
def __init__(self) -> None: super().__init__() self.conf = config.Config() self.sso = SSOClient(self.conf) self.cpi = SnapIndexClient(self.conf) self.updown = UpDownClient(self.conf) self.sca = SCAClient(self.conf)
def test_successful_login_with(self, mock_acl, mock_login, mock_get_account_information): mock_acl.return_value = { 'snap_ids': None, 'channels': None, 'permissions': None, } conf = config.Config() conf.set('macaroon', 'test-macaroon') conf.set('unbound_discharge', 'test-unbound-discharge') with open('exported-login', 'w') as f: conf.save(config_fd=f) f.flush() result = self.run_command(['login', '--with', 'exported-login']) self.assertThat(result.exit_code, Equals(0)) self.assertThat(result.output, Contains('Login successful')) self.assertThat(result.output, MatchesRegex(r'.*snaps:.*?No restriction', re.DOTALL)) self.assertThat( result.output, MatchesRegex(r'.*channels:.*?No restriction', re.DOTALL)) self.assertThat( result.output, MatchesRegex(r'.*permissions:.*?No restriction', re.DOTALL)) self.mock_input.assert_not_called() mock_login.assert_called_once_with('', '', acls=None, packages=None, channels=None, save=True, config_fd=mock.ANY)
def test_download_with_invalid_credentials_raises_exception(self): conf = config.Config() conf.set('macaroon', 'inval"id') conf.save() download_path = os.path.join(self.path, 'test-snap.snap') with self.assertRaises(errors.InvalidCredentialsError): self.client.download('test-snap', 'test-channel', download_path)
def test_section_from_url(self): create_config_from_string('''[example.com]\nfoo=bar''') self.useFixture( fixtures.EnvironmentVariable('UBUNTU_SSO_API_ROOT_URL', 'http://example.com/api/v2')) conf = config.Config() self.assertEqual('bar', conf.get('foo'))
def test_invalid_macaroon_root_raises_exception(self): conf = config.Config() conf.set('macaroon', 'inval"id') conf.save() self.assertRaises( errors.InvalidCredentialsError, storeapi._macaroon_auth, conf)
def test_release_with_invalid_credentials_raises_exception(self): conf = config.Config() conf.set('macaroon', 'inval"id') conf.save() self.assertRaises( errors.InvalidCredentialsError, self.client.release, 'test-snap', '10', ['beta'])
def test_login_successful(self): self.client.login( 'dummy email', 'test correct password') conf = config.Config() self.assertIsNotNone(conf.get('macaroon')) self.assertIsNotNone(conf.get('unbound_discharge'))
def test_failed_login_with_wrong_one_time_password(self): with self.assertRaises(errors.StoreAuthenticationError): self.client.login( 'dummy email', 'test correct password', 'wrong one-time password') self.assertTrue(config.Config().is_empty())
def test_login_successful_with_channel_attenuation(self): self.client.login( 'dummy email', 'test correct password', channels=['edge'], ) conf = config.Config() self.assertIsNotNone(conf.get('macaroon')) self.assertIsNotNone(conf.get('unbound_discharge'))
def test_section_from_url(self): create_config_from_string("""[example.com]\nfoo=bar""") self.useFixture( fixtures.EnvironmentVariable( "UBUNTU_SSO_API_ROOT_URL", "http://example.com/api/v2" ) ) conf = config.Config() self.assertThat(conf.get("foo"), Equals("bar"))
def test_login_successful_with_package_attenuation(self): self.client.login( 'dummy email', 'test correct password', packages=[{'name': 'foo', 'series': '16'}], ) conf = config.Config() self.assertIsNotNone(conf.get('macaroon')) self.assertIsNotNone(conf.get('unbound_discharge'))
def test_load_invalid_config(self): with open("test-config", "w") as f: f.write("invalid config") f.flush() conf = config.Config() with open("test-config", "r") as f: raised = self.assertRaises( errors.InvalidLoginConfig, conf.load, config_fd=f ) self.assertThat(str(raised), Contains("File contains no section headers"))
def test_login_successful_fully_attenuated(self): self.client.login( 'dummy email', 'test correct password', packages=[{'name': 'foo', 'series': '16'}], channels=['edge'], save=False ) # Client configuration is filled, but it's not saved on disk. self.assertIsNotNone(self.client.conf.get('macaroon')) self.assertIsNotNone(self.client.conf.get('unbound_discharge')) self.assertTrue(config.Config().is_empty())
def test_load_invalid_config(self): with open('test-config', 'w') as f: f.write('invalid config') f.flush() conf = config.Config() with open('test-config', 'r') as f: raised = self.assertRaises( errors.InvalidLoginConfig, conf.load, config_fd=f) self.assertThat(str(raised), Contains( 'File contains no section headers'))
def test_successful_login_with(self): self.useFixture( fixtures.MockPatchObject( storeapi.StoreClient, "acl", return_value={ "snap_ids": None, "channels": None, "permissions": None, "expires": "2018-01-01T00:00:00", }, )) self.fake_store_login.mock.side_effect = None conf = config.Config() conf.set("macaroon", "test-macaroon") conf.set("unbound_discharge", "test-unbound-discharge") with open("exported-login", "w") as f: conf.save(config_fd=f) f.flush() result = self.run_command(["login", "--with", "exported-login"]) self.assertThat(result.exit_code, Equals(0)) self.assertThat(result.output, Contains("Login successful")) self.assertThat(result.output, MatchesRegex(r".*snaps:.*?No restriction", re.DOTALL)) self.assertThat( result.output, MatchesRegex(r".*channels:.*?No restriction", re.DOTALL)) self.assertThat( result.output, MatchesRegex(r".*permissions:.*?No restriction", re.DOTALL)) self.assertThat( result.output, MatchesRegex(r".*expires:.*?2018-01-01T00:00:00", re.DOTALL)) self.fake_store_login.mock.assert_called_once_with( "", "", acls=None, packages=None, channels=None, expires=None, save=True, config_fd=mock.ANY, )
def test_successful_login_with(self, mock_acl, mock_login, mock_get_account_information): mock_acl.return_value = { "snap_ids": None, "channels": None, "permissions": None, "expires": "2018-01-01T00:00:00", } conf = config.Config() conf.set("macaroon", "test-macaroon") conf.set("unbound_discharge", "test-unbound-discharge") with open("exported-login", "w") as f: conf.save(config_fd=f) f.flush() result = self.run_command(["login", "--with", "exported-login"]) self.assertThat(result.exit_code, Equals(0)) self.assertThat(result.output, Contains("Login successful")) self.assertThat(result.output, MatchesRegex(r".*snaps:.*?No restriction", re.DOTALL)) self.assertThat( result.output, MatchesRegex(r".*channels:.*?No restriction", re.DOTALL)) self.assertThat( result.output, MatchesRegex(r".*permissions:.*?No restriction", re.DOTALL)) self.assertThat( result.output, MatchesRegex(r".*expires:.*?2018-01-01T00:00:00", re.DOTALL)) self.mock_input.assert_not_called() mock_login.assert_called_once_with( "", "", acls=None, packages=None, channels=None, expires=None, save=True, config_fd=mock.ANY, )
def test_irrelevant_sections_are_ignored(self): create_config_from_string('''[example.com]\nfoo=bar''') conf = config.Config() self.assertThat(conf.get('foo'), Equals(None))
def test_non_existing_file_succeeds(self): conf = config.Config() self.assertThat(conf.parser.sections(), Equals([])) self.assertTrue(conf.is_empty())
def test_local_config_is_preferred(self): create_config_from_string('''[example.com]\nfoo=baz''') create_local_config_from_string('''[example.com]\nfoo=bar''') conf = config.Config() self.assertThat(conf.get('foo'), Equals('bar'))
def test_failed_login_with_invalid_json(self): with self.assertRaises(errors.StoreAuthenticationError): self.client.login('dummy email', 'test 401 invalid json') self.assertTrue(config.Config().is_empty())
def test_failed_login_requires_one_time_password(self): with self.assertRaises(errors.StoreTwoFactorAuthenticationRequired): self.client.login('dummy email', 'test requires 2fa') self.assertTrue(config.Config().is_empty())
def test_save_encoded(self): conf = config.Config() conf.set('bar', 'baz') conf.save(encode=True) new_conf = config.Config() self.assertThat(new_conf.get('bar'), Equals('baz'))
def test_failed_login_with_wrong_password(self): self.assertRaises( errors.StoreAuthenticationError, self.client.login, 'dummy email', 'wrong password') self.assertTrue(config.Config().is_empty())
def test_save_one_option(self): conf = config.Config() conf.set('bar', 'baz') conf.save() new_conf = config.Config() self.assertThat(new_conf.get('bar'), Equals('baz'))
def test_save_encoded(self): conf = config.Config() conf.set("bar", "baz") conf.save(encode=True) new_conf = config.Config() self.assertThat(new_conf.get("bar"), Equals("baz"))
def test_local_config_is_preferred(self): create_config_from_string("""[example.com]\nfoo=baz""") create_local_config_from_string("""[example.com]\nfoo=bar""") conf = config.Config() self.assertThat(conf.get("foo"), Equals("bar"))
def create_config(self, **kwargs): conf = config.Config() for k, v in kwargs.items(): conf.set(k, v) return conf
def test_save_one_option(self): conf = config.Config() conf.set("bar", "baz") conf.save() new_conf = config.Config() self.assertThat(new_conf.get("bar"), Equals("baz"))