def test_private_nonexistent_multiple_dirs(self): root = self.get_temp_dir() path = os.path.join(root, "foo", "bar", "qux.txt") util.make_file_with_directories(path, private=True) self.assertMode(0o700, os.path.dirname(path)) self.assertMode(0o600, path) self.assertEqual(0, os.path.getsize(path))
def test_existing_file(self): root = self.get_temp_dir() path = os.path.join(root, "foo", "bar", "qux.txt") os.makedirs(os.path.dirname(path)) with open(path, mode="w") as f: f.write("foobar") util.make_file_with_directories(path) with open(path, mode="r") as f: self.assertEqual("foobar", f.read())
def test_private_existing_dir(self): root = self.get_temp_dir() path = os.path.join(root, "foo", "bar", "qux.txt") os.makedirs(os.path.dirname(path)) os.chmod(os.path.dirname(path), 0o777) util.make_file_with_directories(path, private=True) self.assertMode(0o700, os.path.dirname(path)) self.assertMode(0o600, path) self.assertEqual(0, os.path.getsize(path))
def test_private_existing_file(self): root = self.get_temp_dir() path = os.path.join(root, "foo", "bar", "qux.txt") os.makedirs(os.path.dirname(path)) with open(path, mode="w") as f: f.write("foobar") os.chmod(os.path.dirname(path), 0o777) os.chmod(path, 0o666) util.make_file_with_directories(path, private=True) self.assertMode(0o700, os.path.dirname(path)) self.assertMode(0o600, path) with open(path, mode="r") as f: self.assertEqual("foobar", f.read())
def write_credentials(self, credentials): """Writes a `google.oauth2.credentials.Credentials` to the store.""" if not isinstance(credentials, google.oauth2.credentials.Credentials): raise TypeError("Cannot write credentials of type %s" % type(credentials)) if self._credentials_filepath is None: return # Make the credential file private if not on Windows; on Windows we rely on # the default user config settings directory being private since we don't # have a straightforward way to make an individual file private. private = os.name != "nt" util.make_file_with_directories(self._credentials_filepath, private=private) data = { "refresh_token": credentials.refresh_token, "token_uri": credentials.token_uri, "client_id": credentials.client_id, "client_secret": credentials.client_secret, "scopes": credentials.scopes, "type": "authorized_user", } with open(self._credentials_filepath, "w") as f: json.dump(data, f)
def test_nonexistent_multiple_dirs(self): root = self.get_temp_dir() path = os.path.join(root, "foo", "bar", "qux.txt") util.make_file_with_directories(path) self.assertEqual(0, os.path.getsize(path))
def test_existing_dir(self): root = self.get_temp_dir() path = os.path.join(root, "foo", "bar", "qux.txt") os.makedirs(os.path.dirname(path)) util.make_file_with_directories(path) self.assertEqual(0, os.path.getsize(path))
def test_windows_private(self): with mock.patch.object(os, "name", "nt"): with self.assertRaisesRegex(RuntimeError, "Windows"): util.make_file_with_directories("/tmp/foo", private=True)