@@ -1748,6 +1748,216 @@ def test_get_table_sets_user_agent(self):
17481748 )
17491749 self .assertIn ("my-application/1.2.3" , expected_user_agent )
17501750
1751+ def test_get_iam_policy (self ):
1752+ from google .cloud .bigquery .iam import BIGQUERY_DATA_OWNER_ROLE
1753+ from google .cloud .bigquery .iam import BIGQUERY_DATA_EDITOR_ROLE
1754+ from google .cloud .bigquery .iam import BIGQUERY_DATA_VIEWER_ROLE
1755+ from google .api_core .iam import Policy
1756+
1757+ PATH = "/projects/{}/datasets/{}/tables/{}:getIamPolicy" .format (
1758+ self .PROJECT , self .DS_ID , self .TABLE_ID ,
1759+ )
1760+ BODY = {"options" : {"requestedPolicyVersion" : 1 }}
1761+ ETAG = "CARDI"
1762+ VERSION = 1
1763+ OWNER1 = "user:phred@example.com"
1764+ OWNER2 = "group:cloud-logs@google.com"
1765+ EDITOR1 = "domain:google.com"
1766+ EDITOR2 = "user:phred@example.com"
1767+ VIEWER1 = "serviceAccount:1234-abcdef@service.example.com"
1768+ VIEWER2 = "user:phred@example.com"
1769+ RETURNED = {
1770+ "resourceId" : PATH ,
1771+ "etag" : ETAG ,
1772+ "version" : VERSION ,
1773+ "bindings" : [
1774+ {"role" : BIGQUERY_DATA_OWNER_ROLE , "members" : [OWNER1 , OWNER2 ]},
1775+ {"role" : BIGQUERY_DATA_EDITOR_ROLE , "members" : [EDITOR1 , EDITOR2 ]},
1776+ {"role" : BIGQUERY_DATA_VIEWER_ROLE , "members" : [VIEWER1 , VIEWER2 ]},
1777+ ],
1778+ }
1779+ EXPECTED = {
1780+ binding ["role" ]: set (binding ["members" ]) for binding in RETURNED ["bindings" ]
1781+ }
1782+
1783+ creds = _make_credentials ()
1784+ http = object ()
1785+ client = self ._make_one (project = self .PROJECT , credentials = creds , _http = http )
1786+ conn = client ._connection = make_connection (RETURNED )
1787+
1788+ policy = client .get_iam_policy (self .TABLE_REF , timeout = 7.5 )
1789+
1790+ conn .api_request .assert_called_once_with (
1791+ method = "POST" , path = PATH , data = BODY , timeout = 7.5
1792+ )
1793+
1794+ self .assertIsInstance (policy , Policy )
1795+ self .assertEqual (policy .etag , RETURNED ["etag" ])
1796+ self .assertEqual (policy .version , RETURNED ["version" ])
1797+ self .assertEqual (dict (policy ), EXPECTED )
1798+
1799+ def test_get_iam_policy_w_invalid_table (self ):
1800+ creds = _make_credentials ()
1801+ http = object ()
1802+ client = self ._make_one (project = self .PROJECT , credentials = creds , _http = http )
1803+
1804+ table_resource_string = "projects/{}/datasets/{}/tables/{}" .format (
1805+ self .PROJECT , self .DS_ID , self .TABLE_ID ,
1806+ )
1807+
1808+ with self .assertRaises (TypeError ):
1809+ client .get_iam_policy (table_resource_string )
1810+
1811+ def test_get_iam_policy_w_invalid_version (self ):
1812+ creds = _make_credentials ()
1813+ http = object ()
1814+ client = self ._make_one (project = self .PROJECT , credentials = creds , _http = http )
1815+
1816+ with self .assertRaises (ValueError ):
1817+ client .get_iam_policy (self .TABLE_REF , requested_policy_version = 2 )
1818+
1819+ def test_set_iam_policy (self ):
1820+ from google .cloud .bigquery .iam import BIGQUERY_DATA_OWNER_ROLE
1821+ from google .cloud .bigquery .iam import BIGQUERY_DATA_EDITOR_ROLE
1822+ from google .cloud .bigquery .iam import BIGQUERY_DATA_VIEWER_ROLE
1823+ from google .api_core .iam import Policy
1824+
1825+ PATH = "/projects/%s/datasets/%s/tables/%s:setIamPolicy" % (
1826+ self .PROJECT ,
1827+ self .DS_ID ,
1828+ self .TABLE_ID ,
1829+ )
1830+ ETAG = "foo"
1831+ VERSION = 1
1832+ OWNER1 = "user:phred@example.com"
1833+ OWNER2 = "group:cloud-logs@google.com"
1834+ EDITOR1 = "domain:google.com"
1835+ EDITOR2 = "user:phred@example.com"
1836+ VIEWER1 = "serviceAccount:1234-abcdef@service.example.com"
1837+ VIEWER2 = "user:phred@example.com"
1838+ BINDINGS = [
1839+ {"role" : BIGQUERY_DATA_OWNER_ROLE , "members" : [OWNER1 , OWNER2 ]},
1840+ {"role" : BIGQUERY_DATA_EDITOR_ROLE , "members" : [EDITOR1 , EDITOR2 ]},
1841+ {"role" : BIGQUERY_DATA_VIEWER_ROLE , "members" : [VIEWER1 , VIEWER2 ]},
1842+ ]
1843+ MASK = "bindings,etag"
1844+ RETURNED = {"etag" : ETAG , "version" : VERSION , "bindings" : BINDINGS }
1845+
1846+ policy = Policy ()
1847+ for binding in BINDINGS :
1848+ policy [binding ["role" ]] = binding ["members" ]
1849+
1850+ BODY = {"policy" : policy .to_api_repr (), "updateMask" : MASK }
1851+
1852+ creds = _make_credentials ()
1853+ http = object ()
1854+ client = self ._make_one (project = self .PROJECT , credentials = creds , _http = http )
1855+ conn = client ._connection = make_connection (RETURNED )
1856+
1857+ returned_policy = client .set_iam_policy (
1858+ self .TABLE_REF , policy , updateMask = MASK , timeout = 7.5
1859+ )
1860+
1861+ conn .api_request .assert_called_once_with (
1862+ method = "POST" , path = PATH , data = BODY , timeout = 7.5
1863+ )
1864+ self .assertEqual (returned_policy .etag , ETAG )
1865+ self .assertEqual (returned_policy .version , VERSION )
1866+ self .assertEqual (dict (returned_policy ), dict (policy ))
1867+
1868+ def test_set_iam_policy_no_mask (self ):
1869+ from google .api_core .iam import Policy
1870+
1871+ PATH = "/projects/%s/datasets/%s/tables/%s:setIamPolicy" % (
1872+ self .PROJECT ,
1873+ self .DS_ID ,
1874+ self .TABLE_ID ,
1875+ )
1876+ RETURNED = {"etag" : "foo" , "version" : 1 , "bindings" : []}
1877+
1878+ policy = Policy ()
1879+ BODY = {"policy" : policy .to_api_repr ()}
1880+
1881+ creds = _make_credentials ()
1882+ http = object ()
1883+ client = self ._make_one (project = self .PROJECT , credentials = creds , _http = http )
1884+ conn = client ._connection = make_connection (RETURNED )
1885+
1886+ client .set_iam_policy (self .TABLE_REF , policy , timeout = 7.5 )
1887+
1888+ conn .api_request .assert_called_once_with (
1889+ method = "POST" , path = PATH , data = BODY , timeout = 7.5
1890+ )
1891+
1892+ def test_set_iam_policy_invalid_policy (self ):
1893+ from google .api_core .iam import Policy
1894+
1895+ policy = Policy ()
1896+ invalid_policy_repr = policy .to_api_repr ()
1897+
1898+ creds = _make_credentials ()
1899+ http = object ()
1900+ client = self ._make_one (project = self .PROJECT , credentials = creds , _http = http )
1901+
1902+ with self .assertRaises (TypeError ):
1903+ client .set_iam_policy (self .TABLE_REF , invalid_policy_repr )
1904+
1905+ def test_set_iam_policy_w_invalid_table (self ):
1906+ from google .api_core .iam import Policy
1907+
1908+ policy = Policy ()
1909+
1910+ creds = _make_credentials ()
1911+ http = object ()
1912+ client = self ._make_one (project = self .PROJECT , credentials = creds , _http = http )
1913+
1914+ table_resource_string = "projects/%s/datasets/%s/tables/%s" % (
1915+ self .PROJECT ,
1916+ self .DS_ID ,
1917+ self .TABLE_ID ,
1918+ )
1919+
1920+ with self .assertRaises (TypeError ):
1921+ client .set_iam_policy (table_resource_string , policy )
1922+
1923+ def test_test_iam_permissions (self ):
1924+ PATH = "/projects/%s/datasets/%s/tables/%s:testIamPermissions" % (
1925+ self .PROJECT ,
1926+ self .DS_ID ,
1927+ self .TABLE_ID ,
1928+ )
1929+
1930+ PERMISSIONS = ["bigquery.tables.get" , "bigquery.tables.update" ]
1931+ BODY = {"permissions" : PERMISSIONS }
1932+ RETURNED = {"permissions" : PERMISSIONS }
1933+
1934+ creds = _make_credentials ()
1935+ http = object ()
1936+ client = self ._make_one (project = self .PROJECT , credentials = creds , _http = http )
1937+ conn = client ._connection = make_connection (RETURNED )
1938+
1939+ client .test_iam_permissions (self .TABLE_REF , PERMISSIONS , timeout = 7.5 )
1940+
1941+ conn .api_request .assert_called_once_with (
1942+ method = "POST" , path = PATH , data = BODY , timeout = 7.5
1943+ )
1944+
1945+ def test_test_iam_permissions_w_invalid_table (self ):
1946+ creds = _make_credentials ()
1947+ http = object ()
1948+ client = self ._make_one (project = self .PROJECT , credentials = creds , _http = http )
1949+
1950+ table_resource_string = "projects/%s/datasets/%s/tables/%s" % (
1951+ self .PROJECT ,
1952+ self .DS_ID ,
1953+ self .TABLE_ID ,
1954+ )
1955+
1956+ PERMISSIONS = ["bigquery.tables.get" , "bigquery.tables.update" ]
1957+
1958+ with self .assertRaises (TypeError ):
1959+ client .test_iam_permissions (table_resource_string , PERMISSIONS )
1960+
17511961 def test_update_dataset_w_invalid_field (self ):
17521962 from google .cloud .bigquery .dataset import Dataset
17531963
0 commit comments