Пример #1
0
    def testFindCredentialsFile(self):
        """Checks that we can correctly locate the JSON files."""
        def RelativeToHome(fpath):
            """Builds paths relative to the home directory."""
            return os.path.relpath(fpath, os.environ['HOME'])

        authorized_user_path = os.path.join(TESTDATA_PATH, 'androidbuild',
                                            'test_creds_authorized_user.json')
        service_account_path = os.path.join(TESTDATA_PATH, 'androidbuild',
                                            'test_creds_service_account.json')
        nonexistent_path = os.path.join(TESTDATA_PATH, 'androidbuild',
                                        'nonexistent.json')

        # Check that we find the authorized user file if it exists.
        json_path = androidbuild.FindCredentialsFile(
            homedir_json_credentials_path=RelativeToHome(authorized_user_path))
        self.assertEqual(os.path.abspath(json_path),
                         os.path.abspath(authorized_user_path))

        # Check that we fail if it can't be found.
        with self.assertRaises(androidbuild.CredentialsNotFoundError):
            json_path = androidbuild.FindCredentialsFile(
                homedir_json_credentials_path=RelativeToHome(nonexistent_path))

        # Check that the ovverride with the service account takes precedence.
        json_path = androidbuild.FindCredentialsFile(
            service_account_path,
            homedir_json_credentials_path=RelativeToHome(authorized_user_path))
        self.assertEqual(os.path.abspath(json_path),
                         os.path.abspath(service_account_path))
Пример #2
0
    def testFindCredentialsFile_PortageUsername(self):
        """Checks that we can locate the JSON files under $PORTAGE_USERNAME."""
        copy_environ = os.environ.copy()
        copy_environ['HOME'] = '/nonexistent'
        copy_environ['PORTAGE_USERNAME'] = '******'

        fakeuser_homedir = os.path.join(TESTDATA_PATH, 'androidbuild')
        fakeuser_pwent = pwd.struct_passwd(
            ('fakeuser', 'x', 1234, 1234, 'Fake User', fakeuser_homedir,
             '/bin/sh'))

        service_account_name = 'test_creds_authorized_user.json'

        with mock.patch.dict(os.environ, copy_environ), \
            mock.patch.object(pwd, 'getpwnam') as mock_getpwnam:
            mock_getpwnam.return_value = fakeuser_pwent

            json_path = androidbuild.FindCredentialsFile(
                homedir_json_credentials_path=service_account_name)

            mock_getpwnam.assert_called_once_with('fakeuser')

            self.assertEqual(
                json_path, os.path.join(fakeuser_homedir,
                                        service_account_name))