Пример #1
0
 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)
Пример #2
0
 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)
Пример #3
0
 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)
Пример #4
0
    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)
Пример #5
0
 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)
Пример #6
0
 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'))
Пример #7
0
 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)
Пример #8
0
 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'])
Пример #9
0
 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'))
Пример #10
0
    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())
Пример #11
0
 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'))
Пример #12
0
 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"))
Пример #13
0
 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'))
Пример #14
0
    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"))
Пример #15
0
 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())
Пример #16
0
    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'))
Пример #17
0
    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,
        )
Пример #18
0
    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,
        )
Пример #19
0
 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))
Пример #20
0
 def test_non_existing_file_succeeds(self):
     conf = config.Config()
     self.assertThat(conf.parser.sections(), Equals([]))
     self.assertTrue(conf.is_empty())
Пример #21
0
 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'))
Пример #22
0
    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())
Пример #23
0
    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())
Пример #24
0
 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'))
Пример #25
0
    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())
Пример #26
0
 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'))
Пример #27
0
 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"))
Пример #28
0
 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"))
Пример #29
0
 def create_config(self, **kwargs):
     conf = config.Config()
     for k, v in kwargs.items():
         conf.set(k, v)
     return conf
Пример #30
0
 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"))